make groups only update update-causing ports
This commit is contained in:
parent
8fe789eade
commit
53e9423ab1
@ -27,7 +27,7 @@ function Gate.addport(self, port)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Gate.getportstate(self, index)
|
function Gate.getportstate(self, index)
|
||||||
return self.ports[index].state
|
return Port.getinputstate(self.ports[index])
|
||||||
end
|
end
|
||||||
|
|
||||||
function Gate.setportstate(self, index, state)
|
function Gate.setportstate(self, index, state)
|
||||||
|
@ -22,6 +22,7 @@ function Group.new(self, sim)
|
|||||||
wires = {},
|
wires = {},
|
||||||
out_ports = {},
|
out_ports = {},
|
||||||
in_ports = {},
|
in_ports = {},
|
||||||
|
in_ports_update = {},
|
||||||
|
|
||||||
state_num = 0,
|
state_num = 0,
|
||||||
|
|
||||||
@ -92,32 +93,65 @@ function Group.removewire(self, wire)
|
|||||||
self.nwires = 0
|
self.nwires = 0
|
||||||
self.nout_ports = 0
|
self.nout_ports = 0
|
||||||
self.nin_ports = 0
|
self.nin_ports = 0
|
||||||
|
|
||||||
|
Simulation.dequeuegroup(Group.getsim(self), self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Group.addport(self, port)
|
function Group.addport(self, port)
|
||||||
|
if port.group~=nil then error("port already has group") end
|
||||||
port.group = self
|
port.group = self
|
||||||
|
|
||||||
if port.type == PortTypes.output then
|
if port.type == PortTypes.output then
|
||||||
|
if self.out_ports[port] then error("port already in group") end
|
||||||
|
|
||||||
self.out_ports[port] = port
|
self.out_ports[port] = port
|
||||||
self.nout_ports = self.nout_ports + 1
|
self.nout_ports = self.nout_ports + 1
|
||||||
|
if Port.getstate(port) then
|
||||||
|
self.state_num = self.state_num + 1
|
||||||
|
end
|
||||||
|
|
||||||
Simulation.queuegroup(Group.getsim(self), self)
|
Simulation.queuegroup(Group.getsim(self), self)
|
||||||
|
|
||||||
elseif port.type == PortTypes.input then
|
elseif port.type == PortTypes.input then
|
||||||
|
if self.in_ports[port] then error("port already in group") end
|
||||||
|
|
||||||
self.in_ports[port] = port
|
self.in_ports[port] = port
|
||||||
self.nin_ports = self.nin_ports + 1
|
self.nin_ports = self.nin_ports + 1
|
||||||
Port.setinputstate(port, self.state)
|
if port.causeupdate then
|
||||||
|
self.in_ports_update[port] = port
|
||||||
|
end
|
||||||
|
|
||||||
|
Simulation.queuegate(Port.getsim(port), Port.getgate(port))
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Group.removeport(self, port)
|
function Group.removeport(self, port)
|
||||||
|
if port.group~=self then error("port does not have group") end
|
||||||
|
port.group = nil
|
||||||
|
|
||||||
if port.type == PortTypes.output then
|
if port.type == PortTypes.output then
|
||||||
|
if not self.out_ports[port] then error("port not in group") end
|
||||||
self.out_ports[port] = nil
|
self.out_ports[port] = nil
|
||||||
self.nout_ports = self.nout_ports - 1
|
self.nout_ports = self.nout_ports - 1
|
||||||
elseif port.type == PortTypes.input then
|
|
||||||
self.in_ports[port] = nil
|
if Port.getstate(port) then
|
||||||
self.nin_ports = self.nin_ports - 1
|
self.state_num = self.state_num - 1
|
||||||
end
|
end
|
||||||
|
|
||||||
Simulation.queuegroup(Group.getsim(self), self)
|
Simulation.queuegroup(Group.getsim(self), self)
|
||||||
|
|
||||||
|
elseif port.type == PortTypes.input then
|
||||||
|
if not self.in_ports[port] then error("port not in group") end
|
||||||
|
|
||||||
|
self.in_ports[port] = nil
|
||||||
|
self.nin_ports = self.nin_ports - 1
|
||||||
|
if port.causeupdate then
|
||||||
|
self.in_ports_update[port] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
Simulation.queuegate(Port.getsim(port), Port.getgate(port))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Group.mergewith(self, group)
|
function Group.mergewith(self, group)
|
||||||
@ -137,10 +171,12 @@ function Group.mergeinto(self, group)
|
|||||||
end
|
end
|
||||||
|
|
||||||
for k, port in pairs(self.out_ports) do
|
for k, port in pairs(self.out_ports) do
|
||||||
|
Port.setgroup(port, nil)
|
||||||
Group.addport(group, port)
|
Group.addport(group, port)
|
||||||
end
|
end
|
||||||
|
|
||||||
for k, port in pairs(self.in_ports) do
|
for k, port in pairs(self.in_ports) do
|
||||||
|
Port.setgroup(port, nil)
|
||||||
Group.addport(group, port)
|
Group.addport(group, port)
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -151,6 +187,8 @@ function Group.mergeinto(self, group)
|
|||||||
self.nwires = 0
|
self.nwires = 0
|
||||||
self.nout_ports = 0
|
self.nout_ports = 0
|
||||||
self.nin_ports = 0
|
self.nin_ports = 0
|
||||||
|
|
||||||
|
Simulation.dequeuegroup(Group.getsim(self), self)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Group.setstate(self, state)
|
function Group.setstate(self, state)
|
||||||
@ -158,8 +196,8 @@ function Group.setstate(self, state)
|
|||||||
self.state = state
|
self.state = state
|
||||||
self.updatetick = Group.getsim(self).currenttick
|
self.updatetick = Group.getsim(self).currenttick
|
||||||
|
|
||||||
for k, port in pairs(self.in_ports) do
|
for k, port in pairs(self.in_ports_update) do
|
||||||
Port.setinputstate(port, state)
|
Simulation.queuegate(Port.getsim(port), Port.getgate(port))
|
||||||
end
|
end
|
||||||
|
|
||||||
Simulation.queuegroupfx(Group.getsim(self), self)
|
Simulation.queuegroupfx(Group.getsim(self), self)
|
||||||
@ -171,13 +209,5 @@ function Group.getsim(group)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Group.update(group)
|
function Group.update(group)
|
||||||
local newstate = false
|
Group.setstate(group, group.state_num>0)
|
||||||
for j, port in pairs(group.out_ports) do
|
|
||||||
newstate = newstate or Port.getstate(port)
|
|
||||||
if newstate then
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
Group.setstate(group, newstate)
|
|
||||||
end
|
end
|
||||||
|
@ -262,6 +262,7 @@ while 1 do
|
|||||||
local group = Wire.getgroup(wire)
|
local group = Wire.getgroup(wire)
|
||||||
local numwires = 0; for k, wire2 in pairs(group.wires ) do numwires = numwires +1 end
|
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 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 numportso = 0; local numportson=0;
|
local numportso = 0; local numportson=0;
|
||||||
for k, port in pairs(group.out_ports) do
|
for k, port in pairs(group.out_ports) do
|
||||||
numportso = numportso+1
|
numportso = numportso+1
|
||||||
@ -272,7 +273,8 @@ while 1 do
|
|||||||
"Wires: "..numwires.."\n"..
|
"Wires: "..numwires.."\n"..
|
||||||
"In Ports: " ..numportsi.."\n"..
|
"In Ports: " ..numportsi.."\n"..
|
||||||
"Out Ports: "..numportso.."\n"..
|
"Out Ports: "..numportso.."\n"..
|
||||||
"Out Ports On: "..numportson
|
"In Ports Update: "..numportsu.."\n"..
|
||||||
|
"Out Ports On: "..(group.state_num)
|
||||||
;
|
;
|
||||||
end
|
end
|
||||||
|
|
||||||
|
24
sim/port.lua
24
sim/port.lua
@ -45,19 +45,15 @@ function Port.new(self, type, direction, position, causeupdate, sim)
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
function Port.setstate(port, state)
|
function Port.setstate(port, state) -- output state
|
||||||
if state ~= port.state then
|
if state ~= port.state then
|
||||||
port.state = state
|
port.state = state
|
||||||
Simulation.queuegroup(Port.getsim(port), port.group)
|
if state then
|
||||||
end
|
Port.getgroup(port).state_num = Port.getgroup(port).state_num + 1
|
||||||
end
|
else
|
||||||
|
Port.getgroup(port).state_num = Port.getgroup(port).state_num - 1
|
||||||
function Port.setinputstate(port, state)
|
|
||||||
if state ~= port.state then
|
|
||||||
port.state = state
|
|
||||||
if port.causeupdate then
|
|
||||||
Simulation.queuegate(Port.getsim(port), port.gate)
|
|
||||||
end
|
end
|
||||||
|
Simulation.queuegroup(Port.getsim(port), Port.getgroup(port))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -80,6 +76,10 @@ function Port.isfalling(port)
|
|||||||
return port.group.state == false and (port.updatetick == Port.getsim(port).currenttick)
|
return port.group.state == false and (port.updatetick == Port.getsim(port).currenttick)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Port.getgate(port)
|
||||||
|
return port.gate
|
||||||
|
end
|
||||||
|
|
||||||
function Port.setgate(port, gate)
|
function Port.setgate(port, gate)
|
||||||
port.gate = gate
|
port.gate = gate
|
||||||
end
|
end
|
||||||
@ -100,6 +100,10 @@ function Port.getstate(port)
|
|||||||
return port.state
|
return port.state
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Port.getinputstate(port)
|
||||||
|
return Port.getgroup(port).state
|
||||||
|
end
|
||||||
|
|
||||||
function Port.getsim(port)
|
function Port.getsim(port)
|
||||||
return port.sim
|
return port.sim
|
||||||
end
|
end
|
||||||
|
@ -165,6 +165,7 @@ function Simulation.removegate(self, objref)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Simulation.dequeuegate(self, gate)
|
||||||
self.gates[objref] = nil
|
self.gates[objref] = nil
|
||||||
self.ngates = self.ngates - 1
|
self.ngates = self.ngates - 1
|
||||||
end
|
end
|
||||||
@ -240,7 +241,7 @@ function Simulation.queuegatelater(self, gate, delay)
|
|||||||
if self.tickqueue[tick] == nil then
|
if self.tickqueue[tick] == nil then
|
||||||
self.tickqueue[tick] = {}
|
self.tickqueue[tick] = {}
|
||||||
end
|
end
|
||||||
table.insert(self.tickqueue[tick], gate)
|
self.tickqueue[tick][gate] = gate
|
||||||
end
|
end
|
||||||
|
|
||||||
function Simulation.queuegateinput(self, gate, argv)
|
function Simulation.queuegateinput(self, gate, argv)
|
||||||
@ -256,6 +257,20 @@ function Simulation.queuegroup(self, group)
|
|||||||
self.groupqueue[group] = group
|
self.groupqueue[group] = group
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Simulation.dequeuegroup(self, group)
|
||||||
|
self.groupqueue[group] = nil
|
||||||
|
self.groupfxqueue[group] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
function Simulation.dequeuegate(self, gate)
|
||||||
|
self.gatequeue[gate] = nil
|
||||||
|
self.initqueue[gate] = nil
|
||||||
|
self.inputqueue[gate] = nil
|
||||||
|
for tick, tickq in pairs(self.tickqueue) do
|
||||||
|
tickq[gate] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Simulation.queuegroupfx(self, group)
|
function Simulation.queuegroupfx(self, group)
|
||||||
self.groupfxqueue[group] = group
|
self.groupfxqueue[group] = group
|
||||||
end
|
end
|
||||||
@ -283,7 +298,7 @@ function Simulation.tick(self)
|
|||||||
self.inputqueue = {}
|
self.inputqueue = {}
|
||||||
|
|
||||||
if self.tickqueue[self.currenttick] ~= nil then
|
if self.tickqueue[self.currenttick] ~= nil then
|
||||||
for i, gate in ipairs(self.tickqueue[self.currenttick]) do
|
for i, gate in pairs(self.tickqueue[self.currenttick]) do
|
||||||
Simulation.queuegate(self, gate)
|
Simulation.queuegate(self, gate)
|
||||||
end
|
end
|
||||||
self.tickqueue[self.currenttick] = nil
|
self.tickqueue[self.currenttick] = nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user