blacksasi.h 16 KB

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