make sim queues use c objs

This commit is contained in:
Redo 2022-11-04 17:06:57 -06:00
parent 658bcc6ad8
commit 85aabd8dcf
4 changed files with 64 additions and 39 deletions

View File

@ -12,6 +12,7 @@ ffi.cdef [[
int** port_net_state_num;
int** port_net_in_queue;
struct Net** port_nets_c;
int objref;
};
]]
@ -40,6 +41,7 @@ function Gate.new(objref, definition)
gate.c.port_net_state_num = gate.port_net_state_num
gate.c.port_net_in_queue = gate.port_net_in_queue
gate.c.port_nets_c = gate.port_nets_c
gate.c.objref = gate.objref
return gate
end
@ -62,11 +64,6 @@ function Gate.setportstate(gate, index, state)
end
end
-- Logic Critical
function Gate.logic(gate)
gate.logic(gate)
end
function Gate.preinit(gate)
end

View File

@ -11,9 +11,11 @@ ffi.cdef [[
int* in_queue;
int* update_tick;
struct Gate** gates_update_c;
int id;
};
]]
local last_net_id = 1
function Group.new()
local net = {
-- Logic Critical
@ -33,7 +35,11 @@ function Group.new()
nwires = 0,
nout_ports = 0,
nin_ports = 0,
id = last_net_id,
}
last_net_id = last_net_id + 1
net.state[0] = 0
net.state_num[0] = 0
net.in_queue[0] = 0
@ -45,6 +51,7 @@ function Group.new()
net.c.in_queue = net.in_queue
net.c.update_tick = net.update_tick
net.c.gates_update_c = ffi.cast("struct Gate**", 0)
net.c.id = net.id
return net
end
@ -107,6 +114,7 @@ function Group.removewire(group, wire)
group.nout_ports = 0
group.nin_ports = 0
Simulation.remove_net(GSim, group)
Simulation.dequeuegroup(GSim, group)
end
@ -195,34 +203,30 @@ function Group.mergeinto(group, group2)
group.nout_ports = 0
group.nin_ports = 0
Simulation.remove_net(GSim, group)
Simulation.dequeuegroup(GSim, group)
end
-- Logic Critical
function Group.setstate(group, state)
if state ~= group.state[0] then
local sim = GSim
function Group.update_net_c(netc, tick)
local state = netc.state_num[0]>0 and 1 or 0
if state ~= netc.state[0] then
netc.state[0] = state
netc.update_tick[0] = tick
group.state[0] = state
group.update_tick[0] = sim.current_tick
local len = group.num_gates_update[0]
local len = netc.num_gates_update[0]
for i = 1, len do
local gate = group.gates_update[i]
if gate and gate.in_queue[0]==0 then
Simulation.queuegate(sim, gate)
local gatec = netc.gates_update_c[i]
if gatec and gatec.in_queue[0]==0 then
Simulation.queue_gate_c(GSim, gatec)
end
end
Simulation.queuegroupfx(sim, group)
local net = Simulation.net_from_netc(GSim, netc)
Simulation.queuegroupfx(GSim, net)
end
end
-- Logic Critical
function Group.update(group)
Group.setstate(group, group.state_num[0]>0 and 1 or 0)
end
function Group.rebuild_ports(net)
net.gates_update = {}
net.num_gates_update[0] = 0

View File

@ -54,9 +54,6 @@ function network_update()
local gate = GateDefinition.constructgate(definition, objref, position, rotation)
Simulation.addgate(sim, gate)
--print(gate.objref)
--Gate.init(gate)
--Gate.logic(gate)
i = i + 4
elseif data[i] == "RW" then

View File

@ -13,6 +13,7 @@ function Simulation.new(sim)
definitions = {},
wires = {},
gates = {},
nets = {},
nwires = 0,
ngates = 0,
ninports = 0,
@ -222,7 +223,9 @@ function Simulation.connectwire(sim, wire)
end
if Wire.getgroup(wire)==nil then
Group.addwire(Group.new(), wire)
local newnet = Group.new()
Simulation.add_net(sim, newnet)
Group.addwire(newnet, wire)
end
end
@ -236,15 +239,21 @@ function Simulation.connectport(sim, port)
end
if Port.getgroup(port) == nil then
Group.addport(Group.new(), port)
local newnet = Group.new()
Simulation.add_net(sim, newnet)
Group.addport(newnet, port)
end
end
-- Logic Critical
function Simulation.queuegate(sim, gate)
sim.gatequeue[sim.num_gatequeue+1] = gate
function Simulation.queue_gate_c(sim, gatec)
sim.gatequeue[sim.num_gatequeue+1] = gatec
sim.num_gatequeue = sim.num_gatequeue + 1
gate.in_queue[0] = 1
gatec.in_queue[0] = 1
end
function Simulation.queuegate(sim, gate)
Simulation.queue_gate_c(sim, gate.c)
end
function Simulation.queuegate_safe(sim, gate)
@ -274,10 +283,14 @@ function Simulation.queuegateinit(sim, gate)
end
-- Logic Critical
function Simulation.queuegroup(sim, group)
sim.groupqueue[sim.num_groupqueue+1] = group
function Simulation.queue_net_c(sim, netc)
sim.groupqueue[sim.num_groupqueue+1] = netc
sim.num_groupqueue = sim.num_groupqueue + 1
group.in_queue[0] = 1
netc.in_queue[0] = 1
end
function Simulation.queuegroup(sim, net)
Simulation.queue_net_c(sim, net.c)
end
function Simulation.queuegroup_safe(sim, group)
@ -288,7 +301,7 @@ end
function Simulation.dequeuegroup(sim, group)
if group.in_queue[0]~=0 then
array_remove(sim.groupqueue, group, true)
array_remove(sim.groupqueue, group.c, true)
sim.num_groupqueue = sim.num_groupqueue - 1
group.in_queue[0] = 0
end
@ -320,12 +333,11 @@ end
-- Logic Critical
function Simulation.ticklogic(sim)
for i = 1, sim.num_groupqueue do
local group = sim.groupqueue[i]
Group.update(group)
group.in_queue[0] = 0
local netc = sim.groupqueue[i]
Group.update_net_c(netc, sim.current_tick)
netc.in_queue[0] = 0
sim.groupqueue[i] = nil
end
--sim.groupqueue = {}
sim.num_groupqueue = 0
if sim.tickqueue[sim.current_tick] ~= nil then
@ -338,12 +350,12 @@ function Simulation.ticklogic(sim)
end
for i = 1, sim.num_gatequeue do
local gate = sim.gatequeue[i]
local gatec = sim.gatequeue[i]
local gate = sim.gate_from_gatec(sim, gatec)
gate.logic(gate)
gate.in_queue[0] = 0
sim.gatequeue[i] = nil
end
--sim.gatequeue = {}
sim.num_gatequeue = 0
sim.current_tick = sim.current_tick + 1
@ -403,3 +415,18 @@ function Simulation.sendcallbacks(sim)
sim.callbacks = nil
end
end
function Simulation.add_net(sim, net)
sim.nets[net.id] = net
end
function Simulation.remove_net(sim, net)
sim.nets[net.id] = nil
end
function Simulation.net_from_netc(sim, netc)
return sim.nets[netc.id] or error("no net for id "..netc.id)
end
function Simulation.gate_from_gatec(sim, gatec)
return sim.gates[gatec.objref] or error("no gate for objref "..gatec.objref)
end