remove c logic and data from network, more work on compiled gate funcs
This commit is contained in:
parent
1eba4bb2fa
commit
bcebfed077
Binary file not shown.
@ -1,17 +1,21 @@
|
|||||||
|
// Auto-generated by gencfuncs.lua
|
||||||
GATEFUNC(Diode) { setport(2, getport(1)); }
|
|
||||||
GATEFUNC(Not) { setport(2, !getport(1)); }
|
|
||||||
|
|
||||||
////
|
|
||||||
|
|
||||||
enum GateFuncs {
|
enum GateFuncs {
|
||||||
GateFunc_None,
|
GateFunc_None,
|
||||||
GateFunc_Diode = 1,
|
GateFunc_Diode,
|
||||||
GateFunc_Not = 2,
|
GateFunc_Not,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
GATEFUNC(Diode) {
|
||||||
|
setport(2, getport(1));
|
||||||
|
}
|
||||||
|
GATEFUNC(Not) {
|
||||||
|
setport(2, !getport(1));
|
||||||
|
}
|
||||||
|
|
||||||
GateFunc sim_logic_functions[] = {
|
GateFunc sim_logic_functions[] = {
|
||||||
0,
|
0,
|
||||||
GATEFUNCID(Diode),
|
GATEFUNCID(Diode),
|
||||||
GATEFUNCID(Not),
|
GATEFUNCID(Not),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
13
sim/compiled_sim_gates.lua
Normal file
13
sim/compiled_sim_gates.lua
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
-- Auto-generated by gencfuncs.lua
|
||||||
|
|
||||||
|
local cFuncsByName = {
|
||||||
|
["diode"] = 1,
|
||||||
|
["not"] = 2,
|
||||||
|
}
|
||||||
|
|
||||||
|
local cDataSizeByName = {
|
||||||
|
["diode"] = 0,
|
||||||
|
["not"] = 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
return cFuncsByName, cDataSizeByName
|
@ -7,7 +7,9 @@ GateDefinition = {
|
|||||||
input = function(gate, argv) end
|
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)
|
name = collapseescape(name)
|
||||||
init = collapseescape(init)
|
init = collapseescape(init)
|
||||||
@ -23,8 +25,8 @@ function GateDefinition.new(objref, name, description, init, logic, input, globa
|
|||||||
ports = ports or {},
|
ports = ports or {},
|
||||||
num_in_ports = 0,
|
num_in_ports = 0,
|
||||||
num_out_ports = 0,
|
num_out_ports = 0,
|
||||||
data_size_c = datasize,
|
data_size_c = cDataSizeByName[name:lower()] or 0,
|
||||||
logic_function_c = logicfuncc,
|
logic_function_c = cFuncsByName[name:lower()] or 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
local initfunc = loadstring(tostring(init))
|
local initfunc = loadstring(tostring(init))
|
||||||
|
@ -1,24 +1,63 @@
|
|||||||
|
local c_code = [[
|
||||||
|
// Auto-generated by gencfuncs.lua
|
||||||
|
|
||||||
local luatoc_subs = {
|
enum GateFuncs {
|
||||||
{"Gate%.setportstate%(gate, *", "setport%("},
|
GateFunc_None,
|
||||||
{"Gate%.getportstate%(gate, *", "getport%("},
|
%s
|
||||||
{"~=", "!="},
|
};
|
||||||
{"!= *0", ""},
|
|
||||||
{"elseif", "els2 if"},
|
%s
|
||||||
{"if", "if("},
|
|
||||||
{"else", "} else {"},
|
GateFunc sim_logic_functions[] = {
|
||||||
{"els2", "else"},
|
0,
|
||||||
{"end", "}"},
|
%s
|
||||||
{"then", ") {"},
|
};
|
||||||
{"return function(gate)", "GATEFUNC"},
|
|
||||||
|
]]
|
||||||
|
|
||||||
|
local lua_code = [[
|
||||||
|
-- Auto-generated by gencfuncs.lua
|
||||||
|
|
||||||
|
local cFuncsByName = {
|
||||||
|
%s
|
||||||
}
|
}
|
||||||
local function luatoc(f)
|
|
||||||
for i, sub in ipairs(luatoc_subs) do
|
local cDataSizeByName = {
|
||||||
f = f:gsub(sub[1], sub[2])
|
%s
|
||||||
end
|
}
|
||||||
return f
|
|
||||||
|
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
|
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
|
end
|
||||||
|
|
||||||
|
local gates = {
|
||||||
|
{ name = "Diode", func = "setport(2, getport(1));" },
|
||||||
|
{ name = "Not" , func = "setport(2, !getport(1));" },
|
||||||
|
}
|
||||||
|
exportGates(gates)
|
||||||
|
@ -75,12 +75,10 @@ function network_update()
|
|||||||
local logic = data[i+5]
|
local logic = data[i+5]
|
||||||
local input = data[i+6]
|
local input = data[i+6]
|
||||||
local global = data[i+7]
|
local global = data[i+7]
|
||||||
local datasize = tonumber(data[i+8])
|
local numports = tonumber(data[i+8])
|
||||||
local logicfuncc = tonumber(data[i+9])
|
i = i + 8
|
||||||
local numports = tonumber(data[i+10])
|
|
||||||
local ports = {}
|
|
||||||
i = i + 10
|
|
||||||
|
|
||||||
|
local ports = {}
|
||||||
for a = i+1, numports*5+i, 5 do
|
for a = i+1, numports*5+i, 5 do
|
||||||
local portd = {
|
local portd = {
|
||||||
type = tonumber(data[a]),
|
type = tonumber(data[a]),
|
||||||
@ -94,7 +92,7 @@ function network_update()
|
|||||||
if not portd.direction then print(line) end
|
if not portd.direction then print(line) end
|
||||||
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)
|
Simulation.addgatedefinition(sim, definition)
|
||||||
|
|
||||||
i = i + numports*5
|
i = i + numports*5
|
||||||
|
Loading…
x
Reference in New Issue
Block a user