remove gate ffi struct
This commit is contained in:
parent
5438de0adf
commit
a9056df54d
25
sim/gate.lua
25
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)
|
||||
|
@ -75,6 +75,7 @@ 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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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 = {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user