Annotation of hatari/src/hdc.c, revision 1.1.1.10

1.1       root        1: /*
1.1.1.3   root        2:   Hatari - hdc.c
1.1       root        3: 
1.1.1.3   root        4:   This file is distributed under the GNU Public License, version 2 or at
                      5:   your option any later version. Read the file gpl.txt for details.
1.1       root        6: 
1.1.1.3   root        7:   Low-level hard drive emulation
1.1       root        8: */
1.1.1.10! root        9: const char HDC_fileid[] = "Hatari hdc.c : " __DATE__ " " __TIME__;
1.1       root       10: 
                     11: #include "main.h"
1.1.1.3   root       12: #include "configuration.h"
1.1.1.6   root       13: #include "debugui.h"
1.1.1.7   root       14: #include "file.h"
1.1       root       15: #include "fdc.h"
                     16: #include "hdc.h"
1.1.1.7   root       17: #include "log.h"
1.1       root       18: #include "memorySnapShot.h"
                     19: #include "mfp.h"
                     20: #include "stMemory.h"
1.1.1.6   root       21: #include "tos.h"
1.1.1.9   root       22: #include "statusbar.h"
                     23: 
1.1       root       24: 
                     25: /*
                     26:   ACSI emulation: 
                     27:   ACSI commands are six byte-packets sent to the
                     28:   hard drive controller (which is on the HD unit, not in the ST)
                     29: 
                     30:   While the hard drive is busy, DRQ is high, polling the DRQ during
                     31:   operation interrupts the current operation. The DRQ status can
                     32:   be polled non-destructively in GPIP.
1.1.1.7   root       33: 
1.1       root       34:   (For simplicity, the operation is finished immediatly,
                     35:   this is a potential bug, but I doubt it is significant,
                     36:   we just appear to have a very fast hard drive.)
                     37: 
                     38:   The ACSI command set is a subset of the SCSI standard.
                     39:   (for details, see the X3T9.2 SCSI draft documents
                     40:   from 1985, for an example of writing ACSI commands,
                     41:   see the TOS DMA boot code) 
                     42: */
                     43: 
1.1.1.7   root       44: // #define DISALLOW_HDC_WRITE
                     45: // #define HDC_VERBOSE           /* display operations */
                     46: // #define HDC_REALLY_VERBOSE    /* display command packets */
1.1       root       47: 
                     48: /* HDC globals */
                     49: HDCOMMAND HDCCommand;
                     50: FILE *hd_image_file = NULL;
                     51: int nPartitions = 0;
                     52: short int HDCSectorCount;
1.1.1.9   root       53: bool bAcsiEmuOn = FALSE;
1.1.1.7   root       54: 
                     55: static Uint32 nLastBlockAddr;
1.1.1.9   root       56: static bool bSetLastBlockAddr;
1.1.1.7   root       57: static Uint8 nLastError;
1.1       root       58: 
1.1.1.7   root       59: /*
1.1.1.4   root       60:   FDC registers used:
                     61:   - FDCSectorCountRegister
1.1.1.6   root       62:   - DiskControllerStatus_ff8604rd
1.1.1.4   root       63:   - DMAModeControl_ff8606wr
1.1       root       64: */
                     65: 
                     66: 
                     67: /* Our dummy INQUIRY response data */
1.1.1.7   root       68: static unsigned char inquiry_bytes[] =
1.1       root       69: {
1.1.1.7   root       70:        0,                /* device type 0 = direct access device */
                     71:        0,                /* device type qualifier (nonremovable) */
                     72:        1,                /* ANSI version */
                     73:        0,                /* reserved */
                     74:        26,               /* length of the following data */
                     75:        ' ', ' ', ' ',                         /* Vendor specific data */
                     76:        'H','a','t','a','r','i',' ','E',       /* Vendor */
                     77:        'm','u','l','a','t','e','d',' ',       /* Model */
                     78:        ' ',' ',' ',' ',                       /* Revision */
                     79:        0,0,0,0,0,0,0,0,0,0                    /* ?? */
1.1       root       80: };
                     81: 
1.1.1.7   root       82: 
1.1       root       83: /*---------------------------------------------------------------------*/
1.1.1.8   root       84: /**
                     85:  * Return the file offset of the sector specified in the current
                     86:  * ACSI command block.
                     87:  */
1.1.1.4   root       88: static unsigned long HDC_GetOffset(void)
                     89: {
1.1.1.7   root       90:        unsigned long offset;
1.1       root       91: 
1.1.1.7   root       92:        /* construct the logical block adress */
                     93:        offset = ((HD_LBA_MSB(HDCCommand) << 16)
                     94:                  |  (HD_LBA_MID(HDCCommand)  << 8)
                     95:                  |  (HD_LBA_LSB(HDCCommand))) ;
                     96: 
                     97:        /* return value in bytes */
                     98:        return(offset * 512);
1.1       root       99: }
                    100: 
1.1.1.7   root      101: 
1.1       root      102: /*---------------------------------------------------------------------*/
1.1.1.8   root      103: /**
                    104:  * Seek - move to a sector
                    105:  */
1.1.1.7   root      106: static void HDC_Cmd_Seek(void)
1.1       root      107: {
1.1.1.7   root      108:        nLastBlockAddr = HDC_GetOffset();
1.1       root      109: 
1.1.1.7   root      110:        if (fseek(hd_image_file, nLastBlockAddr, SEEK_SET) == 0)
                    111:        {
                    112:                HDCCommand.returnCode = HD_STATUS_OK;
                    113:                nLastError = HD_REQSENS_OK;
                    114:        }
                    115:        else
                    116:        {
                    117:                HDCCommand.returnCode = HD_STATUS_ERROR;
                    118:                nLastError = HD_REQSENS_INVADDR;
                    119:        }
1.1       root      120: 
1.1.1.7   root      121:        FDC_SetDMAStatus(FALSE);              /* no DMA error */
                    122:        FDC_AcknowledgeInterrupt();
                    123:        bSetLastBlockAddr = TRUE;
                    124:        //FDCSectorCountRegister = 0;
1.1       root      125: }
                    126: 
1.1.1.7   root      127: 
1.1       root      128: /*---------------------------------------------------------------------*/
1.1.1.8   root      129: /**
                    130:  * Inquiry - return some disk information
                    131:  */
1.1.1.7   root      132: static void HDC_Cmd_Inquiry(void)
                    133: {
                    134: #ifdef HDC_VERBOSE
                    135:                fprintf(stderr,"Inquiry made.\n");
                    136: #endif
                    137: 
                    138:        inquiry_bytes[4] = HD_SECTORCOUNT(HDCCommand) - 8;
                    139:        memcpy(&STRam[FDC_ReadDMAAddress()], inquiry_bytes,
                    140:               HD_SECTORCOUNT(HDCCommand));
                    141: 
                    142:        FDC_SetDMAStatus(FALSE);              /* no DMA error */
                    143:        FDC_AcknowledgeInterrupt();
                    144:        HDCCommand.returnCode = HD_STATUS_OK;
                    145:        nLastError = HD_REQSENS_OK;
                    146:        bSetLastBlockAddr = FALSE;
                    147:        //FDCSectorCountRegister = 0;
                    148: }
                    149: 
                    150: 
                    151: /*---------------------------------------------------------------------*/
1.1.1.8   root      152: /**
                    153:  * Request sense - return some disk information
                    154:  */
1.1.1.7   root      155: static void HDC_Cmd_RequestSense(void)
1.1       root      156: {
1.1.1.7   root      157:        Uint32 nDmaAddr;
                    158:        int nRetLen;
                    159:        Uint8 retbuf[22];
                    160: 
                    161: #ifdef HDC_VERBOSE
                    162:        fprintf(stderr,"HDC: Request Sense.\n");
                    163: #endif
                    164: 
                    165:        nRetLen = HD_SECTORCOUNT(HDCCommand);
                    166: 
                    167:        if ((nRetLen < 4 && nRetLen != 0) || nRetLen > 22)
                    168:        {
1.1.1.9   root      169:                Log_Printf(LOG_WARN, "HDC: *** Strange REQUEST SENSE ***!\n");
1.1.1.7   root      170:        }
                    171: 
                    172:        /* Limit to sane length */
                    173:        if (nRetLen <= 0)
                    174:        {
                    175:                nRetLen = 4;
                    176:        }
                    177:        else if (nRetLen > 22)
                    178:        {
                    179:                nRetLen = 22;
                    180:        }
                    181: 
                    182:        nDmaAddr = FDC_ReadDMAAddress();
                    183: 
                    184:        memset(retbuf, 0, nRetLen);
                    185: 
                    186:        if (nRetLen <= 4)
                    187:        {
                    188:                retbuf[0] = nLastError;
                    189:                if (bSetLastBlockAddr)
                    190:                {
                    191:                        retbuf[0] |= 0x80;
                    192:                        retbuf[1] = nLastBlockAddr >> 16;
                    193:                        retbuf[2] = nLastBlockAddr >> 8;
                    194:                        retbuf[3] = nLastBlockAddr;
                    195:                }
                    196:        }
                    197:        else
                    198:        {
                    199:                retbuf[0] = 0x70;
                    200:                if (bSetLastBlockAddr)
                    201:                {
                    202:                        retbuf[0] |= 0x80;
                    203:                        retbuf[4] = nLastBlockAddr >> 16;
                    204:                        retbuf[5] = nLastBlockAddr >> 8;
                    205:                        retbuf[6] = nLastBlockAddr;
                    206:                }
                    207:                switch (nLastError)
                    208:                {
                    209:                 case HD_REQSENS_OK:  retbuf[2] = 0; break;
                    210:                 case HD_REQSENS_OPCODE:  retbuf[2] = 5; break;
                    211:                 case HD_REQSENS_INVADDR:  retbuf[2] = 5; break;
                    212:                 case HD_REQSENS_INVARG:  retbuf[2] = 5; break;
                    213:                 case HD_REQSENS_NODRIVE:  retbuf[2] = 2; break;
                    214:                 default: retbuf[2] = 4; break;
                    215:                }
                    216:                retbuf[7] = 14;
                    217:                retbuf[12] = nLastError;
                    218:                retbuf[19] = nLastBlockAddr >> 16;
                    219:                retbuf[20] = nLastBlockAddr >> 8;
                    220:                retbuf[21] = nLastBlockAddr;
                    221:        }
                    222: 
                    223:        /*
                    224:        fprintf(stderr,"*** Requested sense packet:\n");
                    225:        int i;
                    226:        for (i = 0; i<nRetLen; i++) fprintf(stderr,"%2x ",retbuf[i]);
                    227:        fprintf(stderr,"\n");
                    228:        */
                    229: 
                    230:        memcpy(&STRam[nDmaAddr], retbuf, nRetLen);
                    231: 
                    232:        FDC_SetDMAStatus(FALSE);            /* no DMA error */
                    233:        FDC_AcknowledgeInterrupt();
                    234:        HDCCommand.returnCode = HD_STATUS_OK;
                    235:        //FDCSectorCountRegister = 0;
                    236: }
                    237: 
                    238: 
                    239: /*---------------------------------------------------------------------*/
1.1.1.8   root      240: /**
                    241:  * Mode sense - Get parameters from disk.
                    242:  * (Just enough to make the HDX tool from AHDI 5.0 happy)
                    243:  */
1.1.1.7   root      244: static void HDC_Cmd_ModeSense(void)
                    245: {
                    246:        Uint32 nDmaAddr;
                    247: 
                    248: #ifdef HDC_VERBOSE
                    249:        fprintf(stderr,"HDC: Mode Sense.\n");
                    250: #endif
                    251: 
                    252:        nDmaAddr = FDC_ReadDMAAddress();
                    253: 
                    254:        if (HDCCommand.command[2] == 0 && HD_SECTORCOUNT(HDCCommand) == 0x10)
                    255:        {
                    256:                size_t blocks;
                    257:                blocks = File_Length(ConfigureParams.HardDisk.szHardDiskImage) / 512;
                    258: 
                    259:                STRam[nDmaAddr+0] = 0;
                    260:                STRam[nDmaAddr+1] = 0;
                    261:                STRam[nDmaAddr+2] = 0;
                    262:                STRam[nDmaAddr+3] = 8;
                    263:                STRam[nDmaAddr+4] = 0;
                    264: 
                    265:                STRam[nDmaAddr+5] = blocks >> 16;  // Number of blocks, high (?)
                    266:                STRam[nDmaAddr+6] = blocks >> 8;   // Number of blocks, med (?)
                    267:                STRam[nDmaAddr+7] = blocks;        // Number of blocks, low (?)
                    268: 
                    269:                STRam[nDmaAddr+8] = 0;
                    270: 
                    271:                STRam[nDmaAddr+9] = 0;      // Block size in bytes, high
                    272:                STRam[nDmaAddr+10] = 2;     // Block size in bytes, med
                    273:                STRam[nDmaAddr+11] = 0;     // Block size in bytes, low
                    274: 
                    275:                STRam[nDmaAddr+12] = 0;
                    276:                STRam[nDmaAddr+13] = 0;
                    277:                STRam[nDmaAddr+14] = 0;
                    278:                STRam[nDmaAddr+15] = 0;
                    279: 
                    280:                HDCCommand.returnCode = HD_STATUS_OK;
                    281:                nLastError = HD_REQSENS_OK;
                    282:        }
                    283:        else
                    284:        {
1.1.1.9   root      285:                Log_Printf(LOG_TODO, "HDC: Unsupported MODE SENSE command\n");
1.1.1.7   root      286:                HDCCommand.returnCode = HD_STATUS_ERROR;
                    287:                nLastError = HD_REQSENS_INVARG;
                    288:        }
                    289: 
                    290:        FDC_SetDMAStatus(FALSE);            /* no DMA error */
                    291:        FDC_AcknowledgeInterrupt();
                    292:        bSetLastBlockAddr = FALSE;
                    293:        //FDCSectorCountRegister = 0;
                    294: }
                    295: 
                    296: 
                    297: /*---------------------------------------------------------------------*/
1.1.1.8   root      298: /**
                    299:  * Format drive.
                    300:  */
1.1.1.7   root      301: static void HDC_Cmd_FormatDrive(void)
                    302: {
                    303: #ifdef HDC_VERBOSE
                    304:        fprintf(stderr,"HDC: Format drive!\n");
                    305: #endif
                    306: 
                    307:        /* Should erase the whole image file here... */
                    308: 
                    309:        FDC_SetDMAStatus(FALSE);            /* no DMA error */
                    310:        FDC_AcknowledgeInterrupt();
                    311:        HDCCommand.returnCode = HD_STATUS_OK;
                    312:        nLastError = HD_REQSENS_OK;
                    313:        bSetLastBlockAddr = FALSE;
                    314:        //FDCSectorCountRegister = 0;
1.1       root      315: }
                    316: 
1.1.1.7   root      317: 
1.1       root      318: /*---------------------------------------------------------------------*/
1.1.1.8   root      319: /**
                    320:  * Write a sector off our disk - (seek implied)
                    321:  */
1.1.1.7   root      322: static void HDC_Cmd_WriteSector(void)
1.1       root      323: {
1.1.1.7   root      324:        nLastBlockAddr = HDC_GetOffset();
1.1       root      325: 
1.1.1.7   root      326:        /* seek to the position */
                    327:        if (fseek(hd_image_file, nLastBlockAddr, SEEK_SET) != 0)
                    328:        {
                    329:                HDCCommand.returnCode = HD_STATUS_ERROR;
                    330:                nLastError = HD_REQSENS_INVADDR;
                    331:        }
                    332:        else
                    333:        {
                    334:                HDCCommand.returnCode = HD_STATUS_OK;
                    335:                nLastError = HD_REQSENS_OK;
                    336: 
                    337:                /* write - if allowed */
1.1       root      338: #ifndef DISALLOW_HDC_WRITE
1.1.1.7   root      339:                fwrite(&STRam[FDC_ReadDMAAddress()], 512, HD_SECTORCOUNT(HDCCommand),
                    340:                       hd_image_file);
1.1       root      341: #endif
1.1.1.9   root      342:                /* Update DMA counter */
                    343:                FDC_WriteDMAAddress(FDC_ReadDMAAddress() + 512*HD_SECTORCOUNT(HDCCommand));
1.1.1.7   root      344:        }
1.1       root      345: 
1.1.1.7   root      346:        FDC_SetDMAStatus(FALSE);              /* no DMA error */
                    347:        FDC_AcknowledgeInterrupt();
                    348:        bSetLastBlockAddr = TRUE;
                    349:        //FDCSectorCountRegister = 0;
1.1       root      350: }
                    351: 
1.1.1.7   root      352: 
1.1       root      353: /*---------------------------------------------------------------------*/
1.1.1.8   root      354: /**
                    355:  * Read a sector off our disk - (implied seek)
                    356:  */
1.1.1.7   root      357: static void HDC_Cmd_ReadSector(void)
1.1       root      358: {
1.1.1.7   root      359:        nLastBlockAddr = HDC_GetOffset();
1.1       root      360: 
                    361: #ifdef HDC_VERBOSE
1.1.1.7   root      362:        fprintf(stderr,"Reading %i sectors from 0x%x to addr: 0x%x\n",
                    363:                HD_SECTORCOUNT(HDCCommand), nLastBlockAddr, FDC_ReadDMAAddress());
1.1       root      364: #endif
                    365: 
1.1.1.7   root      366:        /* seek to the position */
                    367:        if (fseek(hd_image_file, nLastBlockAddr, SEEK_SET) != 0)
                    368:        {
                    369:                HDCCommand.returnCode = HD_STATUS_ERROR;
                    370:                nLastError = HD_REQSENS_INVADDR;
                    371:        }
                    372:        else
                    373:        {
                    374:                fread(&STRam[FDC_ReadDMAAddress()], 512, HD_SECTORCOUNT(HDCCommand),
                    375:                      hd_image_file);
                    376:                HDCCommand.returnCode = HD_STATUS_OK;
                    377:                nLastError = HD_REQSENS_OK;
1.1.1.9   root      378: 
                    379:                /* Update DMA counter */
                    380:                FDC_WriteDMAAddress(FDC_ReadDMAAddress() + 512*HD_SECTORCOUNT(HDCCommand));
1.1.1.7   root      381:        }
1.1       root      382: 
1.1.1.7   root      383:        FDC_SetDMAStatus(FALSE);              /* no DMA error */
                    384:        FDC_AcknowledgeInterrupt();
                    385:        bSetLastBlockAddr = TRUE;
                    386:        //FDCSectorCountRegister = 0;
1.1       root      387: }
                    388: 
1.1.1.7   root      389: 
1.1       root      390: /*---------------------------------------------------------------------*/
1.1.1.8   root      391: /**
                    392:  * Emulation routine for HDC command packets.
                    393:  */
1.1       root      394: void HDC_EmulateCommandPacket()
                    395: {
                    396: 
1.1.1.7   root      397:        switch(HD_OPCODE(HDCCommand))
                    398:        {
1.1       root      399: 
1.1.1.7   root      400:         case HD_READ_SECTOR:
                    401:                HDC_Cmd_ReadSector();
                    402:                break;
                    403:         case HD_WRITE_SECTOR:
                    404:                HDC_Cmd_WriteSector();
                    405:                break;
                    406: 
                    407:         case HD_INQUIRY:
                    408:                HDC_Cmd_Inquiry();
                    409:                break;
                    410: 
                    411:         case HD_SEEK:
                    412:                HDC_Cmd_Seek();
                    413:                break;
                    414: 
                    415:         case HD_SHIP:
                    416:                HDCCommand.returnCode = 0xFF;
                    417:                FDC_AcknowledgeInterrupt();
                    418:                break;
                    419: 
                    420:         case HD_REQ_SENSE:
                    421:                HDC_Cmd_RequestSense();
                    422:                break;
                    423: 
                    424:         case HD_MODESELECT:
1.1.1.9   root      425:                Log_Printf(LOG_TODO, "HDC: MODE SELECT call not implemented yet.\n");
1.1.1.7   root      426:                HDCCommand.returnCode = HD_STATUS_OK;
                    427:                nLastError = HD_REQSENS_OK;
                    428:                bSetLastBlockAddr = FALSE;
                    429:                FDC_SetDMAStatus(FALSE);
                    430:                FDC_AcknowledgeInterrupt();
                    431:                break;
                    432: 
                    433:         case HD_MODESENSE:
                    434:                HDC_Cmd_ModeSense();
                    435:                break;
                    436: 
                    437:         case HD_FORMAT_DRIVE:
                    438:                HDC_Cmd_FormatDrive();
                    439:                break;
                    440: 
                    441:         /* as of yet unsupported commands */
                    442:         case HD_VERIFY_TRACK:
                    443:         case HD_FORMAT_TRACK:
                    444:         case HD_CORRECTION:
                    445: 
                    446:         default:
                    447:                HDCCommand.returnCode = HD_STATUS_ERROR;
                    448:                nLastError = HD_REQSENS_OPCODE;
                    449:                bSetLastBlockAddr = FALSE;
                    450:                FDC_AcknowledgeInterrupt();
                    451:                break;
                    452:        }
1.1.1.9   root      453: 
                    454:        /* Update the led each time a command is processed */
                    455:        Statusbar_EnableHDLed();
1.1       root      456: }
                    457: 
1.1.1.7   root      458: 
1.1       root      459: /*---------------------------------------------------------------------*/
1.1.1.8   root      460: /**
                    461:  * Debug routine for HDC command packets.
                    462:  */
1.1.1.7   root      463: #ifdef HDC_REALLY_VERBOSE
1.1       root      464: void HDC_DebugCommandPacket(FILE *hdlogFile)
                    465: {
1.1.1.7   root      466:        int opcode;
                    467:        static const char *psComNames[] =
                    468:        {
                    469:                "TEST UNIT READY",
                    470:                "REZERO",
                    471:                "???",
                    472:                "REQUEST SENSE",
                    473:                "FORMAT DRIVE",
                    474:                "VERIFY TRACK (?)",
                    475:                "FORMAT TRACK (?)",
                    476:                "REASSIGN BLOCK",
                    477:                "READ SECTOR(S)",
                    478:                "???",
                    479:                "WRITE SECTOR(S)",
                    480:                "SEEK",
                    481:                "???",
                    482:                "CORRECTION",
                    483:                "???",
                    484:                "TRANSLATE",
                    485:                "SET ERROR THRESHOLD",  /* 0x10 */
                    486:                "USAGE COUNTERS",
                    487:                "INQUIRY",
                    488:                "WRITE DATA BUFFER",
                    489:                "READ DATA BUFFER",
                    490:                "MODE SELECT",
                    491:                "???",
                    492:                "???",
                    493:                "EXTENDED READ",
                    494:                "READ TOC",
                    495:                "MODE SENSE",
                    496:                "SHIP",
                    497:                "RECEIVE DIAGNOSTICS",
                    498:                "SEND DIAGNOSTICS"
                    499:        };
                    500: 
                    501:        opcode = HD_OPCODE(HDCCommand);
                    502: 
                    503:        fprintf(hdlogFile,"----\n");
                    504: 
                    505:        if (opcode >= 0 && opcode <= (int)(sizeof(psComNames)/sizeof(psComNames[0])))
                    506:        {
                    507:                fprintf(hdlogFile, "HDC opcode 0x%x : %s\n",opcode,psComNames[opcode]);
                    508:        }
                    509:        else
                    510:        {
                    511:                fprintf(hdlogFile, "Unknown HDC opcode!! Value = 0x%x\n", opcode);
                    512:        }
                    513: 
                    514:        fprintf(hdlogFile, "Controller: %i\n", HD_CONTROLLER(HDCCommand));
                    515:        fprintf(hdlogFile, "Drive: %i\n", HD_DRIVENUM(HDCCommand));
                    516:        fprintf(hdlogFile, "LBA: 0x%lx\n", HDC_GetOffset());
                    517: 
                    518:        fprintf(hdlogFile, "Sector count: 0x%x\n", HD_SECTORCOUNT(HDCCommand));
                    519:        fprintf(hdlogFile, "HDC sector count: 0x%x\n", HDCSectorCount);
                    520:        //fprintf(hdlogFile, "FDC sector count: 0x%x\n", FDCSectorCountRegister);
                    521:        fprintf(hdlogFile, "Control byte: 0x%x\n", HD_CONTROL(HDCCommand));
1.1       root      522: }
1.1.1.7   root      523: #endif
                    524: 
1.1       root      525: 
                    526: /*---------------------------------------------------------------------*/
1.1.1.8   root      527: /**
                    528:  * Print data about the hard drive image
                    529:  */
1.1.1.4   root      530: static void HDC_GetInfo(void)
1.1       root      531: {
1.1.1.7   root      532: /* Partition table contains hd size + 4 partition entries
                    533:  * (composed of flag byte, 3 char ID, start offset and size),
                    534:  * this is followed by bad sector list + count and the root sector checksum.
                    535:  * Before this there's the boot code and with ICD hd driver additional 8
                    536:  * partition entries (at offset 0x156).
                    537:  */
                    538: #define HD_PARTITIONTABLE_SIZE (4+4*12)
                    539: #define HD_PARTITIONTABLE_OFFSET 0x1C2
                    540:        long offset;
                    541:        unsigned char hdinfo[HD_PARTITIONTABLE_SIZE];
                    542:        int i;
1.1.1.2   root      543: #ifdef HDC_VERBOSE
1.1.1.7   root      544:        unsigned long size;
1.1.1.2   root      545: #endif
1.1       root      546: 
1.1.1.7   root      547:        nPartitions = 0;
                    548:        if (hd_image_file == NULL)
                    549:                return;
                    550:        offset = ftell(hd_image_file);
                    551: 
                    552:        fseek(hd_image_file, HD_PARTITIONTABLE_OFFSET, 0);
                    553:        fread(hdinfo, HD_PARTITIONTABLE_SIZE, 1, hd_image_file);
1.1       root      554: 
                    555: #ifdef HDC_VERBOSE
1.1.1.7   root      556:        size = (((unsigned long) hdinfo[0] << 24)
                    557:                | ((unsigned long) hdinfo[1] << 16)
                    558:                | ((unsigned long) hdinfo[2] << 8)
                    559:                | ((unsigned long) hdinfo[3]));
                    560: 
                    561:        fprintf(stderr, "Total disk size %li Mb\n", size>>11);
                    562:        /* flags for each partition entry are zero if they are not valid */
                    563:        fprintf(stderr, "Partition 0 exists?: %s\n", (hdinfo[4] != 0)?"Yes":"No");
                    564:        fprintf(stderr, "Partition 1 exists?: %s\n", (hdinfo[4+12] != 0)?"Yes":"No");
                    565:        fprintf(stderr, "Partition 2 exists?: %s\n", (hdinfo[4+24] != 0)?"Yes":"No");
                    566:        fprintf(stderr, "Partition 3 exists?: %s\n", (hdinfo[4+36] != 0)?"Yes":"No");
1.1       root      567: #endif
                    568: 
1.1.1.7   root      569:        for(i=0;i<4;i++)
                    570:                if(hdinfo[4 + 12*i])
                    571:                        nPartitions++;
1.1       root      572: 
1.1.1.7   root      573:        fseek(hd_image_file, offset, 0);
1.1       root      574: }
                    575: 
1.1.1.7   root      576: 
1.1       root      577: /*---------------------------------------------------------------------*/
1.1.1.8   root      578: /**
                    579:  * Open the disk image file, set partitions.
1.1       root      580:  */
1.1.1.9   root      581: bool HDC_Init(char *filename)
1.1       root      582: {
1.1.1.7   root      583:        bAcsiEmuOn = FALSE;
1.1       root      584: 
1.1.1.7   root      585:        /* Sanity check - is file length a multiple of 512? */
                    586:        if (File_Length(filename) & 0x1ff)
                    587:        {
                    588:                Log_Printf(LOG_ERROR, "HD file '%s' has strange size!\n", filename);
                    589:                return FALSE;
                    590:        }
1.1.1.3   root      591: 
1.1.1.7   root      592: 
                    593:        if ((hd_image_file = fopen(filename, "rb+")) == NULL)
                    594:        {
                    595:                Log_Printf(LOG_ERROR, "Can not open HD file '%s'!\n", filename);
                    596:                return FALSE;
                    597:        }
                    598: 
                    599:        HDC_GetInfo();
                    600: 
                    601:        /* set number of partitions */
                    602:        nNumDrives += nPartitions;
                    603: 
                    604:        bAcsiEmuOn = TRUE;
                    605: 
                    606:        return TRUE;
1.1       root      607: }
1.1.1.3   root      608: 
1.1.1.7   root      609: 
1.1       root      610: /*---------------------------------------------------------------------*/
1.1.1.8   root      611: /**
                    612:  * HDC_UnInit - close image file
                    613:  *
1.1       root      614:  */
1.1.1.4   root      615: void HDC_UnInit(void)
1.1       root      616: {
1.1.1.7   root      617:        if (!bAcsiEmuOn)
                    618:                return;
                    619: 
                    620:        fclose(hd_image_file);
                    621:        hd_image_file = NULL;
                    622: 
                    623:        nNumDrives -= nPartitions;
                    624:        nPartitions = 0;
                    625:        bAcsiEmuOn = FALSE;
1.1       root      626: }
                    627: 
1.1.1.7   root      628: 
1.1       root      629: /*---------------------------------------------------------------------*/
1.1.1.8   root      630: /**
                    631:  * Process HDC command packets, called when bytes are 
                    632:  * written to $FFFF8606 and the HDC (not the FDC) is selected.
                    633:  */
1.1.1.4   root      634: void HDC_WriteCommandPacket(void)
1.1       root      635: {
1.1.1.7   root      636:        /* check status byte */
                    637:        if ((DMAModeControl_ff8606wr & 0x0018) != 8)
                    638:                return;
                    639: 
                    640:        /* is HDC emulation enabled? */
                    641:        if (!bAcsiEmuOn)
                    642:                return;
                    643: 
                    644:        /* command byte sent, store it. */
                    645:        HDCCommand.command[HDCCommand.byteCount++] =  (DiskControllerWord_ff8604wr&0xFF);
                    646: 
                    647:        /* have we received a complete 6-byte packet yet? */
                    648:        if (HDCCommand.byteCount >= 6)
                    649:        {
1.1       root      650: 
                    651: #ifdef HDC_REALLY_VERBOSE
1.1.1.7   root      652:                HDC_DebugCommandPacket(stderr);
1.1       root      653: #endif
                    654: 
1.1.1.7   root      655:                /* If it's aimed for our drive, emulate it! */
                    656:                if ((HD_CONTROLLER(HDCCommand)) == 0)
                    657:                {
                    658:                        if (HD_DRIVENUM(HDCCommand) == 0)
                    659:                                HDC_EmulateCommandPacket();
                    660:                        else
                    661:                                Log_Printf(LOG_WARN, "HDC: Program tries to access illegal drive.\n");
                    662:                }
                    663:                else
                    664:                {
                    665:                        /* No drive/controller */
                    666:                        FDC_SetDMAStatus(TRUE);
                    667:                        //FDC_AcknowledgeInterrupt();
                    668:                        HDCCommand.returnCode = HD_STATUS_ERROR;
                    669:                        //FDCSectorCountRegister = 0;
                    670:                        MFP_GPIP |= 0x20;
                    671:                }
                    672: 
                    673:                HDCCommand.byteCount = 0;
                    674:        }
                    675:        else
                    676:        {
                    677:                if (HD_CONTROLLER(HDCCommand) == 0)
                    678:                {
                    679:                        FDC_AcknowledgeInterrupt();
                    680:                        FDC_SetDMAStatus(FALSE);
                    681:                        HDCCommand.returnCode = HD_STATUS_OK;
                    682:                }
                    683:                else
                    684:                {
                    685:                        /* If there's no controller, the interrupt line stays high */
                    686:                        HDCCommand.returnCode = HD_STATUS_ERROR;
                    687:                        MFP_GPIP |= 0x20;
                    688:                }
                    689:        }
1.1       root      690: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.