summaryrefslogtreecommitdiff
path: root/mesecons_fpga/logic.lua
diff options
context:
space:
mode:
authorSmallJoker <mk939@ymail.com>2019-11-02 12:46:10 +0100
committerSmallJoker <SmallJoker@users.noreply.github.com>2019-11-10 11:35:02 +0100
commite78bbd6f98cb4acd9865e47abfb4ab7103f800c5 (patch)
tree063919ed29b30b2e9f7047408e0c4768fa7c45f5 /mesecons_fpga/logic.lua
parentbfd952b51a61278c7108cd32e75677f93ebcf14d (diff)
downloadmesecons-e78bbd6f98cb4acd9865e47abfb4ab7103f800c5.tar
mesecons-e78bbd6f98cb4acd9865e47abfb4ab7103f800c5.tar.gz
mesecons-e78bbd6f98cb4acd9865e47abfb4ab7103f800c5.tar.bz2
mesecons-e78bbd6f98cb4acd9865e47abfb4ab7103f800c5.tar.xz
mesecons-e78bbd6f98cb4acd9865e47abfb4ab7103f800c5.zip
FPGA: Unify actions in single table
Diffstat (limited to 'mesecons_fpga/logic.lua')
-rw-r--r--mesecons_fpga/logic.lua75
1 files changed, 36 insertions, 39 deletions
diff --git a/mesecons_fpga/logic.lua b/mesecons_fpga/logic.lua
index 3dca154..d9a37ef 100644
--- a/mesecons_fpga/logic.lua
+++ b/mesecons_fpga/logic.lua
@@ -1,5 +1,21 @@
+
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
lg.serialize = function(t)
local function _op(t)
@@ -11,20 +27,14 @@ lg.serialize = function(t)
return tostring(t.n)
end
end
- local function _action(s)
- if s == nil then
- return " "
+ -- Serialize actions (gates) from eg. "and" to "&"
+ local function _action(action)
+ for i, data in ipairs(operands) do
+ if data.gate == action then
+ return data.short
+ end
end
- local mapping = {
- ["and"] = "&",
- ["or"] = "|",
- ["not"] = "~",
- ["xor"] = "^",
- ["nand"] = "?", --dunno
- ["buf"] = "_",
- ["xnor"] = "=",
- }
- return mapping[s]
+ return " "
end
local s = ""
@@ -48,18 +58,14 @@ lg.deserialize = function(s)
return {type = "reg", n = tonumber(c)}
end
end
- local function _action(c)
- local mapping = {
- ["&"] = "and",
- ["|"] = "or",
- ["~"] = "not",
- ["^"] = "xor",
- ["?"] = "nand",
- ["_"] = "buf",
- ["="] = "xnor",
- [" "] = nil,
- }
- return mapping[c]
+ -- Deserialize actions (gates) from eg. "&" to "and"
+ local function _action(action)
+ for i, data in ipairs(operands) do
+ if data.short == action then
+ return data.gate
+ end
+ end
+ -- nil
end
local ret = {}
@@ -159,21 +165,12 @@ end
-- interpreter
lg.interpret = function(t, a, b, c, d)
local function _action(s, v1, v2)
- if s == "and" then
- return v1 and v2
- elseif s == "or" then
- return v1 or v2
- 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
+ for i, data in ipairs(operands) do
+ if data.gate == s then
+ return data.func(v1, v2)
+ end
end
+ return false -- unknown gate
end
local function _op(t, regs, io_in)
if t.type == "reg" then