BlueSCSI.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  1. /*
  2. * BlueSCSI
  3. * Copyright (c) 2021 Eric Helgeson
  4. *
  5. * This file is free software: you may copy, redistribute and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation, either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This file is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see https://github.com/erichelgeson/bluescsi.
  17. *
  18. * This file incorporates work covered by the following copyright and
  19. * permission notice:
  20. *
  21. * Copyright (c) 2019 komatsu
  22. *
  23. * Permission to use, copy, modify, and/or distribute this software
  24. * for any purpose with or without fee is hereby granted, provided
  25. * that the above copyright notice and this permission notice appear
  26. * in all copies.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
  29. * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
  30. * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
  31. * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
  32. * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
  33. * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  34. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  35. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  36. */
  37. #include <Arduino.h> // For Platform.IO
  38. #include <SdFat.h>
  39. #ifdef USE_STM32_DMA
  40. #warning "warning USE_STM32_DMA"
  41. #endif
  42. #define DEBUG 0 // 0:No debug information output
  43. // 1: Debug information output available
  44. #define SCSI_SELECT 0 // 0 for STANDARD
  45. // 1 for SHARP X1turbo
  46. // 2 for NEC PC98
  47. #define READ_SPEED_OPTIMIZE 1 // Faster reads
  48. #define WRITE_SPEED_OPTIMIZE 1 // Speeding up writes
  49. #define USE_DB2ID_TABLE 1 // Use table to get ID from SEL-DB
  50. // SCSI config
  51. #define NUM_SCSIID 7 // Maximum number of supported SCSI-IDs (The minimum is 0)
  52. #define NUM_SCSILUN 2 // Maximum number of LUNs supported (The minimum is 0)
  53. #define READ_PARITY_CHECK 0 // Perform read parity check (unverified)
  54. // HDD format
  55. #define MAX_BLOCKSIZE 1024 // Maximum BLOCK size
  56. // SDFAT
  57. #define SD1_CONFIG SdSpiConfig(PA4, DEDICATED_SPI, SD_SCK_MHZ(SPI_FULL_SPEED), &SPI)
  58. SdFs SD;
  59. #if DEBUG
  60. #define LOG(XX) Serial.print(XX)
  61. #define LOGHEX(XX) Serial.print(XX, HEX)
  62. #define LOGN(XX) Serial.println(XX)
  63. #define LOGHEXN(XX) Serial.println(XX, HEX)
  64. #else
  65. #define LOG(XX) //Serial.print(XX)
  66. #define LOGHEX(XX) //Serial.print(XX, HEX)
  67. #define LOGN(XX) //Serial.println(XX)
  68. #define LOGHEXN(XX) //Serial.println(XX, HEX)
  69. #endif
  70. #define active 1
  71. #define inactive 0
  72. #define high 0
  73. #define low 1
  74. #define isHigh(XX) ((XX) == high)
  75. #define isLow(XX) ((XX) != high)
  76. #define gpio_mode(pin,val) gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val);
  77. #define gpio_write(pin,val) gpio_write_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val)
  78. #define gpio_read(pin) gpio_read_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit)
  79. //#define DB0 PB8 // SCSI:DB0
  80. //#define DB1 PB9 // SCSI:DB1
  81. //#define DB2 PB10 // SCSI:DB2
  82. //#define DB3 PB11 // SCSI:DB3
  83. //#define DB4 PB12 // SCSI:DB4
  84. //#define DB5 PB13 // SCSI:DB5
  85. //#define DB6 PB14 // SCSI:DB6
  86. //#define DB7 PB15 // SCSI:DB7
  87. //#define DBP PB0 // SCSI:DBP
  88. #define ATN PA8 // SCSI:ATN
  89. #define BSY PA9 // SCSI:BSY
  90. #define ACK PA10 // SCSI:ACK
  91. #define RST PA15 // SCSI:RST
  92. #define MSG PB3 // SCSI:MSG
  93. #define SEL PB4 // SCSI:SEL
  94. #define CD PB5 // SCSI:C/D
  95. #define REQ PB6 // SCSI:REQ
  96. #define IO PB7 // SCSI:I/O
  97. #define SD_CS PA4 // SDCARD:CS
  98. #define LED PC13 // LED
  99. // GPIO register port
  100. #define PAREG GPIOA->regs
  101. #define PBREG GPIOB->regs
  102. // LED control
  103. #define LED_ON() gpio_write(LED, high);
  104. #define LED_OFF() gpio_write(LED, low);
  105. // Virtual pin (Arduio compatibility is slow, so make it MCU-dependent)
  106. #define PA(BIT) (BIT)
  107. #define PB(BIT) (BIT+16)
  108. // Virtual pin decoding
  109. #define GPIOREG(VPIN) ((VPIN)>=16?PBREG:PAREG)
  110. #define BITMASK(VPIN) (1<<((VPIN)&15))
  111. #define vATN PA(8) // SCSI:ATN
  112. #define vBSY PA(9) // SCSI:BSY
  113. #define vACK PA(10) // SCSI:ACK
  114. #define vRST PA(15) // SCSI:RST
  115. #define vMSG PB(3) // SCSI:MSG
  116. #define vSEL PB(4) // SCSI:SEL
  117. #define vCD PB(5) // SCSI:C/D
  118. #define vREQ PB(6) // SCSI:REQ
  119. #define vIO PB(7) // SCSI:I/O
  120. #define vSD_CS PA(4) // SDCARD:CS
  121. // SCSI output pin control: opendrain active LOW (direct pin drive)
  122. #define SCSI_OUT(VPIN,ACTIVE) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN)<<((ACTIVE)?16:0); }
  123. // SCSI input pin check (inactive=0,avtive=1)
  124. #define SCSI_IN(VPIN) ((~GPIOREG(VPIN)->IDR>>(VPIN&15))&1)
  125. // GPIO mode
  126. // IN , FLOAT : 4
  127. // IN , PU/PD : 8
  128. // OUT, PUSH/PULL : 3
  129. // OUT, OD : 1
  130. //#define DB_MODE_OUT 3
  131. #define DB_MODE_OUT 1
  132. #define DB_MODE_IN 8
  133. // Put DB and DP in output mode
  134. #define SCSI_DB_OUTPUT() { PBREG->CRL=(PBREG->CRL &0xfffffff0)|DB_MODE_OUT; PBREG->CRH = 0x11111111*DB_MODE_OUT; }
  135. // Put DB and DP in input mode
  136. #define SCSI_DB_INPUT() { PBREG->CRL=(PBREG->CRL &0xfffffff0)|DB_MODE_IN ; PBREG->CRH = 0x11111111*DB_MODE_IN; }
  137. // Turn on the output only for BSY
  138. #define SCSI_BSY_ACTIVE() { gpio_mode(BSY, GPIO_OUTPUT_OD); SCSI_OUT(vBSY, active) }
  139. // BSY,REQ,MSG,CD,IO Turn on the output (no change required for OD)
  140. #define SCSI_TARGET_ACTIVE() { }
  141. // BSY,REQ,MSG,CD,IO Turn off output, BSY is the last input
  142. #define SCSI_TARGET_INACTIVE() { SCSI_OUT(vREQ,inactive); SCSI_OUT(vMSG,inactive); SCSI_OUT(vCD,inactive);SCSI_OUT(vIO,inactive); SCSI_OUT(vBSY,inactive); gpio_mode(BSY, GPIO_INPUT_PU); }
  143. // HDDiamge file
  144. #define HDIMG_ID_POS 2 // Position to embed ID number
  145. #define HDIMG_LUN_POS 3 // Position to embed LUN numbers
  146. #define HDIMG_BLK_POS 5 // Position to embed block size numbers
  147. #define MAX_FILE_PATH 32 // Maximum file name length
  148. // HDD image
  149. typedef struct hddimg_struct
  150. {
  151. FsFile m_file; // File object
  152. uint64_t m_fileSize; // File size
  153. size_t m_blocksize; // SCSI BLOCK size
  154. }HDDIMG;
  155. HDDIMG img[NUM_SCSIID][NUM_SCSILUN]; // Maximum number
  156. uint8_t m_senseKey = 0; // Sense key
  157. volatile bool m_isBusReset = false; // Bus reset
  158. byte scsi_id_mask; // Mask list of responding SCSI IDs
  159. byte m_id; // Currently responding SCSI-ID
  160. byte m_lun; // Logical unit number currently responding
  161. byte m_sts; // Status byte
  162. byte m_msg; // Message bytes
  163. HDDIMG *m_img; // HDD image for current SCSI-ID, LUN
  164. byte m_buf[MAX_BLOCKSIZE+1]; // General purpose buffer + overrun fetch
  165. int m_msc;
  166. bool m_msb[256];
  167. /*
  168. * Data byte to BSRR register setting value and parity table
  169. */
  170. // Parity bit generation
  171. #define PTY(V) (1^((V)^((V)>>1)^((V)>>2)^((V)>>3)^((V)>>4)^((V)>>5)^((V)>>6)^((V)>>7))&1)
  172. // Data byte to BSRR register setting value conversion table
  173. // BSRR[31:24] = DB[7:0]
  174. // BSRR[ 16] = PTY(DB)
  175. // BSRR[15: 8] = ~DB[7:0]
  176. // BSRR[ 0] = ~PTY(DB)
  177. // Set DBP, set REQ = inactive
  178. #define DBP(D) ((((((uint32_t)(D)<<8)|PTY(D))*0x00010001)^0x0000ff01)|BITMASK(vREQ))
  179. #define DBP8(D) DBP(D),DBP(D+1),DBP(D+2),DBP(D+3),DBP(D+4),DBP(D+5),DBP(D+6),DBP(D+7)
  180. #define DBP32(D) DBP8(D),DBP8(D+8),DBP8(D+16),DBP8(D+24)
  181. // BSRR register control value that simultaneously performs DB set, DP set, and REQ = H (inactrive)
  182. static const uint32_t db_bsrr[256]={
  183. DBP32(0x00),DBP32(0x20),DBP32(0x40),DBP32(0x60),
  184. DBP32(0x80),DBP32(0xA0),DBP32(0xC0),DBP32(0xE0)
  185. };
  186. // Parity bit acquisition
  187. #define PARITY(DB) (db_bsrr[DB]&1)
  188. // Macro cleaning
  189. #undef DBP32
  190. #undef DBP8
  191. //#undef DBP
  192. //#undef PTY
  193. #if USE_DB2ID_TABLE
  194. /* DB to SCSI-ID translation table */
  195. static const byte db2scsiid[256]={
  196. 0xff,
  197. 0,
  198. 1,1,
  199. 2,2,2,2,
  200. 3,3,3,3,3,3,3,3,
  201. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  202. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  203. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  204. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  205. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  206. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  207. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  208. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  209. };
  210. #endif
  211. // Log File
  212. #define VERSION "1.0-b"
  213. #define LOG_FILENAME "LOG.txt"
  214. FsFile LOG_FILE;
  215. // SCSI Drive Vendor information
  216. byte SCSI_INFO_BUF[36] = {
  217. 0x00, //device type
  218. 0x00, //RMB = 0
  219. 0x01, //ISO, ECMA, ANSI version
  220. 0x01, //Response data format
  221. 35 - 4, //Additional data length
  222. 0, 0, //Reserve
  223. 0x00, //Support function
  224. 'Q', 'U', 'A', 'N', 'T', 'U', 'M', ' ', // vendor 8
  225. 'F', 'I', 'R', 'E', 'B', 'A', 'L', 'L', '1', ' ', ' ',' ', ' ', ' ', ' ', ' ', // product 16
  226. '1', '.', '0', ' ' // version 4
  227. };
  228. void onFalseInit(void);
  229. void noSDCardFound(void);
  230. void onBusReset(void);
  231. void initFileLog(void);
  232. void finalizeFileLog(void);
  233. /*
  234. * IO read.
  235. */
  236. inline byte readIO(void)
  237. {
  238. // Port input data register
  239. uint32_t ret = GPIOB->regs->IDR;
  240. byte bret = (byte)((~ret)>>8);
  241. #if READ_PARITY_CHECK
  242. if((db_bsrr[bret]^ret)&1)
  243. m_sts |= 0x01; // parity error
  244. #endif
  245. return bret;
  246. }
  247. // If config file exists, read the first three lines and copy the contents.
  248. // File must be well formed or you will get junk in the SCSI Vendor fields.
  249. void readSCSIDeviceConfig() {
  250. FsFile config_file = SD.open("scsi-config.txt", O_RDONLY);
  251. if (!config_file.isOpen()) {
  252. return;
  253. }
  254. char vendor[9];
  255. memset(vendor, 0, sizeof(vendor));
  256. config_file.readBytes(vendor, sizeof(vendor));
  257. LOG_FILE.print("SCSI VENDOR: ");
  258. LOG_FILE.println(vendor);
  259. memcpy(&(SCSI_INFO_BUF[8]), vendor, 8);
  260. char product[17];
  261. memset(product, 0, sizeof(product));
  262. config_file.readBytes(product, sizeof(product));
  263. LOG_FILE.print("SCSI PRODUCT: ");
  264. LOG_FILE.println(product);
  265. memcpy(&(SCSI_INFO_BUF[16]), product, 16);
  266. char version[5];
  267. memset(version, 0, sizeof(version));
  268. config_file.readBytes(version, sizeof(version));
  269. LOG_FILE.print("SCSI VERSION: ");
  270. LOG_FILE.println(version);
  271. memcpy(&(SCSI_INFO_BUF[32]), version, 4);
  272. config_file.close();
  273. }
  274. /*
  275. * Open HDD image file
  276. */
  277. bool hddimageOpen(HDDIMG *h,const char *image_name,int id,int lun,int blocksize)
  278. {
  279. h->m_fileSize = 0;
  280. h->m_blocksize = blocksize;
  281. h->m_file = SD.open(image_name, O_RDWR);
  282. if(h->m_file.isOpen())
  283. {
  284. h->m_fileSize = h->m_file.size();
  285. LOG_FILE.print("Imagefile: ");
  286. LOG_FILE.print(image_name);
  287. if(h->m_fileSize>0)
  288. {
  289. // check blocksize dummy file
  290. LOG_FILE.print(" / ");
  291. LOG_FILE.print(h->m_fileSize);
  292. LOG_FILE.print("bytes / ");
  293. LOG_FILE.print(h->m_fileSize / 1024);
  294. LOG_FILE.print("KiB / ");
  295. LOG_FILE.print(h->m_fileSize / 1024 / 1024);
  296. LOG_FILE.println("MiB");
  297. return true; // File opened
  298. }
  299. else
  300. {
  301. h->m_file.close();
  302. h->m_fileSize = h->m_blocksize = 0; // no file
  303. LOG_FILE.println("FileSizeError");
  304. }
  305. }
  306. return false;
  307. }
  308. /*
  309. * Initialization.
  310. * Initialize the bus and set the PIN orientation
  311. */
  312. void setup()
  313. {
  314. // PA15 / PB3 / PB4 Cannot be used
  315. // JTAG Because it is used for debugging.
  316. // Comment out for Debugging in PlatformIO
  317. disableDebugPorts();
  318. // Serial initialization
  319. #if DEBUG
  320. Serial.begin(9600);
  321. while (!Serial);
  322. #endif
  323. // PIN initialization
  324. gpio_mode(LED, GPIO_OUTPUT_OD);
  325. gpio_write(LED, low);
  326. //GPIO(SCSI BUS)Initialization
  327. //Port setting register (lower)
  328. // GPIOB->regs->CRL |= 0x000000008; // SET INPUT W/ PUPD on PAB-PB0
  329. //Port setting register (upper)
  330. //GPIOB->regs->CRH = 0x88888888; // SET INPUT W/ PUPD on PB15-PB8
  331. // GPIOB->regs->ODR = 0x0000FF00; // SET PULL-UPs on PB15-PB8
  332. // DB and DP are input modes
  333. SCSI_DB_INPUT()
  334. // Input port
  335. gpio_mode(ATN, GPIO_INPUT_PU);
  336. gpio_mode(BSY, GPIO_INPUT_PU);
  337. gpio_mode(ACK, GPIO_INPUT_PU);
  338. gpio_mode(RST, GPIO_INPUT_PU);
  339. gpio_mode(SEL, GPIO_INPUT_PU);
  340. // Output port
  341. gpio_mode(MSG, GPIO_OUTPUT_OD);
  342. gpio_mode(CD, GPIO_OUTPUT_OD);
  343. gpio_mode(REQ, GPIO_OUTPUT_OD);
  344. gpio_mode(IO, GPIO_OUTPUT_OD);
  345. // Turn off the output port
  346. SCSI_TARGET_INACTIVE()
  347. //Occurs when the RST pin state changes from HIGH to LOW
  348. //attachInterrupt(PIN_MAP[RST].gpio_bit, onBusReset, FALLING);
  349. LED_ON();
  350. // clock = 36MHz , about 4Mbytes/sec
  351. if(!SD.begin(SD1_CONFIG)) {
  352. #if DEBUG
  353. Serial.println("SD initialization failed!");
  354. #endif
  355. noSDCardFound();
  356. }
  357. initFileLog();
  358. readSCSIDeviceConfig();
  359. //Sector data overrun byte setting
  360. m_buf[MAX_BLOCKSIZE] = 0xff; // DB0 all off,DBP off
  361. //HD image file open
  362. scsi_id_mask = 0x00;
  363. // Iterate over the root path in the SD card looking for candidate image files.
  364. SdFile root;
  365. root.open("/");
  366. SdFile file;
  367. bool imageReady;
  368. while (1) {
  369. if (!file.openNext(&root, O_READ)) break;
  370. char name[MAX_FILE_PATH+1];
  371. if(!file.isDir()) {
  372. file.getName(name, MAX_FILE_PATH+1);
  373. file.close();
  374. String file_name = String(name);
  375. file_name.toLowerCase();
  376. if(file_name.startsWith("hd") && file_name.endsWith(".hda")) {
  377. int id = name[HDIMG_ID_POS] - '0';
  378. int lun = name[HDIMG_LUN_POS] - '0';
  379. int blk = name[HDIMG_BLK_POS] - '0';
  380. if(blk == 2) {
  381. blk = 256;
  382. } else if(blk == 1) {
  383. blk = 1024;
  384. } else {
  385. blk = 512;
  386. }
  387. if(id < NUM_SCSIID && lun < NUM_SCSILUN) {
  388. HDDIMG *h = &img[id][lun];
  389. imageReady = hddimageOpen(h,name,id,lun,blk);
  390. if(imageReady) { // Marked as a responsive ID
  391. scsi_id_mask |= 1<<id;
  392. }
  393. } else {
  394. LOG_FILE.print("Bad LUN or SCSI id for image: ");
  395. LOG_FILE.println(name);
  396. LOG_FILE.sync();
  397. }
  398. } else {
  399. LOG_FILE.print("Not an image: ");
  400. LOG_FILE.println(name);
  401. LOG_FILE.sync();
  402. }
  403. }
  404. }
  405. root.close();
  406. // Error if there are 0 image files
  407. if(scsi_id_mask==0) {
  408. LOG_FILE.println("ERROR: No valid images found!");
  409. onFalseInit();
  410. }
  411. finalizeFileLog();
  412. LED_OFF();
  413. //Occurs when the RST pin state changes from HIGH to LOW
  414. attachInterrupt(PIN_MAP[RST].gpio_bit, onBusReset, FALLING);
  415. }
  416. /*
  417. * Setup initialization logfile
  418. */
  419. void initFileLog() {
  420. LOG_FILE = SD.open(LOG_FILENAME, O_WRONLY | O_CREAT);
  421. LOG_FILE.println("BlueSCSI <-> SD - https://github.com/erichelgeson/BlueSCSI");
  422. LOG_FILE.print("VERSION: ");
  423. LOG_FILE.println(VERSION);
  424. LOG_FILE.print("DEBUG:");
  425. LOG_FILE.print(DEBUG);
  426. LOG_FILE.print(" SCSI_SELECT:");
  427. LOG_FILE.print(SCSI_SELECT);
  428. LOG_FILE.print(" SDFAT_FILE_TYPE:");
  429. LOG_FILE.println(SDFAT_FILE_TYPE);
  430. LOG_FILE.print("SdFat version: ");
  431. LOG_FILE.println(SD_FAT_VERSION_STR);
  432. LOG_FILE.print("SdFat Max FileName Length: ");
  433. LOG_FILE.println(MAX_FILE_PATH);
  434. LOG_FILE.println("Initialized SD Card - lets go!");
  435. LOG_FILE.sync();
  436. }
  437. /*
  438. * Finalize initialization logfile
  439. */
  440. void finalizeFileLog() {
  441. // View support drive map
  442. LOG_FILE.print("ID");
  443. for(int lun=0;lun<NUM_SCSILUN;lun++)
  444. {
  445. LOG_FILE.print(":LUN");
  446. LOG_FILE.print(lun);
  447. }
  448. LOG_FILE.println(":");
  449. //
  450. for(int id=0;id<NUM_SCSIID;id++)
  451. {
  452. LOG_FILE.print(" ");
  453. LOG_FILE.print(id);
  454. for(int lun=0;lun<NUM_SCSILUN;lun++)
  455. {
  456. HDDIMG *h = &img[id][lun];
  457. if( (lun<NUM_SCSILUN) && (h->m_file))
  458. {
  459. LOG_FILE.print((h->m_blocksize<1000) ? ": " : ":");
  460. LOG_FILE.print(h->m_blocksize);
  461. }
  462. else
  463. LOG_FILE.print(":----");
  464. }
  465. LOG_FILE.println(":");
  466. }
  467. LOG_FILE.println("Finished initialization of SCSI Devices - Entering main loop.");
  468. LOG_FILE.sync();
  469. LOG_FILE.close();
  470. }
  471. /*
  472. * Initialization failed, blink 3x fast
  473. */
  474. void onFalseInit(void)
  475. {
  476. LOG_FILE.sync();
  477. while(true) {
  478. for(int i = 0; i < 3; i++) {
  479. gpio_write(LED, high);
  480. delay(250);
  481. gpio_write(LED, low);
  482. delay(250);
  483. }
  484. delay(3000);
  485. }
  486. }
  487. /*
  488. * No SC Card found, blink 5x fast
  489. */
  490. void noSDCardFound(void)
  491. {
  492. while(true) {
  493. for(int i = 0; i < 5; i++) {
  494. gpio_write(LED, high);
  495. delay(250);
  496. gpio_write(LED, low);
  497. delay(250);
  498. }
  499. delay(3000);
  500. }
  501. }
  502. /*
  503. * Bus reset interrupt.
  504. */
  505. void onBusReset(void)
  506. {
  507. #if SCSI_SELECT == 1
  508. // SASI I / F for X1 turbo has RST pulse write cycle +2 clock ==
  509. // I can't filter because it only activates about 1.25us
  510. {{
  511. #else
  512. if(isHigh(gpio_read(RST))) {
  513. delayMicroseconds(20);
  514. if(isHigh(gpio_read(RST))) {
  515. #endif
  516. // BUS FREE is done in the main process
  517. // gpio_mode(MSG, GPIO_OUTPUT_OD);
  518. // gpio_mode(CD, GPIO_OUTPUT_OD);
  519. // gpio_mode(REQ, GPIO_OUTPUT_OD);
  520. // gpio_mode(IO, GPIO_OUTPUT_OD);
  521. // Should I enter DB and DBP once?
  522. SCSI_DB_INPUT()
  523. LOGN("BusReset!");
  524. m_isBusReset = true;
  525. }
  526. }
  527. }
  528. /*
  529. * Read by handshake.
  530. */
  531. inline byte readHandshake(void)
  532. {
  533. SCSI_OUT(vREQ,active)
  534. //SCSI_DB_INPUT()
  535. while(!SCSI_IN(vACK)) { if(m_isBusReset) return 0; }
  536. byte r = readIO();
  537. SCSI_OUT(vREQ,inactive)
  538. while( SCSI_IN(vACK)) { if(m_isBusReset) return 0; }
  539. return r;
  540. }
  541. /*
  542. * Write with a handshake.
  543. */
  544. inline void writeHandshake(byte d)
  545. {
  546. GPIOB->regs->BSRR = db_bsrr[d]; // setup DB,DBP (160ns)
  547. SCSI_DB_OUTPUT() // (180ns)
  548. // ACK.Fall to DB output delay 100ns(MAX) (DTC-510B)
  549. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  550. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  551. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  552. SCSI_OUT(vREQ,active) // (30ns)
  553. //while(!SCSI_IN(vACK)) { if(m_isBusReset){ SCSI_DB_INPUT() return; }}
  554. while(!m_isBusReset && !SCSI_IN(vACK));
  555. // ACK.Fall to REQ.Raise delay 500ns(typ.) (DTC-510B)
  556. GPIOB->regs->BSRR = DBP(0xff); // DB=0xFF , SCSI_OUT(vREQ,inactive)
  557. // REQ.Raise to DB hold time 0ns
  558. SCSI_DB_INPUT() // (150ns)
  559. while( SCSI_IN(vACK)) { if(m_isBusReset) return; }
  560. }
  561. /*
  562. * Data in phase.
  563. * Send len bytes of data array p.
  564. */
  565. void writeDataPhase(int len, const byte* p)
  566. {
  567. LOGN("DATAIN PHASE");
  568. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  569. SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  570. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  571. for (int i = 0; i < len; i++) {
  572. if(m_isBusReset) {
  573. return;
  574. }
  575. writeHandshake(p[i]);
  576. }
  577. }
  578. /*
  579. * Data in phase.
  580. * Send len block while reading from SD card.
  581. */
  582. void writeDataPhaseSD(uint32_t adds, uint32_t len)
  583. {
  584. LOGN("DATAIN PHASE(SD)");
  585. uint32_t pos = adds * m_img->m_blocksize;
  586. m_img->m_file.seek(pos);
  587. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  588. SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  589. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  590. for(uint32_t i = 0; i < len; i++) {
  591. // Asynchronous reads will make it faster ...
  592. m_img->m_file.read(m_buf, m_img->m_blocksize);
  593. #if READ_SPEED_OPTIMIZE
  594. //#define REQ_ON() SCSI_OUT(vREQ,active)
  595. #define REQ_ON() (*db_dst = BITMASK(vREQ)<<16)
  596. #define FETCH_SRC() (src_byte = *srcptr++)
  597. #define FETCH_BSRR_DB() (bsrr_val = bsrr_tbl[src_byte])
  598. #define REQ_OFF_DB_SET(BSRR_VAL) *db_dst = BSRR_VAL
  599. #define WAIT_ACK_ACTIVE() while(!m_isBusReset && !SCSI_IN(vACK))
  600. #define WAIT_ACK_INACTIVE() do{ if(m_isBusReset) return; }while(SCSI_IN(vACK))
  601. SCSI_DB_OUTPUT()
  602. register byte *srcptr= m_buf; // Source buffer
  603. register byte *endptr= m_buf + m_img->m_blocksize; // End pointer
  604. /*register*/ byte src_byte; // Send data bytes
  605. register const uint32_t *bsrr_tbl = db_bsrr; // Table to convert to BSRR
  606. register uint32_t bsrr_val; // BSRR value to output (DB, DBP, REQ = ACTIVE)
  607. register volatile uint32_t *db_dst = &(GPIOB->regs->BSRR); // Output port
  608. // prefetch & 1st out
  609. FETCH_SRC();
  610. FETCH_BSRR_DB();
  611. REQ_OFF_DB_SET(bsrr_val);
  612. // DB.set to REQ.F setup 100ns max (DTC-510B)
  613. // Maybe there should be some weight here
  614. // WAIT_ACK_INACTIVE();
  615. do{
  616. // 0
  617. REQ_ON();
  618. FETCH_SRC();
  619. FETCH_BSRR_DB();
  620. WAIT_ACK_ACTIVE();
  621. // ACK.F to REQ.R 500ns typ. (DTC-510B)
  622. REQ_OFF_DB_SET(bsrr_val);
  623. WAIT_ACK_INACTIVE();
  624. // 1
  625. REQ_ON();
  626. FETCH_SRC();
  627. FETCH_BSRR_DB();
  628. WAIT_ACK_ACTIVE();
  629. REQ_OFF_DB_SET(bsrr_val);
  630. WAIT_ACK_INACTIVE();
  631. // 2
  632. REQ_ON();
  633. FETCH_SRC();
  634. FETCH_BSRR_DB();
  635. WAIT_ACK_ACTIVE();
  636. REQ_OFF_DB_SET(bsrr_val);
  637. WAIT_ACK_INACTIVE();
  638. // 3
  639. REQ_ON();
  640. FETCH_SRC();
  641. FETCH_BSRR_DB();
  642. WAIT_ACK_ACTIVE();
  643. REQ_OFF_DB_SET(bsrr_val);
  644. WAIT_ACK_INACTIVE();
  645. // 4
  646. REQ_ON();
  647. FETCH_SRC();
  648. FETCH_BSRR_DB();
  649. WAIT_ACK_ACTIVE();
  650. REQ_OFF_DB_SET(bsrr_val);
  651. WAIT_ACK_INACTIVE();
  652. // 5
  653. REQ_ON();
  654. FETCH_SRC();
  655. FETCH_BSRR_DB();
  656. WAIT_ACK_ACTIVE();
  657. REQ_OFF_DB_SET(bsrr_val);
  658. WAIT_ACK_INACTIVE();
  659. // 6
  660. REQ_ON();
  661. FETCH_SRC();
  662. FETCH_BSRR_DB();
  663. WAIT_ACK_ACTIVE();
  664. REQ_OFF_DB_SET(bsrr_val);
  665. WAIT_ACK_INACTIVE();
  666. // 7
  667. REQ_ON();
  668. FETCH_SRC();
  669. FETCH_BSRR_DB();
  670. WAIT_ACK_ACTIVE();
  671. REQ_OFF_DB_SET(bsrr_val);
  672. WAIT_ACK_INACTIVE();
  673. }while(srcptr < endptr);
  674. SCSI_DB_INPUT()
  675. #else
  676. for(int j = 0; j < BLOCKSIZE; j++) {
  677. if(m_isBusReset) {
  678. return;
  679. }
  680. writeHandshake(m_buf[j]);
  681. }
  682. #endif
  683. }
  684. }
  685. /*
  686. * Data out phase.
  687. * len block read
  688. */
  689. void readDataPhase(int len, byte* p)
  690. {
  691. LOGN("DATAOUT PHASE");
  692. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  693. SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  694. SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  695. for(uint32_t i = 0; i < len; i++)
  696. p[i] = readHandshake();
  697. }
  698. /*
  699. * Data out phase.
  700. * Write to SD card while reading len block.
  701. */
  702. void readDataPhaseSD(uint32_t adds, uint32_t len)
  703. {
  704. LOGN("DATAOUT PHASE(SD)");
  705. uint32_t pos = adds * m_img->m_blocksize;
  706. m_img->m_file.seek(pos);
  707. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  708. SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  709. SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  710. for(uint32_t i = 0; i < len; i++) {
  711. #if WRITE_SPEED_OPTIMIZE
  712. register byte *dstptr= m_buf;
  713. register byte *endptr= m_buf + m_img->m_blocksize;
  714. for(dstptr=m_buf;dstptr<endptr;dstptr+=8) {
  715. dstptr[0] = readHandshake();
  716. dstptr[1] = readHandshake();
  717. dstptr[2] = readHandshake();
  718. dstptr[3] = readHandshake();
  719. dstptr[4] = readHandshake();
  720. dstptr[5] = readHandshake();
  721. dstptr[6] = readHandshake();
  722. dstptr[7] = readHandshake();
  723. if(m_isBusReset) {
  724. return;
  725. }
  726. }
  727. #else
  728. for(int j = 0; j < m_img->m_blocksize; j++) {
  729. if(m_isBusReset) {
  730. return;
  731. }
  732. m_buf[j] = readHandshake();
  733. }
  734. #endif
  735. m_img->m_file.write(m_buf, m_img->m_blocksize);
  736. }
  737. m_img->m_file.flush();
  738. }
  739. /*
  740. * INQUIRY command processing.
  741. */
  742. #if SCSI_SELECT == 2
  743. byte onInquiryCommand(byte len)
  744. {
  745. byte buf[36] = {
  746. 0x00, //Device type
  747. 0x00, //RMB = 0
  748. 0x01, //ISO,ECMA,ANSI version
  749. 0x01, //Response data format
  750. 35 - 4, //Additional data length
  751. 0, 0, //Reserve
  752. 0x00, //Support function
  753. 'N', 'E', 'C', 'I', 'T', 'S', 'U', ' ',
  754. 'A', 'r', 'd', 'S', 'C', 'S', 'i', 'n', 'o', ' ', ' ',' ', ' ', ' ', ' ', ' ',
  755. '0', '0', '1', '0',
  756. };
  757. writeDataPhase(len < 36 ? len : 36, buf);
  758. return 0x00;
  759. }
  760. #else
  761. byte onInquiryCommand(byte len)
  762. {
  763. writeDataPhase(len < 36 ? len : 36, SCSI_INFO_BUF);
  764. return 0x00;
  765. }
  766. #endif
  767. /*
  768. * REQUEST SENSE command processing.
  769. */
  770. void onRequestSenseCommand(byte len)
  771. {
  772. byte buf[18] = {
  773. 0x70, //CheckCondition
  774. 0, //Segment number
  775. 0x00, //Sense key
  776. 0, 0, 0, 0, //information
  777. 17 - 7 , //Additional data length
  778. 0,
  779. };
  780. buf[2] = m_senseKey;
  781. m_senseKey = 0;
  782. writeDataPhase(len < 18 ? len : 18, buf);
  783. }
  784. /*
  785. * READ CAPACITY command processing.
  786. */
  787. byte onReadCapacityCommand(byte pmi)
  788. {
  789. if(!m_img) return 0x02; // Image file absent
  790. uint32_t bl = m_img->m_blocksize;
  791. uint32_t bc = m_img->m_fileSize / bl;
  792. uint8_t buf[8] = {
  793. bc >> 24, bc >> 16, bc >> 8, bc,
  794. bl >> 24, bl >> 16, bl >> 8, bl
  795. };
  796. writeDataPhase(8, buf);
  797. return 0x00;
  798. }
  799. /*
  800. * READ6 / 10 Command processing.
  801. */
  802. byte onReadCommand(uint32_t adds, uint32_t len)
  803. {
  804. LOGN("-R");
  805. LOGHEXN(adds);
  806. LOGHEXN(len);
  807. if(!m_img) return 0x02; // Image file absent
  808. gpio_write(LED, high);
  809. writeDataPhaseSD(adds, len);
  810. gpio_write(LED, low);
  811. return 0x00; //sts
  812. }
  813. /*
  814. * WRITE6 / 10 Command processing.
  815. */
  816. byte onWriteCommand(uint32_t adds, uint32_t len)
  817. {
  818. LOGN("-W");
  819. LOGHEXN(adds);
  820. LOGHEXN(len);
  821. if(!m_img) return 0x02; // Image file absent
  822. gpio_write(LED, high);
  823. readDataPhaseSD(adds, len);
  824. gpio_write(LED, low);
  825. return 0; //sts
  826. }
  827. /*
  828. * MODE SENSE command processing.
  829. */
  830. #if SCSI_SELECT == 2
  831. byte onModeSenseCommand(byte dbd, int cmd2, uint32_t len)
  832. {
  833. if(!m_img) return 0x02; // Image file absent
  834. int pageCode = cmd2 & 0x3F;
  835. // Assuming sector size 512, number of sectors 25, number of heads 8 as default settings
  836. int size = m_img->m_fileSize;
  837. int cylinders = (int)(size >> 9);
  838. cylinders >>= 3;
  839. cylinders /= 25;
  840. int sectorsize = 512;
  841. int sectors = 25;
  842. int heads = 8;
  843. // Sector size
  844. int disksize = 0;
  845. for(disksize = 16; disksize > 0; --(disksize)) {
  846. if ((1 << disksize) == sectorsize)
  847. break;
  848. }
  849. // Number of blocks
  850. uint32_t diskblocks = (uint32_t)(size >> disksize);
  851. memset(m_buf, 0, sizeof(m_buf));
  852. int a = 4;
  853. if(dbd == 0) {
  854. uint32_t bl = m_img->m_blocksize;
  855. uint32_t bc = m_img->m_fileSize / bl;
  856. byte c[8] = {
  857. 0,// Density code
  858. bc >> 16, bc >> 8, bc,
  859. 0, //Reserve
  860. bl >> 16, bl >> 8, bl
  861. };
  862. memcpy(&m_buf[4], c, 8);
  863. a += 8;
  864. m_buf[3] = 0x08;
  865. }
  866. switch(pageCode) {
  867. case 0x3F:
  868. {
  869. m_buf[a + 0] = 0x01;
  870. m_buf[a + 1] = 0x06;
  871. a += 8;
  872. }
  873. case 0x03: // drive parameters
  874. {
  875. m_buf[a + 0] = 0x80 | 0x03; // Page code
  876. m_buf[a + 1] = 0x16; // Page length
  877. m_buf[a + 2] = (byte)(heads >> 8);// number of sectors / track
  878. m_buf[a + 3] = (byte)(heads);// number of sectors / track
  879. m_buf[a + 10] = (byte)(sectors >> 8);// number of sectors / track
  880. m_buf[a + 11] = (byte)(sectors);// number of sectors / track
  881. int size = 1 << disksize;
  882. m_buf[a + 12] = (byte)(size >> 8);// number of sectors / track
  883. m_buf[a + 13] = (byte)(size);// number of sectors / track
  884. a += 24;
  885. if(pageCode != 0x3F) {
  886. break;
  887. }
  888. }
  889. case 0x04: // drive parameters
  890. {
  891. LOGN("AddDrive");
  892. m_buf[a + 0] = 0x04; // Page code
  893. m_buf[a + 1] = 0x12; // Page length
  894. m_buf[a + 2] = (cylinders >> 16);// Cylinder length
  895. m_buf[a + 3] = (cylinders >> 8);
  896. m_buf[a + 4] = cylinders;
  897. m_buf[a + 5] = heads; // Number of heads
  898. a += 20;
  899. if(pageCode != 0x3F) {
  900. break;
  901. }
  902. }
  903. default:
  904. break;
  905. }
  906. m_buf[0] = a - 1;
  907. writeDataPhase(len < a ? len : a, m_buf);
  908. return 0x00;
  909. }
  910. #else
  911. byte onModeSenseCommand(byte dbd, int cmd2, uint32_t len)
  912. {
  913. if(!m_img) return 0x02; // No image file
  914. memset(m_buf, 0, sizeof(m_buf));
  915. int pageCode = cmd2 & 0x3F;
  916. int a = 4;
  917. if(dbd == 0) {
  918. uint32_t bl = m_img->m_blocksize;
  919. uint32_t bc = m_img->m_fileSize / bl;
  920. byte c[8] = {
  921. 0,//Density code
  922. bc >> 16, bc >> 8, bc,
  923. 0, //Reserve
  924. bl >> 16, bl >> 8, bl
  925. };
  926. memcpy(&m_buf[4], c, 8);
  927. a += 8;
  928. m_buf[3] = 0x08;
  929. }
  930. switch(pageCode) {
  931. case 0x3F:
  932. case 0x03: //Drive parameters
  933. m_buf[a + 0] = 0x03; //Page code
  934. m_buf[a + 1] = 0x16; // Page length
  935. m_buf[a + 11] = 0x3F;//Number of sectors / track
  936. a += 24;
  937. if(pageCode != 0x3F) {
  938. break;
  939. }
  940. case 0x04: //Drive parameters
  941. {
  942. uint32_t bc = m_img->m_fileSize / m_img->m_file;
  943. m_buf[a + 0] = 0x04; //Page code
  944. m_buf[a + 1] = 0x16; // Page length
  945. m_buf[a + 2] = bc >> 16;// Cylinder length
  946. m_buf[a + 3] = bc >> 8;
  947. m_buf[a + 4] = bc;
  948. m_buf[a + 5] = 1; //Number of heads
  949. a += 24;
  950. }
  951. if(pageCode != 0x3F) {
  952. break;
  953. }
  954. default:
  955. break;
  956. }
  957. m_buf[0] = a - 1;
  958. writeDataPhase(len < a ? len : a, m_buf);
  959. return 0x00;
  960. }
  961. #endif
  962. #if SCSI_SELECT == 1
  963. /*
  964. * dtc510b_setDriveparameter
  965. */
  966. #define PACKED __attribute__((packed))
  967. typedef struct PACKED dtc500_cmd_c2_param_struct
  968. {
  969. uint8_t StepPlusWidth; // Default is 13.6usec (11)
  970. uint8_t StepPeriod; // Default is 3 msec.(60)
  971. uint8_t StepMode; // Default is Bufferd (0)
  972. uint8_t MaximumHeadAdress; // Default is 4 heads (3)
  973. uint8_t HighCylinderAddressByte; // Default set to 0 (0)
  974. uint8_t LowCylinderAddressByte; // Default is 153 cylinders (152)
  975. uint8_t ReduceWrietCurrent; // Default is above Cylinder 128 (127)
  976. uint8_t DriveType_SeekCompleteOption;// (0)
  977. uint8_t Reserved8; // (0)
  978. uint8_t Reserved9; // (0)
  979. } DTC510_CMD_C2_PARAM;
  980. static void logStrHex(const char *msg,uint32_t num)
  981. {
  982. LOG(msg);
  983. LOGHEXN(num);
  984. }
  985. static byte dtc510b_setDriveparameter(void)
  986. {
  987. DTC510_CMD_C2_PARAM DriveParameter;
  988. uint16_t maxCylinder;
  989. uint16_t numLAD;
  990. //uint32_t stepPulseUsec;
  991. int StepPeriodMsec;
  992. // receive paramter
  993. writeDataPhase(sizeof(DriveParameter),(byte *)(&DriveParameter));
  994. maxCylinder =
  995. (((uint16_t)DriveParameter.HighCylinderAddressByte)<<8) |
  996. (DriveParameter.LowCylinderAddressByte);
  997. numLAD = maxCylinder * (DriveParameter.MaximumHeadAdress+1);
  998. //stepPulseUsec = calcStepPulseUsec(DriveParameter.StepPlusWidth);
  999. StepPeriodMsec = DriveParameter.StepPeriod*50;
  1000. logStrHex (" StepPlusWidth : ",DriveParameter.StepPlusWidth);
  1001. logStrHex (" StepPeriod : ",DriveParameter.StepPeriod );
  1002. logStrHex (" StepMode : ",DriveParameter.StepMode );
  1003. logStrHex (" MaximumHeadAdress : ",DriveParameter.MaximumHeadAdress);
  1004. logStrHex (" CylinderAddress : ",maxCylinder);
  1005. logStrHex (" ReduceWrietCurrent : ",DriveParameter.ReduceWrietCurrent);
  1006. logStrHex (" DriveType/SeekCompleteOption : ",DriveParameter.DriveType_SeekCompleteOption);
  1007. logStrHex (" Maximum LAD : ",numLAD-1);
  1008. return 0; // error result
  1009. }
  1010. #endif
  1011. /*
  1012. * MsgIn2.
  1013. */
  1014. void MsgIn2(int msg)
  1015. {
  1016. LOGN("MsgIn2");
  1017. SCSI_OUT(vMSG, active) // gpio_write(MSG, high);
  1018. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  1019. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  1020. writeHandshake(msg);
  1021. }
  1022. /*
  1023. * MsgOut2.
  1024. */
  1025. void MsgOut2()
  1026. {
  1027. LOGN("MsgOut2");
  1028. SCSI_OUT(vMSG, active) // gpio_write(MSG, high);
  1029. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  1030. SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  1031. m_msb[m_msc] = readHandshake();
  1032. m_msc++;
  1033. m_msc %= 256;
  1034. }
  1035. /*
  1036. * Main loop.
  1037. */
  1038. void loop()
  1039. {
  1040. //int msg = 0;
  1041. m_msg = 0;
  1042. // Wait until RST = H, BSY = H, SEL = L
  1043. do {} while( SCSI_IN(vBSY) || !SCSI_IN(vSEL) || SCSI_IN(vRST));
  1044. // BSY+ SEL-
  1045. // If the ID to respond is not driven, wait for the next
  1046. //byte db = readIO();
  1047. //byte scsiid = db & scsi_id_mask;
  1048. byte scsiid = readIO() & scsi_id_mask;
  1049. if((scsiid) == 0) {
  1050. return;
  1051. }
  1052. LOGN("Selection");
  1053. m_isBusReset = false;
  1054. // Set BSY to-when selected
  1055. SCSI_BSY_ACTIVE(); // Turn only BSY output ON, ACTIVE
  1056. // Ask for a TARGET-ID to respond
  1057. #if USE_DB2ID_TABLE
  1058. m_id = db2scsiid[scsiid];
  1059. //if(m_id==0xff) return;
  1060. #else
  1061. for(m_id=7;m_id>=0;m_id--)
  1062. if(scsiid & (1<<m_id)) break;
  1063. //if(m_id<0) return;
  1064. #endif
  1065. // Wait until SEL becomes inactive
  1066. while(isHigh(gpio_read(SEL))) {
  1067. if(m_isBusReset) {
  1068. goto BusFree;
  1069. }
  1070. }
  1071. SCSI_TARGET_ACTIVE() // (BSY), REQ, MSG, CD, IO output turned on
  1072. //
  1073. if(isHigh(gpio_read(ATN))) {
  1074. bool syncenable = false;
  1075. int syncperiod = 50;
  1076. int syncoffset = 0;
  1077. m_msc = 0;
  1078. memset(m_msb, 0x00, sizeof(m_msb));
  1079. while(isHigh(gpio_read(ATN))) {
  1080. MsgOut2();
  1081. }
  1082. for(int i = 0; i < m_msc; i++) {
  1083. // ABORT
  1084. if (m_msb[i] == 0x06) {
  1085. goto BusFree;
  1086. }
  1087. // BUS DEVICE RESET
  1088. if (m_msb[i] == 0x0C) {
  1089. syncoffset = 0;
  1090. goto BusFree;
  1091. }
  1092. // IDENTIFY
  1093. if (m_msb[i] >= 0x80) {
  1094. }
  1095. // Extended message
  1096. if (m_msb[i] == 0x01) {
  1097. // Check only when synchronous transfer is possible
  1098. if (!syncenable || m_msb[i + 2] != 0x01) {
  1099. MsgIn2(0x07);
  1100. break;
  1101. }
  1102. // Transfer period factor(50 x 4 = Limited to 200ns)
  1103. syncperiod = m_msb[i + 3];
  1104. if (syncperiod > 50) {
  1105. syncoffset = 50;
  1106. }
  1107. // REQ/ACK offset(Limited to 16)
  1108. syncoffset = m_msb[i + 4];
  1109. if (syncoffset > 16) {
  1110. syncoffset = 16;
  1111. }
  1112. // STDR response message generation
  1113. MsgIn2(0x01);
  1114. MsgIn2(0x03);
  1115. MsgIn2(0x01);
  1116. MsgIn2(syncperiod);
  1117. MsgIn2(syncoffset);
  1118. break;
  1119. }
  1120. }
  1121. }
  1122. LOG("Command:");
  1123. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  1124. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  1125. SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  1126. int len;
  1127. byte cmd[12];
  1128. cmd[0] = readHandshake(); if(m_isBusReset) goto BusFree;
  1129. LOGHEX(cmd[0]);
  1130. // Command length selection, reception
  1131. static const int cmd_class_len[8]={6,10,10,6,6,12,6,6};
  1132. len = cmd_class_len[cmd[0] >> 5];
  1133. cmd[1] = readHandshake(); LOG(":");LOGHEX(cmd[1]); if(m_isBusReset) goto BusFree;
  1134. cmd[2] = readHandshake(); LOG(":");LOGHEX(cmd[2]); if(m_isBusReset) goto BusFree;
  1135. cmd[3] = readHandshake(); LOG(":");LOGHEX(cmd[3]); if(m_isBusReset) goto BusFree;
  1136. cmd[4] = readHandshake(); LOG(":");LOGHEX(cmd[4]); if(m_isBusReset) goto BusFree;
  1137. cmd[5] = readHandshake(); LOG(":");LOGHEX(cmd[5]); if(m_isBusReset) goto BusFree;
  1138. // Receive the remaining commands
  1139. for(int i = 6; i < len; i++ ) {
  1140. cmd[i] = readHandshake();
  1141. LOG(":");
  1142. LOGHEX(cmd[i]);
  1143. if(m_isBusReset) goto BusFree;
  1144. }
  1145. // LUN confirmation
  1146. m_lun = m_sts>>5;
  1147. m_sts = cmd[1]&0xe0; // Preset LUN in status byte
  1148. // HDD Image selection
  1149. m_img = (HDDIMG *)0; // None
  1150. if( (m_lun <= NUM_SCSILUN) )
  1151. {
  1152. m_img = &(img[m_id][m_lun]); // There is an image
  1153. if(!(m_img->m_file.isOpen()))
  1154. m_img = (HDDIMG *)0; // Image absent
  1155. }
  1156. // if(!m_img) m_sts |= 0x02; // Missing image file for LUN
  1157. //LOGHEX(((uint32_t)m_img));
  1158. LOG(":ID ");
  1159. LOG(m_id);
  1160. LOG(":LUN ");
  1161. LOG(m_lun);
  1162. LOGN("");
  1163. switch(cmd[0]) {
  1164. case 0x00:
  1165. LOGN("[Test Unit]");
  1166. break;
  1167. case 0x01:
  1168. LOGN("[Rezero Unit]");
  1169. break;
  1170. case 0x03:
  1171. LOGN("[RequestSense]");
  1172. onRequestSenseCommand(cmd[4]);
  1173. break;
  1174. case 0x04:
  1175. LOGN("[FormatUnit]");
  1176. break;
  1177. case 0x06:
  1178. LOGN("[FormatUnit]");
  1179. break;
  1180. case 0x07:
  1181. LOGN("[ReassignBlocks]");
  1182. break;
  1183. case 0x08:
  1184. LOGN("[Read6]");
  1185. m_sts |= onReadCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
  1186. break;
  1187. case 0x0A:
  1188. LOGN("[Write6]");
  1189. m_sts |= onWriteCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
  1190. break;
  1191. case 0x0B:
  1192. LOGN("[Seek6]");
  1193. break;
  1194. case 0x12:
  1195. LOGN("[Inquiry]");
  1196. m_sts |= onInquiryCommand(cmd[4]);
  1197. break;
  1198. case 0x1A:
  1199. LOGN("[ModeSense6]");
  1200. m_sts |= onModeSenseCommand(cmd[1]&0x80, cmd[2], cmd[4]);
  1201. break;
  1202. case 0x1B:
  1203. LOGN("[StartStopUnit]");
  1204. break;
  1205. case 0x1E:
  1206. LOGN("[PreAllowMed.Removal]");
  1207. break;
  1208. case 0x25:
  1209. LOGN("[ReadCapacity]");
  1210. m_sts |= onReadCapacityCommand(cmd[8]);
  1211. break;
  1212. case 0x28:
  1213. LOGN("[Read10]");
  1214. m_sts |= onReadCommand(((uint32_t)cmd[2] << 24) | ((uint32_t)cmd[3] << 16) | ((uint32_t)cmd[4] << 8) | cmd[5], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1215. break;
  1216. case 0x2A:
  1217. LOGN("[Write10]");
  1218. m_sts |= onWriteCommand(((uint32_t)cmd[2] << 24) | ((uint32_t)cmd[3] << 16) | ((uint32_t)cmd[4] << 8) | cmd[5], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1219. break;
  1220. case 0x2B:
  1221. LOGN("[Seek10]");
  1222. break;
  1223. case 0x5A:
  1224. LOGN("[ModeSense10]");
  1225. onModeSenseCommand(cmd[1] & 0x80, cmd[2], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1226. break;
  1227. #if SCSI_SELECT == 1
  1228. case 0xc2:
  1229. LOGN("[DTC510B setDriveParameter]");
  1230. m_sts |= dtc510b_setDriveparameter();
  1231. break;
  1232. #endif
  1233. default:
  1234. LOGN("[*Unknown]");
  1235. m_sts |= 0x02;
  1236. m_senseKey = 5;
  1237. break;
  1238. }
  1239. if(m_isBusReset) {
  1240. goto BusFree;
  1241. }
  1242. LOGN("Sts");
  1243. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  1244. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  1245. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  1246. writeHandshake(m_sts);
  1247. if(m_isBusReset) {
  1248. goto BusFree;
  1249. }
  1250. LOGN("MsgIn");
  1251. SCSI_OUT(vMSG, active) // gpio_write(MSG, high);
  1252. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  1253. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  1254. writeHandshake(m_msg);
  1255. BusFree:
  1256. LOGN("BusFree");
  1257. m_isBusReset = false;
  1258. //SCSI_OUT(vREQ,inactive) // gpio_write(REQ, low);
  1259. //SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  1260. //SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  1261. //SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  1262. //SCSI_OUT(vBSY,inactive)
  1263. SCSI_TARGET_INACTIVE() // Turn off BSY, REQ, MSG, CD, IO output
  1264. }