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

@@ -5,7 +5,7 @@ GateDefinition = {
input = function(gate, argv) end
}
function GateDefinition.new(objref, name, description, init, logic, input, global, ports)
function GateDefinition.new(objref, name, description, init, logic, input, code, global, ports, code)
name = collapseescape(name)
init = collapseescape(init)
@@ -13,17 +13,24 @@ function GateDefinition.new(objref, name, description, init, logic, input, globa
input = collapseescape(input)
global = collapseescape(global)
description = collapseescape(description)
code = collapseescape(code)
local o = {
local compiled_size, compiled_code = Simulation.compile_code(nil, code)
local def = {
objref = objref,
name = name,
description = description,
ports = ports or {}
ports = ports or {},
num_in_ports = 0,
num_out_ports = 0,
compiled_program_code = compiled_code,
compiled_program_size = compiled_size,
}
local initfunc = loadstring(tostring(init))
if initfunc~=nil then
o.init = initfunc() or function()end
def.init = initfunc() or function()end
else
print("Error loading init func for ".. (name or ""))
print(init)
@@ -31,7 +38,7 @@ function GateDefinition.new(objref, name, description, init, logic, input, globa
local logicfunc = loadstring(tostring(logic))
if logicfunc ~= nil then
o.logic = logicfunc() or function()end
def.logic = logicfunc() or function()end
else
print("Error loading logic function for " .. (name or ""))
print(logic)
@@ -39,7 +46,7 @@ function GateDefinition.new(objref, name, description, init, logic, input, globa
local inputfunc = loadstring(tostring(input))
if inputfunc ~= nil then
o.input = inputfunc() or function()end
def.input = inputfunc() or function()end
else
print("Error loading input function for " .. (name or ""))
print(input)
@@ -53,7 +60,16 @@ function GateDefinition.new(objref, name, description, init, logic, input, globa
print(global)
end
return o
for i = 1, #def.ports do
local portd = def.ports[i]
if portd.type==PortTypes.output then
def.num_out_ports = def.num_out_ports + 1
elseif portd.type==PortTypes.input then
def.num_in_ports = def.num_in_ports + 1
else error("invalid port type: "..name.." port "..i..) end
end
return def
end
function GateDefinition.constructgate(def, objref, position, rotation)
@@ -85,10 +101,8 @@ function GateDefinition.constructgate(def, objref, position, rotation)
local port = Port.new(type, dir, {position[1]+pos[1], position[2]+pos[2], position[3]+pos[3]}, portd.causeupdate, i, gate)
gate.ports[port.idx] = port
--gate[port.idx*2] = nil
--gate[port.idx*2+1] = 0
gate.port_nets[port.idx] = nil
gate.port_states[port.idx] = 0
gate.c.ports[port.idx].state = 0
end
return gate