Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----------------------------------------------
- --LOGICAL GATE/FLIP-FLOP SIMULATOR BY CRAGRIM--
- -----------------------------------------------
- --INSTRUCTIONS--
- --http://www.promethea-ftb.com/index.php?threads/cragrims-computercraft-thread.639/#post-2881
- ----------------
- --DECLARATIONS--
- ----------------
- --version
- local strVersion = "0.2"
- --get command line arguments in a table
- local tblArgs = { ... }
- --count number of elements in the argument table
- local intArgsCount = 0 --declare it empty
- if next(tblArgs) == nil then
- --check if table is empty (do nothing)
- else
- --for each element add 1
- for _ in pairs(tblArgs) do
- intArgsCount = intArgsCount + 1
- end
- end
- --has an error occured?
- local bolErr = false
- --get the name of the program if named other than "gatesim"
- local GATESIM = shell.getRunningProgram()
- --default latch state off
- local latch_memory = "0"
- --declare special characters to avoid misshaps
- strLP = [[(]] --left parentheses
- strRP = [[)]] --right parentheses
- strQ = [["]] --doublequote
- -------------
- --FUNCTIONS--
- -------------
- -------------------
- --STRING HANDLING--
- -------------------
- --Put quotes around this
- function strQuote(this)
- return strQ..this..strQ
- end
- --Put parentheses around that
- function strParen(that)
- return strLP..that..strRP
- end
- ----------------------
- --COSMETIC FUNCTIONS--
- ----------------------
- function ClearScreen()
- term.clear()
- term.setCursorPos(1,1)
- end
- function Pause()
- write("Press any key to continue...")
- read()
- end
- ---------------------------------
- --STARTUP INFORMATIVE FUNCTIONS--
- ---------------------------------
- --Print information about current gate
- function StartInfo(gateType,gateDescription)
- ClearScreen()
- print("LOGICAL GATE/FLIP-FLOP SIMULATOR BY CRAGRIM")
- print("Program Version: "..strVersion)
- print("Computer Label: "..ComputerLabel())
- print("")
- print("CURRENTLY SIMULATING: "..gateType)
- print(gateDescription)
- print("")
- print("InputA: "..strInputA)
- print("InputB: "..strInputB)
- print("OutputA: "..strOutputA)
- print("OutputB: "..strOutputB)
- print("Variable: "..strVariable1)
- print("")
- SaveGateSetting(gateType)
- if bolErr == false then
- print("")
- print("Program is now running! Hold CTRL+T to abort...")
- end
- end
- --Print information if no gate was entered
- function PrintDescription()
- ClearScreen()
- print("LOGICAL GATE/FLIP-FLOP SIMULATOR BY CRAGRIM")
- print("Program Version: "..strVersion)
- print("Computer Label: "..ComputerLabel())
- print("")
- print("AVAILABLE GATE FUNCTIONS:")
- print("not, and, nand, or, nor, xor, xnor, rslatch, tflipflop, timer")
- print("")
- print("SYNTAX:")
- print(GATESIM.." [Gate] [InpA] [InpB] [OutA] [OutB] [Var]")
- print(strParen("Saves the setting after reboot"))
- print("")
- print("EXAMPLES:")
- print(GATESIM.." AND")
- print(GATESIM.." XOR left front back")
- print(GATESIM.." RSLATCH left right back front")
- print(GATESIM.." TIMER left right back front 2")
- end
- -------------
- --FUNCTIONS--
- -------------
- function SaveGateSetting(fnName)
- --Declare startup string to automatically launch gate program with side arguments on computer reboot
- local strStartup = "shell.run"..strParen(strQuote(GATESIM)..","..strQuote(fnName)..","..strQuote(strInputA)..","..strQuote(strInputB)..","..strQuote(strOutputA)..","..strQuote(strOutputB)..","..strQuote(strVariable1))
- --Write to startup program
- local fileWrite = fs.open ("startup", "w")
- fileWrite.write(strStartup)
- fileWrite.close()
- write("Saving settings to startup...")
- --Read the startup program to make sure it was actually saved, this is sometimes skipped on servers
- local fileRead=fs.open("startup", "r")
- if fileRead.readAll() == strStartup then
- print(" success!")
- else
- print(" writing error!")
- bolErr = true
- end
- fileRead.close()
- end
- --Save latch memory
- function SaveState(num)
- if num == "1" then
- local fileWrite = fs.open ("latch_memory", "w")
- fileWrite.write("1")
- fileWrite.close()
- else
- local fileWrite = fs.open ("latch_memory", "w")
- fileWrite.write("0")
- fileWrite.close()
- end
- end
- --Load latch memory
- function LoadState()
- if fs.exists("latch_memory") then
- local fileRead=fs.open("latch_memory", "r")
- temp_mem = fileRead.readAll()
- if temp_mem == "1" then
- return "1"
- else
- return "0"
- end
- fileRead.close()
- else
- SaveState("0")
- end
- end
- ----------------------------
- --ERROR CHECKING FUNCTIONS--
- ----------------------------
- --Wait for this to happen before updating in order to avoid lag
- --Add more pullevents to reduce lag even more (I can't think of any more though)
- function WaitForMe()
- os.pullEvent("redstone") --wait for a "redstone" event
- end
- --Check if the argument for side is valid or not
- function CheckValidSide(mySide, defaultSide)
- if mySide ~= "" then
- count = 0
- --Go through the list of valid sides
- for k,v in pairs(rs.getSides()) do
- if mySide == v then
- count = count + 1
- end
- end
- if count > 0 then
- --The side is OK
- return mySide
- else
- --The side is not OK, use default instead
- return defaultSide
- end
- end
- --No argument added, use default
- return defaultSide
- end
- --Make sure you have set a computer label.. if not then automatically assign a random label
- function ComputerLabel()
- if os.getComputerLabel() == nil then
- os.setComputerLabel("SomeComputer"..math.random(0,99999))
- return os.getComputerLabel()
- else
- return os.getComputerLabel()
- end
- end
- --Make sure that the entered value is an integer
- function TypeCheck(number)
- number = tonumber(number)
- if type(number) ~= "number" then
- return 2
- else
- return number
- end
- end
- ---------------------------
- --INPUT/OUTPUT ASSIGNMENT--
- ---------------------------
- --Assign input/output sides so that you can easily change this if you want
- --Available: left/right/back/front/bottom/top
- --I plan to maybe make configurable with additional input/outputs in the future, for now this will have to do
- --gatesim [GateType] [InputA] [InputB] [OutputA] [OutputB]
- -- tblArgs[1] tblArgs[2] tblArgs[3] tblArgs[4] tblArgs[5]
- -- "left" "right" "back" "front"
- --Make sure the sides are valid
- strInputA = CheckValidSide(tblArgs[2], "left")
- strInputB = CheckValidSide(tblArgs[3], "right")
- strOutputA = CheckValidSide(tblArgs[4], "back")
- strOutputB = CheckValidSide(tblArgs[5], "front")
- strVariable1 = TypeCheck(tblArgs[6])
- function InputA()
- return rs.getInput(strInputA)
- end
- function InputB()
- return rs.getInput(strInputB)
- end
- function OutputA(set)
- return rs.setOutput(strOutputA, set)
- end
- --This is not always used
- function OutputB(set)
- return rs.setOutput(strOutputB, set)
- end
- ----------------------------
- --GATE SIMULATION PROGRAMS--
- ----------------------------
- if tblArgs[1] == "AND" or tblArgs[1] == "and" then
- --Program AND
- StartInfo("AND","[OutA] is ON when [InpA] AND [InpB] is ON")
- while bolErr == false do
- WaitForMe()
- if InputA() == true and InputB() == true then
- OutputA(true)
- else
- OutputA(false)
- end
- end
- elseif tblArgs[1] == "NAND" or tblArgs[1] == "nand" then
- --Program NAND
- StartInfo("NAND","[OutA] is OFF when [InpA] AND [InpB] is ON")
- while bolErr == false do
- WaitForMe()
- if InputA() == true and InputB() == true then
- OutputA(false)
- else
- OutputA(true)
- end
- end
- elseif tblArgs[1] == "OR" or tblArgs[1] == "or" then
- --Program OR
- StartInfo("OR","[OutA] is ON when [InpA] OR [InpB] is ON")
- while bolErr == false do
- WaitForMe()
- if InputA() == true or InputB() == true then
- OutputA(true)
- else
- OutputA(false)
- end
- end
- elseif tblArgs[1] == "NOR" or tblArgs[1] == "nor" then
- --Program OR
- StartInfo("NOR","[OutA] is OFF when [InpA] OR [InpB] is ON")
- while bolErr == false do
- WaitForMe()
- if InputA() == true or InputB() == true then
- OutputA(false)
- else
- OutputA(true)
- end
- end
- elseif tblArgs[1] == "XOR" or tblArgs[1] == "xor" then
- --Program XOR
- StartInfo("XOR","[OutA] is ON when [InpA] and [InpB] is odd")
- while bolErr == false do
- WaitForMe()
- if InputA() == false and InputB() == false then
- OutputA(false)
- elseif InputA() == true and InputB() == true then
- OutputA(false)
- else
- OutputA(true)
- end
- end
- elseif tblArgs[1] == "XNOR" or tblArgs[1] == "xnor" then
- --Program XNOR
- StartInfo("XNOR","[OutA] is OFF when [InpA] and [InpB] is odd")
- while bolErr == false do
- WaitForMe()
- if InputA() == false and InputB() == false then
- OutputA(true)
- elseif InputA() == true and InputB() == true then
- OutputA(true)
- else
- OutputA(false)
- end
- end
- elseif tblArgs[1] == "TIMER" or tblArgs[1] == "timer" then
- --Program NOT
- StartInfo("TIMER","Pulses with [Var] delay when [InpA] is OFF")
- while bolErr == false do
- if InputA() == false then
- OutputA(true)
- sleep(strVariable1)
- OutputA(false)
- sleep(strVariable1)
- else
- OutputA(false)
- WaitForMe()
- end
- end
- elseif tblArgs[1] == "NTIMER" or tblArgs[1] == "ntimer" then
- --Program NOT
- StartInfo("NTIMER","Pulses with [Var] delay when [InpA] is ON")
- while bolErr == false do
- if InputA() == false then
- OutputA(false)
- WaitForMe()
- else
- OutputA(true)
- sleep(strVariable1)
- OutputA(false)
- sleep(strVariable1)
- end
- end
- elseif tblArgs[1] == "NOT" or tblArgs[1] == "not" then
- --Program NOT
- StartInfo("NOT","[OutA] is ON when [InpA] is OFF")
- while bolErr == false do
- WaitForMe()
- if InputA() == false then
- OutputA(true)
- else
- OutputA(false)
- end
- end
- elseif tblArgs[1] == "RSLATCH" or tblArgs[1] == "rslatch" or tblArgs[1] == "rsnorlatch" or tblArgs[1] == "RSNORLATCH" then
- --Program RS Latch
- StartInfo("RSLATCH","[OutA] is STORED as ON when [InpA] is ON or STORED as OFF when [InptB] is ON. [OutB] is odd to [OutA]")
- while bolErr == false do
- --Load previous memory
- if LoadState() == "1" then
- OutputA(true)
- OutputB(false)
- else
- OutputA(false)
- OutputB(true)
- end
- WaitForMe()
- if InputA() == true then
- SaveState("1")
- elseif InputB() == true then
- SaveState("0")
- end
- end
- elseif tblArgs[1] == "FLIPFLOP" or tblArgs[1] == "flipflop" or tblArgs[1] == "TFLIPFLOP" or tblArgs[1] == "tflipflop" then
- --Program RS Latch
- StartInfo("TFLIPFLOP","[OutA] toggles ON/OFF when [InpA] or [InpB] pulses. [OutB] is odd to [OutA]")
- while bolErr == false do
- --Load previous memory
- if LoadState() == "1" then
- OutputA(true)
- OutputB(false)
- else
- OutputA(false)
- OutputB(true)
- end
- while InputA() or InputB() == true do
- sleep(1.0) --sleep 1 tick
- end
- WaitForMe()
- if InputA() == true or InputB() == true then
- if LoadState() == "1" then
- SaveState("0")
- --WaitForMe()
- else
- SaveState("1")
- --WaitForMe()
- end
- end
- --os.sleep(1)
- end
- else
- --Print instructions
- PrintDescription()
- end
- --Some kind of error occured
- if bolErr == true then
- print("An error occurred... program failed.")
- print("Please check your server settings or try again.")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement