| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374 | 
							- /*  
 
-  *  BlueSCSI
 
-  *  Copyright (c) 2021  Eric Helgeson   
 
-  *  
 
-  *  This file is free software: you may copy, redistribute and/or modify it  
 
-  *  under the terms of the GNU General Public License as published by the  
 
-  *  Free Software Foundation, either version 2 of the License, or (at your  
 
-  *  option) any later version.  
 
-  *  
 
-  *  This file is distributed in the hope that it will be useful, but  
 
-  *  WITHOUT ANY WARRANTY; without even the implied warranty of  
 
-  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  
 
-  *  General Public License for more details.  
 
-  *  
 
-  *  You should have received a copy of the GNU General Public License  
 
-  *  along with this program.  If not, see https://github.com/erichelgeson/bluescsi.  
 
-  *  
 
-  * This file incorporates work covered by the following copyright and  
 
-  * permission notice:  
 
-  *  
 
-  *     Copyright (c) 2019 komatsu   
 
-  *  
 
-  *     Permission to use, copy, modify, and/or distribute this software  
 
-  *     for any purpose with or without fee is hereby granted, provided  
 
-  *     that the above copyright notice and this permission notice appear  
 
-  *     in all copies.  
 
-  *  
 
-  *     THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL  
 
-  *     WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED  
 
-  *     WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE  
 
-  *     AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR  
 
-  *     CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS  
 
-  *     OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,  
 
-  *     NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN  
 
-  *     CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.  
 
-  */
 
- #include <Arduino.h> // For Platform.IO
 
- #include <SdFat.h>
 
- #ifdef USE_STM32_DMA
 
- #warning "warning USE_STM32_DMA"
 
- #endif
 
- #define DEBUG            0      // 0:No debug information output
 
-                                 // 1: Debug information output available
 
- #define SCSI_SELECT      0      // 0 for STANDARD
 
-                                 // 1 for SHARP X1turbo
 
-                                 // 2 for NEC PC98
 
- #define READ_SPEED_OPTIMIZE  1 // Faster reads
 
- #define WRITE_SPEED_OPTIMIZE 1 // Speeding up writes
 
- #define USE_DB2ID_TABLE      1 // Use table to get ID from SEL-DB
 
- // SCSI config
 
- #define NUM_SCSIID  7          // Maximum number of supported SCSI-IDs (The minimum is 0)
 
- #define NUM_SCSILUN 2          // Maximum number of LUNs supported     (The minimum is 0)
 
- #define READ_PARITY_CHECK 0    // Perform read parity check (unverified)
 
- // HDD format
 
- #define MAX_BLOCKSIZE 1024     // Maximum BLOCK size
 
- // SDFAT
 
- #define SD1_CONFIG SdSpiConfig(PA4, DEDICATED_SPI, SD_SCK_MHZ(SPI_FULL_SPEED), &SPI)
 
- SdFs SD;
 
- #if DEBUG
 
- #define LOG(XX)     Serial.print(XX)
 
- #define LOGHEX(XX)  Serial.print(XX, HEX)
 
- #define LOGN(XX)    Serial.println(XX)
 
- #define LOGHEXN(XX) Serial.println(XX, HEX)
 
- #else
 
- #define LOG(XX)     //Serial.print(XX)
 
- #define LOGHEX(XX)  //Serial.print(XX, HEX)
 
- #define LOGN(XX)    //Serial.println(XX)
 
- #define LOGHEXN(XX) //Serial.println(XX, HEX)
 
- #endif
 
- #define active   1
 
- #define inactive 0
 
- #define high 0
 
- #define low 1
 
- #define isHigh(XX) ((XX) == high)
 
- #define isLow(XX) ((XX) != high)
 
- #define gpio_mode(pin,val) gpio_set_mode(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val);
 
- #define gpio_write(pin,val) gpio_write_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit, val)
 
- #define gpio_read(pin) gpio_read_bit(PIN_MAP[pin].gpio_device, PIN_MAP[pin].gpio_bit)
 
- //#define DB0       PB8     // SCSI:DB0
 
- //#define DB1       PB9     // SCSI:DB1
 
- //#define DB2       PB10    // SCSI:DB2
 
- //#define DB3       PB11    // SCSI:DB3
 
- //#define DB4       PB12    // SCSI:DB4
 
- //#define DB5       PB13    // SCSI:DB5
 
- //#define DB6       PB14    // SCSI:DB6
 
- //#define DB7       PB15    // SCSI:DB7
 
- //#define DBP       PB0     // SCSI:DBP
 
- #define ATN       PA8      // SCSI:ATN
 
- #define BSY       PA9      // SCSI:BSY
 
- #define ACK       PA10     // SCSI:ACK
 
- #define RST       PA15     // SCSI:RST
 
- #define MSG       PB3      // SCSI:MSG
 
- #define SEL       PB4      // SCSI:SEL
 
- #define CD        PB5      // SCSI:C/D
 
- #define REQ       PB6      // SCSI:REQ
 
- #define IO        PB7      // SCSI:I/O
 
- #define SD_CS     PA4      // SDCARD:CS
 
- #define LED       PC13     // LED
 
- // GPIO register port
 
- #define PAREG GPIOA->regs
 
- #define PBREG GPIOB->regs
 
- // LED control
 
- #define LED_ON()       gpio_write(LED, high);
 
- #define LED_OFF()      gpio_write(LED, low);
 
- // Virtual pin (Arduio compatibility is slow, so make it MCU-dependent)
 
- #define PA(BIT)       (BIT)
 
- #define PB(BIT)       (BIT+16)
 
- // Virtual pin decoding
 
- #define GPIOREG(VPIN)    ((VPIN)>=16?PBREG:PAREG)
 
- #define BITMASK(VPIN) (1<<((VPIN)&15))
 
- #define vATN       PA(8)      // SCSI:ATN
 
- #define vBSY       PA(9)      // SCSI:BSY
 
- #define vACK       PA(10)     // SCSI:ACK
 
- #define vRST       PA(15)     // SCSI:RST
 
- #define vMSG       PB(3)      // SCSI:MSG
 
- #define vSEL       PB(4)      // SCSI:SEL
 
- #define vCD        PB(5)      // SCSI:C/D
 
- #define vREQ       PB(6)      // SCSI:REQ
 
- #define vIO        PB(7)      // SCSI:I/O
 
- #define vSD_CS     PA(4)      // SDCARD:CS
 
- // SCSI output pin control: opendrain active LOW (direct pin drive)
 
- #define SCSI_OUT(VPIN,ACTIVE) { GPIOREG(VPIN)->BSRR = BITMASK(VPIN)<<((ACTIVE)?16:0); }
 
- // SCSI input pin check (inactive=0,avtive=1)
 
- #define SCSI_IN(VPIN) ((~GPIOREG(VPIN)->IDR>>(VPIN&15))&1)
 
- // GPIO mode
 
- // IN , FLOAT      : 4
 
- // IN , PU/PD      : 8
 
- // OUT, PUSH/PULL  : 3
 
- // OUT, OD         : 1
 
- //#define DB_MODE_OUT 3
 
- #define DB_MODE_OUT 1
 
- #define DB_MODE_IN  8
 
- // Put DB and DP in output mode
 
- #define SCSI_DB_OUTPUT() { PBREG->CRL=(PBREG->CRL &0xfffffff0)|DB_MODE_OUT; PBREG->CRH = 0x11111111*DB_MODE_OUT; }
 
