diff --git a/sim/compile.lua b/sim/compile.lua index 7701c5a..3d08ce5 100644 --- a/sim/compile.lua +++ b/sim/compile.lua @@ -9,15 +9,15 @@ ffi.cdef([[ ]]) function Simulation.compile(sim) - -- assemble a list of all groups - local groups = {} + -- assemble a list of all nets + local all_nets = {} for wire_idx, wire in pairs(sim.wires) do - local group = Wire.getgroup(wire) - groups[group] = group + local net = Wire.getgroup(wire) + all_nets[net] = net end - local num_groups = 0 - for group_id, group in pairs(groups) do - num_groups = num_groups+1 + local num_nets = 0 + for net_id, net in pairs(all_nets) do + num_nets = num_nets+1 end -- construct each gate into an array diff --git a/sim/gate.lua b/sim/gate.lua index 2addafc..3861d9c 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -38,18 +38,22 @@ function Gate.getportisrising(self, index) return Port.isrising(self.ports[index]) end -function Gate:getportisfalling(index) +function Gate.getportisfalling(self, index) return Port.isfalling(self.ports[index]) end function Gate.cb(gate, ...) - Simulation.queuecallback(gate.sim, gate, ...) + Simulation.queuecallback(Gate.getsim(gate), gate, ...) end -function Gate.queue(self, delay) - Simulation.queuegatelater(self.sim, self, delay) +function Gate.queue(gate, delay) + Simulation.queuegatelater(Gate.getsim(gate), gate, delay) end function Gate.gettick(gate) - return gate.sim.currenttick + return Gate.getsim(gate).currenttick +end + +function Gate.getsim(gate) + return gate.sim end diff --git a/sim/gatedef.lua b/sim/gatedef.lua index c6d9ee4..631ceaf 100644 --- a/sim/gatedef.lua +++ b/sim/gatedef.lua @@ -84,7 +84,7 @@ function GateDefinition.constructgate(self, objref, position, rotation, sim) pos[2] = x end - Gate.addport(gate, Port.new(Port, type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, gate.sim)) + Gate.addport(gate, Port.new(Port, type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, Gate.getsim(gate))) end return gate diff --git a/sim/group.lua b/sim/group.lua index 2b4ae92..7aaec39 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -35,7 +35,7 @@ function Group.addwire(self, wire) Wire.setgroup(wire, self) Wire.update(wire) - Simulation.queuegroup(self.sim, self) + Simulation.queuegroup(Group.getsim(self), self) end end end @@ -43,35 +43,37 @@ end function Group.removewire(self, wire) Wire.setgroup(wire, nil) self.wires[wire] = nil - + + local sim = Group.getsim(self) + for k, wire in pairs(self.wires) do Wire.setgroup(wire, nil) end - + for k, port in pairs(self.out_ports) do Port.setgroup(port, nil) end - + for k, port in pairs(self.in_ports) do Port.setgroup(port, nil) end - + for k, wire in pairs(self.wires) do - Simulation.connectwire(self.sim, wire) + Simulation.connectwire(sim. wire) end - + for k, port in pairs(self.out_ports) do - Simulation.connectport(self.sim, port) + Simulation.connectport(sim, port) end - + for k, port in pairs(self.in_ports) do - Simulation.connectport(self.sim, port) + Simulation.connectport(sim, port) end - + self.wires = {} self.out_ports = {} self.in_ports = {} - + self.nwires = 0 self.nout_ports = 0 self.nin_ports = 0 @@ -83,7 +85,7 @@ function Group.addport(self, port) if port.type == PortTypes.output then self.out_ports[port] = port self.nout_ports = self.nout_ports + 1 - self.sim:queuegroup(self) + Simulation.queuegroup(Group.getsim(self), self) elseif port.type == PortTypes.input then self.in_ports[port] = port self.nin_ports = self.nin_ports + 1 @@ -100,11 +102,11 @@ function Group.removeport(self, port) self.nin_ports = self.nin_ports - 1 end - Simulation.queuegroup(self.sim, self) + Simulation.queuegroup(Group.getsim(self), self) end function Group.mergewith(self, group) - if self:getsize() >= group:getsize() then + if Group.getsize(self) >= Group.getsize(group) then Group.mergeinto(group, self) return self else @@ -139,12 +141,16 @@ end function Group.setstate(self, state) if state ~= self.state then self.state = state - self.updatetick = self.sim.currenttick + self.updatetick = Group.getsim(self).currenttick for k, port in pairs(self.in_ports) do Port.setinputstate(port, state) end - Simulation.queuegroupfx(self.sim, self) + Simulation.queuegroupfx(Group.getsim(self), self) end end + +function Group.getsim(group) + return group.sim +end diff --git a/sim/main.lua b/sim/main.lua index 618886e..65f2b3f 100644 --- a/sim/main.lua +++ b/sim/main.lua @@ -48,7 +48,7 @@ local lastfxtime = 0 local avgticks = {} local totalticks = 0 -local sim = Simulation:new() +local sim = Simulation.new(Simulation) local units = { "uHz", @@ -322,12 +322,12 @@ while 1 do if OPT_TICK_TIME==0 then for i = 1, OPT_TICK_INF do - sim:tick() + Simulation.tick(sim) end ticks = ticks+OPT_TICK_INF else for i = 1, OPT_TICK_MULT do - sim:tick() + Simulation.tick(sim) ticks = ticks+1 local elapsed = os.clock()-time diff --git a/sim/port.lua b/sim/port.lua index 94dc694..bb3a23c 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -36,7 +36,7 @@ end function Port.setstate(port, state) if state ~= port.state then port.state = state - Simulation.queuegroup(port.sim, port.group) + Simulation.queuegroup(Port.getsim(port), port.group) end end @@ -44,46 +44,50 @@ function Port.setinputstate(port, state) if state ~= port.state then port.state = state if port.causeupdate then - Simulation.queuegate(port.sim, port.gate) + Simulation.queuegate(Port.getsim(port), port.gate) end end end -function Port.getconnectionposition(self) - local offset = PortDirections[self.direction] - return {self.position[1]+offset[1], self.position[2]+offset[2], self.position[3]+offset[3]} +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]} end -function Port.isrising(self) - if self.group == nil then +function Port.isrising(port) + if port.group == nil then return false end - return self.group.state and (self.group.updatetick == self.sim.currenttick) + return port.group.state and (port.group.updatetick == Port.getsim(port).currenttick) end -function Port.isfalling(self) - if self.group == nil then +function Port.isfalling(port) + if port.group == nil then return false end - return self.group.state == false and (self.group.updatetick == self.sim.currenttick) + return port.group.state == false and (port.updatetick == Port.getsim(port).currenttick) end -function Port.setgate(self, gate) - self.gate = gate +function Port.setgate(port, gate) + port.gate = gate end -function Port.setgroup(self, group) - self.group = group +function Port.setgroup(port, group) + port.group = group end -function Port.getgroup(self) - return self.group +function Port.getgroup(port) + return port.group end -function Port.gettype(self) - return self.type +function Port.gettype(port) + return port.type end -function Port.getstate(self) - return self.state +function Port.getstate(port) + return port.state +end + +function Port.getsim(port) + return port.sim end diff --git a/sim/simulation.lua b/sim/simulation.lua index 525ee00..ded32af 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -213,7 +213,7 @@ end function Simulation.connectport(self, port) local connpos = Port.getconnectionposition(port) - local objs = self:getfromworld(connpos[1], connpos[2], connpos[3]) + local objs = Simulation.getfromworld(self, connpos[1], connpos[2], connpos[3]) for k, obj in pairs(objs) do if obj ~= port and obj.group ~= nil then Group.addport(obj.group, port) diff --git a/sim/wire.lua b/sim/wire.lua index 7ef72d5..25c9713 100644 --- a/sim/wire.lua +++ b/sim/wire.lua @@ -21,7 +21,7 @@ function Wire.setlayer(self, layer) Group.removewire(self.group, self) end self.layer = layer - Simulation.connectwire(self.sim, self) + Simulation.connectwire(Wire.getsim(self), self) end function Wire.update(self) @@ -47,3 +47,7 @@ end function Wire.getbounds(self) return self.bounds end + +function Wire.getsim(wire) + return wire.sim +end