diff --git a/sim/compiled_sim.dll b/sim/compiled_sim.dll index debc1a5..4478d89 100644 Binary files a/sim/compiled_sim.dll and b/sim/compiled_sim.dll differ diff --git a/sim/compiled_sim_gates.c b/sim/compiled_sim_gates.c index 659abc0..66cb5ee 100644 --- a/sim/compiled_sim_gates.c +++ b/sim/compiled_sim_gates.c @@ -1,17 +1,21 @@ - -GATEFUNC(Diode) { setport(2, getport(1)); } -GATEFUNC(Not) { setport(2, !getport(1)); } - -//// +// Auto-generated by gencfuncs.lua enum GateFuncs { GateFunc_None, - GateFunc_Diode = 1, - GateFunc_Not = 2, + GateFunc_Diode, + GateFunc_Not, }; +GATEFUNC(Diode) { + setport(2, getport(1)); +} +GATEFUNC(Not) { + setport(2, !getport(1)); +} + GateFunc sim_logic_functions[] = { 0, GATEFUNCID(Diode), GATEFUNCID(Not), }; + diff --git a/sim/compiled_sim_gates.lua b/sim/compiled_sim_gates.lua new file mode 100644 index 0000000..70611ae --- /dev/null +++ b/sim/compiled_sim_gates.lua @@ -0,0 +1,13 @@ +-- Auto-generated by gencfuncs.lua + +local cFuncsByName = { + ["diode"] = 1, + ["not"] = 2, +} + +local cDataSizeByName = { + ["diode"] = 0, + ["not"] = 0, +} + +return cFuncsByName, cDataSizeByName diff --git a/sim/gatedef.lua b/sim/gatedef.lua index 198a486..4a2c60c 100644 --- a/sim/gatedef.lua +++ b/sim/gatedef.lua @@ -7,7 +7,9 @@ GateDefinition = { input = function(gate, argv) end } -function GateDefinition.new(objref, name, description, init, logic, input, global, ports, datasize, logicfuncc) +local cFuncsByName, cDataSizeByName = require("compiled_sim_gates") + +function GateDefinition.new(objref, name, description, init, logic, input, global, ports) name = collapseescape(name) init = collapseescape(init) @@ -23,8 +25,8 @@ function GateDefinition.new(objref, name, description, init, logic, input, globa ports = ports or {}, num_in_ports = 0, num_out_ports = 0, - data_size_c = datasize, - logic_function_c = logicfuncc, + data_size_c = cDataSizeByName[name:lower()] or 0, + logic_function_c = cFuncsByName[name:lower()] or 0, } local initfunc = loadstring(tostring(init)) diff --git a/sim/gencfuncs.lua b/sim/gencfuncs.lua index 4de9554..e477946 100644 --- a/sim/gencfuncs.lua +++ b/sim/gencfuncs.lua @@ -1,24 +1,63 @@ +local c_code = [[ +// Auto-generated by gencfuncs.lua -local luatoc_subs = { - {"Gate%.setportstate%(gate, *", "setport%("}, - {"Gate%.getportstate%(gate, *", "getport%("}, - {"~=", "!="}, - {"!= *0", ""}, - {"elseif", "els2 if"}, - {"if", "if("}, - {"else", "} else {"}, - {"els2", "else"}, - {"end", "}"}, - {"then", ") {"}, - {"return function(gate)", "GATEFUNC"}, +enum GateFuncs { + GateFunc_None, +%s +}; + +%s + +GateFunc sim_logic_functions[] = { + 0, +%s +}; + +]] + +local lua_code = [[ +-- Auto-generated by gencfuncs.lua + +local cFuncsByName = { +%s } -local function luatoc(f) - for i, sub in ipairs(luatoc_subs) do - f = f:gsub(sub[1], sub[2]) - end - return f + +local cDataSizeByName = { +%s +} + +return cFuncsByName, cDataSizeByName +]] +local function writeFile(fn, data) + local f = io.open(fn, "wb") or error("Could not open file for writing: "..fn) + f:write(data) + f:close() end -local function tstogatedata(ts) +local function exportGates(gates) + local gateList = {} + local gateFuncList = {} + local gateSizeList = {} + for k, gate in pairs(gates) do + table.insert(gateList, gate.name or error("gate "..k.." has no name")) + table.insert(gateFuncList, gate.func or error("gate "..gate.name.." has no c function")) + table.insert(gateSizeList, gate.size or 0) + end + local function map(t, f) local u = {}; for i, v in ipairs(t) do table.insert(u, f(v, i)) end; return u; end + writeFile("compiled_sim_gates.c", string.format(c_code, + table.concat(map(gateList, function(v, i) return string.format("\tGateFunc_%s,", v) end), "\n"), + table.concat(map(gateList, function(v, i) return string.format("GATEFUNC(%s) {\n\t%s\n}", v, gateFuncList[i]:gsub("\n", "\n\t"):gsub("\n+$", ""):gsub("^\n+", "")) end), "\n"), + table.concat(map(gateList, function(v, i) return string.format("\tGATEFUNCID(%s),", v) end), "\n") + )) + writeFile("compiled_sim_gates.lua", string.format(lua_code, + table.concat(map(gateList, function(v, i) return string.format("\t[\"%s\"] = %i,", v:lower(), i) end), "\n"), + table.concat(map(gateList, function(v, i) return string.format("\t[\"%s\"] = %i,", v:lower(), gateSizeList[i]) end), "\n") + )) end + +local gates = { + { name = "Diode", func = "setport(2, getport(1));" }, + { name = "Not" , func = "setport(2, !getport(1));" }, +} +exportGates(gates) diff --git a/sim/network.lua b/sim/network.lua index e11e69a..d1759b5 100644 --- a/sim/network.lua +++ b/sim/network.lua @@ -75,12 +75,10 @@ function network_update() local logic = data[i+5] local input = data[i+6] local global = data[i+7] - local datasize = tonumber(data[i+8]) - local logicfuncc = tonumber(data[i+9]) - local numports = tonumber(data[i+10]) - local ports = {} - i = i + 10 + local numports = tonumber(data[i+8]) + i = i + 8 + local ports = {} for a = i+1, numports*5+i, 5 do local portd = { type = tonumber(data[a]), @@ -94,7 +92,7 @@ function network_update() if not portd.direction then print(line) end end - local definition = GateDefinition.new(objref, name, desc, init, logic, input, global, ports, datasize, logicfuncc) + local definition = GateDefinition.new(objref, name, desc, init, logic, input, global, ports) Simulation.addgatedefinition(sim, definition) i = i + numports*5