FPGA: Add 'unary' value to talbe. Document

This commit is contained in:
SmallJoker 2019-11-03 15:55:46 +01:00 committed by SmallJoker
parent 7784b13da5
commit 9b58f8db29
2 changed files with 30 additions and 16 deletions

@ -192,7 +192,7 @@ plg.to_formspec_string = function(is, err)
local function dropdown_action(x, y, name, val) local function dropdown_action(x, y, name, val)
local selected = 0 local selected = 0
local titles = { " " } local titles = { " " }
for i, data in ipairs(lcore.get_operands()) do for i, data in ipairs(lcore.get_operations()) do
titles[i + 1] = data.fs_name titles[i + 1] = data.fs_name
if val == data.gate then if val == data.gate then
selected = i + 1 selected = i + 1
@ -249,7 +249,7 @@ plg.from_formspec_fields = function(fields)
end end
end end
local function read_action(s) local function read_action(s)
for i, data in ipairs(lcore.get_operands()) do for i, data in ipairs(lcore.get_operations()) do
if data.fs_name == s then if data.fs_name == s then
return data.gate return data.gate
end end

@ -1,20 +1,25 @@
local lg = {} local lg = {}
local operands = { local operations = {
-- index: Index in formspec -- table index: Index in the formspec dropdown
-- gate: Internal name
-- short: Serialized form, single character
-- fs_name: Display name, padded to 4 characters
-- func: Function that applies the operation
-- unary: Whether this gate only has one input
{ gate = "and", short = "&", fs_name = " AND", func = function(a, b) return a and b end }, { 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 = "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 = "not", short = "~", fs_name = " NOT", func = function(a, b) return not b end, unary = true },
{ gate = "xor", short = "^", fs_name = " XOR", func = function(a, b) return a ~= 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 = "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 = "buf", short = "_", fs_name = " =", func = function(a, b) return b end, unary = true },
{ gate = "xnor", short = "=", fs_name = "XNOR", func = function(a, b) return a == b end }, { gate = "xnor", short = "=", fs_name = "XNOR", func = function(a, b) return a == b end },
{ gate = "nor", short = "!", fs_name = " NOR", func = function(a, b) return not (a or b) end }, { gate = "nor", short = "!", fs_name = " NOR", func = function(a, b) return not (a or b) end },
} }
lg.get_operands = function() lg.get_operations = function()
return operands return operations
end end
-- (de)serialize -- (de)serialize
@ -30,7 +35,7 @@ lg.serialize = function(t)
end end
-- Serialize actions (gates) from eg. "and" to "&" -- Serialize actions (gates) from eg. "and" to "&"
local function _action(action) local function _action(action)
for i, data in ipairs(operands) do for i, data in ipairs(operations) do
if data.gate == action then if data.gate == action then
return data.short return data.short
end end
@ -61,7 +66,7 @@ lg.deserialize = function(s)
end end
-- Deserialize actions (gates) from eg. "&" to "and" -- Deserialize actions (gates) from eg. "&" to "and"
local function _action(action) local function _action(action)
for i, data in ipairs(operands) do for i, data in ipairs(operations) do
if data.short == action then if data.short == action then
return data.gate return data.gate
end end
@ -116,16 +121,25 @@ lg.validate_single = function(t, i)
return false return false
end end
local elem = t[i] local elem = t[i]
local gate_data
for j, data in ipairs(operations) do
if data.gate == elem.action then
gate_data = data
break
end
end
-- check for completeness -- check for completeness
if elem.action == nil then if not gate_data then
return {i = i, msg = "Gate type required"} return {i = i, msg = "Gate type is required"}
elseif elem.action == "not" or elem.action == "buf" then elseif gate_data.unary then
if elem.op1 ~= nil or elem.op2 == nil or elem.dst == nil then if elem.op1 ~= nil or elem.op2 == nil or elem.dst == nil then
return {i = i, msg = "Second operand (only) and destination required"} return {i = i, msg = "Second operand (only) and destination are required"}
end end
else else
if elem.op1 == nil or elem.op2 == nil or elem.dst == nil then if elem.op1 == nil or elem.op2 == nil or elem.dst == nil then
return {i = i, msg = "Operands and destination required"} return {i = i, msg = "Operands and destination are required"}
end end
end end
-- check whether operands/destination are identical -- check whether operands/destination are identical
@ -166,7 +180,7 @@ 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)
for i, data in ipairs(operands) do for i, data in ipairs(operations) do
if data.gate == s then if data.gate == s then
return data.func(v1, v2) return data.func(v1, v2)
end end