Quipex

Disk/mem/redworks/programs/circuits/D-FlipFlop

Apr 23rd, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. --2
  2. --2
  3. --0
  4. input = {}
  5. output = {}
  6. extra = {}
  7. input[1] = "front"
  8. input[2] = "left"
  9. output[1] = "right"
  10. output[2] = "back"
  11. -- D-FlipFlop (Coded by Ferial's)
  12. -- press q or Q to exit to shell
  13.  
  14. -- front input: Clock
  15. -- left input: D (Data)
  16. -- right output: Q (Data Out)
  17. -- back output: Inv Q (Inverted Data Out)
  18.  
  19. term.clear()
  20. term.setCursorPos(1,2)
  21. print("D-FlipFlop running. Press Q to stop.")
  22.  
  23. --set initial state
  24. QBit = false;
  25. redstone.setOutput(output[1],QBit)
  26. redstone.setOutput(output[2], not QBit)
  27.  
  28. lastinpfront = false
  29.  
  30. while true do
  31.  
  32. --wait on an event
  33. event, param = os.pullEvent()
  34.  
  35. -- Key event. On a key Q or q we exit to shell
  36. if event == "char" and (param == "q" or param == "Q") then
  37. print("Device stopped")
  38. break
  39. end
  40.  
  41. -- redstone event
  42. if event == "redstone" then
  43. -- read clock input
  44. inpfront = redstone.getInput(input[1])
  45. if (inpfront and not lastinpfront) then
  46. -- rising edge detected
  47. -- read data Input
  48. QBit = redstone.getInput(input[2])
  49. -- delay the output at least a bit more than 0.05 sec to allow synchronous working circuits
  50. sleep(0.06)
  51. -- set new output
  52. -- well I don't think the order matters (single core app) but let's do it the safe way and go low first
  53. if QBit then
  54. redstone.setOutput(output[2], not QBit)
  55. redstone.setOutput(output[1],QBit)
  56. else
  57. redstone.setOutput(output[1],QBit)
  58. redstone.setOutput(output[2], not QBit)
  59. end
  60. end
  61. lastinpfront = inpfront
  62. end
  63.  
  64. end
Advertisement
Add Comment
Please, Sign In to add comment