From c62d7340b0b80ee416795fbbcb18b3e49e0ef007 Mon Sep 17 00:00:00 2001 From: Redo0 Date: Tue, 25 May 2021 20:22:02 -0500 Subject: [PATCH] make nets keep list of updated gates instead of ports --- sim/compile.lua | 68 ++++++++++++++++++++++++++++++++++++------------- sim/gate.lua | 9 ------- sim/group.lua | 40 +++++++++++++---------------- sim/main.lua | 4 +-- sim/port.lua | 24 ++++------------- sim/utility.lua | 8 ++++++ sim/wire.lua | 9 ------- 7 files changed, 82 insertions(+), 80 deletions(-) diff --git a/sim/compile.lua b/sim/compile.lua index f77ac32..70e2c37 100644 --- a/sim/compile.lua +++ b/sim/compile.lua @@ -2,36 +2,68 @@ local ffi = FFI or require("ffi") 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) -- assemble a list of 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) - all_nets[net] = net + all_nets_t[net] = net end local num_nets = 0 - for net_id, net in pairs(all_nets) do - num_nets = num_nets+1 + for net_id, net in pairs(all_nets_t) do + 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 -- construct each gate into an array - -- construct each group into an array - local group_idx = 0 - local array_nets = ffi.new("struct Net["..num_groups.."]") - for group_id, group in pairs(groups) do - local c_net = ffi.new("struct Net") + -- construct array of all nets + local c_nets = ffi.new("struct Net["..(#all_nets).."]") + for net_idx, net in ipairs(all_nets) do + local c_net = ffi.new("struct Net", #net.gates_update) - local ports_update = {} - for port_id, port in pairs(group.in_ports) do - if port.causeupdate then - num_ports_update = num - end + for gate_idx, gate in ipairs(net.gates_update) do + end - --c_net.ports_update = ffi.new("struct Port[".. - array_nets[group_idx] = c_net - group_idx = group_idx + 1 + c_nets[net_idx] = c_net end end @@ -39,6 +71,6 @@ function Simulation.decompile(sim) end -function Simulation.tickcompiled(sim) +function Simulation.tick_compiled(sim) end diff --git a/sim/gate.lua b/sim/gate.lua index 3aa96f1..4159fe0 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -1,15 +1,6 @@ Gate = {} -FFI.cdef[[ - struct Gate { - int objref; - int definition_objref; - bool in_queue; - struct Port ports[1]; - }; -]] - function Gate.new(objref, definition) local o = { objref = objref, diff --git a/sim/group.lua b/sim/group.lua index aff91c9..e5ff8a6 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -1,20 +1,6 @@ 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() local o = { state = false, @@ -23,7 +9,7 @@ function Group.new() wires = {}, out_ports = {}, in_ports = {}, - in_ports_update = {}, + gates_update = {}, state_num = 0, in_queue = false, @@ -49,6 +35,7 @@ function Group.addwire(group, wire) Wire.setgroup(wire, group) Wire.update(wire) + Simulation.queuegroup(GSim, group) end end @@ -115,13 +102,12 @@ function Group.addport(group, port) group.in_ports[port] = port 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)) end + + Group.rebuild_ports(group) end function Group.removeport(group, port) @@ -144,12 +130,11 @@ function Group.removeport(group, port) group.in_ports[port] = nil 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)) end + + Group.rebuild_ports(group) end function Group.mergewith(group, group2) @@ -196,8 +181,8 @@ function Group.setstate(group, state) group.state = state group.updatetick = sim.currenttick - for k, port in ipairs(group.in_ports_update) do - Simulation.queuegate(sim, Port.getgate(port)) + for k, gate in ipairs(group.gates_update) do + Simulation.queuegate(sim, gate) end Simulation.queuegroupfx(sim, group) @@ -207,3 +192,12 @@ end function Group.update(group) Group.setstate(group, group.state_num>0) 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 diff --git a/sim/main.lua b/sim/main.lua index ca57b84..02d8ad4 100644 --- a/sim/main.lua +++ b/sim/main.lua @@ -262,7 +262,7 @@ while 1 do local group = Wire.getgroup(wire) 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 numportsu = #group.in_ports_update + local numgatesu = #group.gates_update local numportso = 0; local numportson=0; for k, port in pairs(group.out_ports) do numportso = numportso+1 @@ -273,7 +273,7 @@ while 1 do "Wires: "..numwires.."\n".. "In Ports: " ..numportsi.."\n".. "Out Ports: "..numportso.."\n".. - "In Ports Update: "..numportsu.."\n".. + "Gates Update: "..numgatesu.."\n".. "Out Ports On: "..(group.state_num) ; end diff --git a/sim/port.lua b/sim/port.lua index e4a5381..5c219b9 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -15,20 +15,6 @@ PortDirections = { 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) local o = { type = type, @@ -44,13 +30,13 @@ end function Port.setstate(port, state) -- output state 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 - if state then - Port.getgroup(port).state_num = Port.getgroup(port).state_num + 1 - else - Port.getgroup(port).state_num = Port.getgroup(port).state_num - 1 + + if (group.state_num>0) ~= group.state then + Simulation.queuegroup(GSim, group) end - Simulation.queuegroup(GSim, Port.getgroup(port)) end end diff --git a/sim/utility.lua b/sim/utility.lua index 8c0684e..b9520b7 100644 --- a/sim/utility.lua +++ b/sim/utility.lua @@ -77,3 +77,11 @@ function array_remove(array, value) end error("element not in array") 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 diff --git a/sim/wire.lua b/sim/wire.lua index 818d381..9c1992b 100644 --- a/sim/wire.lua +++ b/sim/wire.lua @@ -1,15 +1,6 @@ Wire = {} -FFI.cdef[[ - struct Wire { - int objref; - int layer; - struct Net* group; - int bounds[6]; - }; -]] - function Wire.new(objref, layer, bounds) local o = { objref = objref,