lua-logic/sim/port.lua
2021-05-25 17:28:54 -05:00

99 lines
1.7 KiB
Lua

PortTypes = {
output = 0,
input = 1,
}
PortDirections = {
[0] = {-1, 0, 0},
[1] = {0, 1, 0},
[2] = {1, 0, 0},
[3] = {0, -1, 0},
[4] = {0, 0, 1},
[5] = {0, 0, -1}
}
Port = {}
FFI.cdef[[
struct Gate;
struct Net;
struct Port {
bool state;
char type;
char direction;
bool causeupdate;
int position[3];
struct Gate* gate;
struct Net* group;
};
]]
function Port.new(type, direction, position, causeupdate)
local o = {
type = type,
direction = direction,
position = position,
causeupdate = causeupdate,
state = false,
gate = nil,
group = nil,
}
return o
end
function Port.setstate(port, state) -- output state
if state ~= port.state then
port.state = state
if state then
Port.getgroup(port).state_num = Port.getgroup(port).state_num + 1
else
Port.getgroup(port).state_num = Port.getgroup(port).state_num - 1
end
Simulation.queuegroup(GSim, Port.getgroup(port))
end
end
function Port.getconnectionposition(port)
local offset = PortDirections[port.direction]
return {port.position[1]+offset[1], port.position[2]+offset[2], port.position[3]+offset[3]}
end
function Port.isrising(port)
if port.group == nil then
return false
end
return port.group.state and (port.group.updatetick == GSim.currenttick)
end
function Port.isfalling(port)
if port.group == nil then
return false
end
return port.group.state == false and (port.updatetick == GSim.currenttick)
end
function Port.getgate(port)
return port.gate
end
function Port.setgate(port, gate)
port.gate = gate
end
function Port.setgroup(port, group)
port.group = group
end
function Port.getgroup(port)
return port.group
end
function Port.gettype(port)
return port.type
end
function Port.getstate(port)
return port.state
end