ArdSCSinoV2.ino 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  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, SHARED_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_FILE_256 "HDxx_256.HDS" // BLOCKSIZE=256 のHDDイメージファイル
  145. #define HDIMG_FILE_512 "HDxx_512.HDS" // BLOCKSIZE=512 のHDDイメージファイル名ベース
  146. #define HDIMG_FILE_1024 "HDxx_1024.HDS" // BLOCKSIZE=1024 のHDDイメージファイル
  147. #define HDIMG_ID_POS 2 // ID数字を埋め込む位置
  148. #define HDIMG_LUN_POS 3 // LUN数字を埋め込む位置
  149. #define MAX_FILE_PATH 32 // 最大ファイル名長
  150. // HDD image
  151. typedef struct hddimg_struct
  152. {
  153. FsFile m_file; // File object
  154. uint64_t m_fileSize; // File size
  155. size_t m_blocksize; // SCSI BLOCK size
  156. }HDDIMG;
  157. HDDIMG img[NUM_SCSIID][NUM_SCSILUN]; // Maximum number
  158. uint8_t m_senseKey = 0; // Sense key
  159. volatile bool m_isBusReset = false; // Bus reset
  160. byte scsi_id_mask; // Mask list of responding SCSI IDs
  161. byte m_id; // Currently responding SCSI-ID
  162. byte m_lun; // Logical unit number currently responding
  163. byte m_sts; // Status byte
  164. byte m_msg; // Message bytes
  165. HDDIMG *m_img; // HDD image for current SCSI-ID, LUN
  166. byte m_buf[MAX_BLOCKSIZE+1]; // General purpose buffer + overrun fetch
  167. int m_msc;
  168. bool m_msb[256];
  169. /*
  170. * Data byte to BSRR register setting value and parity table
  171. */
  172. // Parity bit generation
  173. #define PTY(V) (1^((V)^((V)>>1)^((V)>>2)^((V)>>3)^((V)>>4)^((V)>>5)^((V)>>6)^((V)>>7))&1)
  174. // Data byte to BSRR register setting value conversion table
  175. // BSRR[31:24] = DB[7:0]
  176. // BSRR[ 16] = PTY(DB)
  177. // BSRR[15: 8] = ~DB[7:0]
  178. // BSRR[ 0] = ~PTY(DB)
  179. // Set DBP, set REQ = inactive
  180. #define DBP(D) ((((((uint32_t)(D)<<8)|PTY(D))*0x00010001)^0x0000ff01)|BITMASK(vREQ))
  181. #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)
  182. #define DBP32(D) DBP8(D),DBP8(D+8),DBP8(D+16),DBP8(D+24)
  183. // BSRR register control value that simultaneously performs DB set, DP set, and REQ = H (inactrive)
  184. static const uint32_t db_bsrr[256]={
  185. DBP32(0x00),DBP32(0x20),DBP32(0x40),DBP32(0x60),
  186. DBP32(0x80),DBP32(0xA0),DBP32(0xC0),DBP32(0xE0)
  187. };
  188. // Parity bit acquisition
  189. #define PARITY(DB) (db_bsrr[DB]&1)
  190. // Macro cleaning
  191. #undef DBP32
  192. #undef DBP8
  193. //#undef DBP
  194. //#undef PTY
  195. #if USE_DB2ID_TABLE
  196. /* DB to SCSI-ID translation table */
  197. static const byte db2scsiid[256]={
  198. 0xff,
  199. 0,
  200. 1,1,
  201. 2,2,2,2,
  202. 3,3,3,3,3,3,3,3,
  203. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  204. 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,
  205. 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,
  206. 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,
  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. 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,
  210. 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
  211. };
  212. #endif
  213. void onFalseInit(void);
  214. void onBusReset(void);
  215. /*
  216. * IO read.
  217. */
  218. inline byte readIO(void)
  219. {
  220. // Port input data register
  221. uint32_t ret = GPIOB->regs->IDR;
  222. byte bret = (byte)((~ret)>>8);
  223. #if READ_PARITY_CHECK
  224. if((db_bsrr[bret]^ret)&1)
  225. m_sts |= 0x01; // parity error
  226. #endif
  227. return bret;
  228. }
  229. /*
  230. * HDDイメージファイルのオープン
  231. */
  232. bool hddimageOpen(HDDIMG *h,const char *image_name,int id,int lun,int blocksize)
  233. {
  234. char file_path[MAX_FILE_PATH+1];
  235. // build file path
  236. strcpy(file_path,image_name);
  237. file_path[HDIMG_ID_POS ] = '0'+id;
  238. file_path[HDIMG_LUN_POS] = '0'+lun;
  239. h->m_fileSize = 0;
  240. h->m_blocksize = blocksize;
  241. h->m_file = SD.open(file_path, O_RDWR);
  242. if(h->m_file.isOpen())
  243. {
  244. h->m_fileSize = h->m_file.size();
  245. #if DEBUG
  246. Serial.print("Imagefile:");
  247. Serial.print(h->m_file.name() );
  248. #endif
  249. if(h->m_fileSize>0)
  250. {
  251. // check blocksize dummy file
  252. #if DEBUG
  253. Serial.print(" / ");
  254. Serial.print(h->m_fileSize);
  255. Serial.print("bytes / ");
  256. Serial.print(h->m_fileSize / 1024);
  257. Serial.print("KiB / ");
  258. Serial.print(h->m_fileSize / 1024 / 1024);
  259. Serial.println("MiB");
  260. #endif
  261. return true; // ファイルが開けた
  262. }
  263. else
  264. {
  265. h->m_file.close();
  266. h->m_fileSize = h->m_blocksize = 0; // no file
  267. #if DEBUG
  268. Serial.println("FileSizeError");
  269. #endif
  270. }
  271. }
  272. return false;
  273. }
  274. /*
  275. * Initialization.
  276. * Initialize the bus and set the PIN orientation
  277. */
  278. void setup()
  279. {
  280. // PA15 / PB3 / PB4 Cannot be used
  281. // JTAG Because it is used for debugging.
  282. disableDebugPorts();
  283. // Serial initialization
  284. #if DEBUG
  285. Serial.begin(9600);
  286. while (!Serial);
  287. #endif
  288. // PIN initialization
  289. gpio_mode(LED, GPIO_OUTPUT_OD);
  290. gpio_write(LED, low);
  291. //GPIO(SCSI BUS)Initialization
  292. //Port setting register (lower)
  293. // GPIOB->regs->CRL |= 0x000000008; // SET INPUT W/ PUPD on PAB-PB0
  294. //Port setting register (upper)
  295. //GPIOB->regs->CRH = 0x88888888; // SET INPUT W/ PUPD on PB15-PB8
  296. // GPIOB->regs->ODR = 0x0000FF00; // SET PULL-UPs on PB15-PB8
  297. // DB,DPは入力モード
  298. SCSI_DB_INPUT()
  299. // Input port
  300. gpio_mode(ATN, GPIO_INPUT_PU);
  301. gpio_mode(BSY, GPIO_INPUT_PU);
  302. gpio_mode(ACK, GPIO_INPUT_PU);
  303. gpio_mode(RST, GPIO_INPUT_PU);
  304. gpio_mode(SEL, GPIO_INPUT_PU);
  305. // Output port
  306. gpio_mode(MSG, GPIO_OUTPUT_OD);
  307. gpio_mode(CD, GPIO_OUTPUT_OD);
  308. gpio_mode(REQ, GPIO_OUTPUT_OD);
  309. gpio_mode(IO, GPIO_OUTPUT_OD);
  310. // Turn off the output port
  311. SCSI_TARGET_INACTIVE()
  312. //RSTピンの状態がHIGHからLOWに変わったときに発生
  313. //attachInterrupt(PIN_MAP[RST].gpio_bit, onBusReset, FALLING);
  314. LED_ON();
  315. // clock = 36MHz , about 4Mbytes/sec
  316. if(!SD.begin(SD1_CONFIG)) {
  317. #if DEBUG
  318. Serial.println("SD initialization failed!");
  319. #endif
  320. onFalseInit();
  321. }
  322. //Sector data overrun byte setting
  323. m_buf[MAX_BLOCKSIZE] = 0xff; // DB0 all off,DBP off
  324. //HD image file open
  325. scsi_id_mask = 0x00;
  326. for(int id=0;id<NUM_SCSIID;id++)
  327. {
  328. for(int lun=0;lun<NUM_SCSILUN;lun++)
  329. {
  330. HDDIMG *h = &img[id][lun];
  331. bool imageReady = false;
  332. if(!imageReady)
  333. {
  334. imageReady = hddimageOpen(h,HDIMG_FILE_256,id,lun,256);
  335. }
  336. if(!imageReady)
  337. {
  338. imageReady = hddimageOpen(h,HDIMG_FILE_512,id,lun,512);
  339. }
  340. if(!imageReady)
  341. {
  342. imageReady = hddimageOpen(h,HDIMG_FILE_1024,id,lun, 1024);
  343. }
  344. if(imageReady)
  345. {
  346. // Marked as a responsive ID
  347. scsi_id_mask |= 1<<id;
  348. //totalImage++;
  349. }
  350. }
  351. }
  352. // Error if there are 0 image files
  353. if(scsi_id_mask==0) onFalseInit();
  354. // View support drive map
  355. #if DEBUG
  356. Serial.print("ID");
  357. for(int lun=0;lun<NUM_SCSILUN;lun++)
  358. {
  359. Serial.print(":LUN");
  360. Serial.print(lun);
  361. }
  362. Serial.println(":");
  363. //
  364. for(int id=0;id<NUM_SCSIID;id++)
  365. {
  366. Serial.print(" ");
  367. Serial.print(id);
  368. for(int lun=0;lun<NUM_SCSILUN;lun++)
  369. {
  370. HDDIMG *h = &img[id][lun];
  371. if( (lun<NUM_SCSILUN) && (h->m_file))
  372. {
  373. Serial.print((h->m_blocksize<1000) ? ": " : ":");
  374. Serial.print(h->m_blocksize);
  375. }
  376. else
  377. Serial.print(":----");
  378. }
  379. Serial.println(":");
  380. }
  381. #endif
  382. LED_OFF();
  383. //Occurs when the RST pin state changes from HIGH to LOW
  384. attachInterrupt(PIN_MAP[RST].gpio_bit, onBusReset, FALLING);
  385. }
  386. /*
  387. * Initialization failed.
  388. */
  389. void onFalseInit(void)
  390. {
  391. while(true) {
  392. gpio_write(LED, high);
  393. delay(500);
  394. gpio_write(LED, low);
  395. delay(500);
  396. }
  397. }
  398. /*
  399. * Bus reset interrupt.
  400. */
  401. void onBusReset(void)
  402. {
  403. #if SCSI_SELECT == 1
  404. // SASI I / F for X1 turbo has RST pulse write cycle +2 clock ==
  405. // I can't filter because it only activates about 1.25us
  406. {{
  407. #else
  408. if(isHigh(gpio_read(RST))) {
  409. delayMicroseconds(20);
  410. if(isHigh(gpio_read(RST))) {
  411. #endif
  412. // BUS FREE is done in the main process
  413. // gpio_mode(MSG, GPIO_OUTPUT_OD);
  414. // gpio_mode(CD, GPIO_OUTPUT_OD);
  415. // gpio_mode(REQ, GPIO_OUTPUT_OD);
  416. // gpio_mode(IO, GPIO_OUTPUT_OD);
  417. // Should I enter DB and DBP once?
  418. SCSI_DB_INPUT()
  419. LOGN("BusReset!");
  420. m_isBusReset = true;
  421. }
  422. }
  423. }
  424. /*
  425. * Read by handshake.
  426. */
  427. inline byte readHandshake(void)
  428. {
  429. SCSI_OUT(vREQ,active)
  430. //SCSI_DB_INPUT()
  431. while(!SCSI_IN(vACK)) { if(m_isBusReset) return 0; }
  432. byte r = readIO();
  433. SCSI_OUT(vREQ,inactive)
  434. while( SCSI_IN(vACK)) { if(m_isBusReset) return 0; }
  435. return r;
  436. }
  437. /*
  438. * Write with a handshake.
  439. */
  440. inline void writeHandshake(byte d)
  441. {
  442. GPIOB->regs->BSRR = db_bsrr[d]; // setup DB,DBP (160ns)
  443. SCSI_DB_OUTPUT() // (180ns)
  444. // ACK.Fall to DB output delay 100ns(MAX) (DTC-510B)
  445. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  446. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  447. SCSI_OUT(vREQ,inactive) // setup wait (30ns)
  448. SCSI_OUT(vREQ,active) // (30ns)
  449. //while(!SCSI_IN(vACK)) { if(m_isBusReset){ SCSI_DB_INPUT() return; }}
  450. while(!m_isBusReset && !SCSI_IN(vACK));
  451. // ACK.Fall to REQ.Raise delay 500ns(typ.) (DTC-510B)
  452. GPIOB->regs->BSRR = DBP(0xff); // DB=0xFF , SCSI_OUT(vREQ,inactive)
  453. // REQ.Raise to DB hold time 0ns
  454. SCSI_DB_INPUT() // (150ns)
  455. while( SCSI_IN(vACK)) { if(m_isBusReset) return; }
  456. }
  457. /*
  458. * Data in phase.
  459. * Send len bytes of data array p.
  460. */
  461. void writeDataPhase(int len, const byte* p)
  462. {
  463. LOGN("DATAIN PHASE");
  464. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  465. SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  466. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  467. for (int i = 0; i < len; i++) {
  468. if(m_isBusReset) {
  469. return;
  470. }
  471. writeHandshake(p[i]);
  472. }
  473. }
  474. /*
  475. * Data in phase.
  476. * Send len block while reading from SD card.
  477. */
  478. void writeDataPhaseSD(uint32_t adds, uint32_t len)
  479. {
  480. LOGN("DATAIN PHASE(SD)");
  481. uint32_t pos = adds * m_img->m_blocksize;
  482. m_img->m_file.seek(pos);
  483. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  484. SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  485. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  486. for(uint32_t i = 0; i < len; i++) {
  487. // Asynchronous reads will make it faster ...
  488. m_img->m_file.read(m_buf, m_img->m_blocksize);
  489. #if READ_SPEED_OPTIMIZE
  490. //#define REQ_ON() SCSI_OUT(vREQ,active)
  491. #define REQ_ON() (*db_dst = BITMASK(vREQ)<<16)
  492. #define FETCH_SRC() (src_byte = *srcptr++)
  493. #define FETCH_BSRR_DB() (bsrr_val = bsrr_tbl[src_byte])
  494. #define REQ_OFF_DB_SET(BSRR_VAL) *db_dst = BSRR_VAL
  495. #define WAIT_ACK_ACTIVE() while(!m_isBusReset && !SCSI_IN(vACK))
  496. #define WAIT_ACK_INACTIVE() do{ if(m_isBusReset) return; }while(SCSI_IN(vACK))
  497. SCSI_DB_OUTPUT()
  498. register byte *srcptr= m_buf; // Source buffer
  499. register byte *endptr= m_buf + m_img->m_blocksize; // 終了ポインタ
  500. /*register*/ byte src_byte; // Send data bytes
  501. register const uint32_t *bsrr_tbl = db_bsrr; // Table to convert to BSRR
  502. register uint32_t bsrr_val; // BSRR value to output (DB, DBP, REQ = ACTIVE)
  503. register volatile uint32_t *db_dst = &(GPIOB->regs->BSRR); // Output port
  504. // prefetch & 1st out
  505. FETCH_SRC();
  506. FETCH_BSRR_DB();
  507. REQ_OFF_DB_SET(bsrr_val);
  508. // DB.set to REQ.F setup 100ns max (DTC-510B)
  509. // Maybe there should be some weight here
  510. // WAIT_ACK_INACTIVE();
  511. do{
  512. // 0
  513. REQ_ON();
  514. FETCH_SRC();
  515. FETCH_BSRR_DB();
  516. WAIT_ACK_ACTIVE();
  517. // ACK.F to REQ.R 500ns typ. (DTC-510B)
  518. REQ_OFF_DB_SET(bsrr_val);
  519. WAIT_ACK_INACTIVE();
  520. // 1
  521. REQ_ON();
  522. FETCH_SRC();
  523. FETCH_BSRR_DB();
  524. WAIT_ACK_ACTIVE();
  525. REQ_OFF_DB_SET(bsrr_val);
  526. WAIT_ACK_INACTIVE();
  527. // 2
  528. REQ_ON();
  529. FETCH_SRC();
  530. FETCH_BSRR_DB();
  531. WAIT_ACK_ACTIVE();
  532. REQ_OFF_DB_SET(bsrr_val);
  533. WAIT_ACK_INACTIVE();
  534. // 3
  535. REQ_ON();
  536. FETCH_SRC();
  537. FETCH_BSRR_DB();
  538. WAIT_ACK_ACTIVE();
  539. REQ_OFF_DB_SET(bsrr_val);
  540. WAIT_ACK_INACTIVE();
  541. // 4
  542. REQ_ON();
  543. FETCH_SRC();
  544. FETCH_BSRR_DB();
  545. WAIT_ACK_ACTIVE();
  546. REQ_OFF_DB_SET(bsrr_val);
  547. WAIT_ACK_INACTIVE();
  548. // 5
  549. REQ_ON();
  550. FETCH_SRC();
  551. FETCH_BSRR_DB();
  552. WAIT_ACK_ACTIVE();
  553. REQ_OFF_DB_SET(bsrr_val);
  554. WAIT_ACK_INACTIVE();
  555. // 6
  556. REQ_ON();
  557. FETCH_SRC();
  558. FETCH_BSRR_DB();
  559. WAIT_ACK_ACTIVE();
  560. REQ_OFF_DB_SET(bsrr_val);
  561. WAIT_ACK_INACTIVE();
  562. // 7
  563. REQ_ON();
  564. FETCH_SRC();
  565. FETCH_BSRR_DB();
  566. WAIT_ACK_ACTIVE();
  567. REQ_OFF_DB_SET(bsrr_val);
  568. WAIT_ACK_INACTIVE();
  569. }while(srcptr < endptr);
  570. SCSI_DB_INPUT()
  571. #else
  572. for(int j = 0; j < BLOCKSIZE; j++) {
  573. if(m_isBusReset) {
  574. return;
  575. }
  576. writeHandshake(m_buf[j]);
  577. }
  578. #endif
  579. }
  580. }
  581. /*
  582. * Data out phase.
  583. * len block read
  584. */
  585. void readDataPhase(int len, byte* p)
  586. {
  587. LOGN("DATAOUT PHASE");
  588. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  589. SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  590. SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  591. for(uint32_t i = 0; i < len; i++)
  592. p[i] = readHandshake();
  593. }
  594. /*
  595. * Data out phase.
  596. * Write to SD card while reading len block.
  597. */
  598. void readDataPhaseSD(uint32_t adds, uint32_t len)
  599. {
  600. LOGN("DATAOUT PHASE(SD)");
  601. uint32_t pos = adds * m_img->m_blocksize;
  602. m_img->m_file.seek(pos);
  603. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  604. SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  605. SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  606. for(uint32_t i = 0; i < len; i++) {
  607. #if WRITE_SPEED_OPTIMIZE
  608. register byte *dstptr= m_buf;
  609. register byte *endptr= m_buf + m_img->m_blocksize;
  610. for(dstptr=m_buf;dstptr<endptr;dstptr+=8) {
  611. dstptr[0] = readHandshake();
  612. dstptr[1] = readHandshake();
  613. dstptr[2] = readHandshake();
  614. dstptr[3] = readHandshake();
  615. dstptr[4] = readHandshake();
  616. dstptr[5] = readHandshake();
  617. dstptr[6] = readHandshake();
  618. dstptr[7] = readHandshake();
  619. if(m_isBusReset) {
  620. return;
  621. }
  622. }
  623. #else
  624. for(int j = 0; j < m_img->m_blocksize; j++) {
  625. if(m_isBusReset) {
  626. return;
  627. }
  628. m_buf[j] = readHandshake();
  629. }
  630. #endif
  631. m_img->m_file.write(m_buf, m_img->m_blocksize);
  632. }
  633. m_img->m_file.flush();
  634. }
  635. /*
  636. * INQUIRY command processing.
  637. */
  638. #if SCSI_SELECT == 2
  639. byte onInquiryCommand(byte len)
  640. {
  641. byte buf[36] = {
  642. 0x00, //デバイスタイプ
  643. 0x00, //RMB = 0
  644. 0x01, //ISO,ECMA,ANSIバージョン
  645. 0x01, //レスポンスデータ形式
  646. 35 - 4, //追加データ長
  647. 0, 0, //Reserve
  648. 0x00, //サポート機能
  649. 'N', 'E', 'C', 'I', 'T', 'S', 'U', ' ',
  650. 'A', 'r', 'd', 'S', 'C', 'S', 'i', 'n', 'o', ' ', ' ',' ', ' ', ' ', ' ', ' ',
  651. '0', '0', '1', '0',
  652. };
  653. writeDataPhase(len < 36 ? len : 36, buf);
  654. return 0x00;
  655. }
  656. #else
  657. byte onInquiryCommand(byte len)
  658. {
  659. byte buf[36] = {
  660. 0x00, //device type
  661. 0x00, //RMB = 0
  662. 0x01, //ISO, ECMA, ANSI version
  663. 0x01, //Response data format
  664. 35 - 4, //Additional data length
  665. 0, 0, //Reserve
  666. 0x00, //Support function
  667. 'T', 'N', 'B', ' ', ' ', ' ', ' ', ' ',
  668. 'A', 'r', 'd', 'S', 'C', 'S', 'i', 'n', 'o', ' ', ' ',' ', ' ', ' ', ' ', ' ',
  669. '0', '0', '1', '0',
  670. };
  671. writeDataPhase(len < 36 ? len : 36, buf);
  672. return 0x00;
  673. }
  674. #endif
  675. /*
  676. * REQUEST SENSE command processing.
  677. */
  678. void onRequestSenseCommand(byte len)
  679. {
  680. byte buf[18] = {
  681. 0x70, //CheckCondition
  682. 0, //Segment number
  683. 0x00, //Sense key
  684. 0, 0, 0, 0, //information
  685. 17 - 7 , //Additional data length
  686. 0,
  687. };
  688. buf[2] = m_senseKey;
  689. m_senseKey = 0;
  690. writeDataPhase(len < 18 ? len : 18, buf);
  691. }
  692. /*
  693. * READ CAPACITY command processing.
  694. */
  695. byte onReadCapacityCommand(byte pmi)
  696. {
  697. if(!m_img) return 0x02; // Image file absent
  698. uint32_t bl = m_img->m_blocksize;
  699. uint32_t bc = m_img->m_fileSize / bl;
  700. uint8_t buf[8] = {
  701. bc >> 24, bc >> 16, bc >> 8, bc,
  702. bl >> 24, bl >> 16, bl >> 8, bl
  703. };
  704. writeDataPhase(8, buf);
  705. return 0x00;
  706. }
  707. /*
  708. * READ6 / 10 Command processing.
  709. */
  710. byte onReadCommand(uint32_t adds, uint32_t len)
  711. {
  712. LOGN("-R");
  713. LOGHEXN(adds);
  714. LOGHEXN(len);
  715. if(!m_img) return 0x02; // Image file absent
  716. gpio_write(LED, high);
  717. writeDataPhaseSD(adds, len);
  718. gpio_write(LED, low);
  719. return 0x00; //sts
  720. }
  721. /*
  722. * WRITE6 / 10 Command processing.
  723. */
  724. byte onWriteCommand(uint32_t adds, uint32_t len)
  725. {
  726. LOGN("-W");
  727. LOGHEXN(adds);
  728. LOGHEXN(len);
  729. if(!m_img) return 0x02; // Image file absent
  730. gpio_write(LED, high);
  731. readDataPhaseSD(adds, len);
  732. gpio_write(LED, low);
  733. return 0; //sts
  734. }
  735. /*
  736. * MODE SENSE command processing.
  737. */
  738. #if SCSI_SELECT == 2
  739. byte onModeSenseCommand(byte dbd, int cmd2, uint32_t len)
  740. {
  741. if(!m_img) return 0x02; // Image file absent
  742. int pageCode = cmd2 & 0x3F;
  743. // デフォルト設定としてセクタサイズ512,セクタ数25,ヘッド数8を想定
  744. int size = m_img->m_fileSize;
  745. int cylinders = (int)(size >> 9);
  746. cylinders >>= 3;
  747. cylinders /= 25;
  748. int sectorsize = 512;
  749. int sectors = 25;
  750. int heads = 8;
  751. // セクタサイズ
  752. int disksize = 0;
  753. for(disksize = 16; disksize > 0; --(disksize)) {
  754. if ((1 << disksize) == sectorsize)
  755. break;
  756. }
  757. // ブロック数
  758. uint32_t diskblocks = (uint32_t)(size >> disksize);
  759. memset(m_buf, 0, sizeof(m_buf));
  760. int a = 4;
  761. if(dbd == 0) {
  762. uint32_t bl = m_img->m_blocksize;
  763. uint32_t bc = m_img->m_fileSize / bl;
  764. byte c[8] = {
  765. 0,//デンシティコード
  766. bc >> 16, bc >> 8, bc,
  767. 0, //Reserve
  768. bl >> 16, bl >> 8, bl
  769. };
  770. memcpy(&m_buf[4], c, 8);
  771. a += 8;
  772. m_buf[3] = 0x08;
  773. }
  774. switch(pageCode) {
  775. case 0x3F:
  776. {
  777. m_buf[a + 0] = 0x01;
  778. m_buf[a + 1] = 0x06;
  779. a += 8;
  780. }
  781. case 0x03: //ドライブパラメータ
  782. {
  783. m_buf[a + 0] = 0x80 | 0x03; //ページコード
  784. m_buf[a + 1] = 0x16; // ページ長
  785. m_buf[a + 2] = (byte)(heads >> 8);//セクタ数/トラック
  786. m_buf[a + 3] = (byte)(heads);//セクタ数/トラック
  787. m_buf[a + 10] = (byte)(sectors >> 8);//セクタ数/トラック
  788. m_buf[a + 11] = (byte)(sectors);//セクタ数/トラック
  789. int size = 1 << disksize;
  790. m_buf[a + 12] = (byte)(size >> 8);//セクタ数/トラック
  791. m_buf[a + 13] = (byte)(size);//セクタ数/トラック
  792. a += 24;
  793. if(pageCode != 0x3F) {
  794. break;
  795. }
  796. }
  797. case 0x04: //ドライブパラメータ
  798. {
  799. LOGN("AddDrive");
  800. m_buf[a + 0] = 0x04; //ページコード
  801. m_buf[a + 1] = 0x12; // ページ長
  802. m_buf[a + 2] = (cylinders >> 16);// シリンダ長
  803. m_buf[a + 3] = (cylinders >> 8);
  804. m_buf[a + 4] = cylinders;
  805. m_buf[a + 5] = heads; //ヘッド数
  806. a += 20;
  807. if(pageCode != 0x3F) {
  808. break;
  809. }
  810. }
  811. default:
  812. break;
  813. }
  814. m_buf[0] = a - 1;
  815. writeDataPhase(len < a ? len : a, m_buf);
  816. return 0x00;
  817. }
  818. #else
  819. byte onModeSenseCommand(byte dbd, int cmd2, uint32_t len)
  820. {
  821. if(!m_img) return 0x02; // イメージファイル不在
  822. memset(m_buf, 0, sizeof(m_buf));
  823. int pageCode = cmd2 & 0x3F;
  824. int a = 4;
  825. if(dbd == 0) {
  826. uint32_t bl = m_img->m_blocksize;
  827. uint32_t bc = m_img->m_fileSize / bl;
  828. byte c[8] = {
  829. 0,//Density code
  830. bc >> 16, bc >> 8, bc,
  831. 0, //Reserve
  832. bl >> 16, bl >> 8, bl
  833. };
  834. memcpy(&m_buf[4], c, 8);
  835. a += 8;
  836. m_buf[3] = 0x08;
  837. }
  838. switch(pageCode) {
  839. case 0x3F:
  840. case 0x03: //Drive parameters
  841. m_buf[a + 0] = 0x03; //Page code
  842. m_buf[a + 1] = 0x16; // Page length
  843. m_buf[a + 11] = 0x3F;//Number of sectors / track
  844. a += 24;
  845. if(pageCode != 0x3F) {
  846. break;
  847. }
  848. case 0x04: //Drive parameters
  849. {
  850. uint32_t bc = m_img->m_fileSize / m_img->m_file;
  851. m_buf[a + 0] = 0x04; //Page code
  852. m_buf[a + 1] = 0x16; // Page length
  853. m_buf[a + 2] = bc >> 16;// Cylinder length
  854. m_buf[a + 3] = bc >> 8;
  855. m_buf[a + 4] = bc;
  856. m_buf[a + 5] = 1; //Number of heads
  857. a += 24;
  858. }
  859. if(pageCode != 0x3F) {
  860. break;
  861. }
  862. default:
  863. break;
  864. }
  865. m_buf[0] = a - 1;
  866. writeDataPhase(len < a ? len : a, m_buf);
  867. return 0x00;
  868. }
  869. #endif
  870. #if SCSI_SELECT == 1
  871. /*
  872. * dtc510b_setDriveparameter
  873. */
  874. #define PACKED __attribute__((packed))
  875. typedef struct PACKED dtc500_cmd_c2_param_struct
  876. {
  877. uint8_t StepPlusWidth; // Default is 13.6usec (11)
  878. uint8_t StepPeriod; // Default is 3 msec.(60)
  879. uint8_t StepMode; // Default is Bufferd (0)
  880. uint8_t MaximumHeadAdress; // Default is 4 heads (3)
  881. uint8_t HighCylinderAddressByte; // Default set to 0 (0)
  882. uint8_t LowCylinderAddressByte; // Default is 153 cylinders (152)
  883. uint8_t ReduceWrietCurrent; // Default is above Cylinder 128 (127)
  884. uint8_t DriveType_SeekCompleteOption;// (0)
  885. uint8_t Reserved8; // (0)
  886. uint8_t Reserved9; // (0)
  887. } DTC510_CMD_C2_PARAM;
  888. static void logStrHex(const char *msg,uint32_t num)
  889. {
  890. LOG(msg);
  891. LOGHEXN(num);
  892. }
  893. static byte dtc510b_setDriveparameter(void)
  894. {
  895. DTC510_CMD_C2_PARAM DriveParameter;
  896. uint16_t maxCylinder;
  897. uint16_t numLAD;
  898. //uint32_t stepPulseUsec;
  899. int StepPeriodMsec;
  900. // receive paramter
  901. writeDataPhase(sizeof(DriveParameter),(byte *)(&DriveParameter));
  902. maxCylinder =
  903. (((uint16_t)DriveParameter.HighCylinderAddressByte)<<8) |
  904. (DriveParameter.LowCylinderAddressByte);
  905. numLAD = maxCylinder * (DriveParameter.MaximumHeadAdress+1);
  906. //stepPulseUsec = calcStepPulseUsec(DriveParameter.StepPlusWidth);
  907. StepPeriodMsec = DriveParameter.StepPeriod*50;
  908. logStrHex (" StepPlusWidth : ",DriveParameter.StepPlusWidth);
  909. logStrHex (" StepPeriod : ",DriveParameter.StepPeriod );
  910. logStrHex (" StepMode : ",DriveParameter.StepMode );
  911. logStrHex (" MaximumHeadAdress : ",DriveParameter.MaximumHeadAdress);
  912. logStrHex (" CylinderAddress : ",maxCylinder);
  913. logStrHex (" ReduceWrietCurrent : ",DriveParameter.ReduceWrietCurrent);
  914. logStrHex (" DriveType/SeekCompleteOption : ",DriveParameter.DriveType_SeekCompleteOption);
  915. logStrHex (" Maximum LAD : ",numLAD-1);
  916. return 0; // error result
  917. }
  918. #endif
  919. /*
  920. * MsgIn2.
  921. */
  922. void MsgIn2(int msg)
  923. {
  924. LOGN("MsgIn2");
  925. SCSI_OUT(vMSG, active) // gpio_write(MSG, high);
  926. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  927. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  928. writeHandshake(msg);
  929. }
  930. /*
  931. * MsgOut2.
  932. */
  933. void MsgOut2()
  934. {
  935. LOGN("MsgOut2");
  936. SCSI_OUT(vMSG, active) // gpio_write(MSG, high);
  937. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  938. SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  939. m_msb[m_msc] = readHandshake();
  940. m_msc++;
  941. m_msc %= 256;
  942. }
  943. /*
  944. * Main loop.
  945. */
  946. void loop()
  947. {
  948. //int msg = 0;
  949. m_msg = 0;
  950. // Wait until RST = H, BSY = H, SEL = L
  951. do {} while( SCSI_IN(vBSY) || !SCSI_IN(vSEL) || SCSI_IN(vRST));
  952. // BSY+ SEL-
  953. // If the ID to respond is not driven, wait for the next
  954. //byte db = readIO();
  955. //byte scsiid = db & scsi_id_mask;
  956. byte scsiid = readIO() & scsi_id_mask;
  957. if((scsiid) == 0) {
  958. return;
  959. }
  960. LOGN("Selection");
  961. m_isBusReset = false;
  962. // Set BSY to-when selected
  963. SCSI_BSY_ACTIVE(); // Turn only BSY output ON, ACTIVE
  964. // Ask for a TARGET-ID to respond
  965. #if USE_DB2ID_TABLE
  966. m_id = db2scsiid[scsiid];
  967. //if(m_id==0xff) return;
  968. #else
  969. for(m_id=7;m_id>=0;m_id--)
  970. if(scsiid & (1<<m_id)) break;
  971. //if(m_id<0) return;
  972. #endif
  973. // Wait until SEL becomes inactive
  974. while(isHigh(gpio_read(SEL))) {
  975. if(m_isBusReset) {
  976. goto BusFree;
  977. }
  978. }
  979. SCSI_TARGET_ACTIVE() // (BSY), REQ, MSG, CD, IO output turned on
  980. //
  981. if(isHigh(gpio_read(ATN))) {
  982. bool syncenable = false;
  983. int syncperiod = 50;
  984. int syncoffset = 0;
  985. m_msc = 0;
  986. memset(m_msb, 0x00, sizeof(m_msb));
  987. while(isHigh(gpio_read(ATN))) {
  988. MsgOut2();
  989. }
  990. for(int i = 0; i < m_msc; i++) {
  991. // ABORT
  992. if (m_msb[i] == 0x06) {
  993. goto BusFree;
  994. }
  995. // BUS DEVICE RESET
  996. if (m_msb[i] == 0x0C) {
  997. syncoffset = 0;
  998. goto BusFree;
  999. }
  1000. // IDENTIFY
  1001. if (m_msb[i] >= 0x80) {
  1002. }
  1003. // Extended message
  1004. if (m_msb[i] == 0x01) {
  1005. // Check only when synchronous transfer is possible
  1006. if (!syncenable || m_msb[i + 2] != 0x01) {
  1007. MsgIn2(0x07);
  1008. break;
  1009. }
  1010. // Transfer period factor(50 x 4 = Limited to 200ns)
  1011. syncperiod = m_msb[i + 3];
  1012. if (syncperiod > 50) {
  1013. syncoffset = 50;
  1014. }
  1015. // REQ/ACK offset(Limited to 16)
  1016. syncoffset = m_msb[i + 4];
  1017. if (syncoffset > 16) {
  1018. syncoffset = 16;
  1019. }
  1020. // STDR response message generation
  1021. MsgIn2(0x01);
  1022. MsgIn2(0x03);
  1023. MsgIn2(0x01);
  1024. MsgIn2(syncperiod);
  1025. MsgIn2(syncoffset);
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. LOG("Command:");
  1031. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  1032. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  1033. SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  1034. int len;
  1035. byte cmd[12];
  1036. cmd[0] = readHandshake(); if(m_isBusReset) goto BusFree;
  1037. LOGHEX(cmd[0]);
  1038. // Command length selection, reception
  1039. static const int cmd_class_len[8]={6,10,10,6,6,12,6,6};
  1040. len = cmd_class_len[cmd[0] >> 5];
  1041. cmd[1] = readHandshake(); LOG(":");LOGHEX(cmd[1]); if(m_isBusReset) goto BusFree;
  1042. cmd[2] = readHandshake(); LOG(":");LOGHEX(cmd[2]); if(m_isBusReset) goto BusFree;
  1043. cmd[3] = readHandshake(); LOG(":");LOGHEX(cmd[3]); if(m_isBusReset) goto BusFree;
  1044. cmd[4] = readHandshake(); LOG(":");LOGHEX(cmd[4]); if(m_isBusReset) goto BusFree;
  1045. cmd[5] = readHandshake(); LOG(":");LOGHEX(cmd[5]); if(m_isBusReset) goto BusFree;
  1046. // Receive the remaining commands
  1047. for(int i = 6; i < len; i++ ) {
  1048. cmd[i] = readHandshake();
  1049. LOG(":");
  1050. LOGHEX(cmd[i]);
  1051. if(m_isBusReset) goto BusFree;
  1052. }
  1053. // LUN confirmation
  1054. m_lun = m_sts>>5;
  1055. m_sts = cmd[1]&0xe0; // Preset LUN in status byte
  1056. // HDD Image selection
  1057. m_img = (HDDIMG *)0; // None
  1058. if( (m_lun <= NUM_SCSILUN) )
  1059. {
  1060. m_img = &(img[m_id][m_lun]); // There is an image
  1061. if(!(m_img->m_file.isOpen()))
  1062. m_img = (HDDIMG *)0; // Image absent
  1063. }
  1064. // if(!m_img) m_sts |= 0x02; // Missing image file for LUN
  1065. //LOGHEX(((uint32_t)m_img));
  1066. LOG(":ID ");
  1067. LOG(m_id);
  1068. LOG(":LUN ");
  1069. LOG(m_lun);
  1070. LOGN("");
  1071. switch(cmd[0]) {
  1072. case 0x00:
  1073. LOGN("[Test Unit]");
  1074. break;
  1075. case 0x01:
  1076. LOGN("[Rezero Unit]");
  1077. break;
  1078. case 0x03:
  1079. LOGN("[RequestSense]");
  1080. onRequestSenseCommand(cmd[4]);
  1081. break;
  1082. case 0x04:
  1083. LOGN("[FormatUnit]");
  1084. break;
  1085. case 0x06:
  1086. LOGN("[FormatUnit]");
  1087. break;
  1088. case 0x07:
  1089. LOGN("[ReassignBlocks]");
  1090. break;
  1091. case 0x08:
  1092. LOGN("[Read6]");
  1093. m_sts |= onReadCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
  1094. break;
  1095. case 0x0A:
  1096. LOGN("[Write6]");
  1097. m_sts |= onWriteCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
  1098. break;
  1099. case 0x0B:
  1100. LOGN("[Seek6]");
  1101. break;
  1102. case 0x12:
  1103. LOGN("[Inquiry]");
  1104. m_sts |= onInquiryCommand(cmd[4]);
  1105. break;
  1106. case 0x1A:
  1107. LOGN("[ModeSense6]");
  1108. m_sts |= onModeSenseCommand(cmd[1]&0x80, cmd[2], cmd[4]);
  1109. break;
  1110. case 0x1B:
  1111. LOGN("[StartStopUnit]");
  1112. break;
  1113. case 0x1E:
  1114. LOGN("[PreAllowMed.Removal]");
  1115. break;
  1116. case 0x25:
  1117. LOGN("[ReadCapacity]");
  1118. m_sts |= onReadCapacityCommand(cmd[8]);
  1119. break;
  1120. case 0x28:
  1121. LOGN("[Read10]");
  1122. 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]);
  1123. break;
  1124. case 0x2A:
  1125. LOGN("[Write10]");
  1126. 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]);
  1127. break;
  1128. case 0x2B:
  1129. LOGN("[Seek10]");
  1130. break;
  1131. case 0x5A:
  1132. LOGN("[ModeSense10]");
  1133. onModeSenseCommand(cmd[1] & 0x80, cmd[2], ((uint32_t)cmd[7] << 8) | cmd[8]);
  1134. break;
  1135. #if SCSI_SELECT == 1
  1136. case 0xc2:
  1137. LOGN("[DTC510B setDriveParameter]");
  1138. m_sts |= dtc510b_setDriveparameter();
  1139. break;
  1140. #endif
  1141. default:
  1142. LOGN("[*Unknown]");
  1143. m_sts |= 0x02;
  1144. m_senseKey = 5;
  1145. break;
  1146. }
  1147. if(m_isBusReset) {
  1148. goto BusFree;
  1149. }
  1150. LOGN("Sts");
  1151. SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  1152. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  1153. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  1154. writeHandshake(m_sts);
  1155. if(m_isBusReset) {
  1156. goto BusFree;
  1157. }
  1158. LOGN("MsgIn");
  1159. SCSI_OUT(vMSG, active) // gpio_write(MSG, high);
  1160. SCSI_OUT(vCD , active) // gpio_write(CD, high);
  1161. SCSI_OUT(vIO , active) // gpio_write(IO, high);
  1162. writeHandshake(m_msg);
  1163. BusFree:
  1164. LOGN("BusFree");
  1165. m_isBusReset = false;
  1166. //SCSI_OUT(vREQ,inactive) // gpio_write(REQ, low);
  1167. //SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
  1168. //SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
  1169. //SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
  1170. //SCSI_OUT(vBSY,inactive)
  1171. SCSI_TARGET_INACTIVE() // Turn off BSY, REQ, MSG, CD, IO output
  1172. }