make gates use cdata for critical logic

This commit is contained in:
Redo
2022-11-04 13:10:10 -06:00
parent 07b3decc45
commit cdbf3ed089
10 changed files with 232 additions and 62 deletions

View File

@@ -1,39 +1,48 @@
local ffi = FFI
local ffi = FFI or require("ffi")
Gate = {}
ffi.cdef [[
struct OutPort {
struct Net* net;
int state;
};
struct Gate {
int in_queue;
struct OutPort ports[0];
};
]]
function Gate.new(objref, definition)
local o = {
local gate = {
-- Logic Critical
in_queue = 0,
c = nil,
logic = definition.logic,
ports = {},
port_nets = {},
port_states = {},
objref = objref,
definition = definition,
}
return o
local cdata = ffi.new("char["..(ffi.sizeof("struct Gate") + ffi.sizeof("struct OutPort")*(#gate.ports+1)).."]")
gate.c = ffi.cast("struct Gate*", cdata)
gate.c.in_queue = 0
Gate.update_c_ports(gate)
return gate
end
-- Logic Critical
function Gate.getportstate(gate, index)
--return gate[index*2].state
return gate.port_nets[index].state
return gate.c.ports[index].state
end
-- Logic Critical
function Gate.setportstate(gate, index, state)
--if state ~= gate[index*2+1] then
if state ~= gate.port_states[index] then
--local group = gate[index*2]
if state ~= gate.c.ports[index].state then
local group = gate.port_nets[index]
--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
group.state_num = group.state_num - gate.c.ports[index].state + state
gate.c.ports[index].state = state
if ((group.state_num>0) ~= (group.state==1)) and (group.in_queue==0) then
Simulation.queuegroup(GSim, group)
@@ -41,6 +50,11 @@ function Gate.setportstate(gate, index, state)
end
end
-- Logic Critical
function Gate.logic(gate)
gate.logic(gate)
end
function Gate.preinit(gate)
end
@@ -83,10 +97,6 @@ function Gate.init(gate)
Gate.getdefinition(gate).init(gate)
end
function Gate.logic(gate)
gate.logic(gate)
end
function Gate.input(gate, argv)
Gate.getdefinition(gate).input(gate, argv)
end