Sorceress

Shift Register Simulator

Jan 17th, 2021
96
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Shift Register Simulator: Noise Generator
  2. '(c)2021, @_sorceress
  3.  
  4. DEFINT A-W
  5. SCREEN 12
  6.  
  7. 'bitmasks for each tap
  8. DIM SHARED Q(8): Q(1) = 1%: FOR i = 2 TO 8: Q(i) = 2 * Q(i - 1): NEXT
  9.  
  10. SR1 = 1 'pre-load the first register
  11.  
  12. DO
  13. 'display current state
  14. COLOR 7: LOCATE 3, 1: PRINT "SR1"; TAB(27); "SR2"; TAB(53); "SR3"
  15. FOR i = 1 TO 8: b = Tap(SR1, i): COLOR 9 + 5 * b: PRINT b;: NEXT
  16. PRINT TAB(27);
  17. FOR i = 1 TO 8: b = Tap(SR2, i): COLOR 9 + 5 * b: PRINT b;: NEXT
  18. PRINT TAB(53);
  19. FOR i = 1 TO 8: b = Tap(SR3, i): COLOR 9 + 5 * b: PRINT b;: NEXT
  20.  
  21. 'scope the output tap
  22. y = Tap(SR1, 8)
  23. 'and a low pass filter of the output
  24. y2 = y2 + (y - y2) * 8000 / 60000 'cutoff f / generator f
  25.  
  26. t = t + 1
  27. IF t MOD 1 = 0 THEN 'optional time division for the oscilloscope
  28. x = (x + 1) MOD 640
  29. LINE (x, 204)-(x, 332), 0
  30. LINE (x + 1, 204)-(x + 1, 332), 8
  31. PSET (x, 300 - 64 * y), 9 + 5 * y
  32. PSET (x, 300 - 64 * y2old), 2
  33. LINE -(x, 300 - 64 * y2), 2
  34. y2old = y2
  35. _DISPLAY
  36. _LIMIT 60
  37. END IF
  38.  
  39. 'wiring the registers together
  40. SR1Data = Tap(SR3, 2) XOR Tap(SR3, 7) 'feedback
  41. SR2Data = Tap(SR1, 8) 'chaining SR1 and SR2
  42. SR3Data = Tap(SR2, 8) 'chaining SR2 and SR3
  43.  
  44. 'clock
  45. SR1 = (SR1 AND 127&) * 2 + SR1Data
  46. SR2 = (SR2 AND 127&) * 2 + SR2Data
  47. SR3 = (SR3 AND 127&) * 2 + SR3Data
  48.  
  49. LOOP
  50. END
  51.  
  52.  
  53. FUNCTION Tap (SR%, bit%)
  54. Tap = SGN(SR% AND Q(bit%))
  55. END FUNCTION
RAW Paste Data