- // Put DB and DP in input mode
 
- #define SCSI_DB_INPUT()  { PBREG->CRL=(PBREG->CRL &0xfffffff0)|DB_MODE_IN ; PBREG->CRH = 0x11111111*DB_MODE_IN;  }
 
- // Turn on the output only for BSY
 
- #define SCSI_BSY_ACTIVE()      { gpio_mode(BSY, GPIO_OUTPUT_OD); SCSI_OUT(vBSY,  active) }
 
- // BSY,REQ,MSG,CD,IO Turn on the output (no change required for OD)
 
- #define SCSI_TARGET_ACTIVE()   { }
 
- // BSY,REQ,MSG,CD,IO Turn off output, BSY is the last input
 
- #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); }
 
- // HDDiamge file
 
- #define HDIMG_ID_POS  2                 // Position to embed ID number
 
- #define HDIMG_LUN_POS 3                 // Position to embed LUN numbers
 
- #define HDIMG_BLK_POS 5                 // Position to embed block size numbers
 
- #define MAX_FILE_PATH 32                // Maximum file name length
 
- // HDD image
 
- typedef struct hddimg_struct
 
- {
 
- 	FsFile      m_file;                 // File object
 
- 	uint64_t    m_fileSize;             // File size
 
- 	size_t      m_blocksize;            // SCSI BLOCK size
 
- }HDDIMG;
 
- HDDIMG  img[NUM_SCSIID][NUM_SCSILUN]; // Maximum number
 
- uint8_t       m_senseKey = 0;         // Sense key
 
- volatile bool m_isBusReset = false;   // Bus reset
 
- byte          scsi_id_mask;           // Mask list of responding SCSI IDs
 
- byte          m_id;                   // Currently responding SCSI-ID
 
- byte          m_lun;                  // Logical unit number currently responding
 
- byte          m_sts;                  // Status byte
 
- byte          m_msg;                  // Message bytes
 
- HDDIMG       *m_img;                  // HDD image for current SCSI-ID, LUN
 
- byte          m_buf[MAX_BLOCKSIZE+1]; // General purpose buffer + overrun fetch
 
- int           m_msc;
 
- bool          m_msb[256];
 
- /*
 
-  *  Data byte to BSRR register setting value and parity table
 
- */
 
- // Parity bit generation
 
- #define PTY(V)   (1^((V)^((V)>>1)^((V)>>2)^((V)>>3)^((V)>>4)^((V)>>5)^((V)>>6)^((V)>>7))&1)
 
- // Data byte to BSRR register setting value conversion table
 
- // BSRR[31:24] =  DB[7:0]
 
- // BSRR[   16] =  PTY(DB)
 
- // BSRR[15: 8] = ~DB[7:0]
 
- // BSRR[    0] = ~PTY(DB)
 
- // Set DBP, set REQ = inactive
 
- #define DBP(D)    ((((((uint32_t)(D)<<8)|PTY(D))*0x00010001)^0x0000ff01)|BITMASK(vREQ))
 
- #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)
 
- #define DBP32(D)  DBP8(D),DBP8(D+8),DBP8(D+16),DBP8(D+24)
 
- // BSRR register control value that simultaneously performs DB set, DP set, and REQ = H (inactrive)
 
- static const uint32_t db_bsrr[256]={
 
-   DBP32(0x00),DBP32(0x20),DBP32(0x40),DBP32(0x60),
 
-   DBP32(0x80),DBP32(0xA0),DBP32(0xC0),DBP32(0xE0)
 
- };
 
- // Parity bit acquisition
 
- #define PARITY(DB) (db_bsrr[DB]&1)
 
- // Macro cleaning
 
- #undef DBP32
 
- #undef DBP8
 
- //#undef DBP
 
- //#undef PTY
 
- #if USE_DB2ID_TABLE
 
- /* DB to SCSI-ID translation table */
 
- static const byte db2scsiid[256]={
 
-   0xff,
 
-   0,
 
-   1,1,
 
-   2,2,2,2,
 
-   3,3,3,3,3,3,3,3,
 
-   4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
 
-   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,
 
-   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,
 
-   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,
 
-   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,
 
-   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,
 
-   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,
 
-   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
 
- };
 
- #endif
 
- // Log File
 
- #define VERSION "1.0-b"
 
- #define LOG_FILENAME "LOG.txt"
 
- FsFile LOG_FILE;
 
- // SCSI Drive Vendor information
 
- byte SCSI_INFO_BUF[36] = {
 
-   0x00, //device type
 
-   0x00, //RMB = 0
 
-   0x01, //ISO, ECMA, ANSI version
 
-   0x01, //Response data format
 
-   35 - 4, //Additional data length
 
-   0, 0, //Reserve
 
-   0x00, //Support function
 
-   'Q', 'U', 'A', 'N', 'T', 'U', 'M', ' ', // vendor 8
 
-   'F', 'I', 'R', 'E', 'B', 'A', 'L', 'L', '1', ' ', ' ',' ', ' ', ' ', ' ', ' ', // product 16
 
-   '1', '.', '0', ' ' // version 4
 
- };
 
- void onFalseInit(void);
 
- void noSDCardFound(void);
 
- void onBusReset(void);
 
- void initFileLog(void);
 
- void finalizeFileLog(void);
 
- /*
 
-  * IO read.
 
-  */
 
- inline byte readIO(void)
 
- {
 
-   // Port input data register
 
-   uint32_t ret = GPIOB->regs->IDR;
 
-   byte bret = (byte)((~ret)>>8);
 
- #if READ_PARITY_CHECK
 
-   if((db_bsrr[bret]^ret)&1)
 
-     m_sts |= 0x01; // parity error
 
- #endif
 
-   return bret;
 
- }
 
- // If config file exists, read the first three lines and copy the contents.
 
- // File must be well formed or you will get junk in the SCSI Vendor fields.
 
- void readSCSIDeviceConfig() {
 
-   FsFile config_file = SD.open("scsi-config.txt", O_RDONLY);
 
-   if (!config_file.isOpen()) {
 
-     return;
 
-   }
 
-   char vendor[9];
 
-   memset(vendor, 0, sizeof(vendor));
 
-   config_file.readBytes(vendor, sizeof(vendor));
 
-   LOG_FILE.print("SCSI VENDOR: ");
 
-   LOG_FILE.println(vendor);
 
-   memcpy(&(SCSI_INFO_BUF[8]), vendor, 8);
 
-   char product[17];
 
-   memset(product, 0, sizeof(product));
 
-   config_file.readBytes(product, sizeof(product));
 
-   LOG_FILE.print("SCSI PRODUCT: ");
 
-   LOG_FILE.println(product);
 
-   memcpy(&(SCSI_INFO_BUF[16]), product, 16);
 
-   char version[5];
 
-   memset(version, 0, sizeof(version));
 
-   config_file.readBytes(version, sizeof(version));
 
-   LOG_FILE.print("SCSI VERSION: ");
 
-   LOG_FILE.println(version);
 
-   memcpy(&(SCSI_INFO_BUF[32]), version, 4);
 
-   config_file.close();
 
- }
 
- /*
 
-  * Open HDD image file
 
-  */
 
- bool hddimageOpen(HDDIMG *h,const char *image_name,int id,int lun,int blocksize)
 
- {
 
-   h->m_fileSize = 0;
 
-   h->m_blocksize = blocksize;
 
-   h->m_file = SD.open(image_name, O_RDWR);
 
-   if(h->m_file.isOpen())
 
-   {
 
-     h->m_fileSize = h->m_file.size();
 
-     LOG_FILE.print("Imagefile: ");
 
-     LOG_FILE.print(image_name);
 
-     if(h->m_fileSize>0)
 
-     {
 
-       // check blocksize dummy file
 
-       LOG_FILE.print(" / ");
 
-       LOG_FILE.print(h->m_fileSize);
 
-       LOG_FILE.print("bytes / ");
 
-       LOG_FILE.print(h->m_fileSize / 1024);
 
-       LOG_FILE.print("KiB / ");
 
-       LOG_FILE.print(h->m_fileSize / 1024 / 1024);
 
-       LOG_FILE.println("MiB");
 
-       return true; // File opened
 
-     }
 
-     else
 
-     {
 
-       h->m_file.close();
 
-       h->m_fileSize = h->m_blocksize = 0; // no file
 
-       LOG_FILE.println("FileSizeError");
 
-     }
 
-   }
 
-   return false;
 
- }
 
- /*
 
-  * Initialization.
 
-  *  Initialize the bus and set the PIN orientation
 
-  */
 
- void setup()
 
- {
 
-   // PA15 / PB3 / PB4 Cannot be used
 
-   // JTAG Because it is used for debugging.
 
-   // Comment out for Debugging in PlatformIO
 
-   disableDebugPorts();
 
-   // Serial initialization
 
- #if DEBUG
 
-   Serial.begin(9600);
 
-   while (!Serial);
 
- #endif
 
-   // PIN initialization
 
-   gpio_mode(LED, GPIO_OUTPUT_OD);
 
-   gpio_write(LED, low);
 
-   //GPIO(SCSI BUS)Initialization
 
-   //Port setting register (lower)
 
- //  GPIOB->regs->CRL |= 0x000000008; // SET INPUT W/ PUPD on PAB-PB0
 
-   //Port setting register (upper)
 
-   //GPIOB->regs->CRH = 0x88888888; // SET INPUT W/ PUPD on PB15-PB8
 
- //  GPIOB->regs->ODR = 0x0000FF00; // SET PULL-UPs on PB15-PB8
 
-   // DB and DP are input modes
 
-   SCSI_DB_INPUT()
 
-   // Input port
 
-   gpio_mode(ATN, GPIO_INPUT_PU);
 
-   gpio_mode(BSY, GPIO_INPUT_PU);
 
-   gpio_mode(ACK, GPIO_INPUT_PU);
 
-   gpio_mode(RST, GPIO_INPUT_PU);
 
-   gpio_mode(SEL, GPIO_INPUT_PU);
 
-   // Output port
 
-   gpio_mode(MSG, GPIO_OUTPUT_OD);
 
-   gpio_mode(CD,  GPIO_OUTPUT_OD);
 
-   gpio_mode(REQ, GPIO_OUTPUT_OD);
 
-   gpio_mode(IO,  GPIO_OUTPUT_OD);
 
-   // Turn off the output port
 
-   SCSI_TARGET_INACTIVE()
 
-   //Occurs when the RST pin state changes from HIGH to LOW
 
-   //attachInterrupt(PIN_MAP[RST].gpio_bit, onBusReset, FALLING);
 
-   LED_ON();
 
-   // clock = 36MHz , about 4Mbytes/sec
 
-   if(!SD.begin(SD1_CONFIG)) {
 
- #if DEBUG
 
-     Serial.println("SD initialization failed!");
 
- #endif
 
-     noSDCardFound();
 
-   }
 
-   initFileLog();
 
-   readSCSIDeviceConfig();
 
-   //Sector data overrun byte setting
 
-   m_buf[MAX_BLOCKSIZE] = 0xff; // DB0 all off,DBP off
 
-   //HD image file open
 
-   scsi_id_mask = 0x00;
 
-   // Iterate over the root path in the SD card looking for candidate image files.
 
-   SdFile root;
 
-   root.open("/");
 
-   SdFile file;
 
-   bool imageReady;
 
-   while (1) {
 
-     if (!file.openNext(&root, O_READ)) break;
 
-     char name[MAX_FILE_PATH+1];
 
-     if(!file.isDir()) {
 
-       file.getName(name, MAX_FILE_PATH+1);
 
-       file.close();
 
-       String file_name = String(name);
 
-       file_name.toLowerCase();
 
-       if(file_name.startsWith("hd") && file_name.endsWith(".hda")) {
 
-         int id  = name[HDIMG_ID_POS] - '0';
 
-         int lun = name[HDIMG_LUN_POS] - '0';
 
-         int blk = name[HDIMG_BLK_POS] - '0';
 
-         if(blk == 2) {
 
-           blk = 256;
 
-         } else if(blk == 1) {
 
-           blk = 1024;
 
-         } else {
 
-           blk = 512;
 
-         }
 
-         if(id < NUM_SCSIID && lun < NUM_SCSILUN) {
 
-           HDDIMG *h = &img[id][lun];
 
-           imageReady = hddimageOpen(h,name,id,lun,blk);
 
-           if(imageReady) { // Marked as a responsive ID
 
-             scsi_id_mask |= 1<<id;
 
-           }
 
-         } else {
 
-           LOG_FILE.print("Bad LUN or SCSI id for image: ");
 
-           LOG_FILE.println(name);
 
-           LOG_FILE.sync();
 
-         }
 
-       } else {
 
-           LOG_FILE.print("Not an image: ");
 
-           LOG_FILE.println(name);
 
-           LOG_FILE.sync();
 
-       }
 
-     }
 
-   }
 
-   root.close();
 
-   // Error if there are 0 image files
 
-   if(scsi_id_mask==0) {
 
-     LOG_FILE.println("ERROR: No valid images found!");
 
-     onFalseInit();
 
-   }
 
-   finalizeFileLog();
 
-   LED_OFF();
 
-   //Occurs when the RST pin state changes from HIGH to LOW
 
-   attachInterrupt(PIN_MAP[RST].gpio_bit, onBusReset, FALLING);
 
- }
 
- /*
 
-  * Setup initialization logfile
 
-  */
 
- void initFileLog() {
 
-   LOG_FILE = SD.open(LOG_FILENAME, O_WRONLY | O_CREAT);
 
-   LOG_FILE.println("BlueSCSI <-> SD - https://github.com/erichelgeson/BlueSCSI");
 
-   LOG_FILE.print("VERSION: ");
 
-   LOG_FILE.println(VERSION);
 
-   LOG_FILE.print("DEBUG:");
 
-   LOG_FILE.print(DEBUG);
 
-   LOG_FILE.print(" SCSI_SELECT:");
 
-   LOG_FILE.print(SCSI_SELECT);
 
-   LOG_FILE.print(" SDFAT_FILE_TYPE:");
 
-   LOG_FILE.println(SDFAT_FILE_TYPE);
 
-   LOG_FILE.print("SdFat version: ");
 
-   LOG_FILE.println(SD_FAT_VERSION_STR);
 
-   LOG_FILE.print("SdFat Max FileName Length: ");
 
-   LOG_FILE.println(MAX_FILE_PATH);
 
-   LOG_FILE.println("Initialized SD Card - lets go!");
 
-   LOG_FILE.sync();
 
- }
 
