make sim local; start circuit compiler

This commit is contained in:
Redo0 2021-05-25 05:26:06 -05:00
parent 569e79ab96
commit 0963ef8ca8
4 changed files with 51 additions and 9 deletions

View File

@ -1,6 +1,44 @@
local ffi = FFI or require("ffi")
Simulation = Simulation or {}
ffi.cdef([[
struct Net {
};
]])
function Simulation.compile(sim) 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 end
function Simulation.decompile(sim) function Simulation.decompile(sim)

View File

@ -10,6 +10,7 @@ OPT_SAVE_DIR = OPT_SAVE_DIR:gsub("/$", "")
print("Save location set to \""..OPT_SAVE_DIR.."\"") print("Save location set to \""..OPT_SAVE_DIR.."\"")
local socket = require("socket") local socket = require("socket")
local ffi = require("ffi")
dofile("iosafe.lua") dofile("iosafe.lua")
@ -21,7 +22,9 @@ dofile("gatedef.lua")
dofile("gate.lua") dofile("gate.lua")
dofile("port.lua") dofile("port.lua")
dofile("save.lua") dofile("save.lua")
FFI = ffi
dofile("compile.lua") dofile("compile.lua")
FFI = nil
OPT_TICK_ENABLED = true OPT_TICK_ENABLED = true
OPT_TICK_TIME = 0.032 OPT_TICK_TIME = 0.032
@ -145,7 +148,7 @@ while 1 do
local max = vectotable(data[i+4]) local max = vectotable(data[i+4])
local bounds = {min[1], min[2], min[3], max[1], max[2], max[3]} 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) Simulation.addwire(sim, wire)
i = i + 4 i = i + 4

View File

@ -125,22 +125,22 @@ function Simulation.removewire(self, objref)
for x = bounds[1]+1, bounds[4]-1, 2 do for x = bounds[1]+1, bounds[4]-1, 2 do
for z = bounds[3]+1, bounds[6]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do
sim[x][bounds[2]][z][wire] = nil self[x][bounds[2]][z][wire] = nil
sim[x][bounds[5]][z][wire] = nil self[x][bounds[5]][z][wire] = nil
end end
end end
for y = bounds[2]+1, bounds[5]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do
for z = bounds[3]+1, bounds[6]-1, 2 do for z = bounds[3]+1, bounds[6]-1, 2 do
sim[bounds[1]][y][z][wire] = nil self[bounds[1]][y][z][wire] = nil
sim[bounds[4]][y][z][wire] = nil self[bounds[4]][y][z][wire] = nil
end end
end end
for x = bounds[1]+1, bounds[4]-1, 2 do for x = bounds[1]+1, bounds[4]-1, 2 do
for y = bounds[2]+1, bounds[5]-1, 2 do for y = bounds[2]+1, bounds[5]-1, 2 do
sim[x][y][bounds[3]][wire] = nil self[x][y][bounds[3]][wire] = nil
sim[x][y][bounds[6]][wire] = nil self[x][y][bounds[6]][wire] = nil
end end
end end

View File

@ -3,12 +3,13 @@ Wire = {
logictype = 0 logictype = 0
} }
function Wire.new(self, objref, layer, bounds) function Wire.new(self, objref, layer, bounds, sim)
local o = { local o = {
objref = objref, objref = objref,
layer = layer, layer = layer,
group = nil, group = nil,
bounds = bounds bounds = bounds,
sim = sim,
} }
setmetatable(o, self) setmetatable(o, self)
self.__index = self self.__index = self