diff --git a/sim/gate.lua b/sim/gate.lua index 7e64d3b..1066b19 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -28,8 +28,8 @@ function Gate.addport(self, port) Port.setgate(port, self) end -function Gate.getportstate(self, index) - return self.ports[index].group.state +function Gate.getportstate(gate, index) + return gate.ports[index].group.state end function Gate.setportstate(self, index, state) diff --git a/sim/group.lua b/sim/group.lua index 4fffb05..f6c2ed0 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -118,7 +118,7 @@ function Group.addport(self, port) self.in_ports[port] = port self.nin_ports = self.nin_ports + 1 if port.causeupdate then - self.in_ports_update[port] = port + table.insert(self.in_ports_update, port) end Simulation.queuegate(GSim, Port.getgate(port)) @@ -147,7 +147,7 @@ function Group.removeport(self, port) self.in_ports[port] = nil self.nin_ports = self.nin_ports - 1 if port.causeupdate then - self.in_ports_update[port] = nil + array_remove(self.in_ports_update, port) end Simulation.queuegate(GSim, Port.getgate(port)) @@ -198,7 +198,7 @@ function Group.setstate(self, state) self.state = state self.updatetick = sim.currenttick - for k, port in pairs(self.in_ports_update) do + for k, port in ipairs(self.in_ports_update) do Simulation.queuegate(sim, Port.getgate(port)) end diff --git a/sim/main.lua b/sim/main.lua index abc1268..cac837f 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 = 0; for k, port in pairs(group.in_ports_update) do numportsu = numportsu+1 end + local numportsu = #group.in_ports_update local numportso = 0; local numportson=0; for k, port in pairs(group.out_ports) do numportso = numportso+1 diff --git a/sim/simulation.lua b/sim/simulation.lua index 44cd992..220c0bd 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -270,18 +270,6 @@ function Simulation.queuegroup(self, group) end end -local function array_remove(array, value) - for i = 1, #array do - local v = array[i] - if v==value then - array[i] = array[#array] - array[#array] = nil - return - end - end - error("element not in array") -end - function Simulation.dequeuegroup(self, group) if group.in_queue then array_remove(self.groupqueue, group) diff --git a/sim/utility.lua b/sim/utility.lua index a5f5034..8c0684e 100644 --- a/sim/utility.lua +++ b/sim/utility.lua @@ -66,3 +66,14 @@ function tobitstring(num, len) return bitstring end +function array_remove(array, value) + for i = 1, #array do + local v = array[i] + if v==value then + array[i] = array[#array] + array[#array] = nil + return + end + end + error("element not in array") +end