- /*
 
-  * Finalize initialization logfile
 
-  */
 
- void finalizeFileLog() {
 
-   // View support drive map
 
-   LOG_FILE.print("ID");
 
-   for(int lun=0;lun<NUM_SCSILUN;lun++)
 
-   {
 
-     LOG_FILE.print(":LUN");
 
-     LOG_FILE.print(lun);
 
-   }
 
-   LOG_FILE.println(":");
 
-   //
 
-   for(int id=0;id<NUM_SCSIID;id++)
 
-   {
 
-     LOG_FILE.print(" ");
 
-     LOG_FILE.print(id);
 
-     for(int lun=0;lun<NUM_SCSILUN;lun++)
 
-     {
 
-       HDDIMG *h = &img[id][lun];
 
-       if( (lun<NUM_SCSILUN) && (h->m_file))
 
-       {
 
-         LOG_FILE.print((h->m_blocksize<1000) ? ": " : ":");
 
-         LOG_FILE.print(h->m_blocksize);
 
-       }
 
-       else      
 
-         LOG_FILE.print(":----");
 
-     }
 
-     LOG_FILE.println(":");
 
-   }
 
-   LOG_FILE.println("Finished initialization of SCSI Devices - Entering main loop.");
 
-   LOG_FILE.sync();
 
-   LOG_FILE.close();
 
- }
 
- /*
 
-  * Initialization failed, blink 3x fast
 
-  */
 
- void onFalseInit(void)
 
- {
 
-   LOG_FILE.sync();
 
-   while(true) {
 
-     for(int i = 0; i < 3; i++) {
 
-       gpio_write(LED, high);
 
-       delay(250);
 
-       gpio_write(LED, low);
 
-       delay(250);
 
-     }
 
-     delay(3000);
 
-   }
 
- }
 
- /*
 
-  * No SC Card found, blink 5x fast
 
-  */
 
- void noSDCardFound(void)
 
- {
 
-   while(true) {
 
-     for(int i = 0; i < 5; i++) {
 
-       gpio_write(LED, high);
 
-       delay(250);
 
-       gpio_write(LED, low);
 
-       delay(250);
 
-     }
 
-     delay(3000);
 
-   }
 
- }
 
- /*
 
-  * Bus reset interrupt.
 
-  */
 
- void onBusReset(void)
 
