rename some variables and begin compiled sim c code

This commit is contained in:
Redo0 2021-05-25 21:23:51 -05:00
parent 09e65faec4
commit 968613a3fc
6 changed files with 25 additions and 45 deletions

View File

@ -3,34 +3,7 @@ local ffi = FFI or require("ffi")
Simulation = Simulation or {} Simulation = Simulation or {}
ffi.cdef[[ ffi.cdef[[
struct Wire {
int objref;
int layer;
struct Net* group;
int bounds[6];
};
struct Port {
bool state;
char type;
struct Gate* gate;
struct Net* group;
};
struct Gate {
int logic_ref;
bool in_queue;
struct Port ports[?];
};
struct Net {
bool state;
bool in_queue;
int updatetick;
int state_num;
int num_gates_update;
struct Gate* gates_update[?];
};
]] ]]
function Simulation.compile(sim) function Simulation.compile(sim)

7
sim/compiled_sim.c Normal file
View File

@ -0,0 +1,7 @@
void sim_init(int num_gates, int num_nets);
void sim_add_gate();
void sim_add_net();
void sim_tick();
void sim_get_net_state(int objref);
void sim_get_port_state(int objref, int index);

View File

@ -60,7 +60,7 @@ function Gate.queue(gate, delay)
end end
function Gate.gettick(gate) function Gate.gettick(gate)
return GSim.currenttick return GSim.current_tick
end end
function Gate.getdefinition(gate) function Gate.getdefinition(gate)

View File

@ -5,7 +5,7 @@ function Group.new()
local o = { local o = {
state = false, state = false,
fxstate = false, fxstate = false,
updatetick = 0, update_tick = 0,
wires = {}, wires = {},
out_ports = {}, out_ports = {},
in_ports = {}, in_ports = {},
@ -179,7 +179,7 @@ function Group.setstate(group, state)
local sim = GSim local sim = GSim
group.state = state group.state = state
group.updatetick = sim.currenttick group.update_tick = sim.current_tick
for k, gate in ipairs(group.gates_update) do for k, gate in ipairs(group.gates_update) do
Simulation.queuegate(sim, gate) Simulation.queuegate(sim, gate)

View File

@ -38,14 +38,14 @@ function Port.isrising(port)
if port.group == nil then if port.group == nil then
return false return false
end end
return port.group.state and (port.group.updatetick == GSim.currenttick) return port.group.state and (port.group.update_tick == GSim.current_tick)
end end
function Port.isfalling(port) function Port.isfalling(port)
if port.group == nil then if port.group == nil then
return false return false
end end
return port.group.state == false and (port.updatetick == GSim.currenttick) return port.group.state == false and (port.group.update_tick == GSim.current_tick)
end end
function Port.getgate(port) function Port.getgate(port)

View File

@ -6,12 +6,12 @@ function Simulation.new(sim)
definitions = {}, definitions = {},
wires = {}, wires = {},
gates = {}, gates = {},
nwires = 0, nwires = 0,
ngates = 0, ngates = 0,
ninports = 0, ninports = 0,
noutports = 0, noutports = 0,
groupqueue = {}, groupqueue = {},
groupfxqueue = {}, groupfxqueue = {},
gatequeue = {}, gatequeue = {},
@ -20,10 +20,10 @@ function Simulation.new(sim)
tickqueue = {}, tickqueue = {},
inputqueue_nonempty = false, inputqueue_nonempty = false,
initqueue_nonempty = false, initqueue_nonempty = false,
callbacks = nil, callbacks = nil,
currenttick = 0 current_tick = 0,
} }
setmetatable(o, sim) setmetatable(o, sim)
sim.__index = sim sim.__index = sim
@ -34,15 +34,15 @@ function Simulation.addtoworld(sim, obj, x, y, z)
if sim[x] == nil then if sim[x] == nil then
sim[x] = {} sim[x] = {}
end end
if sim[x][y] == nil then if sim[x][y] == nil then
sim[x][y] = {} sim[x][y] = {}
end end
if sim[x][y][z] == nil then if sim[x][y][z] == nil then
sim[x][y][z] = {} sim[x][y][z] = {}
end end
sim[x][y][z][obj] = obj sim[x][y][z][obj] = obj
end end
@ -245,7 +245,7 @@ function Simulation.queuegate(sim, gate)
end end
function Simulation.queuegatelater(sim, gate, delay) function Simulation.queuegatelater(sim, gate, delay)
local tick = sim.currenttick + delay local tick = sim.current_tick + delay
if sim.tickqueue[tick] == nil then if sim.tickqueue[tick] == nil then
sim.tickqueue[tick] = {} sim.tickqueue[tick] = {}
end end
@ -322,11 +322,11 @@ function Simulation.tick(sim)
sim.inputqueue_nonempty = false sim.inputqueue_nonempty = false
end end
if sim.tickqueue[sim.currenttick] ~= nil then if sim.tickqueue[sim.current_tick] ~= nil then
for i, gate in pairs(sim.tickqueue[sim.currenttick]) do for i, gate in pairs(sim.tickqueue[sim.current_tick]) do
Simulation.queuegate(sim, gate) Simulation.queuegate(sim, gate)
end end
sim.tickqueue[sim.currenttick] = nil sim.tickqueue[sim.current_tick] = nil
end end
for k, gate in ipairs(sim.gatequeue) do for k, gate in ipairs(sim.gatequeue) do
@ -335,7 +335,7 @@ function Simulation.tick(sim)
end end
sim.gatequeue = {} sim.gatequeue = {}
sim.currenttick = sim.currenttick + 1 sim.current_tick = sim.current_tick + 1
end end
function Simulation.sendfxupdate(sim) function Simulation.sendfxupdate(sim)