add net and wire struct; move net update into its file

This commit is contained in:
Redo0 2021-05-25 15:13:03 -05:00
parent 8561940777
commit d151ab688c
5 changed files with 38 additions and 15 deletions

View File

@ -2,12 +2,6 @@
local ffi = FFI or require("ffi") local ffi = FFI or require("ffi")
Simulation = Simulation or {} Simulation = Simulation or {}
ffi.cdef([[
struct Net {
};
]])
function Simulation.compile(sim) function Simulation.compile(sim)
-- assemble a list of all nets -- assemble a list of all nets
local all_nets = {} local all_nets = {}

View File

@ -1,6 +1,19 @@
Group = {} Group = {}
FFI.cdef[[
struct Port;
struct Net {
bool state;
bool fxstate;
int updatetick;
int internal_ref;
int state_num;
int num_in_ports_update;
struct Port* in_ports_update[1];
};
]]
function Group.new(self, sim) function Group.new(self, sim)
local o = { local o = {
state = false, state = false,
@ -9,6 +22,8 @@ function Group.new(self, sim)
wires = {}, wires = {},
out_ports = {}, out_ports = {},
in_ports = {}, in_ports = {},
state_num = 0,
nwires = 0, nwires = 0,
nout_ports = 0, nout_ports = 0,
@ -154,3 +169,15 @@ end
function Group.getsim(group) function Group.getsim(group)
return group.sim return group.sim
end end
function Group.update(group)
local newstate = false
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

View File

@ -17,6 +17,7 @@ Port = {}
FFI.cdef[[ FFI.cdef[[
struct Gate; struct Gate;
struct Net;
struct Port { struct Port {
bool state; bool state;
char type; char type;

View File

@ -266,15 +266,7 @@ end
function Simulation.tick(self) function Simulation.tick(self)
for k, group in pairs(self.groupqueue) do for k, group in pairs(self.groupqueue) do
local newstate = false Group.update(group)
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
self.groupqueue = {} self.groupqueue = {}

View File

@ -1,6 +1,15 @@
Wire = {} Wire = {}
FFI.cdef[[
struct Wire {
int objref;
int layer;
struct Net* group;
int bounds[6];
};
]]
function Wire.new(self, objref, layer, bounds, sim) function Wire.new(self, objref, layer, bounds, sim)
local o = { local o = {
objref = objref, objref = objref,