Annotation of hatari/src/floppy.c, revision 1.1.1.20

1.1       root        1: /*
1.1.1.5   root        2:   Hatari - floppy.c
1.1       root        3: 
1.1.1.19  root        4:   This file is distributed under the GNU General Public License, version 2
                      5:   or at your option any later version. Read the file gpl.txt for details.
1.1.1.5   root        6: 
1.1.1.12  root        7:   This is where we read/write sectors to/from the disk image buffers.
                      8:   NOTE: these buffers are in memory so we only need to write routines for
                      9:   the .ST format. When the buffer is to be saved (ie eject disk) we save
                     10:   it back to the original file in the correct format (.ST or .MSA).
1.1.1.5   root       11: 
                     12:   There are some important notes about image accessing - as we use TOS and the
1.1.1.9   root       13:   FDC to access the disk the boot-sector MUST be valid. Sometimes this is NOT
                     14:   the case! In these situations we must guess at the disk format. Eg, some disk
1.1.1.5   root       15:   images have a a boot sector which states single-sided, but the images have
                     16:   been created as double-sided. As sides are interleaved we need to read the
                     17:   image as if it was double-sided. Also note that 'NumBytesPerSector' is ALWAYS
                     18:   512 bytes, even if the boot-sector states otherwise.
1.1.1.4   root       19:   Also note that old versions of the MAKEDISK utility do not set the correct
                     20:   boot sector structure for a real ST (and also Hatari) to read it correctly.
                     21:   (PaCifiST will, however, read/write to these images as it does not perform
                     22:   FDC access as on a real ST)
1.1       root       23: */
1.1.1.13  root       24: const char Floppy_fileid[] = "Hatari floppy.c : " __DATE__ " " __TIME__;
1.1.1.8   root       25: 
                     26: #include <sys/stat.h>
1.1.1.12  root       27: #include <assert.h>
1.1.1.4   root       28: #include <SDL_endian.h>
                     29: 
1.1       root       30: #include "main.h"
1.1.1.6   root       31: #include "configuration.h"
1.1       root       32: #include "file.h"
                     33: #include "floppy.h"
1.1.1.9   root       34: #include "gemdos.h"
                     35: #include "hdc.h"
1.1.1.8   root       36: #include "log.h"
1.1       root       37: #include "memorySnapShot.h"
                     38: #include "st.h"
1.1.1.20! root       39: #include "msa.h"
        !            40: #include "dim.h"
        !            41: #include "floppy_ipf.h"
        !            42: #include "floppy_stx.h"
1.1.1.6   root       43: #include "zip.h"
1.1.1.18  root       44: #include "screen.h"
                     45: #include "video.h"
1.1.1.20! root       46: #include "fdc.h"
1.1       root       47: 
1.1.1.10  root       48: 
1.1.1.12  root       49: /* Emulation drive details, eg FileName, Inserted, Changed etc... */
                     50: EMULATION_DRIVE EmulationDrives[MAX_FLOPPYDRIVES];
                     51: /* Drive A is the default */
                     52: int nBootDrive = 0;
1.1       root       53: 
1.1.1.18  root       54: 
1.1.1.9   root       55: /* Possible disk image file extensions to scan for */
1.1.1.10  root       56: static const char * const pszDiskImageNameExts[] =
1.1.1.5   root       57: {
1.1.1.10  root       58:        ".msa",
                     59:        ".st",
                     60:        ".dim",
1.1.1.20! root       61:        ".ipf",
        !            62:        ".raw",
        !            63:        ".ctr",
        !            64:        ".stx",
1.1.1.10  root       65:        NULL
1.1       root       66: };
                     67: 
                     68: 
1.1.1.13  root       69: /* local functions */
1.1.1.18  root       70: static bool    Floppy_EjectBothDrives(void);
                     71: static void    Floppy_DriveTransitionSetState ( int Drive , int State );
1.1.1.13  root       72: 
                     73: 
1.1.1.2   root       74: /*-----------------------------------------------------------------------*/
1.1.1.11  root       75: /**
                     76:  * Initialize emulation floppy drives
                     77:  */
1.1       root       78: void Floppy_Init(void)
                     79: {
1.1.1.10  root       80:        int i;
1.1       root       81: 
1.1.1.10  root       82:        /* Clear drive structures */
1.1.1.12  root       83:        for (i = 0; i < MAX_FLOPPYDRIVES; i++)
1.1.1.10  root       84:        {
1.1.1.12  root       85:                /* Clear structs and if floppies available, insert them */
1.1.1.10  root       86:                memset(&EmulationDrives[i], 0, sizeof(EMULATION_DRIVE));
1.1.1.12  root       87:                if (strlen(ConfigureParams.DiskImage.szDiskFileName[i]) > 0)
                     88:                        Floppy_InsertDiskIntoDrive(i);
1.1.1.10  root       89:        }
1.1       root       90: }
                     91: 
1.1.1.2   root       92: 
                     93: /*-----------------------------------------------------------------------*/
1.1.1.11  root       94: /**
                     95:  * UnInitialize drives
                     96:  */
1.1       root       97: void Floppy_UnInit(void)
                     98: {
1.1.1.10  root       99:        Floppy_EjectBothDrives();
1.1       root      100: }
                    101: 
1.1.1.2   root      102: 
                    103: /*-----------------------------------------------------------------------*/
1.1.1.11  root      104: /**
1.1.1.18  root      105:  * Called on Warm/Cold Reset
                    106:  */
                    107: void Floppy_Reset(void)
                    108: {
                    109:        int     i;
                    110: 
                    111:        /* Cancel any pending disk change transitions */
                    112:        for (i = 0; i < MAX_FLOPPYDRIVES; i++)
                    113:        {
                    114:                EmulationDrives[i].TransitionState1 = 0;
                    115:                EmulationDrives[i].TransitionState2 = 0;
                    116:        }
                    117: }
                    118: 
                    119: 
                    120: /*-----------------------------------------------------------------------*/
                    121: /**
1.1.1.11  root      122:  * Save/Restore snapshot of local variables('MemorySnapShot_Store' handles type)
                    123:  */
1.1.1.12  root      124: void Floppy_MemorySnapShot_Capture(bool bSave)
1.1       root      125: {
1.1.1.10  root      126:        int i;
1.1       root      127: 
1.1.1.10  root      128:        /* If restoring then eject old drives first! */
                    129:        if (!bSave)
                    130:                Floppy_EjectBothDrives();
                    131: 
                    132:        /* Save/Restore details */
1.1.1.12  root      133:        for (i = 0; i < MAX_FLOPPYDRIVES; i++)
1.1.1.10  root      134:        {
1.1.1.20! root      135:                MemorySnapShot_Store(&EmulationDrives[i].ImageType, sizeof(EmulationDrives[i].ImageType));
1.1.1.10  root      136:                MemorySnapShot_Store(&EmulationDrives[i].bDiskInserted, sizeof(EmulationDrives[i].bDiskInserted));
                    137:                MemorySnapShot_Store(&EmulationDrives[i].nImageBytes, sizeof(EmulationDrives[i].nImageBytes));
                    138:                if (!bSave && EmulationDrives[i].bDiskInserted)
                    139:                {
                    140:                        EmulationDrives[i].pBuffer = malloc(EmulationDrives[i].nImageBytes);
                    141:                        if (!EmulationDrives[i].pBuffer)
                    142:                                perror("Floppy_MemorySnapShot_Capture");
                    143:                }
                    144:                if (EmulationDrives[i].pBuffer)
                    145:                        MemorySnapShot_Store(EmulationDrives[i].pBuffer, EmulationDrives[i].nImageBytes);
1.1.1.13  root      146:                MemorySnapShot_Store(EmulationDrives[i].sFileName, sizeof(EmulationDrives[i].sFileName));
1.1.1.10  root      147:                MemorySnapShot_Store(&EmulationDrives[i].bContentsChanged,sizeof(EmulationDrives[i].bContentsChanged));
                    148:                MemorySnapShot_Store(&EmulationDrives[i].bOKToSave,sizeof(EmulationDrives[i].bOKToSave));
1.1.1.18  root      149:                MemorySnapShot_Store(&EmulationDrives[i].TransitionState1,sizeof(EmulationDrives[i].TransitionState1));
                    150:                MemorySnapShot_Store(&EmulationDrives[i].TransitionState1_VBL,sizeof(EmulationDrives[i].TransitionState1_VBL));
                    151:                MemorySnapShot_Store(&EmulationDrives[i].TransitionState2,sizeof(EmulationDrives[i].TransitionState2));
                    152:                MemorySnapShot_Store(&EmulationDrives[i].TransitionState2_VBL,sizeof(EmulationDrives[i].TransitionState2_VBL));
1.1.1.20! root      153: 
        !           154:                /* Because Floppy_EjectBothDrives() was called above before restoring (which cleared */
        !           155:                /* FDC_DRIVES[].DiskInserted that was restored just before), we must call FDC_InsertFloppy */
        !           156:                /* for each restored drive with an inserted disk to set FDC_DRIVES[].DiskInserted=true */
        !           157:                if ( !bSave && ( EmulationDrives[i].bDiskInserted ) )
        !           158:                        FDC_InsertFloppy ( i );
1.1.1.10  root      159:        }
1.1       root      160: }
                    161: 
1.1.1.2   root      162: 
                    163: /*-----------------------------------------------------------------------*/
1.1.1.11  root      164: /**
                    165:  * Find which device to boot from (hard drive or floppy).
                    166:  */
1.1       root      167: void Floppy_GetBootDrive(void)
                    168: {
1.1.1.15  root      169:        /* Default to drive A: */
                    170:        nBootDrive = 0;
                    171: 
                    172:        /* Boot only from hard drive if user wants this */
                    173:        if (!ConfigureParams.HardDisk.bBootFromHardDisk)
                    174:                return;
                    175: 
1.1.1.16  root      176:        if (ACSI_EMU_ON || ConfigureParams.HardDisk.bUseIdeMasterHardDiskImage)
1.1.1.15  root      177:        {
1.1.1.10  root      178:                nBootDrive = 2;  /* Drive C */
1.1.1.15  root      179:        }
                    180:        else if (GEMDOS_EMU_ON)
                    181:        {
                    182:                int i;
                    183:                for (i = 0; i < MAX_HARDDRIVES; i++)
                    184:                {
                    185:                        if (emudrives[i])
                    186:                        {
1.1.1.16  root      187:                                nBootDrive = emudrives[i]->drive_number;
1.1.1.15  root      188:                                break;
                    189:                        }
                    190:                }
                    191:        }
1.1       root      192: }
                    193: 
1.1.1.2   root      194: 
                    195: /*-----------------------------------------------------------------------*/
1.1.1.11  root      196: /**
                    197:  * Test if disk image is write protected. Write protection can be configured
                    198:  * in the GUI. When set to "automatic", we check the file permissions of the
                    199:  * floppy disk image to decide.
                    200:  */
1.1.1.12  root      201: bool Floppy_IsWriteProtected(int Drive)
1.1.1.8   root      202: {
1.1.1.10  root      203:        if (ConfigureParams.DiskImage.nWriteProtection == WRITEPROT_OFF)
                    204:        {
1.1.1.14  root      205:                return false;
1.1.1.10  root      206:        }
                    207:        else if (ConfigureParams.DiskImage.nWriteProtection == WRITEPROT_ON)
                    208:        {
1.1.1.14  root      209:                return true;
1.1.1.10  root      210:        }
                    211:        else
                    212:        {
                    213:                struct stat FloppyStat;
                    214:                /* Check whether disk is writable */
1.1.1.13  root      215:                if (stat(EmulationDrives[Drive].sFileName, &FloppyStat) == 0
1.1.1.12  root      216:                    && (FloppyStat.st_mode & S_IWUSR))
1.1.1.14  root      217:                        return false;
1.1.1.10  root      218:                else
1.1.1.14  root      219:                        return true;
1.1.1.10  root      220:        }
1.1.1.8   root      221: }
                    222: 
                    223: 
                    224: /*-----------------------------------------------------------------------*/
1.1.1.11  root      225: /**
                    226:  * Test disk image for valid boot-sector.
                    227:  * It has been noticed that some disks, eg blank images made by the MakeDisk
                    228:  * utility or PaCifiST emulator fill in the boot-sector with incorrect information.
                    229:  * Such images cannot be read correctly using a real ST, and also Hatari.
                    230:  * To try and prevent data loss, we check for this error and flag the drive so
                    231:  * the image will not be saved back to the file.
                    232:  */
1.1.1.12  root      233: static bool Floppy_IsBootSectorOK(int Drive)
1.1       root      234: {
1.1.1.10  root      235:        Uint8 *pDiskBuffer;
1.1       root      236: 
1.1.1.10  root      237:        /* Does our drive have a disk in? */
                    238:        if (EmulationDrives[Drive].bDiskInserted)
                    239:        {
                    240:                pDiskBuffer = EmulationDrives[Drive].pBuffer;
                    241: 
                    242:                /* Check SPC (byte 13) for !=0 value. If is '0', invalid image and Hatari
                    243:                 * won't be-able to read (nor will a real ST)! */
                    244:                if (pDiskBuffer[13] != 0)
                    245:                {
1.1.1.14  root      246:                        return true;      /* Disk sector is OK! */
1.1.1.10  root      247:                }
                    248:                else
                    249:                {
                    250:                        Log_AlertDlg(LOG_WARN, "Disk in drive %c: maybe suffers from the Pacifist/Makedisk bug.\n"
                    251:                                     "If it does not work, please repair the disk first!\n", 'A' + Drive);
                    252:                }
                    253:        }
1.1       root      254: 
1.1.1.14  root      255:        return false;         /* Bad sector */
1.1       root      256: }
                    257: 
1.1.1.2   root      258: 
                    259: /*-----------------------------------------------------------------------*/
1.1.1.11  root      260: /**
                    261:  * Try to create disk B filename, eg 'auto_100a' becomes 'auto_100b'
                    262:  * Return new filename if think we should try, otherwise NULL
1.1.1.12  root      263:  *
                    264:  * TODO: doesn't work with images in ZIP archives
1.1.1.11  root      265:  */
                    266: static char* Floppy_CreateDiskBFileName(const char *pSrcFileName)
1.1       root      267: {
1.1.1.10  root      268:        char *szDir, *szName, *szExt;
1.1.1.12  root      269: 
1.1.1.10  root      270:        /* Allocate temporary memory for strings: */
                    271:        szDir = malloc(3 * FILENAME_MAX);
                    272:        if (!szDir)
                    273:        {
                    274:                perror("Floppy_CreateDiskBFileName");
1.1.1.14  root      275:                return false;
1.1.1.10  root      276:        }
                    277:        szName = szDir + FILENAME_MAX;
                    278:        szExt = szName + FILENAME_MAX;
                    279: 
                    280:        /* So, first split name into parts */
1.1.1.11  root      281:        File_SplitPath(pSrcFileName, szDir, szName, szExt);
1.1.1.10  root      282: 
                    283:        /* All OK? */
                    284:        if (strlen(szName) > 0)
                    285:        {
1.1.1.12  root      286:                char *last = &(szName[strlen(szName)-1]);
                    287:                /* Now, did filename end with an 'A' or 'a' */
                    288:                if (*last == 'A' || *last == 'a')
1.1.1.10  root      289:                {
1.1.1.11  root      290:                        char *szFull;
1.1.1.10  root      291:                        /* Change 'A' to a 'B' */
1.1.1.12  root      292:                        *last += 1;
1.1.1.10  root      293:                        /* And re-build name into destination */
1.1.1.11  root      294:                        szFull = File_MakePath(szDir, szName, szExt);
                    295:                        if (szFull)
                    296:                        {
                    297:                                /* Does file exist? */
                    298:                                if (File_Exists(szFull))
                    299:                                {
                    300:                                        free(szDir);
                    301:                                        return szFull;
                    302:                                }
                    303:                                free(szFull);
                    304:                        }
1.1.1.10  root      305:                }
                    306:        }
                    307:        free(szDir);
1.1.1.11  root      308:        return NULL;
1.1       root      309: }
                    310: 
1.1.1.2   root      311: 
                    312: /*-----------------------------------------------------------------------*/
1.1.1.11  root      313: /**
1.1.1.12  root      314:  * Set floppy image to be ejected
                    315:  */
                    316: const char* Floppy_SetDiskFileNameNone(int Drive)
                    317: {
                    318:        assert(Drive >= 0 && Drive < MAX_FLOPPYDRIVES);
                    319:        ConfigureParams.DiskImage.szDiskFileName[Drive][0] = '\0';
                    320:        return ConfigureParams.DiskImage.szDiskFileName[Drive];
1.1.1.6   root      321: }
                    322: 
1.1.1.12  root      323: /*-----------------------------------------------------------------------*/
                    324: /**
                    325:  * Set given floppy drive image file name and handle
                    326:  * different image extensions.
                    327:  * Return corrected file name on success and NULL on failure.
1.1.1.11  root      328:  */
1.1.1.12  root      329: const char* Floppy_SetDiskFileName(int Drive, const char *pszFileName, const char *pszZipPath)
1.1.1.6   root      330: {
1.1.1.11  root      331:        char *filename;
1.1.1.16  root      332:        int i;
1.1       root      333: 
1.1.1.12  root      334:        /* setting to empty or "none" ejects */
                    335:        if (!*pszFileName || strcasecmp(pszFileName, "none") == 0)
                    336:        {
                    337:                return Floppy_SetDiskFileNameNone(Drive);
                    338:        }
1.1.1.11  root      339:        /* See if file exists, and if not, get/add correct extension */
1.1.1.12  root      340:        if (!File_Exists(pszFileName))
1.1.1.11  root      341:                filename = File_FindPossibleExtFileName(pszFileName, pszDiskImageNameExts);
                    342:        else
                    343:                filename = strdup(pszFileName);
1.1.1.12  root      344:        if (!filename)
                    345:        {
                    346:                Log_AlertDlg(LOG_INFO, "Image '%s' not found", pszFileName);
                    347:                return NULL;
                    348:        }
                    349: 
                    350:        /* If we insert a disk into Drive A, should we try to put disk 2 into drive B? */
                    351:        if (Drive == 0 && ConfigureParams.DiskImage.bAutoInsertDiskB)
                    352:        {
                    353:                /* Attempt to make up second filename, eg was 'auto_100a' to 'auto_100b' */
                    354:                char *szDiskBFileName = Floppy_CreateDiskBFileName(filename);
                    355:                if (szDiskBFileName)
                    356:                {
                    357:                        /* recurse with Drive B */
                    358:                        Floppy_SetDiskFileName(1, szDiskBFileName, pszZipPath);
                    359:                        free(szDiskBFileName);
                    360:                }
                    361:        }
                    362: 
1.1.1.16  root      363:        /* validity checks */
1.1.1.12  root      364:        assert(Drive >= 0 && Drive < MAX_FLOPPYDRIVES);
1.1.1.16  root      365:        for (i = 0; i < MAX_FLOPPYDRIVES; i++)
                    366:        {
                    367:                if (i == Drive)
                    368:                        continue;
                    369:                /* prevent inserting same image to multiple drives */
                    370:                if (strcmp(filename, ConfigureParams.DiskImage.szDiskFileName[i]) == 0)
                    371:                {
                    372:                        Log_AlertDlg(LOG_ERROR, "ERROR: Cannot insert same floppy to multiple drives!");
                    373:                        return NULL;
                    374:                }
                    375:        }
                    376: 
                    377:        /* do the changes */
1.1.1.12  root      378:        if (pszZipPath)
                    379:                strcpy(ConfigureParams.DiskImage.szDiskZipPath[Drive], pszZipPath);
                    380:        else
                    381:                ConfigureParams.DiskImage.szDiskZipPath[Drive][0] = '\0';
                    382:        strcpy(ConfigureParams.DiskImage.szDiskFileName[Drive], filename);
                    383:        free(filename);
                    384:        //File_MakeAbsoluteName(ConfigureParams.DiskImage.szDiskFileName[Drive]);
                    385:        return ConfigureParams.DiskImage.szDiskFileName[Drive];
                    386: }
                    387: 
                    388: /*-----------------------------------------------------------------------*/
                    389: /**
1.1.1.18  root      390:  * Update the drive when a disk is inserted or ejected. Depending on the state,
                    391:  * we change the Write Protect bit for the drive (the TOS and other programs
                    392:  * monitor this bit to detect that a disk was changed in the drive ; see fdc.c)
                    393:  * The floppy drive transition can be a single action ("eject" or "insert"), or
                    394:  * two actions ("eject then insert" or "insert then eject").
                    395:  * First action is stored in State1 ; State2 store the second (or last) action.
                    396:  * In case the user eject/insert several disks before returning to emulation,
                    397:  * State1 will contain the first action, and State2 the latest action (intermediate
                    398:  * actions are ignored, as they wouldn't be seen while the emulation is paused).
                    399:  * Each action will take FLOPPY_DRIVE_TRANSITION_DELAY_VBL * 2 VBLs to execute,
                    400:  * see fdc.c for details.
                    401:  */
                    402: static void    Floppy_DriveTransitionSetState ( int Drive , int State )
                    403: {
                    404:        /* First, update State1 and State2 depending on the current VBL number */
                    405:        /* (we discard the return value as we don't want to update FDC.STR now) */
                    406:        Floppy_DriveTransitionUpdateState ( Drive );
                    407: 
                    408:        /* If State1 is not defined yet, we set it */
                    409:        if ( EmulationDrives[Drive].TransitionState1 == 0 )
                    410:        {
                    411:                EmulationDrives[Drive].TransitionState1 = State;
                    412:                EmulationDrives[Drive].TransitionState1_VBL = nVBLs;
                    413:                /* Cancel State2 in case we start a new transition before State2 was over */
                    414:                EmulationDrives[Drive].TransitionState2 = 0;    
                    415:        }
                    416: 
                    417:        /* State1 is already set, so we set State2 */
                    418:        else
                    419:        {
                    420:                /* If State2 == State1, ignore it (eg : two inserts in a row) */
                    421:                if ( EmulationDrives[Drive].TransitionState1 == State )
                    422:                        EmulationDrives[Drive].TransitionState2 = 0;
                    423:                else
                    424:                {
                    425:                        /* Set State2 just after State1 ends */
                    426:                        EmulationDrives[Drive].TransitionState2 = State;
                    427:                        EmulationDrives[Drive].TransitionState2_VBL = EmulationDrives[Drive].TransitionState1_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL * 2;
                    428:                }
                    429:        }
                    430: //fprintf ( stderr , "drive transition state1 %d %d state2 %d %d\n" ,
                    431: //       EmulationDrives[Drive].TransitionState1 , EmulationDrives[Drive].TransitionState1_VBL,
                    432: //       EmulationDrives[Drive].TransitionState2 , EmulationDrives[Drive].TransitionState2_VBL );
                    433: }
                    434: 
                    435: 
                    436: /*-----------------------------------------------------------------------*/
                    437: /**
                    438:  * When a disk is inserted or ejected, each transition has 2 phases that
                    439:  * lasts FLOPPY_DRIVE_TRANSITION_DELAY_VBL VBLs. This function checks if
                    440:  * we're during one of these transition phases and tells if the Write
                    441:  * Protect signal should be overwritten.
                    442:  * Returns 0 if there's no change, 1 if WPRT should be forced to 1 and
                    443:  * -1 if WPRT should be forced to 0 (see fdc.c for details).
                    444:  */
                    445: int    Floppy_DriveTransitionUpdateState ( int Drive )
                    446: {
                    447:        int     Force = 0;
                    448: 
                    449:        if ( EmulationDrives[Drive].TransitionState1 != 0 )
                    450:        {
                    451:                if ( nVBLs >= EmulationDrives[Drive].TransitionState1_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL * 2 )
                    452:                        EmulationDrives[Drive].TransitionState1 = 0;    /* State1's delay elapsed */
                    453:                else if ( nVBLs >= EmulationDrives[Drive].TransitionState1_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL )
                    454:                {
                    455:                        if ( EmulationDrives[Drive].TransitionState1 == FLOPPY_DRIVE_TRANSITION_STATE_INSERT )
                    456:                                Force = -1;                             /* Insert phase 2 : clear WPRT */
                    457:                        else
                    458:                                Force = 1;                              /* Eject phase 2 : set WPRT */
                    459:                }
                    460:                else
                    461:                {
                    462:                        if ( EmulationDrives[Drive].TransitionState1 == FLOPPY_DRIVE_TRANSITION_STATE_INSERT )
                    463:                                Force = 1;                              /* Insert phase 1 : set WPRT */
                    464:                        else
                    465:                                Force = -1;                             /* Eject phase 1 : clear WPRT */
                    466:                }
                    467:        }
                    468: 
                    469:        if ( ( EmulationDrives[Drive].TransitionState2 != 0 )
                    470:          && ( nVBLs >= EmulationDrives[Drive].TransitionState2_VBL ) )
                    471:        {
                    472:                if ( nVBLs >= EmulationDrives[Drive].TransitionState2_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL * 2 )
                    473:                        EmulationDrives[Drive].TransitionState2 = 0;    /* State2's delay elapsed */
                    474:                else if ( nVBLs >= EmulationDrives[Drive].TransitionState2_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL )
                    475:                {
                    476:                        if ( EmulationDrives[Drive].TransitionState2 == FLOPPY_DRIVE_TRANSITION_STATE_INSERT )
                    477:                                Force = -1;                             /* Insert phase 2 : clear WPRT */
                    478:                        else
                    479:                                Force = 1;                              /* Eject phase 2 : set WPRT */
                    480:                }
                    481:                else
                    482:                {
                    483:                        if ( EmulationDrives[Drive].TransitionState2 == FLOPPY_DRIVE_TRANSITION_STATE_INSERT )
                    484:                                Force = 1;                              /* Insert phase 1 : set WPRT */
                    485:                        else
                    486:                                Force = -1;                             /* Eject phase 1 : clear WPRT */
                    487:                }
                    488:        }
                    489: 
                    490: 
                    491:        return Force;
                    492: }
                    493: 
                    494: 
                    495: /*-----------------------------------------------------------------------*/
                    496: /**
1.1.1.12  root      497:  * Insert previously set disk file image into floppy drive.
                    498:  * The WHOLE image is copied into Hatari drive buffers, and
                    499:  * uncompressed if necessary.
                    500:  * Return TRUE on success, false otherwise.
                    501:  */
                    502: bool Floppy_InsertDiskIntoDrive(int Drive)
                    503: {
1.1.1.20! root      504:        long    nImageBytes = 0;
        !           505:        char    *filename;
        !           506:        int     ImageType = FLOPPY_IMAGE_TYPE_NONE;
1.1.1.12  root      507: 
                    508:        /* Eject disk, if one is inserted (doesn't inform user) */
                    509:        assert(Drive >= 0 && Drive < MAX_FLOPPYDRIVES);
                    510:        Floppy_EjectDiskFromDrive(Drive);
                    511: 
                    512:        filename = ConfigureParams.DiskImage.szDiskFileName[Drive];
                    513:        if (!filename[0])
                    514:        {
1.1.1.14  root      515:                return true; /* only do eject */
1.1.1.12  root      516:        }
                    517:        if (!File_Exists(filename))
                    518:        {
                    519:                Log_AlertDlg(LOG_INFO, "Image '%s' not found", filename);
1.1.1.14  root      520:                return false;
1.1.1.12  root      521:        }
                    522: 
1.1.1.10  root      523:        /* Check disk image type and read the file: */
1.1.1.14  root      524:        if (MSA_FileNameIsMSA(filename, true))
1.1.1.20! root      525:                EmulationDrives[Drive].pBuffer = MSA_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
1.1.1.14  root      526:        else if (ST_FileNameIsST(filename, true))
1.1.1.20! root      527:                EmulationDrives[Drive].pBuffer = ST_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
1.1.1.14  root      528:        else if (DIM_FileNameIsDIM(filename, true))
1.1.1.20! root      529:                EmulationDrives[Drive].pBuffer = DIM_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
        !           530:        else if (IPF_FileNameIsIPF(filename, true))
        !           531:                EmulationDrives[Drive].pBuffer = IPF_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
        !           532:        else if (STX_FileNameIsSTX(filename, true))
        !           533:                EmulationDrives[Drive].pBuffer = STX_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
1.1.1.11  root      534:        else if (ZIP_FileNameIsZIP(filename))
1.1.1.12  root      535:        {
                    536:                const char *zippath = ConfigureParams.DiskImage.szDiskZipPath[Drive];
1.1.1.20! root      537:                EmulationDrives[Drive].pBuffer = ZIP_ReadDisk(Drive, filename, zippath, &nImageBytes, &ImageType);
1.1.1.12  root      538:        }
1.1.1.11  root      539: 
1.1.1.20! root      540:        if ( (EmulationDrives[Drive].pBuffer == NULL) || ( ImageType == FLOPPY_IMAGE_TYPE_NONE ) )
1.1.1.11  root      541:        {
1.1.1.14  root      542:                return false;
1.1.1.11  root      543:        }
1.1.1.13  root      544: 
1.1.1.20! root      545:        /* For IPF, call specific function to handle the inserted image */
        !           546:        if ( ImageType == FLOPPY_IMAGE_TYPE_IPF )
        !           547:        {
        !           548:                if ( IPF_Insert ( Drive , EmulationDrives[Drive].pBuffer , nImageBytes ) == false )
        !           549:                {
        !           550:                        free ( EmulationDrives[Drive].pBuffer );
        !           551:                        return false;
        !           552:                }
        !           553:        }
        !           554: 
        !           555:        /* For STX, call specific function to handle the inserted image */
        !           556:        else if ( ImageType == FLOPPY_IMAGE_TYPE_STX )
        !           557:        {
        !           558:                if ( STX_Insert ( Drive , filename , EmulationDrives[Drive].pBuffer , nImageBytes ) == false )
        !           559:                {
        !           560:                        free ( EmulationDrives[Drive].pBuffer );
        !           561:                        return false;
        !           562:                }
        !           563:        }
        !           564: 
1.1.1.13  root      565:        /* Store image filename (required for ejecting the disk later!) */
                    566:        strcpy(EmulationDrives[Drive].sFileName, filename);
                    567: 
                    568:        /* Store size and set drive states */
1.1.1.20! root      569:        EmulationDrives[Drive].ImageType = ImageType;
1.1.1.11  root      570:        EmulationDrives[Drive].nImageBytes = nImageBytes;
1.1.1.14  root      571:        EmulationDrives[Drive].bDiskInserted = true;
                    572:        EmulationDrives[Drive].bContentsChanged = false;
1.1.1.20! root      573: 
        !           574:        if ( ( ImageType == FLOPPY_IMAGE_TYPE_ST ) || ( ImageType == FLOPPY_IMAGE_TYPE_MSA )
        !           575:          || ( ImageType == FLOPPY_IMAGE_TYPE_DIM ) )
        !           576:                EmulationDrives[Drive].bOKToSave = Floppy_IsBootSectorOK(Drive);
        !           577:        else if ( ImageType == FLOPPY_IMAGE_TYPE_STX )
        !           578:                EmulationDrives[Drive].bOKToSave = true;
        !           579:        else if ( ImageType == FLOPPY_IMAGE_TYPE_IPF )
        !           580:                EmulationDrives[Drive].bOKToSave = false;
        !           581:        else
        !           582:                EmulationDrives[Drive].bOKToSave = false;
        !           583: 
1.1.1.18  root      584:        Floppy_DriveTransitionSetState ( Drive , FLOPPY_DRIVE_TRANSITION_STATE_INSERT );
1.1.1.20! root      585:        FDC_InsertFloppy ( Drive );
        !           586: 
1.1.1.12  root      587:        Log_Printf(LOG_INFO, "Inserted disk '%s' to drive %c:.",
                    588:                   filename, 'A'+Drive);
1.1.1.14  root      589:        return true;
1.1       root      590: }
                    591: 
1.1.1.2   root      592: 
                    593: /*-----------------------------------------------------------------------*/
1.1.1.11  root      594: /**
                    595:  * Eject disk from floppy drive, save contents back to PCs hard-drive if
                    596:  * they have been changed.
1.1.1.14  root      597:  * Return true if there was something to eject.
1.1.1.11  root      598:  */
1.1.1.12  root      599: bool Floppy_EjectDiskFromDrive(int Drive)
1.1       root      600: {
1.1.1.14  root      601:        bool bEjected = false;
1.1.1.12  root      602: 
1.1.1.10  root      603:        /* Does our drive have a disk in? */
                    604:        if (EmulationDrives[Drive].bDiskInserted)
                    605:        {
1.1.1.16  root      606:                bool bSaved = false;
1.1.1.13  root      607:                char *psFileName = EmulationDrives[Drive].sFileName;
1.1.1.12  root      608: 
1.1.1.10  root      609:                /* OK, has contents changed? If so, need to save */
                    610:                if (EmulationDrives[Drive].bContentsChanged)
                    611:                {
                    612:                        /* Is OK to save image (if boot-sector is bad, don't allow a save) */
1.1.1.20! root      613:                        if (EmulationDrives[Drive].bOKToSave)
1.1.1.10  root      614:                        {
1.1.1.20! root      615:                                /* Save as .MSA, .ST, .DIM, .IPF or .STX image? */
1.1.1.14  root      616:                                if (MSA_FileNameIsMSA(psFileName, true))
1.1.1.20! root      617:                                        bSaved = MSA_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
1.1.1.14  root      618:                                else if (ST_FileNameIsST(psFileName, true))
1.1.1.20! root      619:                                        bSaved = ST_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
1.1.1.14  root      620:                                else if (DIM_FileNameIsDIM(psFileName, true))
1.1.1.20! root      621:                                        bSaved = DIM_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
        !           622:                                else if (IPF_FileNameIsIPF(psFileName, true))
        !           623:                                        bSaved = IPF_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
        !           624:                                else if (STX_FileNameIsSTX(psFileName, true))
        !           625:                                        bSaved = STX_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
1.1.1.12  root      626:                                else if (ZIP_FileNameIsZIP(psFileName))
1.1.1.20! root      627:                                        bSaved = ZIP_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
1.1.1.16  root      628:                                if (bSaved)
                    629:                                        Log_Printf(LOG_INFO, "Updated the contents of floppy image '%s'.", psFileName);
                    630:                                else
                    631:                                        Log_Printf(LOG_INFO, "Writing of this format failed or not supported, discarded the contents\n of floppy image '%s'.", psFileName);
                    632:                        } else
                    633:                                Log_Printf(LOG_INFO, "Writing not possible, discarded the contents of floppy image\n '%s'.", psFileName);
1.1.1.10  root      634:                }
                    635: 
                    636:                /* Inform user that disk has been ejected! */
1.1.1.12  root      637:                Log_Printf(LOG_INFO, "Floppy %c: has been removed from drive.",
                    638:                           'A'+Drive);
                    639: 
1.1.1.18  root      640:                Floppy_DriveTransitionSetState ( Drive , FLOPPY_DRIVE_TRANSITION_STATE_EJECT );
1.1.1.20! root      641:                FDC_EjectFloppy ( Drive );
1.1.1.14  root      642:                bEjected = true;
1.1.1.10  root      643:        }
                    644: 
1.1.1.20! root      645:        /* Free data used by this IPF image */
        !           646:        if ( EmulationDrives[Drive].ImageType == FLOPPY_IMAGE_TYPE_IPF )
        !           647:                IPF_Eject ( Drive );
        !           648:        /* Free data used by this STX image */
        !           649:        else if ( EmulationDrives[Drive].ImageType == FLOPPY_IMAGE_TYPE_STX )
        !           650:                STX_Eject ( Drive );
        !           651: 
        !           652: 
1.1.1.10  root      653:        /* Drive is now empty */
                    654:        if (EmulationDrives[Drive].pBuffer != NULL)
                    655:        {
                    656:                free(EmulationDrives[Drive].pBuffer);
                    657:                EmulationDrives[Drive].pBuffer = NULL;
                    658:        }
1.1.1.12  root      659: 
1.1.1.13  root      660:        EmulationDrives[Drive].sFileName[0] = '\0';
1.1.1.20! root      661:        EmulationDrives[Drive].ImageType = FLOPPY_IMAGE_TYPE_NONE;
1.1.1.10  root      662:        EmulationDrives[Drive].nImageBytes = 0;
1.1.1.14  root      663:        EmulationDrives[Drive].bDiskInserted = false;
                    664:        EmulationDrives[Drive].bContentsChanged = false;
                    665:        EmulationDrives[Drive].bOKToSave = false;
1.1.1.12  root      666: 
                    667:        return bEjected;
1.1       root      668: }
                    669: 
1.1.1.2   root      670: 
                    671: /*-----------------------------------------------------------------------*/
1.1.1.11  root      672: /**
                    673:  * Eject all disk image from floppy drives - call when quit.
1.1.1.14  root      674:  * Return true if there was something to eject.
1.1.1.11  root      675:  */
1.1.1.13  root      676: static bool Floppy_EjectBothDrives(void)
1.1       root      677: {
1.1.1.12  root      678:        bool bEjectedA, bEjectedB;
                    679: 
1.1.1.10  root      680:        /* Eject disk images from drives 'A' and 'B' */
1.1.1.12  root      681:        bEjectedA = Floppy_EjectDiskFromDrive(0);
                    682:        bEjectedB = Floppy_EjectDiskFromDrive(1);
                    683: 
                    684:        return bEjectedA || bEjectedB;
1.1       root      685: }
                    686: 
1.1.1.2   root      687: 
                    688: /*-----------------------------------------------------------------------*/
1.1.1.11  root      689: /**
                    690:  * Double-check information read from boot-sector as this is sometimes found to
1.1.1.17  root      691:  * be incorrect. The .ST image file should be divisible by the sector size,
                    692:  * the sectors per track. the number of tracks and the number of sides.
1.1.1.11  root      693:  * NOTE - Pass information from boot-sector to this function (if we can't
                    694:  * decide we leave it alone).
                    695:  */
1.1.1.17  root      696: static void Floppy_DoubleCheckFormat(long nDiskSize, long nSectorsPerDisk, Uint16 *pnSides, Uint16 *pnSectorsPerTrack)
1.1       root      697: {
1.1.1.17  root      698:        long    TotalSectors;
                    699:        int     Sides_fixed;
                    700:        int     SectorsPerTrack_fixed;
1.1       root      701: 
1.1.1.10  root      702:        /* Now guess at number of sides */
1.1.1.17  root      703:        if ( nDiskSize < (500*1024) )                           /* If size >500k assume 2 sides */
                    704:                Sides_fixed = 1;
1.1.1.10  root      705:        else
1.1.1.17  root      706:                Sides_fixed = 2;
1.1.1.10  root      707: 
1.1.1.17  root      708:        /* Number of 512 bytes sectors for this disk image */
                    709:        TotalSectors = nDiskSize / 512;
                    710: 
                    711:        /* Check some common values */
                    712:        if      ( TotalSectors == 80*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
                    713:        else if ( TotalSectors == 81*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
                    714:        else if ( TotalSectors == 82*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
1.1.1.18  root      715:        else if ( TotalSectors == 83*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
                    716:        else if ( TotalSectors == 84*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
1.1.1.17  root      717:        else if ( TotalSectors == 80*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
                    718:        else if ( TotalSectors == 81*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
                    719:        else if ( TotalSectors == 82*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
1.1.1.18  root      720:        else if ( TotalSectors == 83*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
                    721:        else if ( TotalSectors == 84*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
1.1.1.17  root      722:        else if ( TotalSectors == 80*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
                    723:        else if ( TotalSectors == 81*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
                    724:        else if ( TotalSectors == 82*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
1.1.1.18  root      725:        else if ( TotalSectors == 83*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
                    726:        else if ( TotalSectors == 84*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
1.1.1.17  root      727:        else if ( TotalSectors == 80*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
                    728:        else if ( TotalSectors == 81*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
                    729:        else if ( TotalSectors == 82*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
1.1.1.18  root      730:        else if ( TotalSectors == 83*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
                    731:        else if ( TotalSectors == 84*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
1.1.1.17  root      732: 
                    733:        /* unknown combination, assume boot sector is correct */
                    734:        else                                            { SectorsPerTrack_fixed = *pnSectorsPerTrack; }
                    735: 
                    736:        /* Valid new values if necessary */
                    737:        if ( ( *pnSides != Sides_fixed ) || ( *pnSectorsPerTrack != SectorsPerTrack_fixed ) )
1.1.1.10  root      738:        {
1.1.1.17  root      739: #if 0
                    740:                int TracksPerDisk_fixed = TotalSectors / ( SectorsPerTrack_fixed * Sides_fixed );
                    741:                Log_Printf(LOG_WARN, "Floppy_DoubleCheckFormat: boot sector doesn't match disk image's size :"
                    742:                        " total sectors %ld->%ld sides %d->%d sectors %d->%d tracks %d\n",
                    743:                        nSectorsPerDisk , TotalSectors , *pnSides , Sides_fixed , *pnSectorsPerTrack , SectorsPerTrack_fixed , TracksPerDisk_fixed );
                    744: #endif
                    745:                *pnSides = Sides_fixed;
                    746:                *pnSectorsPerTrack = SectorsPerTrack_fixed;
1.1.1.10  root      747:        }
1.1       root      748: }
                    749: 
1.1.1.2   root      750: 
                    751: /*-----------------------------------------------------------------------*/
1.1.1.11  root      752: /**
                    753:  * Find details of disk image. We need to do this via a function as sometimes the boot-block
                    754:  * is not actually correct with the image - some demos/game disks have incorrect bytes in the
                    755:  * boot sector and this attempts to find the correct values.
                    756:  */
1.1.1.9   root      757: void Floppy_FindDiskDetails(const Uint8 *pBuffer, int nImageBytes,
1.1.1.12  root      758:                             Uint16 *pnSectorsPerTrack, Uint16 *pnSides)
1.1       root      759: {
1.1.1.17  root      760:        Uint16 nSectorsPerTrack, nSides, nSectorsPerDisk;
1.1       root      761: 
1.1.1.10  root      762:        /* First do check to find number of sectors and bytes per sector */
                    763:        nSectorsPerTrack = SDL_SwapLE16(*(const Uint16 *)(pBuffer+24));   /* SPT */
                    764:        nSides = SDL_SwapLE16(*(const Uint16 *)(pBuffer+26));             /* SIDE */
1.1.1.17  root      765:        nSectorsPerDisk = pBuffer[19] | (pBuffer[20] << 8);               /* total sectors */
1.1.1.10  root      766: 
                    767:        /* If the number of sectors announced is incorrect, the boot-sector may
                    768:         * contain incorrect information, eg the 'Eat.st' demo, or wrongly imaged
                    769:         * single/double sided floppies... */
1.1.1.17  root      770:        if (nSectorsPerDisk != nImageBytes/512)
                    771:                Floppy_DoubleCheckFormat(nImageBytes, nSectorsPerDisk, &nSides, &nSectorsPerTrack);
1.1.1.10  root      772: 
                    773:        /* And set values */
                    774:        if (pnSectorsPerTrack)
                    775:                *pnSectorsPerTrack = nSectorsPerTrack;
                    776:        if (pnSides)
                    777:                *pnSides = nSides;
1.1       root      778: }
                    779: 
1.1.1.2   root      780: 
                    781: /*-----------------------------------------------------------------------*/
1.1.1.11  root      782: /**
                    783:  * Read sectors from floppy disk image, return TRUE if all OK
                    784:  * NOTE Pass -ve as Count to read whole track
                    785:  */
1.1.1.20! root      786: bool Floppy_ReadSectors(int Drive, Uint8 **pBuffer, Uint16 Sector,
1.1.1.12  root      787:                         Uint16 Track, Uint16 Side, short Count,
1.1.1.18  root      788:                         int *pnSectorsPerTrack, int *pSectorSize)
1.1       root      789: {
1.1.1.10  root      790:        Uint8 *pDiskBuffer;
1.1.1.12  root      791:        Uint16 nSectorsPerTrack, nSides, nBytesPerTrack;
1.1.1.10  root      792:        long Offset;
                    793:        int nImageTracks;
                    794: 
                    795:        /* Do we have a disk in our drive? */
                    796:        if (EmulationDrives[Drive].bDiskInserted)
                    797:        {
                    798:                /* Looks good */
                    799:                pDiskBuffer = EmulationDrives[Drive].pBuffer;
                    800: 
                    801:                /* Find #sides and #sectors per track */
                    802:                Floppy_FindDiskDetails(EmulationDrives[Drive].pBuffer,EmulationDrives[Drive].nImageBytes,&nSectorsPerTrack,&nSides);
                    803:                nImageTracks = ((EmulationDrives[Drive].nImageBytes / NUMBYTESPERSECTOR) / nSectorsPerTrack) / nSides;
                    804: 
                    805:                /* Need to read whole track? */
                    806:                if (Count<0)
                    807:                        Count = nSectorsPerTrack;
                    808:                /* Write back number of sector per track */
                    809:                if (pnSectorsPerTrack)
                    810:                        *pnSectorsPerTrack = nSectorsPerTrack;
                    811: 
1.1.1.18  root      812:                if (pSectorSize)
                    813:                        *pSectorSize = NUMBYTESPERSECTOR;                       /* Size is 512 bytes for ST/MSA */
                    814: 
1.1.1.10  root      815:                /* Debug check as if we read over the end of a track we read into side 2! */
                    816:                if (Count > nSectorsPerTrack)
                    817:                {
                    818:                        Log_Printf(LOG_DEBUG, "Floppy_ReadSectors: reading over single track\n");
                    819:                }
                    820: 
                    821:                /* Check that the side number (0 or 1) does not exceed the amount of sides (1 or 2).
                    822:                 * (E.g. some games like Drakkhen or Bolo can load additional data from the
                    823:                 * second disk side, but they also work with single side floppy drives) */
                    824:                if (Side >= nSides)
                    825:                {
                    826:                        Log_Printf(LOG_DEBUG, "Floppy_ReadSectors: Program tries to read from side %i "
                    827:                                   "of a disk image with %i sides!\n", Side+1, nSides);
1.1.1.14  root      828:                        return false;
1.1.1.10  root      829:                }
                    830: 
                    831:                /* Check if track number is in range */
                    832:                if (Track >= nImageTracks)
                    833:                {
                    834:                        Log_Printf(LOG_DEBUG, "Floppy_ReadSectors: Program tries to read from track %i "
                    835:                                   "of a disk image with only %i tracks!\n", Track, nImageTracks);
1.1.1.14  root      836:                        return false;
1.1.1.10  root      837:                }
                    838: 
                    839:                /* Check if sector number is in range */
                    840:                if (Sector <= 0 || Sector > nSectorsPerTrack)
                    841:                {
                    842:                        Log_Printf(LOG_DEBUG, "Floppy_ReadSectors: Program tries to read from sector %i "
                    843:                                   "of a disk image with %i sectors per track!\n", Sector, nSectorsPerTrack);
1.1.1.14  root      844:                        return false;
1.1.1.10  root      845:                }
                    846: 
                    847:                /* Seek to sector */
                    848:                nBytesPerTrack = NUMBYTESPERSECTOR*nSectorsPerTrack;
                    849:                Offset = nBytesPerTrack*Side;                 /* First seek to side */
                    850:                Offset += (nBytesPerTrack*nSides)*Track;      /* Then seek to track */
                    851:                Offset += (NUMBYTESPERSECTOR*(Sector-1));     /* And finally to sector */
1.1.1.6   root      852: 
1.1.1.20! root      853:                /* Return a pointer to the sectors data (usually 512 bytes per sector) */
        !           854:                *pBuffer = pDiskBuffer+Offset;
1.1       root      855: 
1.1.1.14  root      856:                return true;
1.1.1.10  root      857:        }
1.1       root      858: 
1.1.1.14  root      859:        return false;
1.1       root      860: }
                    861: 
1.1.1.2   root      862: 
                    863: /*-----------------------------------------------------------------------*/
1.1.1.11  root      864: /**
                    865:  * Write sectors from floppy disk image, return TRUE if all OK
                    866:  * NOTE Pass -ve as Count to write whole track
                    867:  */
1.1.1.12  root      868: bool Floppy_WriteSectors(int Drive, Uint8 *pBuffer, Uint16 Sector,
                    869:                          Uint16 Track, Uint16 Side, short Count,
1.1.1.18  root      870:                          int *pnSectorsPerTrack, int *pSectorSize)
1.1       root      871: {
1.1.1.10  root      872:        Uint8 *pDiskBuffer;
1.1.1.12  root      873:        Uint16 nSectorsPerTrack, nSides, nBytesPerTrack;
1.1.1.10  root      874:        long Offset;
                    875:        int nImageTracks;
                    876: 
                    877:        /* Do we have a writable disk in our drive? */
                    878:        if (EmulationDrives[Drive].bDiskInserted && !Floppy_IsWriteProtected(Drive))
                    879:        {
                    880:                /* Looks good */
                    881:                pDiskBuffer = EmulationDrives[Drive].pBuffer;
                    882: 
                    883:                /* Find #sides and #sectors per track */
                    884:                Floppy_FindDiskDetails(EmulationDrives[Drive].pBuffer,EmulationDrives[Drive].nImageBytes,&nSectorsPerTrack,&nSides);
                    885:                nImageTracks = ((EmulationDrives[Drive].nImageBytes / NUMBYTESPERSECTOR) / nSectorsPerTrack) / nSides;
                    886: 
                    887:                /* Need to write whole track? */
                    888:                if (Count<0)
                    889:                        Count = nSectorsPerTrack;
                    890:                /* Write back number of sector per track */
                    891:                if (pnSectorsPerTrack)
                    892:                        *pnSectorsPerTrack = nSectorsPerTrack;
                    893: 
1.1.1.18  root      894:                if (pSectorSize)
                    895:                        *pSectorSize = NUMBYTESPERSECTOR;                       /* Size is 512 bytes for ST/MSA */
                    896: 
1.1.1.10  root      897:                /* Debug check as if we write over the end of a track we write into side 2! */
                    898:                if (Count > nSectorsPerTrack)
                    899:                {
                    900:                        Log_Printf(LOG_DEBUG, "Floppy_WriteSectors: writing over single track\n");
                    901:                }
                    902: 
                    903:                /* Check that the side number (0 or 1) does not exceed the amount of sides (1 or 2). */
                    904:                if (Side >= nSides)
                    905:                {
                    906:                        Log_Printf(LOG_DEBUG, "Floppy_WriteSectors: Program tries to write to side %i "
                    907:                                   "of a disk image with %i sides!\n", Side+1, nSides);
1.1.1.14  root      908:                        return false;
1.1.1.10  root      909:                }
                    910: 
                    911:                /* Check if track number is in range */
                    912:                if (Track >= nImageTracks)
                    913:                {
                    914:                        Log_Printf(LOG_DEBUG, "Floppy_WriteSectors: Program tries to write to track %i "
                    915:                                   "of a disk image with only %i tracks!\n", Track, nImageTracks);
1.1.1.14  root      916:                        return false;
1.1.1.10  root      917:                }
                    918: 
                    919:                /* Check if sector number is in range */
                    920:                if (Sector <= 0 || Sector > nSectorsPerTrack)
                    921:                {
                    922:                        Log_Printf(LOG_DEBUG, "Floppy_WriteSectors: Program tries to write to sector %i "
                    923:                                   "of a disk image with %i sectors per track!\n", Sector, nSectorsPerTrack);
1.1.1.14  root      924:                        return false;
1.1.1.10  root      925:                }
                    926: 
                    927:                /* Seek to sector */
                    928:                nBytesPerTrack = NUMBYTESPERSECTOR*nSectorsPerTrack;
                    929:                Offset = nBytesPerTrack*Side;               /* First seek to side */
                    930:                Offset += (nBytesPerTrack*nSides)*Track;    /* Then seek to track */
                    931:                Offset += (NUMBYTESPERSECTOR*(Sector-1));   /* And finally to sector */
                    932: 
                    933:                /* Write sectors (usually 512 bytes per sector) */
                    934:                memcpy(pDiskBuffer+Offset, pBuffer, (int)Count*NUMBYTESPERSECTOR);
                    935:                /* And set 'changed' flag */
1.1.1.14  root      936:                EmulationDrives[Drive].bContentsChanged = true;
1.1       root      937: 
1.1.1.14  root      938:                return true;
1.1.1.10  root      939:        }
1.1       root      940: 
1.1.1.14  root      941:        return false;
1.1       root      942: }

unix.superglobalmegacorp.com

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