make sim use proper OOP
This commit is contained in:
parent
0963ef8ca8
commit
be2df1ef33
@ -9,15 +9,15 @@ ffi.cdef([[
|
|||||||
]])
|
]])
|
||||||
|
|
||||||
function Simulation.compile(sim)
|
function Simulation.compile(sim)
|
||||||
-- assemble a list of all groups
|
-- assemble a list of all nets
|
||||||
local groups = {}
|
local all_nets = {}
|
||||||
for wire_idx, wire in pairs(sim.wires) do
|
for wire_idx, wire in pairs(sim.wires) do
|
||||||
local group = Wire.getgroup(wire)
|
local net = Wire.getgroup(wire)
|
||||||
groups[group] = group
|
all_nets[net] = net
|
||||||
end
|
end
|
||||||
local num_groups = 0
|
local num_nets = 0
|
||||||
for group_id, group in pairs(groups) do
|
for net_id, net in pairs(all_nets) do
|
||||||
num_groups = num_groups+1
|
num_nets = num_nets+1
|
||||||
end
|
end
|
||||||
|
|
||||||
-- construct each gate into an array
|
-- construct each gate into an array
|
||||||
|
14
sim/gate.lua
14
sim/gate.lua
@ -38,18 +38,22 @@ function Gate.getportisrising(self, index)
|
|||||||
return Port.isrising(self.ports[index])
|
return Port.isrising(self.ports[index])
|
||||||
end
|
end
|
||||||
|
|
||||||
function Gate:getportisfalling(index)
|
function Gate.getportisfalling(self, index)
|
||||||
return Port.isfalling(self.ports[index])
|
return Port.isfalling(self.ports[index])
|
||||||
end
|
end
|
||||||
|
|
||||||
function Gate.cb(gate, ...)
|
function Gate.cb(gate, ...)
|
||||||
Simulation.queuecallback(gate.sim, gate, ...)
|
Simulation.queuecallback(Gate.getsim(gate), gate, ...)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Gate.queue(self, delay)
|
function Gate.queue(gate, delay)
|
||||||
Simulation.queuegatelater(self.sim, self, delay)
|
Simulation.queuegatelater(Gate.getsim(gate), gate, delay)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Gate.gettick(gate)
|
function Gate.gettick(gate)
|
||||||
return gate.sim.currenttick
|
return Gate.getsim(gate).currenttick
|
||||||
|
end
|
||||||
|
|
||||||
|
function Gate.getsim(gate)
|
||||||
|
return gate.sim
|
||||||
end
|
end
|
||||||
|
@ -84,7 +84,7 @@ function GateDefinition.constructgate(self, objref, position, rotation, sim)
|
|||||||
pos[2] = x
|
pos[2] = x
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
return gate
|
return gate
|
||||||
|
@ -35,7 +35,7 @@ function Group.addwire(self, wire)
|
|||||||
|
|
||||||
Wire.setgroup(wire, self)
|
Wire.setgroup(wire, self)
|
||||||
Wire.update(wire)
|
Wire.update(wire)
|
||||||
Simulation.queuegroup(self.sim, self)
|
Simulation.queuegroup(Group.getsim(self), self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -43,35 +43,37 @@ end
|
|||||||
function Group.removewire(self, wire)
|
function Group.removewire(self, wire)
|
||||||
Wire.setgroup(wire, nil)
|
Wire.setgroup(wire, nil)
|
||||||
self.wires[wire] = nil
|
self.wires[wire] = nil
|
||||||
|
|
||||||
|
local sim = Group.getsim(self)
|
||||||
|
|
||||||
for k, wire in pairs(self.wires) do
|
for k, wire in pairs(self.wires) do
|
||||||
Wire.setgroup(wire, nil)
|
Wire.setgroup(wire, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
for k, port in pairs(self.out_ports) do
|
for k, port in pairs(self.out_ports) do
|
||||||
Port.setgroup(port, nil)
|
Port.setgroup(port, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
for k, port in pairs(self.in_ports) do
|
for k, port in pairs(self.in_ports) do
|
||||||
Port.setgroup(port, nil)
|
Port.setgroup(port, nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
for k, wire in pairs(self.wires) do
|
for k, wire in pairs(self.wires) do
|
||||||
Simulation.connectwire(self.sim, wire)
|
Simulation.connectwire(sim. wire)
|
||||||
end
|
end
|
||||||
|
|
||||||
for k, port in pairs(self.out_ports) do
|
for k, port in pairs(self.out_ports) do
|
||||||
Simulation.connectport(self.sim, port)
|
Simulation.connectport(sim, port)
|
||||||
end
|
end
|
||||||
|
|
||||||
for k, port in pairs(self.in_ports) do
|
for k, port in pairs(self.in_ports) do
|
||||||
Simulation.connectport(self.sim, port)
|
Simulation.connectport(sim, port)
|
||||||
end
|
end
|
||||||
|
|
||||||
self.wires = {}
|
self.wires = {}
|
||||||
self.out_ports = {}
|
self.out_ports = {}
|
||||||
self.in_ports = {}
|
self.in_ports = {}
|
||||||
|
|
||||||
self.nwires = 0
|
self.nwires = 0
|
||||||
self.nout_ports = 0
|
self.nout_ports = 0
|
||||||
self.nin_ports = 0
|
self.nin_ports = 0
|
||||||
@ -83,7 +85,7 @@ function Group.addport(self, port)
|
|||||||
if port.type == PortTypes.output then
|
if port.type == PortTypes.output then
|
||||||
self.out_ports[port] = port
|
self.out_ports[port] = port
|
||||||
self.nout_ports = self.nout_ports + 1
|
self.nout_ports = self.nout_ports + 1
|
||||||
self.sim:queuegroup(self)
|
Simulation.queuegroup(Group.getsim(self), self)
|
||||||
elseif port.type == PortTypes.input then
|
elseif port.type == PortTypes.input then
|
||||||
self.in_ports[port] = port
|
self.in_ports[port] = port
|
||||||
self.nin_ports = self.nin_ports + 1
|
self.nin_ports = self.nin_ports + 1
|
||||||
@ -100,11 +102,11 @@ function Group.removeport(self, port)
|
|||||||
self.nin_ports = self.nin_ports - 1
|
self.nin_ports = self.nin_ports - 1
|
||||||
end
|
end
|
||||||
|
|
||||||
Simulation.queuegroup(self.sim, self)
|
Simulation.queuegroup(Group.getsim(self), self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Group.mergewith(self, group)
|
function Group.mergewith(self, group)
|
||||||
if self:getsize() >= group:getsize() then
|
if Group.getsize(self) >= Group.getsize(group) then
|
||||||
Group.mergeinto(group, self)
|
Group.mergeinto(group, self)
|
||||||
return self
|
return self
|
||||||
else
|
else
|
||||||
@ -139,12 +141,16 @@ end
|
|||||||
function Group.setstate(self, state)
|
function Group.setstate(self, state)
|
||||||
if state ~= self.state then
|
if state ~= self.state then
|
||||||
self.state = state
|
self.state = state
|
||||||
self.updatetick = self.sim.currenttick
|
self.updatetick = Group.getsim(self).currenttick
|
||||||
|
|
||||||
for k, port in pairs(self.in_ports) do
|
for k, port in pairs(self.in_ports) do
|
||||||
Port.setinputstate(port, state)
|
Port.setinputstate(port, state)
|
||||||
end
|
end
|
||||||
|
|
||||||
Simulation.queuegroupfx(self.sim, self)
|
Simulation.queuegroupfx(Group.getsim(self), self)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Group.getsim(group)
|
||||||
|
return group.sim
|
||||||
|
end
|
||||||
|
@ -48,7 +48,7 @@ local lastfxtime = 0
|
|||||||
local avgticks = {}
|
local avgticks = {}
|
||||||
local totalticks = 0
|
local totalticks = 0
|
||||||
|
|
||||||
local sim = Simulation:new()
|
local sim = Simulation.new(Simulation)
|
||||||
|
|
||||||
local units = {
|
local units = {
|
||||||
"uHz",
|
"uHz",
|
||||||
@ -322,12 +322,12 @@ while 1 do
|
|||||||
|
|
||||||
if OPT_TICK_TIME==0 then
|
if OPT_TICK_TIME==0 then
|
||||||
for i = 1, OPT_TICK_INF do
|
for i = 1, OPT_TICK_INF do
|
||||||
sim:tick()
|
Simulation.tick(sim)
|
||||||
end
|
end
|
||||||
ticks = ticks+OPT_TICK_INF
|
ticks = ticks+OPT_TICK_INF
|
||||||
else
|
else
|
||||||
for i = 1, OPT_TICK_MULT do
|
for i = 1, OPT_TICK_MULT do
|
||||||
sim:tick()
|
Simulation.tick(sim)
|
||||||
ticks = ticks+1
|
ticks = ticks+1
|
||||||
|
|
||||||
local elapsed = os.clock()-time
|
local elapsed = os.clock()-time
|
||||||
|
46
sim/port.lua
46
sim/port.lua
@ -36,7 +36,7 @@ end
|
|||||||
function Port.setstate(port, state)
|
function Port.setstate(port, state)
|
||||||
if state ~= port.state then
|
if state ~= port.state then
|
||||||
port.state = state
|
port.state = state
|
||||||
Simulation.queuegroup(port.sim, port.group)
|
Simulation.queuegroup(Port.getsim(port), port.group)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -44,46 +44,50 @@ function Port.setinputstate(port, state)
|
|||||||
if state ~= port.state then
|
if state ~= port.state then
|
||||||
port.state = state
|
port.state = state
|
||||||
if port.causeupdate then
|
if port.causeupdate then
|
||||||
Simulation.queuegate(port.sim, port.gate)
|
Simulation.queuegate(Port.getsim(port), port.gate)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Port.getconnectionposition(self)
|
function Port.getconnectionposition(port)
|
||||||
local offset = PortDirections[self.direction]
|
local offset = PortDirections[port.direction]
|
||||||
return {self.position[1]+offset[1], self.position[2]+offset[2], self.position[3]+offset[3]}
|
return {port.position[1]+offset[1], port.position[2]+offset[2], port.position[3]+offset[3]}
|
||||||
end
|
end
|
||||||
|
|
||||||
function Port.isrising(self)
|
function Port.isrising(port)
|
||||||
if self.group == nil then
|
if port.group == nil then
|
||||||
return false
|
return false
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
function Port.isfalling(self)
|
function Port.isfalling(port)
|
||||||
if self.group == nil then
|
if port.group == nil then
|
||||||
return false
|
return false
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
function Port.setgate(self, gate)
|
function Port.setgate(port, gate)
|
||||||
self.gate = gate
|
port.gate = gate
|
||||||
end
|
end
|
||||||
|
|
||||||
function Port.setgroup(self, group)
|
function Port.setgroup(port, group)
|
||||||
self.group = group
|
port.group = group
|
||||||
end
|
end
|
||||||
|
|
||||||
function Port.getgroup(self)
|
function Port.getgroup(port)
|
||||||
return self.group
|
return port.group
|
||||||
end
|
end
|
||||||
|
|
||||||
function Port.gettype(self)
|
function Port.gettype(port)
|
||||||
return self.type
|
return port.type
|
||||||
end
|
end
|
||||||
|
|
||||||
function Port.getstate(self)
|
function Port.getstate(port)
|
||||||
return self.state
|
return port.state
|
||||||
|
end
|
||||||
|
|
||||||
|
function Port.getsim(port)
|
||||||
|
return port.sim
|
||||||
end
|
end
|
||||||
|
@ -213,7 +213,7 @@ end
|
|||||||
|
|
||||||
function Simulation.connectport(self, port)
|
function Simulation.connectport(self, port)
|
||||||
local connpos = Port.getconnectionposition(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
|
for k, obj in pairs(objs) do
|
||||||
if obj ~= port and obj.group ~= nil then
|
if obj ~= port and obj.group ~= nil then
|
||||||
Group.addport(obj.group, port)
|
Group.addport(obj.group, port)
|
||||||
|
@ -21,7 +21,7 @@ function Wire.setlayer(self, layer)
|
|||||||
Group.removewire(self.group, self)
|
Group.removewire(self.group, self)
|
||||||
end
|
end
|
||||||
self.layer = layer
|
self.layer = layer
|
||||||
Simulation.connectwire(self.sim, self)
|
Simulation.connectwire(Wire.getsim(self), self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Wire.update(self)
|
function Wire.update(self)
|
||||||
@ -47,3 +47,7 @@ end
|
|||||||
function Wire.getbounds(self)
|
function Wire.getbounds(self)
|
||||||
return self.bounds
|
return self.bounds
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Wire.getsim(wire)
|
||||||
|
return wire.sim
|
||||||
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user