FPGA: Unify actions in single table

This commit is contained in:
SmallJoker 2019-11-02 12:46:10 +01:00 committed by SmallJoker
parent bfd952b51a
commit e78bbd6f98
2 changed files with 50 additions and 68 deletions

@ -1,10 +1,10 @@
local plg = {} local plg = {}
plg.rules = {} plg.rules = {}
local lcore = dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/logic.lua") local lcore = dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/logic.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/tool.lua")(plg) dofile(minetest.get_modpath(minetest.get_current_modname()) .. "/tool.lua")(plg)
plg.register_nodes = function(template) plg.register_nodes = function(template)
-- each loop is for one of the 4 IO ports -- each loop is for one of the 4 IO ports
for a = 0, 1 do for a = 0, 1 do
@ -180,22 +180,16 @@ plg.to_formspec_string = function(is)
return s .. "]" return s .. "]"
end end
local function dropdown_action(x, y, name, val) local function dropdown_action(x, y, name, val)
local s = "dropdown[" .. tostring(x) .. "," .. tostring(y) .. ";" local selected = 0
.. "1.125,0.5;" .. name .. ";" -- the height seems to be ignored? local titles = { " " }
s = s .. " , AND, OR, NOT, XOR,NAND, =,XNOR;" for i, data in ipairs(lcore.get_operands()) do
if val == nil then titles[i + 1] = data.fs_name
return s .. "0]" -- actually selects no field at all if val == data.gate then
selected = i + 1
end
end end
local mapping = { return ("dropdown[%f,%f;1.125,0.5;%s;%s;%i]"):format(
["and"] = 1, x, y, name, table.concat(titles, ","), selected)
["or"] = 2,
["not"] = 3,
["xor"] = 4,
["nand"] = 5,
["buf"] = 6,
["xnor"] = 7,
}
return s .. tostring(1 + mapping[val]) .. "]"
end end
local s = "size[9,9]".. local s = "size[9,9]"..
"label[3.4,-0.15;FPGA gate configuration]".. "label[3.4,-0.15;FPGA gate configuration]"..
@ -239,20 +233,11 @@ plg.from_formspec_fields = function(fields)
end end
end end
local function read_action(s) local function read_action(s)
if s == nil or s == " " then for i, data in ipairs(lcore.get_operands()) do
return nil if data.fs_name == s then
return data.gate
end
end end
local mapping = {
["AND"] = "and",
["OR"] = "or",
["NOT"] = "not",
["XOR"] = "xor",
["NAND"] = "nand",
["="] = "buf",
["XNOR"] = "xnor",
}
s = s:gsub("^%s*", "") -- remove leading spaces
return mapping[s]
end end
local is = {} local is = {}
for i = 1, 14 do for i = 1, 14 do

@ -1,5 +1,21 @@
local lg = {} local lg = {}
local operands = {
-- index: Index in formspec
{ gate = "and", short = "&", fs_name = " AND", func = function(a, b) return a and b end },
{ gate = "or", short = "|", fs_name = " OR", func = function(a, b) return a or b end },
{ gate = "not", short = "~", fs_name = " NOT", func = function(a, b) return not b end },
{ gate = "xor", short = "^", fs_name = " XOR", func = function(a, b) return a ~= b end },
{ gate = "nand", short = "?", fs_name = "NAND", func = function(a, b) return not (a and b) end },
{ gate = "buf", short = "_", fs_name = " =", func = function(a, b) return b end },
{ gate = "xnor", short = "=", fs_name = "XNOR", func = function(a, b) return a == b end }
}
lg.get_operands = function()
return operands
end
-- (de)serialize -- (de)serialize
lg.serialize = function(t) lg.serialize = function(t)
local function _op(t) local function _op(t)
@ -11,20 +27,14 @@ lg.serialize = function(t)
return tostring(t.n) return tostring(t.n)
end end
end end
local function _action(s) -- Serialize actions (gates) from eg. "and" to "&"
if s == nil then local function _action(action)
return " " for i, data in ipairs(operands) do
if data.gate == action then
return data.short
end
end end
local mapping = { return " "
["and"] = "&",
["or"] = "|",
["not"] = "~",
["xor"] = "^",
["nand"] = "?", --dunno
["buf"] = "_",
["xnor"] = "=",
}
return mapping[s]
end end
local s = "" local s = ""
@ -48,18 +58,14 @@ lg.deserialize = function(s)
return {type = "reg", n = tonumber(c)} return {type = "reg", n = tonumber(c)}
end end
end end
local function _action(c) -- Deserialize actions (gates) from eg. "&" to "and"
local mapping = { local function _action(action)
["&"] = "and", for i, data in ipairs(operands) do
["|"] = "or", if data.short == action then
["~"] = "not", return data.gate
["^"] = "xor", end
["?"] = "nand", end
["_"] = "buf", -- nil
["="] = "xnor",
[" "] = nil,
}
return mapping[c]
end end
local ret = {} local ret = {}
@ -159,21 +165,12 @@ end
-- interpreter -- interpreter
lg.interpret = function(t, a, b, c, d) lg.interpret = function(t, a, b, c, d)
local function _action(s, v1, v2) local function _action(s, v1, v2)
if s == "and" then for i, data in ipairs(operands) do
return v1 and v2 if data.gate == s then
elseif s == "or" then return data.func(v1, v2)
return v1 or v2 end
elseif s == "not" then
return not v2
elseif s == "xor" then
return v1 ~= v2
elseif s == "nand" then
return not (v1 and v2)
elseif s == "buf" then
return v2
else -- s == "xnor"
return v1 == v2
end end
return false -- unknown gate
end end
local function _op(t, regs, io_in) local function _op(t, regs, io_in)
if t.type == "reg" then if t.type == "reg" then