more optimizations: move port states into gates, inline net queue checks
This commit is contained in:
parent
14c61e35d1
commit
19d2e36fd6
31
sim/gate.lua
31
sim/gate.lua
@ -5,13 +5,11 @@ Gate = {}
|
|||||||
|
|
||||||
function Gate.new(objref, definition)
|
function Gate.new(objref, definition)
|
||||||
local o = {
|
local o = {
|
||||||
--in_queue = ffi.new("bool", false),
|
|
||||||
--in_queue = false,
|
|
||||||
--in_queue = ffi.new("long long", 0),
|
|
||||||
in_queue = 0,
|
in_queue = 0,
|
||||||
port_nets = {},
|
|
||||||
logic = definition.logic,
|
logic = definition.logic,
|
||||||
ports = {},
|
ports = {},
|
||||||
|
port_nets = {},
|
||||||
|
port_states = {},
|
||||||
|
|
||||||
objref = objref,
|
objref = objref,
|
||||||
definition = definition,
|
definition = definition,
|
||||||
@ -19,30 +17,33 @@ function Gate.new(objref, definition)
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
function Gate.addport(gate, port)
|
|
||||||
gate.ports[#gate.ports+1] = port
|
|
||||||
Port.setgate(port, gate)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Gate.getportstate(gate, index)
|
function Gate.getportstate(gate, index)
|
||||||
|
--return gate[index*2].state
|
||||||
return gate.port_nets[index].state
|
return gate.port_nets[index].state
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Gate.setportstate(gate, index, state)
|
function Gate.setportstate(gate, index, state)
|
||||||
local port = gate.ports[index]
|
--if state ~= gate[index*2+1] then
|
||||||
if state ~= port.state then
|
if state ~= gate.port_states[index] then
|
||||||
local group = port.group
|
--local group = gate[index*2]
|
||||||
group.state_num = group.state_num - port.state + state
|
local group = gate.port_nets[index]
|
||||||
port.state = state
|
--group.state_num = group.state_num - gate[index*2+1] + state
|
||||||
|
group.state_num = group.state_num - gate.port_states[index] + state
|
||||||
|
--gate[index*2+1] = state
|
||||||
|
gate.port_states[index] = state
|
||||||
|
|
||||||
if (group.state_num>0) ~= (group.state==1) then
|
if (group.state_num>0) ~= (group.state==1) and (group.in_queue==0) then
|
||||||
Simulation.queuegroup(GSim, group)
|
Simulation.queuegroup(GSim, group)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Gate.preinit(gate)
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
function Gate.initdata(gate)
|
function Gate.initdata(gate)
|
||||||
gate.data = {}
|
gate.data = {}
|
||||||
end
|
end
|
||||||
|
@ -81,9 +81,15 @@ function GateDefinition.constructgate(def, objref, position, rotation)
|
|||||||
pos[1] = -pos[2]
|
pos[1] = -pos[2]
|
||||||
pos[2] = x
|
pos[2] = x
|
||||||
end
|
end
|
||||||
|
|
||||||
Gate.addport(gate, Port.new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, i))
|
local port = Port.new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, i, gate)
|
||||||
|
|
||||||
|
gate.ports[port.idx] = port
|
||||||
|
--gate[port.idx*2] = nil
|
||||||
|
--gate[port.idx*2+1] = 0
|
||||||
|
gate.port_nets[port.idx] = nil
|
||||||
|
gate.port_states[port.idx] = 0
|
||||||
end
|
end
|
||||||
|
|
||||||
return gate
|
return gate
|
||||||
end
|
end
|
||||||
|
@ -5,13 +5,8 @@ Group = {}
|
|||||||
|
|
||||||
function Group.new()
|
function Group.new()
|
||||||
local o = {
|
local o = {
|
||||||
--state = ffi.new("long long", 0),
|
|
||||||
state = 0,
|
state = 0,
|
||||||
--state_num = ffi.new("long long", 0),
|
|
||||||
state_num = 0,
|
state_num = 0,
|
||||||
--in_queue = ffi.new("bool", false),
|
|
||||||
--in_queue = false,
|
|
||||||
--in_queue = ffi.new("long long", 0),
|
|
||||||
in_queue = 0,
|
in_queue = 0,
|
||||||
gates_update = {},
|
gates_update = {},
|
||||||
num_gates_update = 0,
|
num_gates_update = 0,
|
||||||
@ -44,7 +39,9 @@ function Group.addwire(group, wire)
|
|||||||
Wire.setgroup(wire, group)
|
Wire.setgroup(wire, group)
|
||||||
Wire.update(wire)
|
Wire.update(wire)
|
||||||
|
|
||||||
Simulation.queuegroup(GSim, group)
|
if group.in_queue==0 then
|
||||||
|
Simulation.queuegroup(GSim, group)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -186,11 +183,9 @@ function Group.setstate(group, state)
|
|||||||
group.state = state
|
group.state = state
|
||||||
group.update_tick = sim.current_tick
|
group.update_tick = sim.current_tick
|
||||||
|
|
||||||
--for k, gate in ipairs(group.gates_update) do
|
|
||||||
local len = group.num_gates_update
|
local len = group.num_gates_update
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
local gate = group.gates_update[i]
|
Simulation.queuegate(sim, group.gates_update[i])
|
||||||
Simulation.queuegate(sim, gate)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
Simulation.queuegroupfx(sim, group)
|
Simulation.queuegroupfx(sim, group)
|
||||||
|
@ -205,8 +205,6 @@ function network_update()
|
|||||||
i = i + 1
|
i = i + 1
|
||||||
end
|
end
|
||||||
elseif err == "closed" then
|
elseif err == "closed" then
|
||||||
--sim = Simulation.new(Simulation)
|
|
||||||
--acceptclient()
|
|
||||||
print("Connection closed")
|
print("Connection closed")
|
||||||
error()
|
error()
|
||||||
end
|
end
|
||||||
|
13
sim/port.lua
13
sim/port.lua
@ -17,17 +17,15 @@ PortDirections = {
|
|||||||
|
|
||||||
Port = {}
|
Port = {}
|
||||||
|
|
||||||
function Port.new(type, direction, position, causeupdate, idx)
|
function Port.new(type, direction, position, causeupdate, idx, gate)
|
||||||
local o = {
|
local o = {
|
||||||
--state = ffi.new("long long", 0),
|
|
||||||
state = 0,
|
|
||||||
group = nil,
|
group = nil,
|
||||||
|
|
||||||
type = type,
|
type = type,
|
||||||
direction = direction,
|
direction = direction,
|
||||||
position = position,
|
position = position,
|
||||||
causeupdate = causeupdate,
|
causeupdate = causeupdate,
|
||||||
gate = nil,
|
gate = gate,
|
||||||
idx = idx,
|
idx = idx,
|
||||||
}
|
}
|
||||||
return o
|
return o
|
||||||
@ -50,12 +48,9 @@ function Port.getgate(port)
|
|||||||
return port.gate
|
return port.gate
|
||||||
end
|
end
|
||||||
|
|
||||||
function Port.setgate(port, gate)
|
|
||||||
port.gate = gate
|
|
||||||
end
|
|
||||||
|
|
||||||
function Port.setgroup(port, group)
|
function Port.setgroup(port, group)
|
||||||
port.group = group
|
port.group = group
|
||||||
|
--Port.getgate(port)[port.idx*2] = group
|
||||||
Port.getgate(port).port_nets[port.idx] = group
|
Port.getgate(port).port_nets[port.idx] = group
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -68,5 +63,5 @@ function Port.gettype(port)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function Port.getstate(port)
|
function Port.getstate(port)
|
||||||
return port.state
|
return Port.getgate(port).port_states[port.idx]
|
||||||
end
|
end
|
||||||
|
@ -116,6 +116,7 @@ function Simulation.addgate(sim, gate)
|
|||||||
|
|
||||||
sim.ngates = sim.ngates + 1
|
sim.ngates = sim.ngates + 1
|
||||||
|
|
||||||
|
Gate.preinit(gate)
|
||||||
Simulation.queuegateinit(sim, gate)
|
Simulation.queuegateinit(sim, gate)
|
||||||
Simulation.queuegate(sim, gate)
|
Simulation.queuegate(sim, gate)
|
||||||
end
|
end
|
||||||
@ -239,7 +240,8 @@ end
|
|||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Simulation.queuegate(sim, gate)
|
function Simulation.queuegate(sim, gate)
|
||||||
if gate.in_queue==0 then
|
if gate.in_queue==0 then
|
||||||
table.insert(sim.gatequeue, gate)
|
--table.insert(sim.gatequeue, gate)
|
||||||
|
sim.gatequeue[sim.num_gatequeue+1] = gate
|
||||||
sim.num_gatequeue = sim.num_gatequeue + 1
|
sim.num_gatequeue = sim.num_gatequeue + 1
|
||||||
gate.in_queue = 1
|
gate.in_queue = 1
|
||||||
end
|
end
|
||||||
@ -265,12 +267,11 @@ end
|
|||||||
|
|
||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Simulation.queuegroup(sim, group)
|
function Simulation.queuegroup(sim, group)
|
||||||
if group.in_queue==0 then
|
--if group.in_queue==0 then
|
||||||
--table.insert(sim.groupqueue, group)
|
|
||||||
sim.groupqueue[sim.num_groupqueue+1] = group
|
sim.groupqueue[sim.num_groupqueue+1] = group
|
||||||
sim.num_groupqueue = sim.num_groupqueue + 1
|
sim.num_groupqueue = sim.num_groupqueue + 1
|
||||||
group.in_queue = 1
|
group.in_queue = 1
|
||||||
end
|
--end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Simulation.dequeuegroup(sim, group)
|
function Simulation.dequeuegroup(sim, group)
|
||||||
@ -306,7 +307,6 @@ end
|
|||||||
|
|
||||||
-- Logic Critical
|
-- Logic Critical
|
||||||
function Simulation.ticklogic(sim)
|
function Simulation.ticklogic(sim)
|
||||||
--for k, group in ipairs(sim.groupqueue) do
|
|
||||||
local len = sim.num_groupqueue
|
local len = sim.num_groupqueue
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
local group = sim.groupqueue[i]
|
local group = sim.groupqueue[i]
|
||||||
@ -323,7 +323,6 @@ function Simulation.ticklogic(sim)
|
|||||||
sim.tickqueue[sim.current_tick] = nil
|
sim.tickqueue[sim.current_tick] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
--for k, gate in ipairs(sim.gatequeue) do
|
|
||||||
local len = sim.num_gatequeue
|
local len = sim.num_gatequeue
|
||||||
for i = 1, len do
|
for i = 1, len do
|
||||||
local gate = sim.gatequeue[i]
|
local gate = sim.gatequeue[i]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user