diff --git a/sim/compile.lua b/sim/compile.lua new file mode 100644 index 0000000..3ecdd0d --- /dev/null +++ b/sim/compile.lua @@ -0,0 +1,12 @@ + +function Simulation.compile(sim) + +end + +function Simulation.decompile(sim) + +end + +function Simulation.tickcompiled(sim) + +end diff --git a/sim/gate.lua b/sim/gate.lua index 047315b..2addafc 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -1,11 +1,12 @@ Gate = {} -function Gate.new(self, objref, definition) +function Gate.new(self, objref, definition, sim) local o = { objref = objref, definition = definition, - ports = {} + ports = {}, + sim = sim, } setmetatable(o, self) self.__index = self @@ -41,34 +42,14 @@ function Gate:getportisfalling(index) return Port.isfalling(self.ports[index]) end --- function Gate:cb(...) --- local args = {...} --- local str = tostring(#args) - --- for i, v in ipairs(args) do --- v = bool_to_int[v] or tostring(v) --- str = str .. "\t" .. tostring(v) --- end - --- sim:queuecallback(self, str) --- end - -function Gate.cb(self, ...) - Simulation.queuecallback(sim, self, ...) +function Gate.cb(gate, ...) + Simulation.queuecallback(gate.sim, gate, ...) end function Gate.queue(self, delay) - Simulation.queuegatelater(sim, self, delay) + Simulation.queuegatelater(self.sim, self, delay) end -function Gate.testlogic(self, n) - --local time = os.clock() - --for i = 1, n do - -- self.definition.logic(self) - --end - --client:send("TEST\t" .. (os.clock()-time) .. "\n") -end - -function Gate.gettick(self) - return sim.currenttick +function Gate.gettick(gate) + return gate.sim.currenttick end diff --git a/sim/gatedef.lua b/sim/gatedef.lua index a61feaf..c6d9ee4 100644 --- a/sim/gatedef.lua +++ b/sim/gatedef.lua @@ -58,8 +58,8 @@ function GateDefinition.new(self, objref, name, description, init, logic, input, return o end -function GateDefinition.constructgate(self, objref, position, rotation) - local gate = Gate.new(Gate, objref, self) +function GateDefinition.constructgate(self, objref, position, rotation, sim) + local gate = Gate.new(Gate, objref, self, sim) for i = 1, #self.ports do local portd = self.ports[i] @@ -84,7 +84,7 @@ function GateDefinition.constructgate(self, objref, position, rotation) 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.addport(gate, Port.new(Port, type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, gate.sim)) end return gate diff --git a/sim/group.lua b/sim/group.lua index b5d8ab9..2b4ae92 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -1,7 +1,7 @@ Group = {} -function Group.new(self) +function Group.new(self, sim) local o = { state = false, fxstate = false, @@ -12,7 +12,9 @@ function Group.new(self) nwires = 0, nout_ports = 0, - nin_ports = 0 + nin_ports = 0, + + sim = sim, } setmetatable(o, self) self.__index = self @@ -33,7 +35,7 @@ function Group.addwire(self, wire) Wire.setgroup(wire, self) Wire.update(wire) - Simulation.queuegroup(sim, self) + Simulation.queuegroup(self.sim, self) end end end @@ -55,15 +57,15 @@ function Group.removewire(self, wire) end for k, wire in pairs(self.wires) do - Simulation.connectwire(sim, wire) + Simulation.connectwire(self.sim, wire) end for k, port in pairs(self.out_ports) do - Simulation.connectport(sim, port) + Simulation.connectport(self.sim, port) end for k, port in pairs(self.in_ports) do - Simulation.connectport(sim, port) + Simulation.connectport(self.sim, port) end self.wires = {} @@ -81,7 +83,7 @@ function Group.addport(self, port) if port.type == PortTypes.output then self.out_ports[port] = port self.nout_ports = self.nout_ports + 1 - sim:queuegroup(self) + self.sim:queuegroup(self) elseif port.type == PortTypes.input then self.in_ports[port] = port self.nin_ports = self.nin_ports + 1 @@ -98,7 +100,7 @@ function Group.removeport(self, port) self.nin_ports = self.nin_ports - 1 end - Simulation.queuegroup(sim, self) + Simulation.queuegroup(self.sim, self) end function Group.mergewith(self, group) @@ -137,12 +139,12 @@ end function Group.setstate(self, state) if state ~= self.state then self.state = state - self.updatetick = sim.currenttick + self.updatetick = self.sim.currenttick for k, port in pairs(self.in_ports) do Port.setinputstate(port, state) end - Simulation.queuegroupfx(sim, self) + Simulation.queuegroupfx(self.sim, self) end end diff --git a/sim/main.lua b/sim/main.lua index a1b61c3..05d40ab 100644 --- a/sim/main.lua +++ b/sim/main.lua @@ -1,4 +1,9 @@ +assert(getmetatable(_G)==nil, "_G already has a metatable") +setmetatable(_G, { + __index = function(t, i) error("attempt to access nil variable "..i, 2) end +}) + OPT_SAVE_DIR = arg[1] or error("must specify save location") OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("\\", "/") OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("/$", "") @@ -16,6 +21,7 @@ dofile("gatedef.lua") dofile("gate.lua") dofile("port.lua") dofile("save.lua") +dofile("compile.lua") OPT_TICK_ENABLED = true OPT_TICK_TIME = 0.032 @@ -39,7 +45,7 @@ local lastfxtime = 0 local avgticks = {} local totalticks = 0 -sim = Simulation:new() +local sim = Simulation:new() local units = { "uHz", @@ -151,7 +157,7 @@ while 1 do local position = vectotable(data[i+3]) local rotation = tonumber(data[i+4]) - local gate = GateDefinition.constructgate(definition, objref, position, rotation) + local gate = GateDefinition.constructgate(definition, objref, position, rotation, sim) Simulation.addgate(sim, gate) --print(gate.objref) @@ -279,10 +285,6 @@ while 1 do elseif data[i] == "TICK" then Simulation.tick(sim) ticks = ticks + 1 - elseif data[i] == "TEST" then - local gate = Simulation.getgatebyref(sim, tonumber(data[i+1])) - Gate.testlogic(gate, tonumber(data[i+2])) - i = i + 2 elseif data[i] == "IN" then local gate = Simulation.getgatebyref(sim, tonumber(data[i+1])) local argc = tonumber(data[i+2]) diff --git a/sim/port.lua b/sim/port.lua index 4280ef6..94dc694 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -17,7 +17,7 @@ Port = { logictype = 1, } -function Port.new(self, type, direction, position, causeupdate) +function Port.new(self, type, direction, position, causeupdate, sim) local o = { type = type, direction = direction, @@ -26,6 +26,7 @@ function Port.new(self, type, direction, position, causeupdate) state = false, gate = nil, group = nil, + sim = sim, } setmetatable(o, self) self.__index = self @@ -35,7 +36,7 @@ end function Port.setstate(port, state) if state ~= port.state then port.state = state - Simulation.queuegroup(sim, port.group) + Simulation.queuegroup(port.sim, port.group) end end @@ -43,7 +44,7 @@ function Port.setinputstate(port, state) if state ~= port.state then port.state = state if port.causeupdate then - Simulation.queuegate(sim, port.gate) + Simulation.queuegate(port.sim, port.gate) end end end @@ -57,14 +58,14 @@ function Port.isrising(self) if self.group == nil then return false end - return self.group.state and (self.group.updatetick == sim.currenttick) + return self.group.state and (self.group.updatetick == self.sim.currenttick) end function Port.isfalling(self) if self.group == nil then return false end - return self.group.state == false and (self.group.updatetick == sim.currenttick) + return self.group.state == false and (self.group.updatetick == self.sim.currenttick) end function Port.setgate(self, gate) diff --git a/sim/simulation.lua b/sim/simulation.lua index c8c18d3..3093498 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -207,7 +207,7 @@ function Simulation.connectwire(self, wire) end if Wire.getgroup(wire)==nil then - Group.addwire(Group.new(Group), wire) + Group.addwire(Group.new(Group, self), wire) end end @@ -221,7 +221,7 @@ function Simulation.connectport(self, port) end if Port.getgroup(port) == nil then - Group.addport(Group.new(Group), port) + Group.addport(Group.new(Group, self), port) end end diff --git a/sim/wire.lua b/sim/wire.lua index 1fa409b..7e032ce 100644 --- a/sim/wire.lua +++ b/sim/wire.lua @@ -20,7 +20,7 @@ function Wire.setlayer(self, layer) Group.removewire(self.group, self) end self.layer = layer - Simulation.connectwire(sim, self) + Simulation.connectwire(self.sim, self) end function Wire.update(self)