scsiPhy.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // Implements the low level interface to SCSI bus
  2. // Partially derived from scsiPhy.c from SCSI2SD-V6
  3. #include "scsiPhy.h"
  4. #include "ZuluSCSI_platform.h"
  5. #include "ZuluSCSI_log.h"
  6. #include "ZuluSCSI_log_trace.h"
  7. #include "ZuluSCSI_config.h"
  8. #include <scsi2sd.h>
  9. extern "C" {
  10. #include <scsi.h>
  11. #include <scsi2sd_time.h>
  12. }
  13. /***********************/
  14. /* SCSI status signals */
  15. /***********************/
  16. extern "C" bool scsiStatusATN()
  17. {
  18. return SCSI_IN(ATN);
  19. }
  20. extern "C" bool scsiStatusBSY()
  21. {
  22. return SCSI_IN(BSY);
  23. }
  24. /************************/
  25. /* SCSI selection logic */
  26. /************************/
  27. volatile uint8_t g_scsi_sts_selection;
  28. volatile uint8_t g_scsi_ctrl_bsy;
  29. void scsi_bsy_deassert_interrupt()
  30. {
  31. if (SCSI_IN(SEL) && !SCSI_IN(BSY))
  32. {
  33. // Check if any of the targets we simulate is selected
  34. uint8_t sel_bits = SCSI_IN_DATA();
  35. int sel_id = -1;
  36. for (int i = 0; i < S2S_MAX_TARGETS; i++)
  37. {
  38. if (scsiDev.targets[i].targetId <= 7 && scsiDev.targets[i].cfg)
  39. {
  40. if (sel_bits & (1 << scsiDev.targets[i].targetId))
  41. {
  42. sel_id = scsiDev.targets[i].targetId;
  43. break;
  44. }
  45. }
  46. }
  47. if (sel_id >= 0)
  48. {
  49. uint8_t atn_flag = SCSI_IN(ATN) ? SCSI_STS_SELECTION_ATN : 0;
  50. g_scsi_sts_selection = SCSI_STS_SELECTION_SUCCEEDED | atn_flag | sel_id;
  51. }
  52. // selFlag is required for Philips P2000C which releases it after 600ns
  53. // without waiting for BSY.
  54. // Also required for some early Mac Plus roms
  55. scsiDev.selFlag = *SCSI_STS_SELECTED;
  56. }
  57. }
  58. extern "C" bool scsiStatusSEL()
  59. {
  60. if (g_scsi_ctrl_bsy)
  61. {
  62. // We don't have direct register access to BSY bit like SCSI2SD scsi.c expects.
  63. // Instead update the state here.
  64. // Releasing happens with bus release.
  65. g_scsi_ctrl_bsy = 0;
  66. SCSI_OUT(BSY, 1);
  67. }
  68. return SCSI_IN(SEL);
  69. }
  70. /************************/
  71. /* SCSI bus reset logic */
  72. /************************/
  73. static void scsi_rst_assert_interrupt()
  74. {
  75. // Glitch filtering
  76. bool rst1 = SCSI_IN(RST);
  77. delay_ns(500);
  78. bool rst2 = SCSI_IN(RST);
  79. if (rst1 && rst2)
  80. {
  81. azdbg("BUS RESET");
  82. scsiDev.resetFlag = 1;
  83. }
  84. }
  85. // This function is called to initialize the phy code.
  86. // It is called after power-on and after SCSI bus reset.
  87. extern "C" void scsiPhyReset(void)
  88. {
  89. SCSI_RELEASE_OUTPUTS();
  90. g_scsi_sts_selection = 0;
  91. g_scsi_ctrl_bsy = 0;
  92. /* Implement here code to enable two interrupts:
  93. * scsi_bsy_deassert_interrupt() on rising edge of BSY pin
  94. * scsi_rst_assert_interrupt() on falling edge of RST pin
  95. *
  96. * For SCSI-1 single-initiator support, also call:
  97. * scsi_bsy_deassert_interrupt() on falling edge of SEL pin
  98. */
  99. }
  100. /************************/
  101. /* SCSI bus phase logic */
  102. /************************/
  103. static SCSI_PHASE g_scsi_phase;
  104. extern "C" void scsiEnterPhase(int phase)
  105. {
  106. int delay = scsiEnterPhaseImmediate(phase);
  107. if (delay > 0)
  108. {
  109. s2s_delay_ns(delay);
  110. }
  111. }
  112. // Change state and return nanosecond delay to wait
  113. extern "C" uint32_t scsiEnterPhaseImmediate(int phase)
  114. {
  115. // ANSI INCITS 362-2002 SPI-3 10.7.1:
  116. // Phase changes are not allowed while REQ or ACK is asserted.
  117. while (likely(!scsiDev.resetFlag) && SCSI_IN(ACK)) {}
  118. if (phase != g_scsi_phase)
  119. {
  120. int oldphase = g_scsi_phase;
  121. g_scsi_phase = (SCSI_PHASE)phase;
  122. scsiLogPhaseChange(phase);
  123. if (phase < 0)
  124. {
  125. // Other communication on bus or reset state
  126. SCSI_RELEASE_OUTPUTS();
  127. return 0;
  128. }
  129. else
  130. {
  131. SCSI_OUT(MSG, phase & __scsiphase_msg);
  132. SCSI_OUT(CD, phase & __scsiphase_cd);
  133. SCSI_OUT(IO, phase & __scsiphase_io);
  134. int delayNs = 400; // Bus settle delay
  135. if ((oldphase & __scsiphase_io) != (phase & __scsiphase_io))
  136. {
  137. delayNs += 400; // Data release delay
  138. }
  139. if (scsiDev.compatMode < COMPAT_SCSI2)
  140. {
  141. // EMU EMAX needs 100uS ! 10uS is not enough.
  142. delayNs += 100000;
  143. }
  144. return delayNs;
  145. }
  146. }
  147. else
  148. {
  149. return 0;
  150. }
  151. }
  152. // Release all signals
  153. void scsiEnterBusFree(void)
  154. {
  155. g_scsi_phase = BUS_FREE;
  156. g_scsi_sts_selection = 0;
  157. g_scsi_ctrl_bsy = 0;
  158. scsiDev.cdbLen = 0;
  159. SCSI_RELEASE_OUTPUTS();
  160. }
  161. /********************/
  162. /* Transmit to host */
  163. /********************/
  164. #define SCSI_WAIT_ACTIVE(pin) \
  165. if (!SCSI_IN(pin)) { \
  166. if (!SCSI_IN(pin)) { \
  167. while(!SCSI_IN(pin) && !scsiDev.resetFlag); \
  168. } \
  169. }
  170. #define SCSI_WAIT_INACTIVE(pin) \
  171. if (SCSI_IN(pin)) { \
  172. if (SCSI_IN(pin)) { \
  173. while(SCSI_IN(pin) && !scsiDev.resetFlag); \
  174. } \
  175. }
  176. // Write one byte to SCSI host using the handshake mechanism
  177. static inline void scsiWriteOneByte(uint8_t value)
  178. {
  179. SCSI_OUT_DATA(value);
  180. delay_100ns(); // DB setup time before REQ
  181. SCSI_OUT(REQ, 1);
  182. SCSI_WAIT_ACTIVE(ACK);
  183. SCSI_RELEASE_DATA_REQ();
  184. SCSI_WAIT_INACTIVE(ACK);
  185. }
  186. extern "C" void scsiWriteByte(uint8_t value)
  187. {
  188. scsiLogDataIn(&value, 1);
  189. scsiWriteOneByte(value);
  190. }
  191. extern "C" void scsiWrite(const uint8_t* data, uint32_t count)
  192. {
  193. scsiLogDataIn(data, count);
  194. for (uint32_t i = 0; i < count; i++)
  195. {
  196. if (scsiDev.resetFlag) break;
  197. scsiWriteOneByte(data[i]);
  198. }
  199. }
  200. extern "C" void scsiStartWrite(const uint8_t* data, uint32_t count)
  201. {
  202. // If the platform supports DMA for either SD card access or for SCSI bus,
  203. // this function can be used to execute SD card transfers in parallel with
  204. // SCSI transfers. This usually doubles the transfer speed.
  205. //
  206. // For simplicity, this example only implements blocking writes.
  207. scsiWrite(data, count);
  208. }
  209. extern "C" bool scsiIsWriteFinished(const uint8_t *data)
  210. {
  211. // Asynchronous writes are not implemented in this example.
  212. return true;
  213. }
  214. extern "C" void scsiFinishWrite()
  215. {
  216. // Asynchronous writes are not implemented in this example.
  217. }
  218. /*********************/
  219. /* Receive from host */
  220. /*********************/
  221. // Read one byte from SCSI host using the handshake mechanism.
  222. static inline uint8_t scsiReadOneByte(void)
  223. {
  224. SCSI_OUT(REQ, 1);
  225. SCSI_WAIT_ACTIVE(ACK);
  226. delay_100ns();
  227. uint8_t r = SCSI_IN_DATA();
  228. SCSI_OUT(REQ, 0);
  229. SCSI_WAIT_INACTIVE(ACK);
  230. return r;
  231. }
  232. extern "C" uint8_t scsiReadByte(void)
  233. {
  234. uint8_t r = scsiReadOneByte();
  235. scsiLogDataOut(&r, 1);
  236. return r;
  237. }
  238. extern "C" void scsiRead(uint8_t* data, uint32_t count, int* parityError)
  239. {
  240. *parityError = 0;
  241. for (uint32_t i = 0; i < count; i++)
  242. {
  243. if (scsiDev.resetFlag) break;
  244. data[i] = scsiReadOneByte();
  245. }
  246. scsiLogDataOut(data, count);
  247. }