Sorceress

Shift Register Simulator with file output

Jan 25th, 2021 (edited)
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Shift Register Simulator: Noise Generator with file output
  2. '(c)2021, @_sorceress
  3.  
  4. DEFLNG A-Z
  5. DIM b AS _UNSIGNED _BYTE
  6.  
  7. samplerate = 44100 'generator frequency
  8. filename$ = "C:\noise.raw" 'where you want the noise saved
  9. seconds = 10 'and the amount of noise to generate
  10.  
  11. 'the sound will be saved as unsigned 8-bit mono PCM at the given samplerate
  12. ' you will need to specify this when you import the file into a sound editing program
  13. ' because the format isn't saved in the file
  14.  
  15. 'bitmasks for each tap
  16. DIM SHARED Q(8): Q(1) = 1: FOR i = 2 TO 8: Q(i) = 2 * Q(i - 1): NEXT
  17.  
  18. SR1 = 1 'pre-load the first register
  19.  
  20. OPEN filename$ FOR BINARY AS #1
  21.  
  22. FOR j = 1 TO seconds
  23. LOCATE 1, 1: PRINT j; "/"; seconds; " seconds"
  24. FOR i = 1 TO samplerate
  25.  
  26. 'select an output tap
  27. b = Tap(SR3, 8)
  28.  
  29. 'set level and zero-offset for unsigned 8-bit
  30. b = 96 + 64 * b
  31.  
  32. 'output byte to file
  33. PUT #1, , b
  34.  
  35. 'wiring the registers together
  36. SR1Data = Tap(SR3, 2) XOR Tap(SR3, 7) 'feedback
  37. SR2Data = Tap(SR1, 8) 'chaining SR1 and SR2
  38. SR3Data = Tap(SR2, 8) 'chaining SR2 and SR3
  39.  
  40. 'clock
  41. SR1 = (SR1 AND 127%) * 2 + SR1Data
  42. SR2 = (SR2 AND 127%) * 2 + SR2Data
  43. SR3 = (SR3 AND 127%) * 2 + SR3Data
  44.  
  45. NEXT
  46. NEXT
  47. CLOSE #1
  48.  
  49. END
  50.  
  51. FUNCTION Tap (SR%, bit%)
  52. Tap = SGN(SR% AND Q(bit%))
  53. END FUNCTION
RAW Paste Data