make sim queues use c objs
This commit is contained in:
parent
658bcc6ad8
commit
85aabd8dcf
@ -12,6 +12,7 @@ ffi.cdef [[
|
|||||||
int** port_net_state_num;
|
int** port_net_state_num;
|
||||||
int** port_net_in_queue;
|
int** port_net_in_queue;
|
||||||
struct Net** port_nets_c;
|
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_state_num = gate.port_net_state_num
|
||||||
gate.c.port_net_in_queue = gate.port_net_in_queue
|
gate.c.port_net_in_queue = gate.port_net_in_queue
|
||||||
gate.c.port_nets_c = gate.port_nets_c
|
gate.c.port_nets_c = gate.port_nets_c
|
||||||
|
gate.c.objref = gate.objref
|
||||||
|
|
||||||
return gate
|
return gate
|
||||||
end
|
end
|
||||||
@ -62,11 +64,6 @@ function Gate.setportstate(gate, index, state)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Logic Critical
|
|
||||||
function Gate.logic(gate)
|
|
||||||
gate.logic(gate)
|
|
||||||
end
|
|
||||||
|
|
||||||
function Gate.preinit(gate)
|
function Gate.preinit(gate)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -11,9 +11,11 @@ ffi.cdef [[
|
|||||||
int* in_queue;
|
int* in_queue;
|
||||||
int* update_tick;
|
int* update_tick;
|
||||||
struct Gate** gates_update_c;
|
struct Gate** gates_update_c;
|
||||||
|
int id;
|
||||||
};
|
};
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
local last_net_id = 1
|
||||||
function Group.new()
|
function Group.new()
|
||||||
local net = {
|
local net = {
|
||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
@ -33,7 +35,11 @@ function Group.new()
|
|||||||
nwires = 0,
|
nwires = 0,
|
||||||
nout_ports = 0,
|
nout_ports = 0,
|
||||||
nin_ports = 0,
|
nin_ports = 0,
|
||||||
|
|
||||||
|
id = last_net_id,
|
||||||
}
|
}
|
||||||
|
last_net_id = last_net_id + 1
|
||||||
|
|
||||||
net.state[0] = 0
|
net.state[0] = 0
|
||||||
net.state_num[0] = 0
|
net.state_num[0] = 0
|
||||||
net.in_queue[0] = 0
|
net.in_queue[0] = 0
|
||||||
@ -45,6 +51,7 @@ function Group.new()
|
|||||||
net.c.in_queue = net.in_queue
|
net.c.in_queue = net.in_queue
|
||||||
net.c.update_tick = net.update_tick
|
net.c.update_tick = net.update_tick
|
||||||
net.c.gates_update_c = ffi.cast("struct Gate**", 0)
|
net.c.gates_update_c = ffi.cast("struct Gate**", 0)
|
||||||
|
net.c.id = net.id
|
||||||
|
|
||||||
return net
|
return net
|
||||||
end
|
end
|
||||||
@ -107,6 +114,7 @@ function Group.removewire(group, wire)
|
|||||||
group.nout_ports = 0
|
group.nout_ports = 0
|
||||||
group.nin_ports = 0
|
group.nin_ports = 0
|
||||||
|
|
||||||
|
Simulation.remove_net(GSim, group)
|
||||||
Simulation.dequeuegroup(GSim, group)
|
Simulation.dequeuegroup(GSim, group)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -195,34 +203,30 @@ function Group.mergeinto(group, group2)
|
|||||||
group.nout_ports = 0
|
group.nout_ports = 0
|
||||||
group.nin_ports = 0
|
group.nin_ports = 0
|
||||||
|
|
||||||
|
Simulation.remove_net(GSim, group)
|
||||||
Simulation.dequeuegroup(GSim, group)
|
Simulation.dequeuegroup(GSim, group)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Group.setstate(group, state)
|
function Group.update_net_c(netc, tick)
|
||||||
if state ~= group.state[0] then
|
local state = netc.state_num[0]>0 and 1 or 0
|
||||||
local sim = GSim
|
if state ~= netc.state[0] then
|
||||||
|
netc.state[0] = state
|
||||||
|
netc.update_tick[0] = tick
|
||||||
|
|
||||||
group.state[0] = state
|
local len = netc.num_gates_update[0]
|
||||||
group.update_tick[0] = sim.current_tick
|
|
||||||
|
|
||||||
local len = group.num_gates_update[0]
|
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
local gate = group.gates_update[i]
|
local gatec = netc.gates_update_c[i]
|
||||||
if gate and gate.in_queue[0]==0 then
|
if gatec and gatec.in_queue[0]==0 then
|
||||||
Simulation.queuegate(sim, gate)
|
Simulation.queue_gate_c(GSim, gatec)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Simulation.queuegroupfx(sim, group)
|
local net = Simulation.net_from_netc(GSim, netc)
|
||||||
|
Simulation.queuegroupfx(GSim, net)
|
||||||
end
|
end
|
||||||
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)
|
function Group.rebuild_ports(net)
|
||||||
net.gates_update = {}
|
net.gates_update = {}
|
||||||
net.num_gates_update[0] = 0
|
net.num_gates_update[0] = 0
|
||||||
|
@ -54,9 +54,6 @@ function network_update()
|
|||||||
local gate = GateDefinition.constructgate(definition, objref, position, rotation)
|
local gate = GateDefinition.constructgate(definition, objref, position, rotation)
|
||||||
|
|
||||||
Simulation.addgate(sim, gate)
|
Simulation.addgate(sim, gate)
|
||||||
--print(gate.objref)
|
|
||||||
--Gate.init(gate)
|
|
||||||
--Gate.logic(gate)
|
|
||||||
|
|
||||||
i = i + 4
|
i = i + 4
|
||||||
elseif data[i] == "RW" then
|
elseif data[i] == "RW" then
|
||||||
|
@ -13,6 +13,7 @@ function Simulation.new(sim)
|
|||||||
definitions = {},
|
definitions = {},
|
||||||
wires = {},
|
wires = {},
|
||||||
gates = {},
|
gates = {},
|
||||||
|
nets = {},
|
||||||
nwires = 0,
|
nwires = 0,
|
||||||
ngates = 0,
|
ngates = 0,
|
||||||
ninports = 0,
|
ninports = 0,
|
||||||
@ -222,7 +223,9 @@ function Simulation.connectwire(sim, wire)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if Wire.getgroup(wire)==nil then
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -236,15 +239,21 @@ function Simulation.connectport(sim, port)
|
|||||||
end
|
end
|
||||||
|
|
||||||
if Port.getgroup(port) == nil then
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Simulation.queuegate(sim, gate)
|
function Simulation.queue_gate_c(sim, gatec)
|
||||||
sim.gatequeue[sim.num_gatequeue+1] = gate
|
sim.gatequeue[sim.num_gatequeue+1] = gatec
|
||||||
sim.num_gatequeue = sim.num_gatequeue + 1
|
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
|
end
|
||||||
|
|
||||||
function Simulation.queuegate_safe(sim, gate)
|
function Simulation.queuegate_safe(sim, gate)
|
||||||
@ -274,10 +283,14 @@ function Simulation.queuegateinit(sim, gate)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Simulation.queuegroup(sim, group)
|
function Simulation.queue_net_c(sim, netc)
|
||||||
sim.groupqueue[sim.num_groupqueue+1] = group
|
sim.groupqueue[sim.num_groupqueue+1] = netc
|
||||||
sim.num_groupqueue = sim.num_groupqueue + 1
|
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
|
end
|
||||||
|
|
||||||
function Simulation.queuegroup_safe(sim, group)
|
function Simulation.queuegroup_safe(sim, group)
|
||||||
@ -288,7 +301,7 @@ end
|
|||||||
|
|
||||||
function Simulation.dequeuegroup(sim, group)
|
function Simulation.dequeuegroup(sim, group)
|
||||||
if group.in_queue[0]~=0 then
|
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
|
sim.num_groupqueue = sim.num_groupqueue - 1
|
||||||
group.in_queue[0] = 0
|
group.in_queue[0] = 0
|
||||||
end
|
end
|
||||||
@ -320,12 +333,11 @@ end
|
|||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Simulation.ticklogic(sim)
|
function Simulation.ticklogic(sim)
|
||||||
for i = 1, sim.num_groupqueue do
|
for i = 1, sim.num_groupqueue do
|
||||||
local group = sim.groupqueue[i]
|
local netc = sim.groupqueue[i]
|
||||||
Group.update(group)
|
Group.update_net_c(netc, sim.current_tick)
|
||||||
group.in_queue[0] = 0
|
netc.in_queue[0] = 0
|
||||||
sim.groupqueue[i] = nil
|
sim.groupqueue[i] = nil
|
||||||
end
|
end
|
||||||
--sim.groupqueue = {}
|
|
||||||
sim.num_groupqueue = 0
|
sim.num_groupqueue = 0
|
||||||
|
|
||||||
if sim.tickqueue[sim.current_tick] ~= nil then
|
if sim.tickqueue[sim.current_tick] ~= nil then
|
||||||
@ -338,12 +350,12 @@ function Simulation.ticklogic(sim)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for i = 1, sim.num_gatequeue do
|
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.logic(gate)
|
||||||
gate.in_queue[0] = 0
|
gate.in_queue[0] = 0
|
||||||
sim.gatequeue[i] = nil
|
sim.gatequeue[i] = nil
|
||||||
end
|
end
|
||||||
--sim.gatequeue = {}
|
|
||||||
sim.num_gatequeue = 0
|
sim.num_gatequeue = 0
|
||||||
|
|
||||||
sim.current_tick = sim.current_tick + 1
|
sim.current_tick = sim.current_tick + 1
|
||||||
@ -403,3 +415,18 @@ function Simulation.sendcallbacks(sim)
|
|||||||
sim.callbacks = nil
|
sim.callbacks = nil
|
||||||
end
|
end
|
||||||
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user