From 19d2e36fd6e81d4696756479681213e5c56351a9 Mon Sep 17 00:00:00 2001 From: Redo0 Date: Sat, 5 Jun 2021 18:41:50 -0500 Subject: [PATCH] more optimizations: move port states into gates, inline net queue checks --- sim/gate.lua | 31 ++++++++++++++++--------------- sim/gatedef.lua | 12 +++++++++--- sim/group.lua | 13 ++++--------- sim/network.lua | 2 -- sim/port.lua | 13 ++++--------- sim/simulation.lua | 11 +++++------ 6 files changed, 38 insertions(+), 44 deletions(-) diff --git a/sim/gate.lua b/sim/gate.lua index 5aa11fe..9aa64d8 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -5,13 +5,11 @@ Gate = {} function Gate.new(objref, definition) local o = { - --in_queue = ffi.new("bool", false), - --in_queue = false, - --in_queue = ffi.new("long long", 0), in_queue = 0, - port_nets = {}, logic = definition.logic, ports = {}, + port_nets = {}, + port_states = {}, objref = objref, definition = definition, @@ -19,30 +17,33 @@ function Gate.new(objref, definition) return o end -function Gate.addport(gate, port) - gate.ports[#gate.ports+1] = port - Port.setgate(port, gate) -end - -- Logic Critical function Gate.getportstate(gate, index) + --return gate[index*2].state return gate.port_nets[index].state end -- Logic Critical function Gate.setportstate(gate, index, state) - local port = gate.ports[index] - if state ~= port.state then - local group = port.group - group.state_num = group.state_num - port.state + state - port.state = state + --if state ~= gate[index*2+1] then + if state ~= gate.port_states[index] then + --local group = gate[index*2] + local group = gate.port_nets[index] + --group.state_num = group.state_num - gate[index*2+1] + state + group.state_num = group.state_num - gate.port_states[index] + state + --gate[index*2+1] = state + gate.port_states[index] = state - if (group.state_num>0) ~= (group.state==1) then + if (group.state_num>0) ~= (group.state==1) and (group.in_queue==0) then Simulation.queuegroup(GSim, group) end end end +function Gate.preinit(gate) + +end + function Gate.initdata(gate) gate.data = {} end diff --git a/sim/gatedef.lua b/sim/gatedef.lua index 8699550..f14cb7b 100644 --- a/sim/gatedef.lua +++ b/sim/gatedef.lua @@ -81,9 +81,15 @@ function GateDefinition.constructgate(def, objref, position, rotation) pos[1] = -pos[2] pos[2] = x end - - Gate.addport(gate, Port.new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, i)) + + local port = Port.new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, i, gate) + + gate.ports[port.idx] = port + --gate[port.idx*2] = nil + --gate[port.idx*2+1] = 0 + gate.port_nets[port.idx] = nil + gate.port_states[port.idx] = 0 end - + return gate end diff --git a/sim/group.lua b/sim/group.lua index 679aa75..e1ef34e 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -5,13 +5,8 @@ Group = {} function Group.new() local o = { - --state = ffi.new("long long", 0), state = 0, - --state_num = ffi.new("long long", 0), state_num = 0, - --in_queue = ffi.new("bool", false), - --in_queue = false, - --in_queue = ffi.new("long long", 0), in_queue = 0, gates_update = {}, num_gates_update = 0, @@ -44,7 +39,9 @@ function Group.addwire(group, wire) Wire.setgroup(wire, group) Wire.update(wire) - Simulation.queuegroup(GSim, group) + if group.in_queue==0 then + Simulation.queuegroup(GSim, group) + end end end end @@ -186,11 +183,9 @@ function Group.setstate(group, state) group.state = state group.update_tick = sim.current_tick - --for k, gate in ipairs(group.gates_update) do local len = group.num_gates_update for i = 1, len do - local gate = group.gates_update[i] - Simulation.queuegate(sim, gate) + Simulation.queuegate(sim, group.gates_update[i]) end Simulation.queuegroupfx(sim, group) diff --git a/sim/network.lua b/sim/network.lua index 0b91bc6..60ba011 100644 --- a/sim/network.lua +++ b/sim/network.lua @@ -205,8 +205,6 @@ function network_update() i = i + 1 end elseif err == "closed" then - --sim = Simulation.new(Simulation) - --acceptclient() print("Connection closed") error() end diff --git a/sim/port.lua b/sim/port.lua index 5b426d2..59a724d 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -17,17 +17,15 @@ PortDirections = { Port = {} -function Port.new(type, direction, position, causeupdate, idx) +function Port.new(type, direction, position, causeupdate, idx, gate) local o = { - --state = ffi.new("long long", 0), - state = 0, group = nil, type = type, direction = direction, position = position, causeupdate = causeupdate, - gate = nil, + gate = gate, idx = idx, } return o @@ -50,12 +48,9 @@ function Port.getgate(port) return port.gate end -function Port.setgate(port, gate) - port.gate = gate -end - function Port.setgroup(port, group) port.group = group + --Port.getgate(port)[port.idx*2] = group Port.getgate(port).port_nets[port.idx] = group end @@ -68,5 +63,5 @@ function Port.gettype(port) end function Port.getstate(port) - return port.state + return Port.getgate(port).port_states[port.idx] end diff --git a/sim/simulation.lua b/sim/simulation.lua index 6eb8de9..f8631bb 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -116,6 +116,7 @@ function Simulation.addgate(sim, gate) sim.ngates = sim.ngates + 1 + Gate.preinit(gate) Simulation.queuegateinit(sim, gate) Simulation.queuegate(sim, gate) end @@ -239,7 +240,8 @@ end -- Logic Critical function Simulation.queuegate(sim, gate) if gate.in_queue==0 then - table.insert(sim.gatequeue, gate) + --table.insert(sim.gatequeue, gate) + sim.gatequeue[sim.num_gatequeue+1] = gate sim.num_gatequeue = sim.num_gatequeue + 1 gate.in_queue = 1 end @@ -265,12 +267,11 @@ end -- Logic Critical function Simulation.queuegroup(sim, group) - if group.in_queue==0 then - --table.insert(sim.groupqueue, group) + --if group.in_queue==0 then sim.groupqueue[sim.num_groupqueue+1] = group sim.num_groupqueue = sim.num_groupqueue + 1 group.in_queue = 1 - end + --end end function Simulation.dequeuegroup(sim, group) @@ -306,7 +307,6 @@ end -- Logic Critical function Simulation.ticklogic(sim) - --for k, group in ipairs(sim.groupqueue) do local len = sim.num_groupqueue for i = 1, len do local group = sim.groupqueue[i] @@ -323,7 +323,6 @@ function Simulation.ticklogic(sim) sim.tickqueue[sim.current_tick] = nil end - --for k, gate in ipairs(sim.gatequeue) do local len = sim.num_gatequeue for i = 1, len do local gate = sim.gatequeue[i]