From 0963ef8ca871af0b69d9a7b0b3818025c4b1f4cf Mon Sep 17 00:00:00 2001 From: Redo0 Date: Tue, 25 May 2021 05:26:06 -0500 Subject: [PATCH] make sim local; start circuit compiler --- sim/compile.lua | 38 ++++++++++++++++++++++++++++++++++++++ sim/main.lua | 5 ++++- sim/simulation.lua | 12 ++++++------ sim/wire.lua | 5 +++-- 4 files changed, 51 insertions(+), 9 deletions(-) diff --git a/sim/compile.lua b/sim/compile.lua index 3ecdd0d..7701c5a 100644 --- a/sim/compile.lua +++ b/sim/compile.lua @@ -1,6 +1,44 @@ +local ffi = FFI or require("ffi") +Simulation = Simulation or {} + +ffi.cdef([[ + struct Net { + + }; +]]) + function Simulation.compile(sim) + -- assemble a list of all groups + local groups = {} + for wire_idx, wire in pairs(sim.wires) do + local group = Wire.getgroup(wire) + groups[group] = group + end + local num_groups = 0 + for group_id, group in pairs(groups) do + num_groups = num_groups+1 + end + -- construct each gate into an array + + -- construct each group into an array + local group_idx = 0 + local array_nets = ffi.new("struct Net["..num_groups.."]") + for group_id, group in pairs(groups) do + local c_net = ffi.new("struct Net") + + local ports_update = {} + for port_id, port in pairs(group.in_ports) do + if port.causeupdate then + num_ports_update = num + end + end + --c_net.ports_update = ffi.new("struct Port[".. + + array_nets[group_idx] = c_net + group_idx = group_idx + 1 + end end function Simulation.decompile(sim) diff --git a/sim/main.lua b/sim/main.lua index 05d40ab..618886e 100644 --- a/sim/main.lua +++ b/sim/main.lua @@ -10,6 +10,7 @@ OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("/$", "") print("Save location set to \""..OPT_SAVE_DIR.."\"") local socket = require("socket") +local ffi = require("ffi") dofile("iosafe.lua") @@ -21,7 +22,9 @@ dofile("gatedef.lua") dofile("gate.lua") dofile("port.lua") dofile("save.lua") +FFI = ffi dofile("compile.lua") +FFI = nil OPT_TICK_ENABLED = true OPT_TICK_TIME = 0.032 @@ -145,7 +148,7 @@ while 1 do local max = vectotable(data[i+4]) local bounds = {min[1], min[2], min[3], max[1], max[2], max[3]} - local wire = Wire.new(Wire, tonumber(data[i+1]), tonumber(data[i+2]), bounds) + local wire = Wire.new(Wire, tonumber(data[i+1]), tonumber(data[i+2]), bounds, sim) Simulation.addwire(sim, wire) i = i + 4 diff --git a/sim/simulation.lua b/sim/simulation.lua index 3093498..525ee00 100644 --- a/sim/simulation.lua +++ b/sim/simulation.lua @@ -125,22 +125,22 @@ function Simulation.removewire(self, objref) for x = bounds[1]+1, bounds[4]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do - sim[x][bounds[2]][z][wire] = nil - sim[x][bounds[5]][z][wire] = nil + self[x][bounds[2]][z][wire] = nil + self[x][bounds[5]][z][wire] = nil end end for y = bounds[2]+1, bounds[5]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do - sim[bounds[1]][y][z][wire] = nil - sim[bounds[4]][y][z][wire] = nil + self[bounds[1]][y][z][wire] = nil + self[bounds[4]][y][z][wire] = nil end end for x = bounds[1]+1, bounds[4]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do - sim[x][y][bounds[3]][wire] = nil - sim[x][y][bounds[6]][wire] = nil + self[x][y][bounds[3]][wire] = nil + self[x][y][bounds[6]][wire] = nil end end diff --git a/sim/wire.lua b/sim/wire.lua index 7e032ce..7ef72d5 100644 --- a/sim/wire.lua +++ b/sim/wire.lua @@ -3,12 +3,13 @@ Wire = { logictype = 0 } -function Wire.new(self, objref, layer, bounds) +function Wire.new(self, objref, layer, bounds, sim) local o = { objref = objref, layer = layer, group = nil, - bounds = bounds + bounds = bounds, + sim = sim, } setmetatable(o, self) self.__index = self