From 968613a3fc9245d8dbb85664b5c174f0a912e4fa Mon Sep 17 00:00:00 2001 From: Redo0 Date: Tue, 25 May 2021 21:23:51 -0500 Subject: [PATCH] rename some variables and begin compiled sim c code --- sim/compile.lua | 27 --------------------------- sim/compiled_sim.c | 7 +++++++ sim/gate.lua | 2 +- sim/group.lua | 4 ++-- sim/port.lua | 4 ++-- sim/simulation.lua | 26 +++++++++++++------------- 6 files changed, 25 insertions(+), 45 deletions(-) create mode 100644 sim/compiled_sim.c diff --git a/sim/compile.lua b/sim/compile.lua index 70e2c37..db4930e 100644 --- a/sim/compile.lua +++ b/sim/compile.lua @@ -3,34 +3,7 @@ local ffi = FFI or require("ffi") Simulation = Simulation or {} ffi.cdef[[ - struct Wire { - int objref; - int layer; - struct Net* group; - int bounds[6]; - }; - struct Port { - bool state; - char type; - struct Gate* gate; - struct Net* group; - }; - - struct Gate { - int logic_ref; - bool in_queue; - struct Port ports[?]; - }; - - struct Net { - bool state; - bool in_queue; - int updatetick; - int state_num; - int num_gates_update; - struct Gate* gates_update[?]; - }; ]] function Simulation.compile(sim) diff --git a/sim/compiled_sim.c b/sim/compiled_sim.c new file mode 100644 index 0000000..442f766 --- /dev/null +++ b/sim/compiled_sim.c @@ -0,0 +1,7 @@ + +void sim_init(int num_gates, int num_nets); +void sim_add_gate(); +void sim_add_net(); +void sim_tick(); +void sim_get_net_state(int objref); +void sim_get_port_state(int objref, int index); diff --git a/sim/gate.lua b/sim/gate.lua index f8c278c..c0d4025 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -60,7 +60,7 @@ function Gate.queue(gate, delay) end function Gate.gettick(gate) - return GSim.currenttick + return GSim.current_tick end function Gate.getdefinition(gate) diff --git a/sim/group.lua b/sim/group.lua index b699202..a1f52f4 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -5,7 +5,7 @@ function Group.new() local o = { state = false, fxstate = false, - updatetick = 0, + update_tick = 0, wires = {}, out_ports = {}, in_ports = {}, @@ -179,7 +179,7 @@ function Group.setstate(group, state) local sim = GSim group.state = state - group.updatetick = sim.currenttick + group.update_tick = sim.current_tick for k, gate in ipairs(group.gates_update) do Simulation.queuegate(sim, gate) diff --git a/sim/port.lua b/sim/port.lua index fdd6fec..ba55e24 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -38,14 +38,14 @@ function Port.isrising(port) if port.group == nil then return false end - return port.group.state and (port.group.updatetick == GSim.currenttick) + return port.group.state and (port.group.update_tick == GSim.current_tick) end function Port.isfalling(port) if port.group == nil then return false end - return port.group.state == false and (port.updatetick == GSim.currenttick) + return port.group.state == false and (port.group.update_tick == GSim.current_tick) end function Port.getgate(port) diff --git a/sim/simulation.lua b/sim/simulation.lua index 4433d86..db7c979 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -6,12 +6,12 @@ function Simulation.new(sim) definitions = {}, wires = {}, gates = {}, - + nwires = 0, ngates = 0, ninports = 0, noutports = 0, - + groupqueue = {}, groupfxqueue = {}, gatequeue = {}, @@ -20,10 +20,10 @@ function Simulation.new(sim) tickqueue = {}, inputqueue_nonempty = false, initqueue_nonempty = false, - + callbacks = nil, - - currenttick = 0 + + current_tick = 0, } setmetatable(o, sim) sim.__index = sim @@ -34,15 +34,15 @@ function Simulation.addtoworld(sim, obj, x, y, z) if sim[x] == nil then sim[x] = {} end - + if sim[x][y] == nil then sim[x][y] = {} end - + if sim[x][y][z] == nil then sim[x][y][z] = {} end - + sim[x][y][z][obj] = obj end @@ -245,7 +245,7 @@ function Simulation.queuegate(sim, gate) end function Simulation.queuegatelater(sim, gate, delay) - local tick = sim.currenttick + delay + local tick = sim.current_tick + delay if sim.tickqueue[tick] == nil then sim.tickqueue[tick] = {} end @@ -322,11 +322,11 @@ function Simulation.tick(sim) sim.inputqueue_nonempty = false end - if sim.tickqueue[sim.currenttick] ~= nil then - for i, gate in pairs(sim.tickqueue[sim.currenttick]) do + if sim.tickqueue[sim.current_tick] ~= nil then + for i, gate in pairs(sim.tickqueue[sim.current_tick]) do Simulation.queuegate(sim, gate) end - sim.tickqueue[sim.currenttick] = nil + sim.tickqueue[sim.current_tick] = nil end for k, gate in ipairs(sim.gatequeue) do @@ -335,7 +335,7 @@ function Simulation.tick(sim) end sim.gatequeue = {} - sim.currenttick = sim.currenttick + 1 + sim.current_tick = sim.current_tick + 1 end function Simulation.sendfxupdate(sim)