diff --git a/sim/gate.lua b/sim/gate.lua index c0d4025..7b28322 100644 --- a/sim/gate.lua +++ b/sim/gate.lua @@ -23,13 +23,15 @@ function Gate.getportstate(gate, index) end function Gate.setportstate(gate, index, state) + if type(state)~="number" then error("invalid state type - must be number (gate "..gate.objref..")") end + 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) + group.state_num = group.state_num - port.state + state port.state = state - if (group.state_num>0) ~= group.state then + if (group.state_num>0) ~= (group.state==1) then Simulation.queuegroup(GSim, group) end end diff --git a/sim/group.lua b/sim/group.lua index a1f52f4..0a6b707 100644 --- a/sim/group.lua +++ b/sim/group.lua @@ -3,8 +3,8 @@ Group = {} function Group.new() local o = { - state = false, - fxstate = false, + state = 0, + fxstate = 0, update_tick = 0, wires = {}, out_ports = {}, @@ -91,9 +91,7 @@ function Group.addport(group, port) group.out_ports[port] = port group.nout_ports = group.nout_ports + 1 - if Port.getstate(port) then - group.state_num = group.state_num + 1 - end + group.state_num = group.state_num + Port.getstate(port) Simulation.queuegroup(GSim, group) @@ -119,9 +117,7 @@ function Group.removeport(group, port) group.out_ports[port] = nil group.nout_ports = group.nout_ports - 1 - if Port.getstate(port) then - group.state_num = group.state_num - 1 - end + group.state_num = group.state_num - Port.getstate(port) Simulation.queuegroup(GSim, group) @@ -175,6 +171,7 @@ function Group.mergeinto(group, group2) end function Group.setstate(group, state) + if type(state)~="number" then error("group state type must be number") end if state ~= group.state then local sim = GSim @@ -190,7 +187,7 @@ function Group.setstate(group, state) end function Group.update(group) - Group.setstate(group, group.state_num>0) + Group.setstate(group, group.state_num>0 and 1 or 0) end function Group.rebuild_ports(group) diff --git a/sim/main.lua b/sim/main.lua index 25fffcc..c0f817a 100644 --- a/sim/main.lua +++ b/sim/main.lua @@ -252,10 +252,10 @@ while 1 do local numportso = 0; local numportson=0; for k, port in pairs(group.out_ports) do numportso = numportso+1 - if Port.getstate(port) then numportson = numportson+1 end + if Port.getstate(port)==1 then numportson = numportson+1 end end - info = "\\c5Net " .. tostring(group):match("table: 0x(.+)"):upper() .. "\n" .. (Wire.getgroup(wire).state and "\\c2On" or "\\c0Off") .. "\n" .. + info = "\\c5Net " .. tostring(group):match("table: 0x(.+)"):upper() .. "\n" .. (Wire.getgroup(wire).state==1 and "\\c2On" or "\\c0Off") .. "\n" .. "Wires: "..numwires.."\n".. "In Ports: " ..numportsi.."\n".. "Out Ports: "..numportso.."\n".. @@ -276,7 +276,7 @@ while 1 do else state = Port.getstate(port) end - info = info .. (state and "\\c2" or "\\c0") .. def.ports[i].name .. (i ~= #gate.ports and " " or "") + info = info .. (state==1 and "\\c2" or "\\c0") .. def.ports[i].name .. (i ~= #gate.ports and " " or "") end end diff --git a/sim/port.lua b/sim/port.lua index ba55e24..b6d805f 100644 --- a/sim/port.lua +++ b/sim/port.lua @@ -21,7 +21,7 @@ function Port.new(type, direction, position, causeupdate, idx) direction = direction, position = position, causeupdate = causeupdate, - state = false, + state = 0, gate = nil, group = nil, idx = idx, @@ -35,17 +35,11 @@ function Port.getconnectionposition(port) end function Port.isrising(port) - if port.group == nil then - return false - end - return port.group.state and (port.group.update_tick == GSim.current_tick) + return port.group.state==1 and (port.group.update_tick == GSim.current_tick) end function Port.isfalling(port) - if port.group == nil then - return false - end - return port.group.state == false and (port.group.update_tick == GSim.current_tick) + return port.group.state==0 and (port.group.update_tick == GSim.current_tick) end function Port.getgate(port) diff --git a/sim/simulation.lua b/sim/simulation.lua index db7c979..6375c3c 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -15,11 +15,9 @@ function Simulation.new(sim) groupqueue = {}, groupfxqueue = {}, gatequeue = {}, - initqueue = {}, - inputqueue = {}, + initqueue = nil, + inputqueue = nil, tickqueue = {}, - inputqueue_nonempty = false, - initqueue_nonempty = false, callbacks = nil, @@ -253,14 +251,13 @@ function Simulation.queuegatelater(sim, gate, delay) end function Simulation.queuegateinput(sim, gate, argv) - sim.inputqueue[gate] = sim.inputqueue[gate] or {} - table.insert(sim.inputqueue[gate], argv) - sim.inputqueue_nonempty = true + sim.inputqueue = sim.inputqueue or {} + sim.inputqueue[gate] = argv end function Simulation.queuegateinit(sim, gate) + sim.initqueue = sim.initqueue or {} sim.initqueue[gate] = gate - sim.initqueue_nonempty = true end function Simulation.queuegroup(sim, group) @@ -281,8 +278,8 @@ function Simulation.dequeuegate(sim, gate) if gate.in_queue then array_remove(sim.gatequeue, gate) end - sim.initqueue[gate] = nil - sim.inputqueue[gate] = nil + if sim.inputqueue~=nil then sim.inputqueue[gate] = nil end + if sim.initqueue ~=nil then sim.initqueue [gate] = nil end for tick, tickq in pairs(sim.tickqueue) do tickq[gate] = nil end @@ -304,22 +301,18 @@ function Simulation.tick(sim) end sim.groupqueue = {} - if sim.initqueue_nonempty then + if sim.initqueue ~= nil then for k, gate in pairs(sim.initqueue) do Gate.init(gate) end - sim.initqueue = {} - sim.initqueue_nonempty = false + sim.initqueue = nil end - if sim.inputqueue_nonempty then - for gate, inputs in pairs(sim.inputqueue) do - for inputidx, argv in ipairs(inputs) do - Gate.input(gate, argv) - end + if sim.inputqueue ~= nil then + for gate, argv in pairs(sim.inputqueue) do + Gate.input(gate, argv) end - sim.inputqueue = {} - sim.inputqueue_nonempty = false + sim.inputqueue = nil end if sim.tickqueue[sim.current_tick] ~= nil then @@ -343,7 +336,7 @@ function Simulation.sendfxupdate(sim) if group.state ~= group.fxstate then group.fxstate = group.state - local data = bool_to_int[group.state] + local data = group.state for i, wire in pairs(group.wires) do data = data .. "\t" .. Wire.getobjref(wire) diff --git a/sim/wire.lua b/sim/wire.lua index 9c1992b..91e9daf 100644 --- a/sim/wire.lua +++ b/sim/wire.lua @@ -20,7 +20,7 @@ function Wire.setlayer(wire, layer) end function Wire.update(wire) - client:send("WU\t" .. bool_to_int[wire.group.state] .. "\t" .. wire.objref .. "\n") + client:send("WU\t" .. wire.group.state .. "\t" .. wire.objref .. "\n") end function Wire.setgroup(wire, group)