- {
 
- #if SCSI_SELECT == 1
 
-   // SASI I / F for X1 turbo has RST pulse write cycle +2 clock ==
 
-   // I can't filter because it only activates about 1.25us
 
-   {{
 
- #else
 
-   if(isHigh(gpio_read(RST))) {
 
-     delayMicroseconds(20);
 
-     if(isHigh(gpio_read(RST))) {
 
- #endif  
 
-   // BUS FREE is done in the main process
 
- //      gpio_mode(MSG, GPIO_OUTPUT_OD);
 
- //      gpio_mode(CD,  GPIO_OUTPUT_OD);
 
- //      gpio_mode(REQ, GPIO_OUTPUT_OD);
 
- //      gpio_mode(IO,  GPIO_OUTPUT_OD);
 
-       // Should I enter DB and DBP once?
 
-       SCSI_DB_INPUT()
 
-       LOGN("BusReset!");
 
-       m_isBusReset = true;
 
-     }
 
-   }
 
- }
 
- /*
 
-  * Read by handshake.
 
-  */
 
- inline byte readHandshake(void)
 
- {
 
-   SCSI_OUT(vREQ,active)
 
-   //SCSI_DB_INPUT()
 
-   while(!SCSI_IN(vACK)) { if(m_isBusReset) return 0; }
 
-   byte r = readIO();
 
-   SCSI_OUT(vREQ,inactive)
 
-   while( SCSI_IN(vACK)) { if(m_isBusReset) return 0; }
 
-   return r;  
 
- }
 
- /*
 
-  * Write with a handshake.
 
-  */
 
- inline void writeHandshake(byte d)
 
- {
 
-   GPIOB->regs->BSRR = db_bsrr[d]; // setup DB,DBP (160ns)
 
-   SCSI_DB_OUTPUT() // (180ns)
 
-   // ACK.Fall to DB output delay 100ns(MAX)  (DTC-510B)
 
-   SCSI_OUT(vREQ,inactive) // setup wait (30ns)
 
-   SCSI_OUT(vREQ,inactive) // setup wait (30ns)
 
-   SCSI_OUT(vREQ,inactive) // setup wait (30ns)
 
-   SCSI_OUT(vREQ,active)   // (30ns)
 
-   //while(!SCSI_IN(vACK)) { if(m_isBusReset){ SCSI_DB_INPUT() return; }}
 
-   while(!m_isBusReset && !SCSI_IN(vACK));
 
-   // ACK.Fall to REQ.Raise delay 500ns(typ.) (DTC-510B)
 
-   GPIOB->regs->BSRR = DBP(0xff);  // DB=0xFF , SCSI_OUT(vREQ,inactive)
 
-   // REQ.Raise to DB hold time 0ns
 
-   SCSI_DB_INPUT() // (150ns)
 
-   while( SCSI_IN(vACK)) { if(m_isBusReset) return; }
 
- }
 
- /*
 
-  * Data in phase.
 
-  *  Send len bytes of data array p.
 
-  */
 
- void writeDataPhase(int len, const byte* p)
 
- {
 
-   LOGN("DATAIN PHASE");
 
-   SCSI_OUT(vMSG,inactive) //  gpio_write(MSG, low);
 
-   SCSI_OUT(vCD ,inactive) //  gpio_write(CD, low);
 
-   SCSI_OUT(vIO ,  active) //  gpio_write(IO, high);
 
-   for (int i = 0; i < len; i++) {
 
-     if(m_isBusReset) {
 
-       return;
 
-     }
 
-     writeHandshake(p[i]);
 
-   }
 
- }
 
- /* 
 
-  * Data in phase.
 
-  *  Send len block while reading from SD card.
 
-  */
 
- void writeDataPhaseSD(uint32_t adds, uint32_t len)
 
- {
 
-   LOGN("DATAIN PHASE(SD)");
 
-   uint32_t pos = adds * m_img->m_blocksize;
 
-   m_img->m_file.seek(pos);
 
-   SCSI_OUT(vMSG,inactive) //  gpio_write(MSG, low);
 
-   SCSI_OUT(vCD ,inactive) //  gpio_write(CD, low);
 
-   SCSI_OUT(vIO ,  active) //  gpio_write(IO, high);
 
-   for(uint32_t i = 0; i < len; i++) {
 
-       // Asynchronous reads will make it faster ...
 
-     m_img->m_file.read(m_buf, m_img->m_blocksize);
 
- #if READ_SPEED_OPTIMIZE
 
- //#define REQ_ON() SCSI_OUT(vREQ,active)
 
- #define REQ_ON() (*db_dst = BITMASK(vREQ)<<16)
 
- #define FETCH_SRC()   (src_byte = *srcptr++)
 
- #define FETCH_BSRR_DB() (bsrr_val = bsrr_tbl[src_byte])
 
- #define REQ_OFF_DB_SET(BSRR_VAL) *db_dst = BSRR_VAL
 
- #define WAIT_ACK_ACTIVE()   while(!m_isBusReset && !SCSI_IN(vACK))
 
- #define WAIT_ACK_INACTIVE() do{ if(m_isBusReset) return; }while(SCSI_IN(vACK)) 
 
-     SCSI_DB_OUTPUT()
 
-     register byte *srcptr= m_buf;                 // Source buffer
 
-     register byte *endptr= m_buf +  m_img->m_blocksize; // End pointer
 
-     /*register*/ byte src_byte;                       // Send data bytes
 
-     register const uint32_t *bsrr_tbl = db_bsrr;  // Table to convert to BSRR
 
-     register uint32_t bsrr_val;                   // BSRR value to output (DB, DBP, REQ = ACTIVE)
 
-     register volatile uint32_t *db_dst = &(GPIOB->regs->BSRR); // Output port
 
-     // prefetch & 1st out
 
-     FETCH_SRC();
 
-     FETCH_BSRR_DB();
 
-     REQ_OFF_DB_SET(bsrr_val);
 
-     // DB.set to REQ.F setup 100ns max (DTC-510B)
 
-     // Maybe there should be some weight here
 
-     // WAIT_ACK_INACTIVE();
 
-     do{
 
-       // 0
 
-       REQ_ON();
 
-       FETCH_SRC();
 
-       FETCH_BSRR_DB();
 
-       WAIT_ACK_ACTIVE();
 
-       // ACK.F  to REQ.R       500ns typ. (DTC-510B)
 
-       REQ_OFF_DB_SET(bsrr_val);
 
-       WAIT_ACK_INACTIVE();
 
-       // 1
 
-       REQ_ON();
 
-       FETCH_SRC();
 
-       FETCH_BSRR_DB();
 
-       WAIT_ACK_ACTIVE();
 
-       REQ_OFF_DB_SET(bsrr_val);
 
-       WAIT_ACK_INACTIVE();
 
-       // 2
 
-       REQ_ON();
 
-       FETCH_SRC();
 
-       FETCH_BSRR_DB();
 
-       WAIT_ACK_ACTIVE();
 
-       REQ_OFF_DB_SET(bsrr_val);
 
-       WAIT_ACK_INACTIVE();
 
-       // 3
 
-       REQ_ON();
 
-       FETCH_SRC();
 
-       FETCH_BSRR_DB();
 
-       WAIT_ACK_ACTIVE();
 
-       REQ_OFF_DB_SET(bsrr_val);
 
-       WAIT_ACK_INACTIVE();
 
-       // 4
 
-       REQ_ON();
 
-       FETCH_SRC();
 
-       FETCH_BSRR_DB();
 
-       WAIT_ACK_ACTIVE();
 
-       REQ_OFF_DB_SET(bsrr_val);
 
-       WAIT_ACK_INACTIVE();
 
-       // 5
 
-       REQ_ON();
 
-       FETCH_SRC();
 
-       FETCH_BSRR_DB();
 
-       WAIT_ACK_ACTIVE();
 
-       REQ_OFF_DB_SET(bsrr_val);
 
-       WAIT_ACK_INACTIVE();
 
-       // 6
 
-       REQ_ON();
 
-       FETCH_SRC();
 
-       FETCH_BSRR_DB();
 
-       WAIT_ACK_ACTIVE();
 
-       REQ_OFF_DB_SET(bsrr_val);
 
-       WAIT_ACK_INACTIVE();
 
-       // 7
 
-       REQ_ON();
 
-       FETCH_SRC();
 
-       FETCH_BSRR_DB();
 
-       WAIT_ACK_ACTIVE();
 
-       REQ_OFF_DB_SET(bsrr_val);
 
-       WAIT_ACK_INACTIVE();
 
-     }while(srcptr < endptr);
 
-     SCSI_DB_INPUT()
 
- #else
 
-     for(int j = 0; j < BLOCKSIZE; j++) {
 
-       if(m_isBusReset) {
 
-         return;
 
-       }
 
-       writeHandshake(m_buf[j]);
 
-     }
 
- #endif
 
-   }
 
- }
 
- /*
 
-  * Data out phase.
 
-  *  len block read
 
-  */
 
- void readDataPhase(int len, byte* p)
 
- {
 
-   LOGN("DATAOUT PHASE");
 
-   SCSI_OUT(vMSG,inactive) //  gpio_write(MSG, low);
 
-   SCSI_OUT(vCD ,inactive) //  gpio_write(CD, low);
 
-   SCSI_OUT(vIO ,inactive) //  gpio_write(IO, low);
 
-   for(uint32_t i = 0; i < len; i++)
 
-     p[i] = readHandshake();
 
- }
 
- /*
 
-  * Data out phase.
 
-  *  Write to SD card while reading len block.
 
-  */
 
- void readDataPhaseSD(uint32_t adds, uint32_t len)
 
- {
 
-   LOGN("DATAOUT PHASE(SD)");
 
-   uint32_t pos = adds * m_img->m_blocksize;
 
-   m_img->m_file.seek(pos);
 
-   SCSI_OUT(vMSG,inactive) //  gpio_write(MSG, low);
 
-   SCSI_OUT(vCD ,inactive) //  gpio_write(CD, low);
 
-   SCSI_OUT(vIO ,inactive) //  gpio_write(IO, low);
 
-   for(uint32_t i = 0; i < len; i++) {
 
- #if WRITE_SPEED_OPTIMIZE
 
-   register byte *dstptr= m_buf;
 
- 	register byte *endptr= m_buf + m_img->m_blocksize;
 
-     for(dstptr=m_buf;dstptr<endptr;dstptr+=8) {
 
-       dstptr[0] = readHandshake();
 
-       dstptr[1] = readHandshake();
 
-       dstptr[2] = readHandshake();
 
-       dstptr[3] = readHandshake();
 
-       dstptr[4] = readHandshake();
 
-       dstptr[5] = readHandshake();
 
-       dstptr[6] = readHandshake();
 
-       dstptr[7] = readHandshake();
 
-       if(m_isBusReset) {
 
-         return;
 
-       }
 
-     }
 
- #else
 
-     for(int j = 0; j <  m_img->m_blocksize; j++) {
 
-       if(m_isBusReset) {
 
-         return;
 
-       }
 
-       m_buf[j] = readHandshake();
 
-     }
 
- #endif
 
-     m_img->m_file.write(m_buf, m_img->m_blocksize);
 
-   }
 
-   m_img->m_file.flush();
 
- }
 
- /*
 
-  * INQUIRY command processing.
 
-  */
 
- #if SCSI_SELECT == 2
 
- byte onInquiryCommand(byte len)
 
- {
 
-   byte buf[36] = {
 
-     0x00, //Device type
 
-     0x00, //RMB = 0
 
-     0x01, //ISO,ECMA,ANSI version
 
-     0x01, //Response data format
 
-     35 - 4, //Additional data length
 
-     0, 0, //Reserve
 
-     0x00, //Support function
 
-     'N', 'E', 'C', 'I', 'T', 'S', 'U', ' ',
 
-     'A', 'r', 'd', 'S', 'C', 'S', 'i', 'n', 'o', ' ', ' ',' ', ' ', ' ', ' ', ' ',
 
-     '0', '0', '1', '0',
 
-   };
 
-   writeDataPhase(len < 36 ? len : 36, buf);
 
-   return 0x00;
 
- }
 
- #else
 
- byte onInquiryCommand(byte len)
 
- {
 
-   writeDataPhase(len < 36 ? len : 36, SCSI_INFO_BUF);
 
-   return 0x00;
 
- }
 
- #endif
 
- /*
 
-  * REQUEST SENSE command processing.
 
-  */
 
- void onRequestSenseCommand(byte len)
 
- {
 
-   byte buf[18] = {
 
-     0x70,   //CheckCondition
 
-     0,      //Segment number
 
-     0x00,   //Sense key
 
-     0, 0, 0, 0,  //information
 
-     17 - 7 ,   //Additional data length
 
-     0,
 
-   };
 
-   buf[2] = m_senseKey;
 
-   m_senseKey = 0;
 
-   writeDataPhase(len < 18 ? len : 18, buf);  
 
- }
 
- /*
 
-  * READ CAPACITY command processing.
 
-  */
 
- byte onReadCapacityCommand(byte pmi)
 
- {
 
-   if(!m_img) return 0x02; // Image file absent
 
-   
 
-   uint32_t bl = m_img->m_blocksize;
 
-   uint32_t bc = m_img->m_fileSize / bl;
 
-   uint8_t buf[8] = {
 
-     bc >> 24, bc >> 16, bc >> 8, bc,
 
-     bl >> 24, bl >> 16, bl >> 8, bl    
 
-   };
 
-   writeDataPhase(8, buf);
 
-   return 0x00;
 
- }
 
- /*
 
-  * READ6 / 10 Command processing.
 
-  */
 
- byte onReadCommand(uint32_t adds, uint32_t len)
 
- {
 
-   LOGN("-R");
 
-   LOGHEXN(adds);
 
-   LOGHEXN(len);
 
-   if(!m_img) return 0x02; // Image file absent
 
-   
 
-   gpio_write(LED, high);
 
-   writeDataPhaseSD(adds, len);
 
-   gpio_write(LED, low);
 
-   return 0x00; //sts
 
- }
 
- /*
 
-  * WRITE6 / 10 Command processing.
 
-  */
 
- byte onWriteCommand(uint32_t adds, uint32_t len)
 
- {
 
-   LOGN("-W");
 
-   LOGHEXN(adds);
 
-   LOGHEXN(len);
 
-   
 
-   if(!m_img) return 0x02; // Image file absent
 
-   
 
-   gpio_write(LED, high);
 
-   readDataPhaseSD(adds, len);
 
-   gpio_write(LED, low);
 
-   return 0; //sts
 
- }
 
- /*
 
-  * MODE SENSE command processing.
 
-  */
 
- #if SCSI_SELECT == 2
 
- byte onModeSenseCommand(byte dbd, int cmd2, uint32_t len)
 
- {
 
-   if(!m_img) return 0x02; // Image file absent
 
-   int pageCode = cmd2 & 0x3F;
 
-   // Assuming sector size 512, number of sectors 25, number of heads 8 as default settings
 
-   int size = m_img->m_fileSize;
 
-   int cylinders = (int)(size >> 9);
 
-   cylinders >>= 3;
 
-   cylinders /= 25;
 
-   int sectorsize = 512;
 
-   int sectors = 25;
 
-   int heads = 8;
 
-   // Sector size
 
-  int disksize = 0;
 
-   for(disksize = 16; disksize > 0; --(disksize)) {
 
-     if ((1 << disksize) == sectorsize)
 
-       break;
 
-   }
 
-   // Number of blocks
 
-   uint32_t diskblocks = (uint32_t)(size >> disksize);
 
-   memset(m_buf, 0, sizeof(m_buf)); 
 
-   int a = 4;
 
-   if(dbd == 0) {
 
-     uint32_t bl = m_img->m_blocksize;
 
-     uint32_t bc = m_img->m_fileSize / bl;
 
-     byte c[8] = {
 
-       0,// Density code
 
-       bc >> 16, bc >> 8, bc,
 
-       0, //Reserve
 
-       bl >> 16, bl >> 8, bl
 
-     };
 
-     memcpy(&m_buf[4], c, 8);
 
-     a += 8;
 
-     m_buf[3] = 0x08;
 
-   }
 
-   switch(pageCode) {
 
-   case 0x3F:
 
-   {
 
-     m_buf[a + 0] = 0x01;
 
-     m_buf[a + 1] = 0x06;
 
-     a += 8;
 
-   }
 
-   case 0x03:  // drive parameters
 
-   {
 
-     m_buf[a + 0] = 0x80 | 0x03; // Page code
 
-     m_buf[a + 1] = 0x16; // Page length
 
-     m_buf[a + 2] = (byte)(heads >> 8);// number of sectors / track
 
-     m_buf[a + 3] = (byte)(heads);// number of sectors / track
 
-     m_buf[a + 10] = (byte)(sectors >> 8);// number of sectors / track
 
-     m_buf[a + 11] = (byte)(sectors);// number of sectors / track
 
-     int size = 1 << disksize;
 
-     m_buf[a + 12] = (byte)(size >> 8);// number of sectors / track
 
-     m_buf[a + 13] = (byte)(size);// number of sectors / track
 
-     a += 24;
 
-     if(pageCode != 0x3F) {
 
-       break;
 
-     }
 
-   }
 
-   case 0x04:  // drive parameters
 
-   {
 
-       LOGN("AddDrive");
 
-       m_buf[a + 0] = 0x04; // Page code
 
-       m_buf[a + 1] = 0x12; // Page length
 
-       m_buf[a + 2] = (cylinders >> 16);// Cylinder length
 
-       m_buf[a + 3] = (cylinders >> 8);
 
-       m_buf[a + 4] = cylinders;
 
-       m_buf[a + 5] = heads;   // Number of heads
 
-       a += 20;
 
-     if(pageCode != 0x3F) {
 
-       break;
 
-     }
 
-   }
 
-   default:
 
-     break;
 
-   }
 
-   m_buf[0] = a - 1;
 
-   writeDataPhase(len < a ? len : a, m_buf);
 
-   return 0x00;
 
- }
 
- #else
 
- byte onModeSenseCommand(byte dbd, int cmd2, uint32_t len)
 
- {
 
-   if(!m_img) return 0x02; // No image file
 
-   memset(m_buf, 0, sizeof(m_buf));
 
-   int pageCode = cmd2 & 0x3F;
 
-   int a = 4;
 
-   if(dbd == 0) {
 
-     uint32_t bl =  m_img->m_blocksize;
 
-     uint32_t bc = m_img->m_fileSize / bl;
 
-     byte c[8] = {
 
-       0,//Density code
 
-       bc >> 16, bc >> 8, bc,
 
-       0, //Reserve
 
-       bl >> 16, bl >> 8, bl    
 
-     };
 
-     memcpy(&m_buf[4], c, 8);
 
-     a += 8;
 
-     m_buf[3] = 0x08;
 
-   }
 
-   switch(pageCode) {
 
-   case 0x3F:
 
-   case 0x03:  //Drive parameters
 
-     m_buf[a + 0] = 0x03; //Page code
 
-     m_buf[a + 1] = 0x16; // Page length
 
-     m_buf[a + 11] = 0x3F;//Number of sectors / track
 
-     a += 24;
 
-     if(pageCode != 0x3F) {
 
-       break;
 
-     }
 
-   case 0x04:  //Drive parameters
 
-     {
 
-       uint32_t bc = m_img->m_fileSize / m_img->m_file;
 
-       m_buf[a + 0] = 0x04; //Page code
 
-       m_buf[a + 1] = 0x16; // Page length
 
-       m_buf[a + 2] = bc >> 16;// Cylinder length
 
-       m_buf[a + 3] = bc >> 8;
 
-       m_buf[a + 4] = bc;
 
-       m_buf[a + 5] = 1;   //Number of heads
 
-       a += 24;
 
-     }
 
-     if(pageCode != 0x3F) {
 
-       break;
 
-     }
 
-   default:
 
-     break;
 
-   }
 
-   m_buf[0] = a - 1;
 
-   writeDataPhase(len < a ? len : a, m_buf);
 
-   return 0x00;
 
- }
 
- #endif
 
-     
 
- #if SCSI_SELECT == 1
 
- /*
 
-  * dtc510b_setDriveparameter
 
-  */
 
- #define PACKED  __attribute__((packed))
 
- typedef struct PACKED dtc500_cmd_c2_param_struct
 
- {
 
-   uint8_t StepPlusWidth;        // Default is 13.6usec (11)
 
-   uint8_t StepPeriod;         // Default is  3  msec.(60)
 
-   uint8_t StepMode;         // Default is  Bufferd (0)
 
-   uint8_t MaximumHeadAdress;      // Default is 4 heads (3)
 
-   uint8_t HighCylinderAddressByte;  // Default set to 0   (0)
 
-   uint8_t LowCylinderAddressByte;   // Default is 153 cylinders (152)
 
-   uint8_t ReduceWrietCurrent;     // Default is above Cylinder 128 (127)
 
-   uint8_t DriveType_SeekCompleteOption;// (0)
 
-   uint8_t Reserved8;          // (0)
 
-   uint8_t Reserved9;          // (0)
 
- } DTC510_CMD_C2_PARAM;
 
- static void logStrHex(const char *msg,uint32_t num)
 
- {
 
-     LOG(msg);
 
-     LOGHEXN(num);
 
- }
 
- static byte dtc510b_setDriveparameter(void)
 
- {
 
-   DTC510_CMD_C2_PARAM DriveParameter;
 
-   uint16_t maxCylinder;
 
-   uint16_t numLAD;
 
-   //uint32_t stepPulseUsec;
 
-   int StepPeriodMsec;
 
-   // receive paramter
 
-   writeDataPhase(sizeof(DriveParameter),(byte *)(&DriveParameter));
 
-  
 
-   maxCylinder =
 
-     (((uint16_t)DriveParameter.HighCylinderAddressByte)<<8) |
 
-     (DriveParameter.LowCylinderAddressByte);
 
-   numLAD = maxCylinder * (DriveParameter.MaximumHeadAdress+1);
 
-   //stepPulseUsec  = calcStepPulseUsec(DriveParameter.StepPlusWidth);
 
-   StepPeriodMsec = DriveParameter.StepPeriod*50;
 
-   logStrHex (" StepPlusWidth      : ",DriveParameter.StepPlusWidth);
 
-   logStrHex (" StepPeriod         : ",DriveParameter.StepPeriod   );
 
-   logStrHex (" StepMode           : ",DriveParameter.StepMode     );
 
-   logStrHex (" MaximumHeadAdress  : ",DriveParameter.MaximumHeadAdress);
 
-   logStrHex (" CylinderAddress    : ",maxCylinder);
 
-   logStrHex (" ReduceWrietCurrent : ",DriveParameter.ReduceWrietCurrent);
 
-   logStrHex (" DriveType/SeekCompleteOption : ",DriveParameter.DriveType_SeekCompleteOption);
 
-   logStrHex (" Maximum LAD        : ",numLAD-1);
 
-   return  0; // error result
 
- }
 
- #endif
 
- /*
 
-  * MsgIn2.
 
-  */
 
- void MsgIn2(int msg)
 
- {
 
-   LOGN("MsgIn2");
 
-   SCSI_OUT(vMSG,  active) //  gpio_write(MSG, high);
 
-   SCSI_OUT(vCD ,  active) //  gpio_write(CD, high);
 
-   SCSI_OUT(vIO ,  active) //  gpio_write(IO, high);
 
-   writeHandshake(msg);
 
- }
 
- /*
 
-  * MsgOut2.
 
-  */
 
- void MsgOut2()
 
- {
 
-   LOGN("MsgOut2");
 
-   SCSI_OUT(vMSG,  active) //  gpio_write(MSG, high);
 
-   SCSI_OUT(vCD ,  active) //  gpio_write(CD, high);
 
-   SCSI_OUT(vIO ,inactive) //  gpio_write(IO, low);
 
-   m_msb[m_msc] = readHandshake();
 
-   m_msc++;
 
-   m_msc %= 256;
 
- }
 
- /*
 
-  * Main loop.
 
-  */
 
- void loop() 
 
- {
 
-   //int msg = 0;
 
-   m_msg = 0;
 
-   // Wait until RST = H, BSY = H, SEL = L
 
-   do {} while( SCSI_IN(vBSY) || !SCSI_IN(vSEL) || SCSI_IN(vRST));
 
-   // BSY+ SEL-
 
-   // If the ID to respond is not driven, wait for the next
 
-   //byte db = readIO();
 
-   //byte scsiid = db & scsi_id_mask;
 
-   byte scsiid = readIO() & scsi_id_mask;
 
-   if((scsiid) == 0) {
 
-     return;
 
-   }
 
-   LOGN("Selection");
 
-   m_isBusReset = false;
 
-   // Set BSY to-when selected
 
-   SCSI_BSY_ACTIVE();     // Turn only BSY output ON, ACTIVE
 
-   // Ask for a TARGET-ID to respond
 
- #if USE_DB2ID_TABLE
 
-   m_id = db2scsiid[scsiid];
 
-   //if(m_id==0xff) return;
 
- #else
 
-   for(m_id=7;m_id>=0;m_id--)
 
-     if(scsiid & (1<<m_id)) break;
 
-   //if(m_id<0) return;
 
- #endif
 
-   // Wait until SEL becomes inactive
 
-   while(isHigh(gpio_read(SEL))) {
 
-     if(m_isBusReset) {
 
-       goto BusFree;
 
-     }
 
-   }
 
-   SCSI_TARGET_ACTIVE()  // (BSY), REQ, MSG, CD, IO output turned on
 
-   //  
 
-   if(isHigh(gpio_read(ATN))) {
 
-     bool syncenable = false;
 
-     int syncperiod = 50;
 
-     int syncoffset = 0;
 
-     m_msc = 0;
 
-     memset(m_msb, 0x00, sizeof(m_msb));
 
-     while(isHigh(gpio_read(ATN))) {
 
-       MsgOut2();
 
-     }
 
-     for(int i = 0; i < m_msc; i++) {
 
-       // ABORT
 
-       if (m_msb[i] == 0x06) {
 
-         goto BusFree;
 
-       }
 
-       // BUS DEVICE RESET
 
-       if (m_msb[i] == 0x0C) {
 
-         syncoffset = 0;
 
-         goto BusFree;
 
-       }
 
-       // IDENTIFY
 
-       if (m_msb[i] >= 0x80) {
 
-       }
 
-       // Extended message
 
-       if (m_msb[i] == 0x01) {
 
-         // Check only when synchronous transfer is possible
 
-         if (!syncenable || m_msb[i + 2] != 0x01) {
 
-           MsgIn2(0x07);
 
-           break;
 
-         }
 
-         // Transfer period factor(50 x 4 = Limited to 200ns)
 
-         syncperiod = m_msb[i + 3];
 
-         if (syncperiod > 50) {
 
-           syncoffset = 50;
 
-         }
 
-         // REQ/ACK offset(Limited to 16)
 
-         syncoffset = m_msb[i + 4];
 
-         if (syncoffset > 16) {
 
-           syncoffset = 16;
 
-         }
 
-         // STDR response message generation
 
-         MsgIn2(0x01);
 
-         MsgIn2(0x03);
 
-         MsgIn2(0x01);
 
-         MsgIn2(syncperiod);
 
-         MsgIn2(syncoffset);
 
-         break;
 
-       }
 
-     }
 
-   }
 
-   LOG("Command:");
 
-   SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
 
-   SCSI_OUT(vCD ,  active) // gpio_write(CD, high);
 
-   SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
 
-   
 
-   int len;
 
-   byte cmd[12];
 
-   cmd[0] = readHandshake(); if(m_isBusReset) goto BusFree;
 
-   LOGHEX(cmd[0]);
 
-   // Command length selection, reception
 
-   static const int cmd_class_len[8]={6,10,10,6,6,12,6,6};
 
-   len = cmd_class_len[cmd[0] >> 5];
 
-   cmd[1] = readHandshake(); LOG(":");LOGHEX(cmd[1]); if(m_isBusReset) goto BusFree;
 
-   cmd[2] = readHandshake(); LOG(":");LOGHEX(cmd[2]); if(m_isBusReset) goto BusFree;
 
-   cmd[3] = readHandshake(); LOG(":");LOGHEX(cmd[3]); if(m_isBusReset) goto BusFree;
 
-   cmd[4] = readHandshake(); LOG(":");LOGHEX(cmd[4]); if(m_isBusReset) goto BusFree;
 
-   cmd[5] = readHandshake(); LOG(":");LOGHEX(cmd[5]); if(m_isBusReset) goto BusFree;
 
-   // Receive the remaining commands
 
-   for(int i = 6; i < len; i++ ) {
 
-     cmd[i] = readHandshake();
 
-     LOG(":");
 
-     LOGHEX(cmd[i]);
 
-     if(m_isBusReset) goto BusFree;
 
-   }
 
-   // LUN confirmation
 
-   m_lun = m_sts>>5;
 
-   m_sts = cmd[1]&0xe0;      // Preset LUN in status byte
 
-   // HDD Image selection
 
-   m_img = (HDDIMG *)0; // None
 
-   if( (m_lun <= NUM_SCSILUN) )
 
-   {
 
-     m_img = &(img[m_id][m_lun]); // There is an image
 
-     if(!(m_img->m_file.isOpen()))
 
-       m_img = (HDDIMG *)0;       // Image absent
 
-   }
 
-   // if(!m_img) m_sts |= 0x02;            // Missing image file for LUN
 
-   //LOGHEX(((uint32_t)m_img));
 
-   
 
-   LOG(":ID ");
 
-   LOG(m_id);
 
-   LOG(":LUN ");
 
-   LOG(m_lun);
 
-   LOGN("");
 
-   switch(cmd[0]) {
 
-   case 0x00:
 
-     LOGN("[Test Unit]");
 
-     break;
 
-   case 0x01:
 
-     LOGN("[Rezero Unit]");
 
-     break;
 
-   case 0x03:
 
-     LOGN("[RequestSense]");
 
-     onRequestSenseCommand(cmd[4]);
 
-     break;
 
-   case 0x04:
 
-     LOGN("[FormatUnit]");
 
-     break;
 
-   case 0x06:
 
-     LOGN("[FormatUnit]");
 
-     break;
 
-   case 0x07:
 
-     LOGN("[ReassignBlocks]");
 
-     break;
 
-   case 0x08:
 
-     LOGN("[Read6]");
 
-     m_sts |= onReadCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
 
-     break;
 
-   case 0x0A:
 
-     LOGN("[Write6]");
 
-     m_sts |= onWriteCommand((((uint32_t)cmd[1] & 0x1F) << 16) | ((uint32_t)cmd[2] << 8) | cmd[3], (cmd[4] == 0) ? 0x100 : cmd[4]);
 
-     break;
 
-   case 0x0B:
 
-     LOGN("[Seek6]");
 
-     break;
 
-   case 0x12:
 
-     LOGN("[Inquiry]");
 
-     m_sts |= onInquiryCommand(cmd[4]);
 
-     break;
 
-   case 0x1A:
 
-     LOGN("[ModeSense6]");
 
-     m_sts |= onModeSenseCommand(cmd[1]&0x80, cmd[2], cmd[4]);
 
-     break;
 
-   case 0x1B:
 
-     LOGN("[StartStopUnit]");
 
-     break;
 
-   case 0x1E:
 
-     LOGN("[PreAllowMed.Removal]");
 
-     break;
 
-   case 0x25:
 
-     LOGN("[ReadCapacity]");
 
-     m_sts |= onReadCapacityCommand(cmd[8]);
 
-     break;
 
-   case 0x28:
 
-     LOGN("[Read10]");
 
-     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]);
 
-     break;
 
-   case 0x2A:
 
-     LOGN("[Write10]");
 
-     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]);
 
