make nets keep list of updated gates instead of ports
This commit is contained in:
parent
e92cc50186
commit
c62d7340b0
@ -2,36 +2,68 @@
|
|||||||
local ffi = FFI or require("ffi")
|
local ffi = FFI or require("ffi")
|
||||||
Simulation = Simulation or {}
|
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)
|
function Simulation.compile(sim)
|
||||||
-- assemble a list of all nets
|
-- assemble a list of all nets
|
||||||
local all_nets = {}
|
local all_nets = {}
|
||||||
for wire_idx, wire in pairs(sim.wires) do
|
local all_nets_t = {}
|
||||||
|
for k, wire in pairs(sim.wires) do
|
||||||
local net = Wire.getgroup(wire)
|
local net = Wire.getgroup(wire)
|
||||||
all_nets[net] = net
|
all_nets_t[net] = net
|
||||||
end
|
end
|
||||||
local num_nets = 0
|
local num_nets = 0
|
||||||
for net_id, net in pairs(all_nets) do
|
for net_id, net in pairs(all_nets_t) do
|
||||||
num_nets = num_nets+1
|
table.insert(all_nets, net)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- assemble a list of all gates
|
||||||
|
local all_gates = {}
|
||||||
|
for k, gate in pairs(sim.gates) do
|
||||||
|
table.insert(all_gates, gate)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- construct each gate into an array
|
-- construct each gate into an array
|
||||||
|
|
||||||
-- construct each group into an array
|
-- construct array of all nets
|
||||||
local group_idx = 0
|
local c_nets = ffi.new("struct Net["..(#all_nets).."]")
|
||||||
local array_nets = ffi.new("struct Net["..num_groups.."]")
|
for net_idx, net in ipairs(all_nets) do
|
||||||
for group_id, group in pairs(groups) do
|
local c_net = ffi.new("struct Net", #net.gates_update)
|
||||||
local c_net = ffi.new("struct Net")
|
|
||||||
|
|
||||||
local ports_update = {}
|
for gate_idx, gate in ipairs(net.gates_update) do
|
||||||
for port_id, port in pairs(group.in_ports) do
|
|
||||||
if port.causeupdate then
|
|
||||||
num_ports_update = num
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
--c_net.ports_update = ffi.new("struct Port["..
|
|
||||||
|
|
||||||
array_nets[group_idx] = c_net
|
c_nets[net_idx] = c_net
|
||||||
group_idx = group_idx + 1
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -39,6 +71,6 @@ function Simulation.decompile(sim)
|
|||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function Simulation.tickcompiled(sim)
|
function Simulation.tick_compiled(sim)
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -1,15 +1,6 @@
|
|||||||
|
|
||||||
Gate = {}
|
Gate = {}
|
||||||
|
|
||||||
FFI.cdef[[
|
|
||||||
struct Gate {
|
|
||||||
int objref;
|
|
||||||
int definition_objref;
|
|
||||||
bool in_queue;
|
|
||||||
struct Port ports[1];
|
|
||||||
};
|
|
||||||
]]
|
|
||||||
|
|
||||||
function Gate.new(objref, definition)
|
function Gate.new(objref, definition)
|
||||||
local o = {
|
local o = {
|
||||||
objref = objref,
|
objref = objref,
|
||||||
|
@ -1,20 +1,6 @@
|
|||||||
|
|
||||||
Group = {}
|
Group = {}
|
||||||
|
|
||||||
FFI.cdef[[
|
|
||||||
struct Port;
|
|
||||||
struct Net {
|
|
||||||
bool state;
|
|
||||||
bool fxstate;
|
|
||||||
bool in_queue;
|
|
||||||
int updatetick;
|
|
||||||
int internal_ref;
|
|
||||||
int state_num;
|
|
||||||
int num_in_ports_update;
|
|
||||||
struct Port* in_ports_update[1];
|
|
||||||
};
|
|
||||||
]]
|
|
||||||
|
|
||||||
function Group.new()
|
function Group.new()
|
||||||
local o = {
|
local o = {
|
||||||
state = false,
|
state = false,
|
||||||
@ -23,7 +9,7 @@ function Group.new()
|
|||||||
wires = {},
|
wires = {},
|
||||||
out_ports = {},
|
out_ports = {},
|
||||||
in_ports = {},
|
in_ports = {},
|
||||||
in_ports_update = {},
|
gates_update = {},
|
||||||
|
|
||||||
state_num = 0,
|
state_num = 0,
|
||||||
in_queue = false,
|
in_queue = false,
|
||||||
@ -49,6 +35,7 @@ function Group.addwire(group, wire)
|
|||||||
|
|
||||||
Wire.setgroup(wire, group)
|
Wire.setgroup(wire, group)
|
||||||
Wire.update(wire)
|
Wire.update(wire)
|
||||||
|
|
||||||
Simulation.queuegroup(GSim, group)
|
Simulation.queuegroup(GSim, group)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -115,13 +102,12 @@ function Group.addport(group, port)
|
|||||||
|
|
||||||
group.in_ports[port] = port
|
group.in_ports[port] = port
|
||||||
group.nin_ports = group.nin_ports + 1
|
group.nin_ports = group.nin_ports + 1
|
||||||
if port.causeupdate then
|
|
||||||
table.insert(group.in_ports_update, port)
|
|
||||||
end
|
|
||||||
|
|
||||||
Simulation.queuegate(GSim, Port.getgate(port))
|
Simulation.queuegate(GSim, Port.getgate(port))
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Group.rebuild_ports(group)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Group.removeport(group, port)
|
function Group.removeport(group, port)
|
||||||
@ -144,12 +130,11 @@ function Group.removeport(group, port)
|
|||||||
|
|
||||||
group.in_ports[port] = nil
|
group.in_ports[port] = nil
|
||||||
group.nin_ports = group.nin_ports - 1
|
group.nin_ports = group.nin_ports - 1
|
||||||
if port.causeupdate then
|
|
||||||
array_remove(group.in_ports_update, port)
|
|
||||||
end
|
|
||||||
|
|
||||||
Simulation.queuegate(GSim, Port.getgate(port))
|
Simulation.queuegate(GSim, Port.getgate(port))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Group.rebuild_ports(group)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Group.mergewith(group, group2)
|
function Group.mergewith(group, group2)
|
||||||
@ -196,8 +181,8 @@ function Group.setstate(group, state)
|
|||||||
group.state = state
|
group.state = state
|
||||||
group.updatetick = sim.currenttick
|
group.updatetick = sim.currenttick
|
||||||
|
|
||||||
for k, port in ipairs(group.in_ports_update) do
|
for k, gate in ipairs(group.gates_update) do
|
||||||
Simulation.queuegate(sim, Port.getgate(port))
|
Simulation.queuegate(sim, gate)
|
||||||
end
|
end
|
||||||
|
|
||||||
Simulation.queuegroupfx(sim, group)
|
Simulation.queuegroupfx(sim, group)
|
||||||
@ -207,3 +192,12 @@ end
|
|||||||
function Group.update(group)
|
function Group.update(group)
|
||||||
Group.setstate(group, group.state_num>0)
|
Group.setstate(group, group.state_num>0)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Group.rebuild_ports(group)
|
||||||
|
group.gates_update = {}
|
||||||
|
for k, port in pairs(group.in_ports) do
|
||||||
|
if port.causeupdate then
|
||||||
|
array_add(group.gates_update, Port.getgate(port))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
@ -262,7 +262,7 @@ while 1 do
|
|||||||
local group = Wire.getgroup(wire)
|
local group = Wire.getgroup(wire)
|
||||||
local numwires = 0; for k, wire2 in pairs(group.wires ) do numwires = numwires +1 end
|
local numwires = 0; for k, wire2 in pairs(group.wires ) do numwires = numwires +1 end
|
||||||
local numportsi = 0; for k, port in pairs(group.in_ports ) do numportsi = numportsi+1 end
|
local numportsi = 0; for k, port in pairs(group.in_ports ) do numportsi = numportsi+1 end
|
||||||
local numportsu = #group.in_ports_update
|
local numgatesu = #group.gates_update
|
||||||
local numportso = 0; local numportson=0;
|
local numportso = 0; local numportson=0;
|
||||||
for k, port in pairs(group.out_ports) do
|
for k, port in pairs(group.out_ports) do
|
||||||
numportso = numportso+1
|
numportso = numportso+1
|
||||||
@ -273,7 +273,7 @@ while 1 do
|
|||||||
"Wires: "..numwires.."\n"..
|
"Wires: "..numwires.."\n"..
|
||||||
"In Ports: " ..numportsi.."\n"..
|
"In Ports: " ..numportsi.."\n"..
|
||||||
"Out Ports: "..numportso.."\n"..
|
"Out Ports: "..numportso.."\n"..
|
||||||
"In Ports Update: "..numportsu.."\n"..
|
"Gates Update: "..numgatesu.."\n"..
|
||||||
"Out Ports On: "..(group.state_num)
|
"Out Ports On: "..(group.state_num)
|
||||||
;
|
;
|
||||||
end
|
end
|
||||||
|
24
sim/port.lua
24
sim/port.lua
@ -15,20 +15,6 @@ PortDirections = {
|
|||||||
|
|
||||||
Port = {}
|
Port = {}
|
||||||
|
|
||||||
FFI.cdef[[
|
|
||||||
struct Gate;
|
|
||||||
struct Net;
|
|
||||||
struct Port {
|
|
||||||
bool state;
|
|
||||||
char type;
|
|
||||||
char direction;
|
|
||||||
bool causeupdate;
|
|
||||||
int position[3];
|
|
||||||
struct Gate* gate;
|
|
||||||
struct Net* group;
|
|
||||||
};
|
|
||||||
]]
|
|
||||||
|
|
||||||
function Port.new(type, direction, position, causeupdate)
|
function Port.new(type, direction, position, causeupdate)
|
||||||
local o = {
|
local o = {
|
||||||
type = type,
|
type = type,
|
||||||
@ -44,13 +30,13 @@ end
|
|||||||
|
|
||||||
function Port.setstate(port, state) -- output state
|
function Port.setstate(port, state) -- output state
|
||||||
if state ~= port.state then
|
if state ~= port.state then
|
||||||
|
local group = port.group
|
||||||
|
group.state_num = group.state_num - (port.state and 1 or 0) + (state and 1 or 0)
|
||||||
port.state = state
|
port.state = state
|
||||||
if state then
|
|
||||||
Port.getgroup(port).state_num = Port.getgroup(port).state_num + 1
|
if (group.state_num>0) ~= group.state then
|
||||||
else
|
Simulation.queuegroup(GSim, group)
|
||||||
Port.getgroup(port).state_num = Port.getgroup(port).state_num - 1
|
|
||||||
end
|
end
|
||||||
Simulation.queuegroup(GSim, Port.getgroup(port))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -77,3 +77,11 @@ function array_remove(array, value)
|
|||||||
end
|
end
|
||||||
error("element not in array")
|
error("element not in array")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function array_add(array, value)
|
||||||
|
for i = 1, #array do
|
||||||
|
local v = array[i]
|
||||||
|
if v==value then return end
|
||||||
|
end
|
||||||
|
table.insert(array, value)
|
||||||
|
end
|
||||||
|
@ -1,15 +1,6 @@
|
|||||||
|
|
||||||
Wire = {}
|
Wire = {}
|
||||||
|
|
||||||
FFI.cdef[[
|
|
||||||
struct Wire {
|
|
||||||
int objref;
|
|
||||||
int layer;
|
|
||||||
struct Net* group;
|
|
||||||
int bounds[6];
|
|
||||||
};
|
|
||||||
]]
|
|
||||||
|
|
||||||
function Wire.new(objref, layer, bounds)
|
function Wire.new(objref, layer, bounds)
|
||||||
local o = {
|
local o = {
|
||||||
objref = objref,
|
objref = objref,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user