From 09e65faec41ea0e3113ad41735e898cbcaeff0cb Mon Sep 17 00:00:00 2001 From: Redo0 Date: Tue, 25 May 2021 20:37:29 -0500 Subject: [PATCH] inline Port.setstate; remove unused network codes --- sim/gate.lua | 14 ++++++++++++-- sim/gatedef.lua | 2 +- sim/group.lua | 4 ++-- sim/main.lua | 14 -------------- sim/port.lua | 16 +++------------- 5 files changed, 18 insertions(+), 32 deletions(-) diff --git a/sim/gate.lua b/sim/gate.lua index 4159fe0..f8c278c 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -6,6 +6,7 @@ function Gate.new(objref, definition) objref = objref, definition = definition, ports = {}, + port_nets = {}, in_queue = false, logic = definition.logic, } @@ -18,11 +19,20 @@ function Gate.addport(gate, port) end function Gate.getportstate(gate, index) - return gate.ports[index].group.state + return gate.port_nets[index].state end function Gate.setportstate(gate, index, state) - Port.setstate(gate.ports[index], state) + local port = gate.ports[index] + 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 (group.state_num>0) ~= group.state then + Simulation.queuegroup(GSim, group) + end + end end function Gate.initdata(gate) diff --git a/sim/gatedef.lua b/sim/gatedef.lua index dca697a..8699550 100644 --- a/sim/gatedef.lua +++ b/sim/gatedef.lua @@ -82,7 +82,7 @@ function GateDefinition.constructgate(def, objref, position, rotation) 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)) + Gate.addport(gate, Port.new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, i)) end return gate diff --git a/sim/group.lua b/sim/group.lua index e5ff8a6..b699202 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -84,7 +84,7 @@ end function Group.addport(group, port) if port.group~=nil then error("port already has group") end - port.group = group + Port.setgroup(port, group) if port.type == PortTypes.output then if group.out_ports[port] then error("port already in group") end @@ -112,7 +112,7 @@ end function Group.removeport(group, port) if port.group~=group then error("port does not have group") end - port.group = nil + Port.setgroup(port, nil) if port.type == PortTypes.output then if not group.out_ports[port] then error("port not in group") end diff --git a/sim/main.lua b/sim/main.lua index 02d8ad4..f7db532 100644 --- a/sim/main.lua +++ b/sim/main.lua @@ -212,20 +212,6 @@ while 1 do Wire.setlayer(wire, tonumber(data[i+2])) end - i = i + 2 - elseif data[i] == "SP" then - local gate = Simulation.getgatebyref(sim, tonumber(data[i+1])) - if gate ~= nil then - Port.setstate(gate.ports[tonumber(data[i+2])], toboolean(data[i+3])) - end - - i = i + 3 - elseif data[i] == "SG" then - local wire = Simulation.getwirebyref(sim, tonumber(data[i+1])) - if wire ~= nil then - Group.setstate(Wire.getgroup(wire), toboolean(data[i+2])) - end - i = i + 2 elseif data[i] == "OPT" then local option = data[i+1] diff --git a/sim/port.lua b/sim/port.lua index 5c219b9..fdd6fec 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -15,7 +15,7 @@ PortDirections = { Port = {} -function Port.new(type, direction, position, causeupdate) +function Port.new(type, direction, position, causeupdate, idx) local o = { type = type, direction = direction, @@ -24,22 +24,11 @@ function Port.new(type, direction, position, causeupdate) state = false, gate = nil, group = nil, + idx = idx, } return o 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 (group.state_num>0) ~= group.state then - Simulation.queuegroup(GSim, group) - end - end -end - function Port.getconnectionposition(port) local offset = PortDirections[port.direction] return {port.position[1]+offset[1], port.position[2]+offset[2], port.position[3]+offset[3]} @@ -69,6 +58,7 @@ end function Port.setgroup(port, group) port.group = group + Port.getgate(port).port_nets[port.idx] = group end function Port.getgroup(port)