scsiPhy.cpp 8.2 KB

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