-     break;
 
-   case 0x2B:
 
-     LOGN("[Seek10]");
 
-     break;
 
-   case 0x5A:
 
-     LOGN("[ModeSense10]");
 
-     onModeSenseCommand(cmd[1] & 0x80, cmd[2], ((uint32_t)cmd[7] << 8) | cmd[8]);
 
-     break;
 
- #if SCSI_SELECT == 1
 
-   case 0xc2:
 
-     LOGN("[DTC510B setDriveParameter]");
 
-     m_sts |= dtc510b_setDriveparameter();
 
-     break;
 
- #endif    
 
-   default:
 
-     LOGN("[*Unknown]");
 
-     m_sts |= 0x02;
 
-     m_senseKey = 5;
 
-     break;
 
-   }
 
-   if(m_isBusReset) {
 
-      goto BusFree;
 
-   }
 
-   LOGN("Sts");
 
-   SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
 
-   SCSI_OUT(vCD ,  active) // gpio_write(CD, high);
 
-   SCSI_OUT(vIO ,  active) // gpio_write(IO, high);
 
-   writeHandshake(m_sts);
 
-   if(m_isBusReset) {
 
-      goto BusFree;
 
-   }
 
-   LOGN("MsgIn");
 
-   SCSI_OUT(vMSG,  active) // gpio_write(MSG, high);
 
-   SCSI_OUT(vCD ,  active) // gpio_write(CD, high);
 
-   SCSI_OUT(vIO ,  active) // gpio_write(IO, high);
 
-   writeHandshake(m_msg);
 
- BusFree:
 
-   LOGN("BusFree");
 
-   m_isBusReset = false;
 
-   //SCSI_OUT(vREQ,inactive) // gpio_write(REQ, low);
 
-   //SCSI_OUT(vMSG,inactive) // gpio_write(MSG, low);
 
-   //SCSI_OUT(vCD ,inactive) // gpio_write(CD, low);
 
-   //SCSI_OUT(vIO ,inactive) // gpio_write(IO, low);
 
-   //SCSI_OUT(vBSY,inactive)
 
-   SCSI_TARGET_INACTIVE() // Turn off BSY, REQ, MSG, CD, IO output
 
- }
 
 
  |