diff --git a/sim/gate.lua b/sim/gate.lua index ae3bcbc..e07d0ad 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -3,21 +3,11 @@ local ffi = FFI or require("ffi") Gate = {} -ffi.cdef [[ - struct OutPort { - struct Net* net; - int state; - }; - struct Gate { - int in_queue; - struct OutPort ports[0]; - }; -]] - function Gate.new(objref, definition) local gate = { -- Logic Critical - c = nil, + in_queue = ffi.new("int"), + port_states = ffi.new("int["..(#definition.ports+1).."]"), logic = definition.logic, ports = {}, port_nets = {}, @@ -25,23 +15,20 @@ function Gate.new(objref, definition) objref = objref, definition = definition, } - local cdata = ffi.new("char["..(ffi.sizeof("struct Gate") + ffi.sizeof("struct OutPort")*(#definition.ports+1)).."]") - gate.c = ffi.cast("struct Gate*", cdata) - gate.c.in_queue = 0 return gate end -- Logic Critical function Gate.getportstate(gate, index) - return gate.c.ports[index].state + return gate.port_states[index] end -- Logic Critical function Gate.setportstate(gate, index, state) - if state ~= gate.c.ports[index].state then + if state ~= gate.port_states[index] then local group = gate.port_nets[index] - group.state_num = group.state_num - gate.c.ports[index].state + state - gate.c.ports[index].state = state + group.state_num = group.state_num - gate.port_states[index] + state + gate.ports_states[index] = state if ((group.state_num>0) ~= (group.state==1)) and (group.in_queue==0) then Simulation.queuegroup(GSim, group) diff --git a/sim/gatedef.lua b/sim/gatedef.lua index 7016ad7..6748d0b 100644 --- a/sim/gatedef.lua +++ b/sim/gatedef.lua @@ -74,7 +74,8 @@ end function GateDefinition.constructgate(def, objref, position, rotation) local gate = Gate.new(objref, def) - + + gate.in_queue = 0 for i = 1, #def.ports do local portd = def.ports[i] local type = portd.type @@ -102,7 +103,7 @@ function GateDefinition.constructgate(def, objref, position, rotation) gate.ports[port.idx] = port gate.port_nets[port.idx] = nil - gate.c.ports[port.idx].state = 0 + gate.ports_states[port.idx] = 0 end return gate diff --git a/sim/group.lua b/sim/group.lua index 5d371df..dc148dc 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -195,7 +195,7 @@ function Group.setstate(group, state) local len = group.num_gates_update for i = 1, len do local gate = group.gates_update[i] - if gate and gate.c.in_queue==0 then + if gate and gate.in_queue==0 then Simulation.queuegate(sim, gate) end end diff --git a/sim/port.lua b/sim/port.lua index 5a92e25..305c80a 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -62,5 +62,5 @@ function Port.gettype(port) end function Port.getstate(port) - return Port.getgate(port).c.ports[port.idx].state + return Port.getgate(port).port_states[port.idx] end diff --git a/sim/simulation.lua b/sim/simulation.lua index f4ab6f8..010a8f6 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -244,11 +244,11 @@ end function Simulation.queuegate(sim, gate) sim.gatequeue[sim.num_gatequeue+1] = gate sim.num_gatequeue = sim.num_gatequeue + 1 - gate.c.in_queue = 1 + gate.in_queue = 1 end function Simulation.queuegate_safe(sim, gate) - if gate.c.in_queue==0 then + if gate.in_queue==0 then Simulation.queuegate(sim, gate) end end @@ -296,10 +296,10 @@ function Simulation.dequeuegroup(sim, group) end function Simulation.dequeuegate(sim, gate) - if gate.c.in_queue~=0 then + if gate.in_queue~=0 then array_remove(sim.gatequeue, gate, true) sim.num_gatequeue = sim.num_gatequeue - 1 - gate.c.in_queue = 0 + gate.in_queue = 0 end if sim.inputqueue~=nil then sim.inputqueue[gate] = nil end if sim.initqueue ~=nil then sim.initqueue [gate] = nil end @@ -330,7 +330,7 @@ function Simulation.ticklogic(sim) if sim.tickqueue[sim.current_tick] ~= nil then for i, gate in pairs(sim.tickqueue[sim.current_tick]) do - if gate.c.in_queue==0 then + if gate.in_queue==0 then Simulation.queuegate(sim, gate) end end @@ -340,7 +340,7 @@ function Simulation.ticklogic(sim) for i = 1, sim.num_gatequeue do local gate = sim.gatequeue[i] gate.logic(gate) - gate.c.in_queue = 0 + gate.in_queue = 0 sim.gatequeue[i] = nil end --sim.gatequeue = {}