BlueSCSI.cpp 39 KB

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