scsi_accel.pio 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. ; RP2040 PIO program for accelerating SCSI communication
  2. ;
  3. ; Copyright (c) 2022 Rabbit Hole Computing™
  4. ;
  5. ; Run "pioasm scsi_accel.pio scsi_accel.pio.h" to regenerate the C header from this.
  6. ; GPIO mapping:
  7. ; - 0-7: DB0-DB7
  8. ; - 8: DBP
  9. ; Side set is REQ pin
  10. .define REQ 19 ; was 17
  11. .define ACK 26 ; was 10
  12. ; Delay from data setup to REQ assertion.
  13. ; deskew delay + cable skew delay = 55 ns minimum
  14. ; One clock cycle is 8 ns => delay 7 clocks
  15. .define REQ_DLY 7
  16. ; Adds parity to data that is to be written to SCSI
  17. ; This works by generating addresses for DMA to fetch data from.
  18. ; Register X should be initialized to the base address of the lookup table.
  19. .program scsi_parity
  20. pull block
  21. in NULL, 1
  22. in OSR, 8
  23. in X, 23
  24. ; Write to SCSI bus using asynchronous handshake.
  25. ; Data is written as 32-bit words that contain the 8 data bits + 1 parity bit.
  26. ; 23 bits in each word are discarded.
  27. ; Number of bytes to send must be multiple of 2.
  28. .program scsi_accel_async_write
  29. .side_set 1
  30. pull ifempty block side 1 ; Get data from TX FIFO
  31. out pins, 9 side 1 ; Write data and parity bit
  32. out null, 23 [REQ_DLY-2] side 1 ; Discard unused bits, wait for data preset time
  33. wait 1 gpio ACK side 1 ; Wait for ACK to be inactive
  34. wait 0 gpio ACK side 0 ; Assert REQ, wait for ACK low
  35. ; Read from SCSI bus using sync or async handshake.
  36. ; Data is returned as 32-bit words:
  37. ; - bit 0: always zero
  38. ; - bits 1-8: data byte
  39. ; - bit 9: parity bit
  40. ; - bits 10-31: lookup table address
  41. ; Lookup table address should be loaded into register Y.
  42. ; One dummy word should be written to TX fifo for every byte to receive.
  43. .program scsi_accel_read
  44. .side_set 1
  45. pull block side 1 ; Pull from TX fifo for counting bytes and pacing sync mode
  46. wait 1 gpio ACK side 1 ; Wait for ACK high
  47. in null, 1 side 0 ; Zero bit because lookup table entries are 16-bit
  48. wait 0 gpio ACK side 0 ; Assert REQ, wait for ACK low
  49. in pins, 9 side 1 ; Deassert REQ, read GPIO
  50. in y, 22 side 1 ; Copy parity lookup table address
  51. ; Data state machine for synchronous writes.
  52. ; Takes the lowest 9 bits of each 32 bit word and writes them to bus with REQ pulse.
  53. ; The delay times will be rewritten by C code to match the negotiated SCSI sync speed.
  54. ;
  55. ; Shifts one bit to ISR per every byte transmitted. This is used to control the transfer
  56. ; pace, the RX fifo acts as a counter to keep track of unacknowledged bytes. The C code
  57. ; can set the syncOffset by changing autopush threshold, e.g. threshold 3 = 12 bytes offset.
  58. .program scsi_sync_write
  59. .side_set 1
  60. out pins, 9 [0] side 1 ; Write data and parity bit, wait for deskew delay
  61. out null, 23 [0] side 0 ; Assert REQ, wait for assert time
  62. in null, 1 [0] side 1 ; Deassert REQ, wait for transfer period, wait for space in ACK buffer
  63. ; Data pacing state machine for synchronous writes.
  64. ; Takes one bit from ISR on every falling edge of ACK.
  65. ; The C code should set autopull threshold to match scsi_sync_write autopush threshold.
  66. ; System DMA will then move words from scsi_sync_write RX fifo to scsi_sync_write_pacer TX fifo.
  67. .program scsi_sync_write_pacer
  68. wait 1 gpio ACK
  69. wait 0 gpio ACK ; Wait for falling edge on ACK
  70. out null, 1 ; Let scsi_sync_write send one more byte
  71. ; Data pacing state machine for synchronous reads.
  72. ; The delay times will be rewritten by C code to match the negotiated SCSI sync speed.
  73. ; Number of bytes to receive minus one should be loaded into register X.
  74. ; In synchronous mode this generates the REQ pulses and dummy words.
  75. ; In asynchronous mode it just generates dummy words to feed to scsi_accel_read.
  76. .program scsi_sync_read_pacer
  77. .side_set 1
  78. start:
  79. push block [0] side 1 ; Send dummy word to scsi_accel_read, wait for transfer period
  80. jmp x-- start [0] side 0 ; Assert REQ, wait for assert time
  81. finish:
  82. jmp finish [0] side 1
  83. ; Parity checker for reads from SCSI bus.
  84. ; Receives 16-bit words from g_scsi_parity_check_lookup
  85. ; Bottom 8 bits are the data byte, which is passed to output FIFO
  86. ; The 9th bit is parity valid bit, which is 1 for valid and 0 for parity error.
  87. .program scsi_read_parity
  88. parity_valid:
  89. out isr, 8 ; Take the 8 data bits for passing to RX fifo
  90. push block ; Push the data to RX fifo
  91. out x, 24 ; Take the parity valid bit, and the rest of 32-bit word
  92. jmp x-- parity_valid ; If parity valid bit is 1, repeat from start
  93. irq set 0 ; Parity error, set interrupt flag