blacksasi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #ifndef __BLACKSASI_H__
  2. #define __BLACKSASI_H__
  3. #include "sdcard.h"
  4. #include "gpio.h"
  5. #include "log.h"
  6. #include "config.h"
  7. #include "scsi.h"
  8. // Log File
  9. #define VERSION "0.xx-SNAPSHOT-BLACKSASI-2022-09-28"
  10. #define LOG_FILENAME "LOG.txt"
  11. #define SPI_CLOCK SD_SCK_MHZ(50)
  12. #define DEBUG 1 // 0:No debug information output
  13. // 1: Debug information output to USB Serial
  14. // 2: Debug information output to LOG.txt (slow)
  15. #define SCSI_SELECT 0 // 0 for STANDARD
  16. // 1 for SHARP X1turbo
  17. // 2 for NEC PC98
  18. #define READ_SPEED_OPTIMIZE 1 // Faster reads
  19. #define WRITE_SPEED_OPTIMIZE 1 // Speeding up writes
  20. #define USE_DB2ID_TABLE 1 // Use table to get ID from SEL-DB
  21. // SCSI config
  22. #define NUM_SCSIID 7 // Maximum number of supported SCSI-IDs (The minimum is 0)
  23. #define NUM_SCSILUN 2 // Maximum number of LUNs supported (The minimum is 0)
  24. #define READ_PARITY_CHECK 0 // Perform read parity check (unverified)
  25. // HDD format
  26. #define MAX_BLOCKSIZE 2048 // Maximum BLOCK size
  27. #if DEBUG == 1
  28. #define serial Serial
  29. #define LOG(XX) serial.print(XX)
  30. #define LOGHEX(XX) serial.print(XX, HEX)
  31. #define LOGDEC(XX) serial.print(XX, DEC)
  32. #define LOGBIN(XX) serial.print(XX, BIN)
  33. #define LOGN(XX) serial.println(XX)
  34. #define LOGHEXN(XX) serial.println(XX, HEX)
  35. #define LOGDECN(XX) serial.println(XX, DEC)
  36. #define LOGBIN_N(XX) serial.println(XX, BIN)
  37. #elif DEBUG == 2
  38. #define LOG(XX) LOG_FILE.print(XX); LOG_FILE.sync();
  39. #define LOGHEX(XX) LOG_FILE.print(XX, HEX); LOG_FILE.sync();
  40. #define LOGDEC(XX) LOG_FILE.print(XX, DEC); LOG_FILE.sync();
  41. #define LOGBIN(XX) LOG_FILE.print(XX, BIN); LOG_FILE.sync();
  42. #define LOGN(XX) LOG_FILE.println(XX); LOG_FILE.sync();
  43. #define LOGHEXN(XX) LOG_FILE.println(XX, HEX); LOG_FILE.sync();
  44. #define LOGDECN(XX) LOG_FILE.println(XX, DEC); LOG_FILE.sync();
  45. #define LOGBIN_N(XX) LOG_FILE.println(XX, BIN); LOG_FILE.sync();
  46. #else
  47. #define LOG(XX) //serial.print(XX)
  48. #define LOGHEX(XX) //serial.print(XX, HEX)
  49. #define LOGDEC(XX) //serial.print(XX, DEC)
  50. #define LOGBIN(XX) //serial.print(XX, BIN)
  51. #define LOGN(XX) //serial.println(XX)
  52. #define LOGHEXN(XX) //serial.println(XX, HEX)
  53. #define LOGDECN(XX) //serial.println(XX, DEC)
  54. #define LOGBIN_N(XX) //serial.println(XX, BIN)
  55. #endif
  56. #define active 1
  57. #define inactive 0
  58. #define high 0
  59. #define low 1
  60. #define isHigh(XX) ((XX) == high)
  61. #define isLow(XX) ((XX) != high)
  62. #define TR_INPUT 1
  63. #define TR_OUTPUT 0
  64. #define DB_INPUT 0
  65. #define DB_OUTPUT 1
  66. // GPIO register port
  67. #define PAREG GPIOA->regs
  68. #define PBREG GPIOB->regs
  69. #define PCREG GPIOC->regs
  70. #define PDREG GPIOD->regs
  71. #define PEREG GPIOE->regs
  72. // Termination control (LOW is active)
  73. #define TERMINATION_HIGH() GPIOREG(BOARD_SCSI_TERM_HIGH)->BSRR = (1 << BOARD_SCSI_TERM_HIGH % 16) << 16 | (1 << BOARD_SCSI_TERM_LOW % 16);
  74. #define TERMINATION_LOW() GPIOREG(BOARD_SCSI_TERM_HIGH)->BSRR = (1 << BOARD_SCSI_TERM_LOW % 16) << 16 | (1 << BOARD_SCSI_TERM_HIGH % 16);
  75. #define TERMINATION_OFF() GPIOREG(BOARD_SCSI_TERM_HIGH)->BSRR = (1 << BOARD_SCSI_TERM_HIGH % 16) | (1 << BOARD_SCSI_TERM_LOW % 16);
  76. // Enable SCSI buffers
  77. #define SCSI_OUTPUT_DISABLE() GPIOREG(BOARD_TRANS_OE)->BSRR = (1 << (BOARD_TRANS_OE & 15)) << 16;
  78. #define SCSI_OUTPUT_ENABLE() GPIOREG(BOARD_TRANS_OE)->BSRR = (1 << (BOARD_TRANS_OE & 15));
  79. // SCSI Data Direction
  80. #define SCSI_DATABUS_OUT() GPIOREG(BOARD_SCSI_DTD)->BSRR = (1 << (BOARD_SCSI_DTD & 15)) << 16;
  81. #define SCSI_DATABUS_IN() GPIOREG(BOARD_SCSI_DTD)->BSRR = (1 << (BOARD_SCSI_DTD & 15))
  82. // Virtual pin (Arduio compatibility is slow, so make it MCU-dependent)
  83. #define PA(BIT) (BIT)
  84. #define PB(BIT) (BIT + 16)
  85. #define PC(BIT) (BIT + 32)
  86. #define PD(BIT) (BIT + 48)
  87. #define PE(BIT) (BIT + 64)
  88. // Virtual pin decoding
  89. #define GPIOREG(VPIN) ((VPIN) >= 16 ? ((VPIN) >= 32 ? ((VPIN) >= 48 ? ((VPIN) >= 64 ? PEREG : PDREG) : PCREG) : PBREG) : PAREG)
  90. #define BITMASK(VPIN) (1 << ((VPIN) & 15))
  91. #define vATN PB(14) // SCSI:ATN
  92. #define vBSY PB(6) // SCSI:BSY
  93. #define vACK PB(7) // SCSI:ACK
  94. #define vRST PA(15) // SCSI:RST
  95. #define vMSG PE(2) // SCSI:MSG
  96. #define vSEL PE(3) // SCSI:SEL
  97. #define vCD PE(4) // SCSI:C/D
  98. #define vREQ PE(5) // SCSI:REQ
  99. #define vIO PE(6) // SCSI:I/O
  100. #define vSD_CS PB(1) // SDCARD:CS
  101. #define vDTD PC(0) // SCSI:DTD
  102. #define vIND PC(1) // SCSI:IND
  103. #define vTAD PC(2) // SCSI:TAD
  104. #define vTRANS_OE PB(12) // SCSI:TRANS_OE
  105. // SCSI output pin control: active LOW (direct pin drive)
  106. #define SCSI_OUT(VPIN,ACTIVE) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN) << ((ACTIVE) ? 16 : 0); }
  107. // SCSI input pin check (inactive=0,active=1)
  108. #define SCSI_IN(VPIN) ((~GPIOREG(VPIN)->IDR >> ((VPIN) & 15)) & 1)
  109. // HDDiamge file
  110. #define HDIMG_ID_POS 2 // Position to embed ID number
  111. #define HDIMG_LUN_POS 3 // Position to embed LUN numbers
  112. #define HDIMG_BLK_POS 5 // Position to embed block size numbers
  113. #define MAX_FILE_PATH 32 // Maximum file name length
  114. // SCSI
  115. #define SCSI_INFO_BUF_SIZE 36
  116. #define SCSI_INFO_VENDOR_SIZE 9
  117. #define SCSI_INFO_PRODUCT_SIZE 17
  118. #define SCSI_INFO_VERSION_SIZE 5
  119. typedef struct hddimg_struct
  120. {
  121. FsFile m_file; // File object
  122. uint64_t m_fileSize; // File size
  123. size_t m_blocksize; // SCSI BLOCK size
  124. }HDDIMG;
  125. // Declare functions
  126. void onFalseInit(void);
  127. void onBusReset(void);
  128. void switchImage(void);
  129. void initFileLog(int);
  130. void finalizeFileLog(void);
  131. // SCSI config
  132. #define MAX_SCSIID 7 // Maximum number of supported SCSI-IDs (The minimum is 0)
  133. #define MAX_SCSILUN 8 // Maximum number of LUNs supported (The minimum is 0)
  134. #define NUM_SCSIID MAX_SCSIID // Number of enabled SCSI IDs
  135. #define NUM_SCSILUN 1 // Number of enabled LUNs
  136. #define READ_PARITY_CHECK 0 // Perform read parity check (unverified)
  137. #define DEFAULT_SCSI_ID 1
  138. #define DEFAULT_SCSI_LUN 0
  139. #define SCSI_BUF_SIZE 512 // Size of the SCSI Buffer
  140. #define HDD_BLOCK_SIZE 512
  141. #define OPTICAL_BLOCK_SIZE 2048
  142. // HDD format
  143. #define MAX_BLOCKSIZE 4096 // Maximum BLOCK size
  144. // LED ERRORS
  145. #define ERROR_FALSE_INIT 3
  146. #define ERROR_NO_SDCARD 5
  147. enum SCSI_DEVICE_TYPE
  148. {
  149. SCSI_DEVICE_HDD,
  150. SCSI_DEVICE_OPTICAL,
  151. };
  152. #define CDROM_RAW_SECTORSIZE 2352
  153. #define CDROM_COMMON_SECTORSIZE 2048
  154. #define MAX_SCSI_COMMAND 0xff
  155. #define SCSI_COMMAND_HANDLER(x) static byte x(SCSI_DEVICE *dev, const byte *cdb)
  156. #define NOP(x) for(unsigned _nopcount = x; _nopcount; _nopcount--) { asm("NOP"); }
  157. /* SCSI Timing delays */
  158. // Due to limitations in timing granularity all of these are "very" rough estimates
  159. #define SCSI_BUS_SETTLE() NOP(30); // spec 400ns ours ~420us
  160. #define SCSI_DATA_RELEASE() NOP(30); // spec 400ns ours ~420us
  161. #define SCSI_HOLD_TIME() asm("NOP"); asm("NOP"); asm("NOP"); // spec 45ns ours ~42ns
  162. #define SCSI_DESKEW() // asm("NOP"); asm("NOP"); asm("NOP"); // spec 45ns ours ~42ns
  163. #define SCSI_CABLE_SKEW() // asm("NOP"); // spec 10ns ours ~14ns
  164. #define SCSI_RESET_HOLD() asm("NOP"); asm("NOP"); // spec 25ns ours ~28ns
  165. #define SCSI_DISCONNECTION_DELAY() NOP(15); // spec 200ns ours ~210ns
  166. /* SCSI phases
  167. +=============-===============-==================================-============+
  168. | Signal | Phase name | Direction of transfer | Comment |
  169. |-------------| | | |
  170. | MSG|C/D|I/O | | | |
  171. |----+---+----+---------------+----------------------------------+------------|
  172. | 0 | 0 | 0 | DATA OUT | Initiator to target \ | Data |
  173. | 0 | 0 | 1 | DATA IN | Initiator from target / | phase |
  174. | 0 | 1 | 0 | COMMAND | Initiator to target | |
  175. | 0 | 1 | 1 | STATUS | Initiator from target | |
  176. | 1 | 0 | 0 | * | | |
  177. | 1 | 0 | 1 | * | | |
  178. | 1 | 1 | 0 | MESSAGE OUT | Initiator to target \ | Message |
  179. | 1 | 1 | 1 | MESSAGE IN | Initiator from target / | phase |
  180. |-----------------------------------------------------------------------------|
  181. | Key: 0 = False, 1 = True, * = Reserved for future standardization |
  182. +=============================================================================+
  183. */
  184. // SCSI phase change as single write to port B
  185. #define SCSIPHASEMASK(MSGACTIVE, CDACTIVE, IOACTIVE) ((BITMASK(vMSG)<<((MSGACTIVE)?16:0)) | (BITMASK(vCD)<<((CDACTIVE)?16:0)) | (BITMASK(vIO)<<((IOACTIVE)?16:0)))
  186. #define SCSI_PHASE_DATAOUT SCSIPHASEMASK(inactive, inactive, inactive)
  187. #define SCSI_PHASE_DATAIN SCSIPHASEMASK(inactive, inactive, active)
  188. #define SCSI_PHASE_COMMAND SCSIPHASEMASK(inactive, active, inactive)
  189. #define SCSI_PHASE_STATUS SCSIPHASEMASK(inactive, active, active)
  190. #define SCSI_PHASE_MESSAGEOUT SCSIPHASEMASK(active, active, inactive)
  191. #define SCSI_PHASE_MESSAGEIN SCSIPHASEMASK(active, active, active)
  192. #define SCSI_PHASE_CHANGE(MASK) { PEREG->BSRR = (MASK); }
  193. //BLACKSASI clean this up and use defines
  194. // Data pins
  195. // 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  196. static const uint32_t scsiDbOutputRegOr_PDREG = 0b00000000000000010101010101010101;
  197. static const uint32_t scsiDbInputOutputAnd_PDREG = 0b00000000000000000000000000000000;
  198. static const uint32_t scsiDbInputOutputPullAnd_PDREG = 0b00000000000000010101010101010101;
  199. // Control pins
  200. // 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  201. static const uint32_t scsiDbInputOutputPullAnd_PEREG = 0b00000000000000000001010101010001;
  202. static const uint32_t scsiDbInputOutputPullAnd_PBREG = 0b00010000000000000101000000000000;
  203. // Put DB and DP in output mode and control buffers
  204. #define SCSI_DB_OUTPUT() { PDREG->MODER = (PDREG->MODER & scsiDbInputOutputAnd_PDREG) | scsiDbOutputRegOr_PDREG; SCSI_DATABUS_OUT() ;}
  205. // Put DB and DP in input mode and control buffers
  206. #define SCSI_DB_INPUT() { PDREG->MODER = (PDREG->MODER & scsiDbInputOutputAnd_PDREG); SCSI_DATABUS_IN();}
  207. #define SCSI_SET_PULL() { PDREG->PUPDR |= scsiDbInputOutputPullAnd_PDREG; PEREG->PUPDR = PEREG->PUPDR | scsiDbInputOutputPullAnd_PEREG; PBREG->PUPDR = PBREG->PUPDR | scsiDbInputOutputPullAnd_PBREG; }
  208. // Transceiver control definitions
  209. #define TRANSCEIVER_IO_SET(VPIN,TRX_INPUT) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN) << ((TRX_INPUT) ? 16 : 0); }
  210. #define PTY(V) (1^((V)^((V)>>1)^((V)>>2)^((V)>>3)^((V)>>4)^((V)>>5)^((V)>>6)^((V)>>7))&1)
  211. // 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  212. static const uint32_t SCSI_TARGET_PORTB_AND = 0b11001111111111110000111111111111;
  213. static const uint32_t SCSI_TARGET_PORTB_OR = 0b00010000000000000101000000000000;
  214. // 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
  215. static const uint32_t SCSI_TARGET_PORTE_AND = 0b11111111111111111100000011000011;
  216. static const uint32_t SCSI_TARGET_PORTE_OR = 0b00000000000000000001010100010000;
  217. // Turn on the output only for BSY
  218. //#define SCSI_BSY_ACTIVE() { PEREG->MODER = (PEREG->MODER & SCSI_TARGET_PORTE_AND) | SCSI_TARGET_PORTE_OR; PDREG->MODER = (PDREG->MODER & SCSI_TARGET_PORTD_AND) | SCSI_TARGET_PORTD_OR; SCSI_OUT(vBSY, active) }
  219. #define SCSI_BSY_ACTIVE() { pinMode(BOARD_SCSI_BSY, OUTPUT); SCSI_OUT(vBSY, active) }
  220. // BSY,REQ,MSG,CD,IO Turn off output, BSY is the last input
  221. #define SCSI_TARGET_INACTIVE() { PEREG->MODER = (PEREG->MODER & SCSI_TARGET_PORTE_AND); PBREG->MODER = (PBREG->MODER & SCSI_TARGET_PORTB_AND); TRANSCEIVER_IO_SET(vTAD,TR_INPUT); }
  222. #define SCSI_TARGET_ACTIVE() { PEREG->MODER = (PEREG->MODER & SCSI_TARGET_PORTE_AND) | SCSI_TARGET_PORTE_OR; PBREG->MODER = (PBREG->MODER & SCSI_TARGET_PORTB_AND) | SCSI_TARGET_PORTB_OR; }
  223. // HDDimage file
  224. #define HDIMG_ID_POS 2 // Position to embed ID number
  225. #define HDIMG_LUN_POS 3 // Position to embed LUN numbers
  226. #define HDIMG_BLK_POS 5 // Position to embed block size numbers
  227. #define MAX_FILE_PATH 64 // Maximum file name length
  228. /*
  229. * Data byte to BSRR register setting value and parity table
  230. */
  231. /**
  232. * BSRR register generator
  233. * Totally configurable for which pin is each data bit, which pin is PTY, and which pin is REQ.
  234. * The only requirement is that data and parity pins are in the same GPIO block.
  235. * REQ can be specified as -1 to ignore, as it doesn't have to be in the same GPIO block.
  236. * This is dramatically slower than the original static array, but is easier to configure
  237. */
  238. static uint32_t genBSRR(uint32_t data) {
  239. uint8_t masks[] = {0UL, 1UL, 2UL, 3UL, 4UL, 5UL, 6UL, 7UL};
  240. // Positions array indicates which bit position each data bit goes in
  241. // positions[0] is for data bit 0, position[1] for data bit 1, etc
  242. // DB0, DB1, DB2, DB4, DB5, DB6, DB7 in order
  243. uint8_t positions[] = {0UL, 1UL, 2UL, 3UL, 4UL, 5UL, 6UL, 7UL};
  244. uint8_t dbpPosition = 8UL;
  245. int reqPosition = 0;
  246. uint8_t bitsAsserted = 0;
  247. //PM2022 Fix this in better way since we now have the bits correct
  248. uint32_t output = 0x00000000;
  249. //output |=(~data) | (data << 16);
  250. for (int i = 0; i < 8; i++) {
  251. if (data & (0x1 << masks[i])) {
  252. // There's a one in this bit position, BSRR reset
  253. output |= 0x1 << (positions[i] + 16);
  254. bitsAsserted++;
  255. } else {
  256. // There's a 0 in this bit position, BSRR set high
  257. output |= (0x1 << positions[i]);
  258. }
  259. }
  260. // Set the parity bit
  261. if (bitsAsserted % 2 == 0) {
  262. // Even number of bits asserted, Parity asserted (0, low, BSRR reset)
  263. output |= 0x01 << (dbpPosition + 16);
  264. } else {
  265. // Odd number of bits asserted, Parity deasserted (1, high, BSRR set)
  266. output |= 0x01 << dbpPosition;
  267. }
  268. // BSRR set REQ if specified
  269. // Only set > 0 if it's in the same GPIO block as DB and DBP
  270. if (reqPosition > 0) {
  271. output |= 0x01 << reqPosition;
  272. }
  273. return output;
  274. }
  275. // BSRR register control value that simultaneously performs DB set, DP set, and REQ = H (inactrive)
  276. //uint32_t db_bsrr[256];
  277. // Parity bit acquisition
  278. #define PARITY(DB) (db_bsrr[DB]&1)
  279. #define READ_DATA_BUS() (byte)((~(uint32_t)GPIOD->regs->IDR)>>8)
  280. struct SCSI_INQUIRY_DATA
  281. {
  282. union
  283. {
  284. struct {
  285. // bitfields are in REVERSE order for ARM
  286. // byte 0
  287. byte peripheral_device_type:5;
  288. byte peripheral_qualifier:3;
  289. // byte 1
  290. byte reserved_byte2:7;
  291. byte rmb:1;
  292. // byte 2
  293. byte ansi_version:3;
  294. byte always_zero_byte3:5;
  295. // byte 3
  296. byte response_format:4;
  297. byte reserved_byte4:2;
  298. byte tiop:1;
  299. byte always_zero_byte4:1;
  300. // byte 4
  301. byte additional_length;
  302. // byte 5-6
  303. byte reserved_byte5;
  304. byte reserved_byte6;
  305. // byte 7
  306. byte sync:1;
  307. byte always_zero_byte7_more:4;
  308. byte always_zero_byte7:3;
  309. // byte 8-15
  310. char vendor[8];
  311. // byte 16-31
  312. char product[16];
  313. // byte 32-35
  314. char revision[4];
  315. // byte 36
  316. byte release;
  317. // 37-46
  318. char revision_date[10];
  319. };
  320. // raw bytes
  321. byte raw[64];
  322. };
  323. };
  324. // HDD image
  325. typedef __attribute__((aligned(4))) struct _SCSI_DEVICE
  326. {
  327. FsFile *m_file; // File object
  328. uint64_t m_fileSize; // File size
  329. uint16_t m_blocksize; // SCSI BLOCK size
  330. uint16_t m_rawblocksize; // OPTICAL raw sector size
  331. uint8_t m_type; // SCSI device type
  332. uint32_t m_blockcount; // blockcount
  333. bool m_raw; // Raw disk
  334. SCSI_INQUIRY_DATA *inquiry_block; // SCSI information
  335. uint8_t m_senseKey; // Sense key
  336. uint16_t m_additional_sense_code; // ASC/ASCQ
  337. bool m_mode2; // MODE2 CDROM
  338. uint8_t m_sector_offset; // optical sector offset for missing sync header
  339. } SCSI_DEVICE;
  340. #endif // __BLACKSASI_H__