Advertisement
jig487

boolTable

Feb 10th, 2022 (edited)
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.38 KB | None | 0 0
  1. local tArgs = {...}
  2.  
  3. local function val(a)
  4.     local result = 0
  5.     if a == true then
  6.         result = 1
  7.     elseif a == false then
  8.         result = 0
  9.     elseif a == "1" then
  10.         result = true
  11.     elseif a == "0" then
  12.         result = false
  13.     end
  14.     return result
  15. end
  16.  
  17. local function txt(x,y,t,c)
  18.     if c then term.setTextColour(c) end
  19.     term.setCursorPos(x,y)
  20.     if type(t) == "string" or type(t) =="number" then
  21.         write(t)
  22.     else
  23.         write(textutils.serialise(t))
  24.     end
  25.     term.setTextColour(colors.white)
  26. end
  27.  
  28. local function debug(x,y,str)
  29.     if not str then
  30.         str = ""
  31.     else
  32.         str = str..", "
  33.     end
  34.     txt(x,y,"Debug: "..str,colors.red)
  35.     read()
  36.     return var
  37. end
  38.  
  39. --[[
  40.     UTF8
  41. 65 - 90 Capital letters
  42. 97 - 122 Lowercase letters
  43. 1,  2,  3,  4,  5,  6,  7,  8,  9,  10,  11,  12,  13,  14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26
  44. a,  b,  c,  d,  e,  f,  g,  h,  i,  j,   k,   l,   m,   n,   o,   p,   q,   r,   s,   t,   u,   v,   w,   x,   y,   z
  45. ]]
  46.  
  47. local function makeBooleanTables(tArgs)
  48.     local list = {}
  49.     if #tArgs > 1 then
  50.         for i = 2, #tArgs do
  51.             list[i-1] = {val(tArgs[i])}
  52.         end
  53.         return list
  54.     end
  55.     local inputs = tonumber(tArgs[1])
  56.     local length = 2^inputs
  57.     for i = 1, inputs do
  58.         local counter = 2^(i-1)
  59.         local listIndex = inputs+1-i
  60.         list[listIndex] = {}
  61.         while #list[listIndex] < length do
  62.             for j = 1, counter do
  63.                 list[listIndex][#list[listIndex]+1] = false
  64.             end
  65.             for j = 1, counter do
  66.                 list[listIndex][#list[listIndex]+1] = true
  67.             end
  68.         end
  69.     end
  70.     return list
  71. end
  72.  
  73. local gates = {
  74.     gand = function(...)
  75.         for i = 1, #arg do
  76.             if arg[i] == false then
  77.                 return false
  78.             end
  79.         end
  80.         return true
  81.     end,
  82.  
  83.     gor = function(...)
  84.         for i = 1, #arg do
  85.             if arg[i] == true then
  86.                 return true
  87.             end
  88.         end
  89.         return false
  90.     end,
  91.  
  92.     gxor = function(...)
  93.         for i = 1, #arg do
  94.             if arg[i] == true then
  95.                 for j = 1, #arg do
  96.                     if arg[j] == true and j ~= i then
  97.                         return false
  98.                     elseif j == #arg then
  99.                         return true
  100.                     end
  101.                 end
  102.             end
  103.         end
  104.         return false
  105.     end,
  106.  
  107.     gxand = function(...)
  108.         for i = 1, #arg do
  109.             if arg[1] ~= arg[i] then
  110.                 return false
  111.             end
  112.         end
  113.         return true
  114.     end,
  115.  
  116.     ginv = function(a)
  117.         if a == true then
  118.             return false
  119.         else
  120.             return true
  121.         end
  122.     end,
  123. }
  124.  
  125. local ic = {
  126.     test = function(a,b)
  127.         local result = gates.gor(
  128.             gates.gand(gates.ginv(b),a),
  129.             gates.gand(gates.ginv(a),b)
  130.         )
  131.         return result
  132.     end,
  133.     c74181_1 = function(a,b,s0,s1,s2,s3)
  134.         local ia = gates.ginv(a)
  135.         local ib = gates.ginv(b)
  136.         local r1 = gates.ginv(gates.gor(
  137.             gates.gand(s3,ia,b),
  138.             gates.gand(s2,ia,b)
  139.         ))
  140.         local r2 = gates.ginv(gates.gor(
  141.             gates.gand(s1,b),
  142.             gates.gand(s0,ib),
  143.             a
  144.         ))
  145.         return r1, r2
  146.     end,
  147.     c74181_2 = function()
  148.     end,
  149.     fadd = function(a,b,cin)
  150.         local xorab = gates.gxor(a,b)
  151.         local sum = gates.gxor(xorab,cin)
  152.         local cout = gates.gor(
  153.             gates.gand(cin,xorab),
  154.             gates.gand(a,b)
  155.         )
  156.         return sum,cout
  157.     end,
  158.     decoder = function(a,b,e)
  159.         local ia = gates.ginv(a)
  160.         local ib = gates.ginv(b)
  161.         local q1,q2,q3,q4 = gates.gand(ia,ib,e),gates.gand(ia,b,e),gates.gand(a,ib,e),gates.gand(a,b,e)
  162.         return q1,q2,q3,q4
  163.     end,
  164.     encoder = function(a,b,c,d)
  165.         local ia,ib,ic,id = gates.ginv(a),gates.ginv(b),gates.ginv(c),gates.ginv(d)
  166.         local x1 = gates.ginv(gates.gor(a,b))
  167.         local x2 = gates.ginv(gates.gor(ia,b))
  168.         local x3 = gates.ginv(gates.gor(a,ib))
  169.         local x4 = gates.ginv(gates.gor(ia,ib))
  170.         local x5 = gates.ginv(gates.gor(c,d))
  171.         local x6 = gates.ginv(gates.gor(ic,d))
  172.         local x7 = gates.ginv(gates.gor(c,id))
  173.  
  174.         local r0 = gates.ginv(gates.gand(x1,x5))
  175.         local r1 = gates.ginv(gates.gand(x2,x5))
  176.         local r2 = gates.ginv(gates.gand(x3,x5))
  177.         local r3 = gates.ginv(gates.gand(x4,x5))
  178.         local r4 = gates.ginv(gates.gand(x5,x6))
  179.         local r5 = gates.ginv(gates.gand(x2,x6))
  180.         local r6 = gates.ginv(gates.gand(x3,x6))
  181.         local r7 = gates.ginv(gates.gand(x4,x6))
  182.         local r8 = gates.ginv(gates.gand(x1,x7))
  183.         local r9 = gates.ginv(gates.gand(x2,x7))
  184.         return r0,r1,r2,r3,r4,r5,r6,r7,r8,r9
  185.     end,
  186. }
  187.  
  188. local ic2 = {
  189.     fadd4 = function(cin,a1,a2,a3,a4,b1,b2,b3,b4)
  190.         --If subtracting, cin is 1. if adding, cin is 0. Cout is disregarded, the result is (16-|result|)
  191.         --If adding, Cout == 16. So result is (result + Cout)
  192.         --A's are one number, B's are the other number.
  193.         local x1,x2,x3,x4 = gates.gxor(b1,cin),gates.gxor(b2,cin),gates.gxor(b3,cin),gates.gxor(b4,cin)
  194.         local sum1,cout1 = ic.fadd(a1, x1, cin)
  195.         local sum2,cout2 = ic.fadd(a2, x2, cout1)
  196.         local sum3,cout3 = ic.fadd(a3, x3, cout2)
  197.         local sum4,cout4 = ic.fadd(a4, x4, cout3)
  198.         return sum1,sum2,sum3,sum4,cout4
  199.     end,
  200.     c74181 = function(c,m,a0,a1,a2,a3,b0,b1,b2,b3,s0,s1,s2,s3)
  201.         local r1,r2 = ic.c74181_1(a3,b3,s0,s1,s2,s3)
  202.         local r3,r4 = ic.c74181_1(a3,b3,s0,s1,s2,s3)
  203.         local r5,r6 = ic.c74181_1(a3,b3,s0,s1,s2,s3)
  204.         local r7,r8 = ic.c74181_1(a3,b3,s0,s1,s2,s3)
  205.     end
  206. }
  207.  
  208. local function processLogicResults(...)
  209.     return arg
  210. end
  211.  
  212. local function doLogic(b,g1,g2,g3) --b for boolean, g for logic gate list, c for circuits list, n for 2nd generation circuits
  213.     local result = processLogicResults(
  214.         --g3.fadd4(b.a, b.b, b.c, b.d, b.e, b.f, b.g, b.h, b.i)
  215.         g2.fadd(b.a,b.b,b.c)
  216.         --gates.ginv(g2.test(b.a,b.b))
  217.     )
  218.     return result
  219. end
  220.  
  221. local function processBooleanTables(blist,gates,ic)
  222.     local results = {}
  223.     local inputs = #blist
  224.     local length = #blist[1]
  225.     for i = 1, length do
  226.         local tl = {}
  227.         for j = 1, inputs do
  228.             tl[utf8.char(96+j)] = blist[j][i]
  229.         end
  230.         results[#results+1] = doLogic(tl,gates,ic,ic2)
  231.     end
  232.     return results
  233. end
  234.  
  235. local function drawKarnaugh(x,y,results,tArgs)
  236.     local inputs = tonumber(tArgs[1])
  237.     if inputs <= 1 or inputs > 4 or #tArgs > 1 then
  238.         return
  239.     end
  240.     local il = { --index list From left to right on output table
  241.         {
  242.             1,3,
  243.             2,4
  244.         },
  245.         {
  246.             1,3,7,5,
  247.             2,4,8,6
  248.         },
  249.         {
  250.             1,5,13,9,
  251.             2,6,14,10,
  252.             4,8,16,12,
  253.             3,7,15,11
  254.         },
  255.     }
  256.     local labels = {
  257.         two = {"0","1"},
  258.         four = {"00","01","11","10"}
  259.     }
  260.  
  261.     local tbl = {
  262.         top = "A",
  263.         bottom = "B",
  264.         col = 2,
  265.         row = 2
  266.     }
  267.  
  268.    
  269.     local xspace = 2
  270.     local yspace = 2
  271.     local indexTable = il[1]
  272.  
  273.     if inputs == 2 then
  274.         xspace = 2
  275.     elseif inputs == 3 then
  276.         indexTable = il[2]
  277.         xspace = 3
  278.         tbl.top = "AB"
  279.         tbl.bottom = "C"
  280.         tbl.col = 4
  281.     elseif inputs == 4 then
  282.         indexTable = il[3]
  283.         xspace = 3
  284.         tbl.top = "AB"
  285.         tbl.bottom = "CD"
  286.         tbl.col = 4
  287.         tbl.row = 4
  288.     end
  289.  
  290.     for k = 1, #results[1] do
  291.         local y = y + (k-1)*tbl.row*3 + (k-1)*2
  292.         txt(x+4,y-2,tbl.top,colors.green)
  293.         txt(x,y-2,k..":",colors.orange)
  294.         txt(x+2,y-1,"\\",colors.green)
  295.         txt(x+3,y,"\\",colors.green)
  296.         txt(x,y,tbl.bottom,colors.green)
  297.  
  298.         for i = 1, tbl.col do
  299.             local toplabel
  300.             if inputs == 2 then
  301.                 toplabel = labels.two[i]
  302.             elseif inputs == 3 or inputs == 4 then
  303.                 toplabel = labels.four[i]
  304.             end
  305.             txt(x+2+i*xspace,y,toplabel,colors.lime)
  306.         end
  307.  
  308.         for i = 1, tbl.row do
  309.             local sidelabel
  310.             if inputs == 2 or inputs == 3 then
  311.                 sidelabel = labels.two[i]
  312.             elseif inputs == 4 then
  313.                 sidelabel = labels.four[i]
  314.             end
  315.             txt(x+2,y+i*yspace,sidelabel,colors.lime)
  316.         end
  317.  
  318.         for i = 1, tbl.row do
  319.             local offset = (i-1)*tbl.col
  320.             for h = 1, tbl.col do
  321.                 local index = offset+h
  322.                 txt( x+2+h*xspace, y+i*yspace, val(results[indexTable[index]][k])  )
  323.             end
  324.         end
  325.     end
  326. end
  327.  
  328. local function binaryToTen(list,s,length)
  329.     if not s then s = 1 end
  330.     if not length then length = 4 end
  331.     local result = 0
  332.  
  333.     if list[s] == true or list[s] == 1 or list[s] == "1" then
  334.         result = result + 1
  335.     end
  336.     if list[s+1] == true or list[s+1] == 1 or list[s+1] == "1" then
  337.         result = result + 2
  338.     end
  339.     if list[s+2] == true or list[s+2] == 1 or list[s+2] == "1" then
  340.         result = result + 4
  341.     end
  342.     if list[s+3] == true or list[s+3] == 1 or list[s+3] == "1" then
  343.         result = result + 8
  344.     end
  345.     if length == 5 then
  346.         if list[s+4] then
  347.             if list[s+4] == 1 or list[s+4] == "1" or list[s+4] == true then
  348.                 result = result + 16
  349.             end
  350.         end
  351.     end
  352.     return result
  353. end
  354.  
  355. local function drawTruthTable(x,y,results,tArgs,btable)
  356.     local inputs = tonumber(tArgs[1])
  357.     local length = #results
  358.  
  359.     --Draw input labels
  360.     for i = 1, inputs do
  361.         txt(x-2+i*2,y,utf8.char(96+i))
  362.     end
  363.     --Draw inputs
  364.     if #tArgs == 1 then
  365.         for i = 1, inputs do
  366.             for j = 1, length do
  367.                 txt(x-2+i*2,y+j+1,val(btable[i][j]))
  368.             end
  369.         end
  370.     end
  371.     --Not sure tbh
  372.     for i = 1, length do
  373.         txt(inputs*2+x-1,y+i+1,":",colors.green)
  374.     end
  375.     --Draw results and result labels
  376.     for i = 1, #results[1] do
  377.         local x = x+(i-1)*2
  378.         txt(inputs*2 + (i)*2,y,i,colors.orange)
  379.         for k = 1, length do
  380.             txt(x+inputs*2+1,y+k+1,val(results[k][i]),colors.green)
  381.         end
  382.     end
  383. end
  384.  
  385. local function updateCursor(list,cursor)
  386.     local _, key = os.pullEvent("key")
  387.     local choice = false
  388.     if key == keys.left then
  389.         if cursor > 1 then
  390.             cursor = cursor - 1
  391.         end
  392.     elseif key == keys.right then
  393.         if cursor < #list then
  394.             cursor = cursor + 1
  395.         end
  396.     elseif key == keys.up or key == keys.down then
  397.         choice = tonumber(cursor)
  398.     end
  399.     return cursor,choice
  400. end
  401.  
  402. local function drawOptionsList(x,y,cursor,list)
  403.     for i = 1, #list do
  404.         local j = i-1
  405.         local cursCol = colors.white
  406.         local ctop = " "
  407.         local cbot = " "
  408.         if i == cursor then
  409.             ctop,cbot = "v","^"
  410.             cursCol = colors.lime
  411.         end
  412.         txt(x+(j*2),y-1,ctop,cursCol)
  413.         txt(x+(j*2),y+1,cbot,cursCol)
  414.         txt(x+(j*2),y,val(list[i][1]),colors.white)
  415.     end
  416. end
  417.  
  418. local function makeNode(x,y,logic,count)
  419.     return {
  420.         id = count + 1,
  421.         x,
  422.         y,
  423.         inputs = {},
  424.         outputs = {},
  425.         logic = logic,
  426.     }, count + 1
  427. end
  428.  
  429. local function getMouseEvent()
  430.     local eventData = {}
  431.     while eventData[1] ~= "mouse_click" and eventData[1] ~= "mouse_drag" and eventData[1] ~= "mouse_up" do
  432.         eventData = {os.pullEvent()}
  433.     end
  434.     return eventData
  435. end
  436.  
  437. local function makeButton(x,y,name,logic,logicList)
  438.     return {
  439.         x = x,
  440.         y = y,
  441.         name = name,
  442.         logic = logicList[logic]
  443.     }
  444. end
  445.  
  446. local function drawNode(x,y,b,col,col2)
  447.     txt(x,y,"[",col2)
  448.     txt(x+#b.name+1,y,"]",col2)
  449.     txt(x+1,y,b.name,col)
  450. end
  451.  
  452. local function mouseDebug(x,y,active,ml,cursx,cursy)
  453.     for i = 1, #ml do
  454.         local drawColor = colors.white
  455.         if active == i then
  456.             drawColor = colors.green
  457.         end
  458.         txt(x,y+i-1,ml[i],drawColor)
  459.     end
  460.     local drawString = "X: "..cursx..", Y: "..cursy
  461.     local clearString = ""
  462.     for i = 1, #drawString+5 do
  463.        clearString = clearString.." "
  464.     end
  465.     txt(x,y+#ml,clearString)
  466.     txt(x,y+#ml,drawString)
  467. end
  468.  
  469. term.clear()
  470. local btable = makeBooleanTables(tArgs)
  471. local results = processBooleanTables(btable, gates, ic)
  472.  
  473. drawKarnaugh(17,3,results,tArgs)
  474. drawTruthTable(1,1,results,tArgs,btable)
  475.  
  476. local adderCircuit = false --Set to true to show the binary --> decimal conversions
  477.  
  478. if #tArgs > 1 then
  479.     local cursor = 1
  480.     while true do
  481.         local choice = false
  482.         while choice == false do
  483.             drawOptionsList(1,3,cursor,btable)
  484.             cursor,choice = updateCursor(btable,cursor)
  485.         end
  486.         if btable[cursor][1] == true then
  487.             btable[cursor][1] = false
  488.         else
  489.             btable[cursor][1] = true
  490.         end
  491.         local results = processBooleanTables(btable, gates, ic)
  492.         drawTruthTable(1,1,results,tArgs,btable)
  493.  
  494.         if adderCircuit == true then
  495.             local boolList = {}
  496.             for i = 1, #btable do
  497.                 boolList[i] = btable[i][1]
  498.             end
  499.  
  500.             txt(1,20,textutils.serialise(results))
  501.             txt(27,5, binaryToTen(results[1],1,5).." ", colors.orange)
  502.  
  503.             local result4 = binaryToTen(results[1],1,4)
  504.             txt(23,6, result4.." ", colors.yellow)
  505.             txt(23,7, "16-"..result4.." = "..(16-result4).."    ", colors.yellow)
  506.  
  507.             txt(4,5,  binaryToTen(boolList,2,4).." ", colors.green)
  508.             txt(16,5, binaryToTen(boolList,6,4).." ", colors.green)
  509.         end
  510.     end
  511. end
  512. --[[
  513. local nodeList = {}
  514. local w,h = term.getSize()
  515.  
  516. local buttonY = 12
  517. local buttonList = {
  518.     makeButton(1,buttonY,"AND","gand",gates),
  519.     makeButton(1,buttonY+2,"OR","gor",gates),
  520.     makeButton(1,buttonY+4,"XOR","gxor",gates),
  521.     makeButton(1,buttonY+6,"INV","ginv",gates),
  522.     makeButton(1,buttonY+8,"FADD","fadd",ic),
  523. }
  524.  
  525. local buttonPosList = {}
  526. for i = 1, #buttonList do
  527.     local b = buttonList[i]
  528.     drawNode(b.x,b.y,b,colors.white,colors.orange)
  529.     buttonPosList[i] = {
  530.         x1 = b.x,
  531.         x2 = b.x+#b.name+1,
  532.         y = b.y,
  533.     }
  534. end
  535.  
  536. local mouseDebugList = {
  537.     "Waiting",
  538.     "Click",
  539.     "Pressed",
  540.     "Release"
  541. }
  542.  
  543. local mdx = 1
  544. local mdy = 31
  545. local active = 1
  546. while true do
  547.     for i = 1, #buttonList do
  548.         local b = buttonList[i]
  549.         drawNode(b.x,b.y,b,colors.white,colors.orange)
  550.     end
  551.  
  552.     active = 1
  553.     mouseDebug(mdx,mdy,active,mouseDebugList,"..","..")
  554.     local eventData = getMouseEvent()
  555.     local event,type,x,y = eventData[1],eventData[2],eventData[3],eventData[4]
  556.     local bChoice
  557.  
  558.     if type == 1 then
  559.         if event == "mouse_click" then
  560.  
  561.             active = 2
  562.             mouseDebug(mdx,mdy,active,mouseDebugList,x,y)
  563.  
  564.             for i = 1, #buttonPosList do  
  565.                 local b = buttonPosList[i]
  566.                 txt(1,27,"Checking button: "..i.."       ")
  567.                 txt(1,28,"Cursor pos @ click: "..x..", "..y.."         ")
  568.                 txt(1,29,"Button coords: "..b.x1..", "..b.x2..", "..b.y.."         ")
  569.                 if x >= b.x1 and x <= b.x2 and y == b.y then
  570.                     bChoice = i
  571.                     txt(1,27,"Picked button: "..i.."          ",colors.orange)
  572.  
  573.                     local b = buttonList[i]
  574.                     drawNode(b.x,b.y,b,colors.green,colors.orange)
  575.                     break
  576.                 end
  577.             end
  578.  
  579.             for i = 1, #buttonList do
  580.                
  581.             end
  582.  
  583.             local eventData = {}
  584.             while eventData[1] ~= "mouse_up" do
  585.                 eventData = getMouseEvent()
  586.                 local event,type,x,y = eventData[1],eventData[2],eventData[3],eventData[4]
  587.                 if event == "mouse_drag" then
  588.  
  589.                     active = 3
  590.                     mouseDebug(mdx,mdy,active,mouseDebugList,x,y)
  591.                 end
  592.             end
  593.  
  594.             active = 4
  595.             mouseDebug(mdx,mdy,active,mouseDebugList,x,y)
  596.             sleep(0.1)
  597.         end
  598.     end
  599. end
  600. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement