Advertisement
VikeStep

Logic Gates Testing

May 10th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 17.43 KB | None | 0 0
  1. --Logic Gates by Croswat & VikeStep
  2.  
  3. local gatevars = nil --variable for gates
  4. local inputs = nil
  5. local outputs = nil
  6.  
  7. function change(event)
  8.     print("Press x to change gates")
  9.     parallel.waitForAll(waitforkey,event)
  10. end
  11.  
  12. function run()
  13.     term.clear()
  14.     term.setCursorPos(1,1)
  15.     print("running "..gatevars[1])
  16.     if gatevars[1] == "timer" then
  17.         change(timer)
  18.     elseif gatevars[1] == "and" then
  19.         change(andgate)
  20.     elseif gatevars[1] == "or" then
  21.         change(orgate)
  22.     elseif gatevars[1] == "nor" then
  23.         change(norgate)
  24.     elseif gatevars[1] == "nand" then
  25.         change(nandgate)
  26.     elseif gatevars[1] == "repeater" then
  27.         change(repeater)
  28.     elseif gatevars[1] == "xor" then
  29.         change(xorgate)
  30.     elseif gatevars[1] == "xnor" then
  31.         change(xnorgate)
  32.     end
  33. end
  34.  
  35. function waitforkey()
  36.     while true do
  37.         local sEvent, param = os.pullEvent("key")
  38.         if sEvent == "key" then
  39.             if param == 45 then
  40.                 print("Are you sure? (y/n)")
  41.                 local sEvent, param2 = os.pullEvent("key")
  42.                 if sEvent == "key" then
  43.                     if param2 == 21 then
  44.                         fs.delete("lgvariables")
  45.                         os.reboot()
  46.                     else
  47.                         os.reboot()
  48.                     end
  49.                end
  50.             end
  51.         end
  52.     end
  53. end
  54.  
  55. function loadVariables() -- get fom file
  56.      local file = fs.open("lgvariables","r")
  57.      gatevars = split(file.readLine(), "@")
  58.       file.close()
  59. end
  60.  
  61. function saveVariables(variables)
  62.      local file = fs.open("lgvariables","w")
  63.      file.writeLine(variables)
  64.      file.close()
  65. end
  66.  
  67.  
  68. function split(pString, pPattern)
  69. numchanged = 0
  70.    local Table = {}  -- NOTE: use {n = 0} in Lua-5.0
  71.    local fpat = "(.-)" .. pPattern
  72.    local last_end = 1
  73.    local s, e, cap = pString:find(fpat, 1)
  74.    while s do
  75.           if s ~= 1 or cap ~= "" then
  76.          table.insert(Table,cap)
  77. numchanged = numchanged +1
  78.           end
  79.           last_end = e+1
  80.           s, e, cap = pString:find(fpat, last_end)
  81.    end
  82.    if last_end <= #pString then
  83.           cap = pString:sub(last_end)
  84.           table.insert(Table, cap)
  85.           numchanged = numchanged +1
  86.    end
  87.    return Table
  88. end
  89.  
  90. function readgate()
  91.     local loopflag = true
  92.     local gatetype = nil
  93.     while loopflag do
  94.         gatetype = read()
  95.         gatetype = string.lower(gatetype)
  96.         if gatetype == "list" then
  97.             print("AND, OR, NOT, NOR, NAND, XOR, XNOR, Timer, Repeater")
  98.         elseif gatetype == "timer" or gatetype == "and" or gatetype == "or" or gatetype == "not" or gatetype == "nor" or gatetype == "inverter" or gatetype == "nand" or gatetype == "xor" or gatetype == "xnor" or gatetype == "buffer" or gatetype == "repeater" then -- check for gate types here
  99.             loopflag = false
  100.         else
  101.             print("gate not recognised, try again")
  102.         end
  103.     end
  104.     return gatetype
  105. end
  106.  
  107. function printCube() -- The info for whether to type in inputs or not will be displayed after this
  108.     term.clear()
  109.     term.setCursorPos(1,1)
  110.     print [[
  111.            Back        Please type in the
  112.              |             the side(s) in                          
  113.           +---+---+  the following format                          
  114.          /|  V   /|        
  115.         / |     / |  "front, left, right"                    
  116.        /  |Top /  |              
  117.       +-------+   |<-Right      
  118.       |   |   |   |                            
  119.       |   +---|---+                              
  120. Left->| Front |  /                              
  121.       | /     | /                                
  122.       |/  ^   |/                                
  123.       +-------+                                  
  124.           |                                                                              
  125.         Bottom  
  126. ]]
  127. end
  128.  
  129. --menu and setting up gates
  130. function setup()
  131.         term.clear()
  132.         term.setCursorPos(1,1)
  133.         print("To get started, write down what gate you wish to use")
  134.         print("type \"list\" to see what gates are available")
  135.         local gatetype = readgate()
  136.         --work out what gate, then get the other inputs, and save to file. then run()
  137.         if gatetype == "timer" then
  138.             setuptimer()
  139.         elseif gatetype == "and" then
  140.             setupand()
  141.         elseif gatetype == "or" then
  142.             setupor()
  143.         elseif gatetype == "nor" or gatetype == "not" or gatetype== "inverter" then
  144.             setupnor()
  145.         elseif gatetype == "nand" then
  146.             setupnand()
  147.         elseif gatetype == "xor" then
  148.             setupxor()
  149.         elseif gatetype == "xnor" then
  150.             setupxnor()
  151.         elseif gatetype == "buffer" or gatetype == "repeater" then
  152.             setuprepeater()
  153.         end
  154.        
  155. end
  156.  
  157. function ioToArray()
  158.     inputs = split(gatevars[2],", ")
  159.     outputs = split(gatevars[3],", ")
  160. end
  161.  
  162. function getNumberInputs()
  163.     local number = table.getn(inputs)
  164.     return number
  165. end
  166.  
  167. function getNumberOutputs()
  168.     local number = table.getn(outputs)
  169.     return number
  170. end
  171.  
  172. function round(x)
  173.     floor = tonumber(x)-math.floor(tonumber(x))
  174.     ceil = math.ceil(tonumber(x))-tonumber(x)
  175.     if floor==ceil then
  176.         return math.ceil(tonumber(x))
  177.     elseif floor<ceil then
  178.         return math.floor(tonumber(x))
  179.     elseif floor>ceil then
  180.         return math.ceil(tonumber(x))
  181.     end    
  182. end
  183.  
  184. function getside(printstring, minimum)
  185.     valid=false
  186.     tempsidesvalid=0
  187.     printCube()
  188.     print(printstring)
  189.     while not valid do
  190.         text = read()
  191.         sides = split(text,", ")
  192.         amountsides = table.getn(sides)
  193.         if amountsides >= minimum then
  194.             for i=1, amountsides do
  195.                 if sides[i] == "front" or sides[i] == "left" or sides[i] == "right" or sides[i] == "back" or sides[i] == "bottom" or sides[i] == "top" then
  196.                     tempsidesvalid = tempsidesvalid + 1
  197.                 else
  198.                     printCube()
  199.                     print(sides[i].." is not a valid side")
  200.                     print(printstring)
  201.                     break
  202.                 end
  203.             end
  204.         else
  205.             print("Please Enter more than "..minimum.." side")
  206.             tempsidesvalid=amountsides+1
  207.         end
  208.         if tempsidesvalid==amountsides then
  209.             valid=true
  210.         else
  211.             tempsidesvalid=0
  212.         end
  213.     end
  214.     return text
  215. end
  216.  
  217. --Timer
  218.  
  219. function setuptimer()
  220.     local timerinput = getside("Type in the inputs (disables the timer)",1)
  221.     local timeroutput = getside("Type in the outputs (side pulsed to)",1)
  222.     term.clear()
  223.     term.setCursorPos(1,1)  
  224.     print("enter how many seconds you would like the timer to cycle")
  225.     local loopflag = true
  226.     while loopflag do
  227.         timerseconds = read()
  228.         integer=tonumber(timerseconds)
  229.         if integer == nil or integer <= 0 then
  230.             term.clear()
  231.             term.setCursorPos(1,1)
  232.             print(timerseconds.." is not a valid positive number")
  233.             print("enter how many seconds you would like the timer to cycle (can enter decimal)")
  234.         else
  235.             loopflag = false
  236.         end
  237.     end
  238.     saveVariables("timer".."@"..tostring(timerinput).."@"..tostring(timeroutput).."@"..tostring(timerseconds))
  239.     loadVariables()
  240.     term.clear()
  241.     term.setCursorPos(1,1)
  242.     change(timer)
  243. end
  244.  
  245. function timer()
  246.     input = false
  247.     pulseLength = 1 -- In ticks
  248.     ioToArray()
  249.     numberInputs=tonumber(getNumberInputs())
  250.     numberOutputs=tonumber(getNumberOutputs())
  251.     clockSpeed = round(20*tonumber(gatevars[4])) -- In ticks, remember 20 ticks a second
  252.     while true do
  253.         for i=1, numberInputs do
  254.             if rs.getInput(inputs[i])==true then
  255.                 input=true
  256.             else
  257.             end
  258.         end                
  259.         while not input do
  260.             for i=1, numberOutputs do
  261.                 rs.setOutput(outputs[i], true)
  262.             end
  263.             sleep(pulseLength / 20)
  264.             for i=1, numberOutputs do
  265.                 rs.setOutput(outputs[i], false)
  266.             end
  267.             sleep((clockSpeed - pulseLength) / 20)
  268.             for i=1, numberInputs do
  269.                 if rs.getInput(inputs[i])==true then
  270.                     input=true
  271.                 else
  272.                 end
  273.             end  
  274.         end
  275.         event = os.pullEvent("redstone")
  276.         input=false
  277.     end
  278. end
  279.  
  280. --AND Gate
  281.  
  282. function setupand()
  283.     local andinput = getside("Type in the inputs (when all are inputs are on, output is on)",1)
  284.     local andoutput = getside("Type in the outputs (when all inputs are on, output is on)",1)
  285.     saveVariables("and".."@"..tostring(andinput).."@"..tostring(andoutput))
  286.     loadVariables()
  287.     term.clear()
  288.     term.setCursorPos(1,1)
  289.     change(andgate)
  290. end
  291.  
  292. function andgate()
  293.     input = false
  294.     ioToArray()
  295.     numberInputs=tonumber(getNumberInputs())
  296.     numberOutputs=tonumber(getNumberOutputs())
  297.     amount = 0
  298.        
  299.     while true do
  300.         amount=0
  301.         for i=1, numberInputs do
  302.             if rs.getInput(inputs[i])==true then
  303.                 amount = amount + 1
  304.             else
  305.             end
  306.         end
  307.         if amount == numberInputs then
  308.             input=true
  309.         else
  310.             amount = 0
  311.             input=false
  312.         end
  313.         if input == true then
  314.             for i=1,numberOutputs do
  315.                 rs.setOutput(outputs[i],true)
  316.             end
  317.         else
  318.             for i=1,numberOutputs do
  319.                 rs.setOutput(outputs[i],false)
  320.             end
  321.         end
  322.         event = os.pullEvent("redstone")
  323.     end
  324. end
  325.  
  326. -- OR Gate
  327.  
  328. function setupor()
  329.     local orinput = getside("Type in the inputs (when one of the inputs are on, output is on)",1)
  330.     local oroutput = getside("Type in the outputs (when one of the inputs are on, output is on)",1)
  331.     saveVariables("or".."@"..tostring(orinput).."@"..tostring(oroutput))
  332.     loadVariables()
  333.     term.clear()
  334.     term.setCursorPos(1,1)
  335.     change(orgate)
  336. end
  337.  
  338. function orgate()
  339.     input = false
  340.     ioToArray()
  341.     numberInputs=tonumber(getNumberInputs())
  342.     numberOutputs=tonumber(getNumberOutputs())
  343.        
  344.     while true do
  345.         input=false
  346.         for i=1, numberInputs do
  347.             if rs.getInput(inputs[i])==true then
  348.                 input=true
  349.             else
  350.             end
  351.         end
  352.         if input == true then
  353.             for i=1,numberOutputs do
  354.                 rs.setOutput(outputs[i],true)
  355.             end
  356.         else
  357.             for i=1,numberOutputs do
  358.                 rs.setOutput(outputs[i],false)
  359.             end
  360.         end
  361.         event = os.pullEvent("redstone")
  362.     end
  363. end
  364.  
  365. -- Repeater
  366.  
  367. function setuprepeater()
  368.     local delay
  369.     local repeaterinput = getside("Type in the inputs (when one of the inputs are on, output is on)",1)
  370.     local repeateroutput = getside("Type in the outputs (when one of the inputs are on, output is on)",1)
  371.     term.clear()
  372.     term.setCursorPos(1,1)
  373.     print("Enter the delay (seconds) for the signal, type 0 for no delay (can be decimal)")
  374.     local loopflag = true
  375.     while loopflag do
  376.         delay = read()
  377.         integer=tonumber(delay)
  378.         if integer == nil or integer < 0 then
  379.             term.clear()
  380.             term.setCursorPos(1,1)
  381.             print(delay.." is not a valid positive number")
  382.             print("Enter the delay for the signal, type 0 for no delay")
  383.         else
  384.             loopflag = false
  385.         end
  386.     end
  387.     saveVariables("repeater".."@"..tostring(repeaterinput).."@"..tostring(repeateroutput).."@"..tostring(delay))
  388.     loadVariables()
  389.     term.clear()
  390.     term.setCursorPos(1,1)
  391.     change(orgate)
  392. end
  393.  
  394. function repeater()
  395.     local delay = round(20*tonumber(gatevars[4]))
  396.     input = false
  397.     ioToArray()
  398.     numberInputs=tonumber(getNumberInputs())
  399.     numberOutputs=tonumber(getNumberOutputs())
  400.        
  401.     while true do
  402.         input=false
  403.         for i=1, numberInputs do
  404.             if rs.getInput(inputs[i])==true then
  405.                 input=true
  406.             else
  407.             end
  408.         end
  409.         if input == true then
  410.             sleep(delay)
  411.             for i=1,numberOutputs do
  412.                 rs.setOutput(outputs[i],true)
  413.             end
  414.         else
  415.             for i=1,numberOutputs do
  416.                 rs.setOutput(outputs[i],false)
  417.             end
  418.         end
  419.         event = os.pullEvent("redstone")
  420.     end
  421. end
  422.  
  423. -- NOT/NOR Gate
  424.  
  425. function setupnor()
  426.     local norinput = getside("Type in the inputs (if 1 input is on, output is off)",1)
  427.     local noroutput = getside("Type in the outputs (if 1 input is on, output is off)",1)
  428.     saveVariables("nor".."@"..tostring(norinput).."@"..tostring(noroutput))
  429.     loadVariables()
  430.     term.clear()
  431.     term.setCursorPos(1,1)
  432.     change(norgate)
  433. end
  434.  
  435. function norgate()
  436.     input = false
  437.     ioToArray()
  438.     numberInputs=tonumber(getNumberInputs())
  439.     numberOutputs=tonumber(getNumberOutputs())
  440.        
  441.     while true do
  442.         input=false
  443.         for i=1, numberInputs do
  444.             if rs.getInput(inputs[i])==true then
  445.                 input=true
  446.             else
  447.             end
  448.         end
  449.         if input == false then
  450.             for i=1,numberOutputs do
  451.                 rs.setOutput(outputs[i],true)
  452.             end
  453.         else
  454.             for i=1,numberOutputs do
  455.                 rs.setOutput(outputs[i],false)
  456.             end
  457.         end
  458.         event = os.pullEvent("redstone")
  459.     end
  460. end
  461.  
  462. --NAND Gate
  463.  
  464. function setupnand()
  465.     local nandinput = getside("Type in the inputs (when at least 1 input is off, output is on)",1)
  466.     local nandoutput = getside("Type in the outputs (when at least 1 input is off, output is on)",1)
  467.     saveVariables("nand".."@"..tostring(nandinput).."@"..tostring(nandoutput))
  468.     loadVariables()
  469.     term.clear()
  470.     term.setCursorPos(1,1)
  471.     change(nandgate)
  472. end
  473.  
  474. function nandgate()
  475.     input = false
  476.     ioToArray()
  477.     numberInputs=tonumber(getNumberInputs())
  478.     numberOutputs=tonumber(getNumberOutputs())
  479.     amount = 0
  480.        
  481.     while true do
  482.         amount=0
  483.         for i=1, numberInputs do
  484.             if rs.getInput(inputs[i])==true then
  485.                 amount = amount + 1
  486.             else
  487.             end
  488.         end
  489.         if amount == numberInputs then
  490.             input=true
  491.         else
  492.             amount = 0
  493.             input=false
  494.         end
  495.         if input == false then
  496.             for i=1,numberOutputs do
  497.                 rs.setOutput(outputs[i],true)
  498.             end
  499.         else
  500.             for i=1,numberOutputs do
  501.                 rs.setOutput(outputs[i],false)
  502.             end
  503.         end
  504.         event = os.pullEvent("redstone")
  505.     end
  506. end
  507.  
  508. -- XOR Gate
  509.  
  510. function setupxor()
  511.     local xorinput = getside("Type in the inputs (when an odd number of inputs are on, output is on)",1)
  512.     local xoroutput = getside("Type in the outputs (when an odd number of inputs are on, output is on)",1)
  513.     saveVariables("xor".."@"..tostring(xorinput).."@"..tostring(xoroutput))
  514.     loadVariables()
  515.     term.clear()
  516.     term.setCursorPos(1,1)
  517.     change(xorgate)
  518. end
  519.  
  520. function xorgate()
  521.     input = false
  522.     ioToArray()
  523.     numberInputs=tonumber(getNumberInputs())
  524.     numberOutputs=tonumber(getNumberOutputs())
  525.     amount = 0
  526.        
  527.     while true do
  528.         amount=0
  529.         for i=1, numberInputs do
  530.             if rs.getInput(inputs[i])==true then
  531.                 amount = amount + 1
  532.             else
  533.             end
  534.         end
  535.         if amount % 2 == 1 then
  536.             input=true
  537.         else
  538.             amount = 0
  539.             input=false
  540.         end
  541.         if input == true then
  542.             for i=1,numberOutputs do
  543.                 rs.setOutput(outputs[i],true)
  544.             end
  545.         else
  546.             for i=1,numberOutputs do
  547.                 rs.setOutput(outputs[i],false)
  548.             end
  549.         end
  550.         event = os.pullEvent("redstone")
  551.     end
  552. end
  553.  
  554. -- XNOR Gate
  555.  
  556. function setupxnor()
  557.     local xnorinput = getside("Type in the inputs (when an even number of inputs are on, output is on)",1)
  558.     local xnoroutput = getside("Type in the outputs (when an even number of inputs are on, output is on)",1)
  559.     saveVariables("xnor".."@"..tostring(xnorinput).."@"..tostring(xnoroutput))
  560.     loadVariables()
  561.     term.clear()
  562.     term.setCursorPos(1,1)
  563.     change(xnorgate)
  564. end
  565.  
  566. function xnorgate()
  567.     input = false
  568.     ioToArray()
  569.     numberInputs=tonumber(getNumberInputs())
  570.     numberOutputs=tonumber(getNumberOutputs())
  571.     amount = 0
  572.        
  573.     while true do
  574.         amount=0
  575.         for i=1, numberInputs do
  576.             if rs.getInput(inputs[i])==true then
  577.                 amount = amount + 1
  578.             end
  579.         end
  580.         if amount % 2 == 1 then
  581.             input=true
  582.         else
  583.             amount = 0
  584.             input=false
  585.         end
  586.         if input == false then
  587.             for i=1,numberOutputs do
  588.                 rs.setOutput(outputs[i],true)
  589.             end
  590.         else
  591.             for i=1,numberOutputs do
  592.                 rs.setOutput(outputs[i],false)
  593.             end
  594.         end
  595.         event = os.pullEvent("redstone")
  596.     end
  597. end
  598.  
  599. -- functions go above here
  600. term.clear()
  601. term.setCursorPos(1,1)
  602. --start
  603. if fs.exists("lgvariables") then -- variables saved
  604.     loadVariables()
  605.     run()
  606. else
  607.     setup()
  608. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement