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

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.21! root      275:                return NULL;
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!");
1.1.1.21! root      373:                        free(filename);
1.1.1.16  root      374:                        return NULL;
                    375:                }
                    376:        }
                    377: 
                    378:        /* do the changes */
1.1.1.12  root      379:        if (pszZipPath)
                    380:                strcpy(ConfigureParams.DiskImage.szDiskZipPath[Drive], pszZipPath);
                    381:        else
                    382:                ConfigureParams.DiskImage.szDiskZipPath[Drive][0] = '\0';
                    383:        strcpy(ConfigureParams.DiskImage.szDiskFileName[Drive], filename);
                    384:        free(filename);
                    385:        //File_MakeAbsoluteName(ConfigureParams.DiskImage.szDiskFileName[Drive]);
                    386:        return ConfigureParams.DiskImage.szDiskFileName[Drive];
                    387: }
                    388: 
                    389: /*-----------------------------------------------------------------------*/
                    390: /**
1.1.1.18  root      391:  * Update the drive when a disk is inserted or ejected. Depending on the state,
                    392:  * we change the Write Protect bit for the drive (the TOS and other programs
                    393:  * monitor this bit to detect that a disk was changed in the drive ; see fdc.c)
                    394:  * The floppy drive transition can be a single action ("eject" or "insert"), or
                    395:  * two actions ("eject then insert" or "insert then eject").
                    396:  * First action is stored in State1 ; State2 store the second (or last) action.
                    397:  * In case the user eject/insert several disks before returning to emulation,
                    398:  * State1 will contain the first action, and State2 the latest action (intermediate
                    399:  * actions are ignored, as they wouldn't be seen while the emulation is paused).
                    400:  * Each action will take FLOPPY_DRIVE_TRANSITION_DELAY_VBL * 2 VBLs to execute,
                    401:  * see fdc.c for details.
                    402:  */
                    403: static void    Floppy_DriveTransitionSetState ( int Drive , int State )
                    404: {
                    405:        /* First, update State1 and State2 depending on the current VBL number */
                    406:        /* (we discard the return value as we don't want to update FDC.STR now) */
                    407:        Floppy_DriveTransitionUpdateState ( Drive );
                    408: 
                    409:        /* If State1 is not defined yet, we set it */
                    410:        if ( EmulationDrives[Drive].TransitionState1 == 0 )
                    411:        {
                    412:                EmulationDrives[Drive].TransitionState1 = State;
                    413:                EmulationDrives[Drive].TransitionState1_VBL = nVBLs;
                    414:                /* Cancel State2 in case we start a new transition before State2 was over */
                    415:                EmulationDrives[Drive].TransitionState2 = 0;    
                    416:        }
                    417: 
                    418:        /* State1 is already set, so we set State2 */
                    419:        else
                    420:        {
                    421:                /* If State2 == State1, ignore it (eg : two inserts in a row) */
                    422:                if ( EmulationDrives[Drive].TransitionState1 == State )
                    423:                        EmulationDrives[Drive].TransitionState2 = 0;
                    424:                else
                    425:                {
                    426:                        /* Set State2 just after State1 ends */
                    427:                        EmulationDrives[Drive].TransitionState2 = State;
                    428:                        EmulationDrives[Drive].TransitionState2_VBL = EmulationDrives[Drive].TransitionState1_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL * 2;
                    429:                }
                    430:        }
                    431: //fprintf ( stderr , "drive transition state1 %d %d state2 %d %d\n" ,
                    432: //       EmulationDrives[Drive].TransitionState1 , EmulationDrives[Drive].TransitionState1_VBL,
                    433: //       EmulationDrives[Drive].TransitionState2 , EmulationDrives[Drive].TransitionState2_VBL );
                    434: }
                    435: 
                    436: 
                    437: /*-----------------------------------------------------------------------*/
                    438: /**
                    439:  * When a disk is inserted or ejected, each transition has 2 phases that
                    440:  * lasts FLOPPY_DRIVE_TRANSITION_DELAY_VBL VBLs. This function checks if
                    441:  * we're during one of these transition phases and tells if the Write
                    442:  * Protect signal should be overwritten.
                    443:  * Returns 0 if there's no change, 1 if WPRT should be forced to 1 and
                    444:  * -1 if WPRT should be forced to 0 (see fdc.c for details).
                    445:  */
                    446: int    Floppy_DriveTransitionUpdateState ( int Drive )
                    447: {
                    448:        int     Force = 0;
                    449: 
                    450:        if ( EmulationDrives[Drive].TransitionState1 != 0 )
                    451:        {
                    452:                if ( nVBLs >= EmulationDrives[Drive].TransitionState1_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL * 2 )
                    453:                        EmulationDrives[Drive].TransitionState1 = 0;    /* State1's delay elapsed */
                    454:                else if ( nVBLs >= EmulationDrives[Drive].TransitionState1_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL )
                    455:                {
                    456:                        if ( EmulationDrives[Drive].TransitionState1 == FLOPPY_DRIVE_TRANSITION_STATE_INSERT )
                    457:                                Force = -1;                             /* Insert phase 2 : clear WPRT */
                    458:                        else
                    459:                                Force = 1;                              /* Eject phase 2 : set WPRT */
                    460:                }
                    461:                else
                    462:                {
                    463:                        if ( EmulationDrives[Drive].TransitionState1 == FLOPPY_DRIVE_TRANSITION_STATE_INSERT )
                    464:                                Force = 1;                              /* Insert phase 1 : set WPRT */
                    465:                        else
                    466:                                Force = -1;                             /* Eject phase 1 : clear WPRT */
                    467:                }
                    468:        }
                    469: 
                    470:        if ( ( EmulationDrives[Drive].TransitionState2 != 0 )
                    471:          && ( nVBLs >= EmulationDrives[Drive].TransitionState2_VBL ) )
                    472:        {
                    473:                if ( nVBLs >= EmulationDrives[Drive].TransitionState2_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL * 2 )
                    474:                        EmulationDrives[Drive].TransitionState2 = 0;    /* State2's delay elapsed */
                    475:                else if ( nVBLs >= EmulationDrives[Drive].TransitionState2_VBL + FLOPPY_DRIVE_TRANSITION_DELAY_VBL )
                    476:                {
                    477:                        if ( EmulationDrives[Drive].TransitionState2 == FLOPPY_DRIVE_TRANSITION_STATE_INSERT )
                    478:                                Force = -1;                             /* Insert phase 2 : clear WPRT */
                    479:                        else
                    480:                                Force = 1;                              /* Eject phase 2 : set WPRT */
                    481:                }
                    482:                else
                    483:                {
                    484:                        if ( EmulationDrives[Drive].TransitionState2 == FLOPPY_DRIVE_TRANSITION_STATE_INSERT )
                    485:                                Force = 1;                              /* Insert phase 1 : set WPRT */
                    486:                        else
                    487:                                Force = -1;                             /* Eject phase 1 : clear WPRT */
                    488:                }
                    489:        }
                    490: 
                    491: 
                    492:        return Force;
                    493: }
                    494: 
                    495: 
                    496: /*-----------------------------------------------------------------------*/
                    497: /**
1.1.1.12  root      498:  * Insert previously set disk file image into floppy drive.
                    499:  * The WHOLE image is copied into Hatari drive buffers, and
                    500:  * uncompressed if necessary.
                    501:  * Return TRUE on success, false otherwise.
                    502:  */
                    503: bool Floppy_InsertDiskIntoDrive(int Drive)
                    504: {
1.1.1.20  root      505:        long    nImageBytes = 0;
                    506:        char    *filename;
                    507:        int     ImageType = FLOPPY_IMAGE_TYPE_NONE;
1.1.1.12  root      508: 
                    509:        /* Eject disk, if one is inserted (doesn't inform user) */
                    510:        assert(Drive >= 0 && Drive < MAX_FLOPPYDRIVES);
                    511:        Floppy_EjectDiskFromDrive(Drive);
                    512: 
                    513:        filename = ConfigureParams.DiskImage.szDiskFileName[Drive];
                    514:        if (!filename[0])
                    515:        {
1.1.1.14  root      516:                return true; /* only do eject */
1.1.1.12  root      517:        }
                    518:        if (!File_Exists(filename))
                    519:        {
                    520:                Log_AlertDlg(LOG_INFO, "Image '%s' not found", filename);
1.1.1.14  root      521:                return false;
1.1.1.12  root      522:        }
                    523: 
1.1.1.10  root      524:        /* Check disk image type and read the file: */
1.1.1.14  root      525:        if (MSA_FileNameIsMSA(filename, true))
1.1.1.20  root      526:                EmulationDrives[Drive].pBuffer = MSA_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
1.1.1.14  root      527:        else if (ST_FileNameIsST(filename, true))
1.1.1.20  root      528:                EmulationDrives[Drive].pBuffer = ST_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
1.1.1.14  root      529:        else if (DIM_FileNameIsDIM(filename, true))
1.1.1.20  root      530:                EmulationDrives[Drive].pBuffer = DIM_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
                    531:        else if (IPF_FileNameIsIPF(filename, true))
                    532:                EmulationDrives[Drive].pBuffer = IPF_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
                    533:        else if (STX_FileNameIsSTX(filename, true))
                    534:                EmulationDrives[Drive].pBuffer = STX_ReadDisk(Drive, filename, &nImageBytes, &ImageType);
1.1.1.11  root      535:        else if (ZIP_FileNameIsZIP(filename))
1.1.1.12  root      536:        {
                    537:                const char *zippath = ConfigureParams.DiskImage.szDiskZipPath[Drive];
1.1.1.20  root      538:                EmulationDrives[Drive].pBuffer = ZIP_ReadDisk(Drive, filename, zippath, &nImageBytes, &ImageType);
1.1.1.12  root      539:        }
1.1.1.11  root      540: 
1.1.1.20  root      541:        if ( (EmulationDrives[Drive].pBuffer == NULL) || ( ImageType == FLOPPY_IMAGE_TYPE_NONE ) )
1.1.1.11  root      542:        {
1.1.1.14  root      543:                return false;
1.1.1.11  root      544:        }
1.1.1.13  root      545: 
1.1.1.20  root      546:        /* For IPF, call specific function to handle the inserted image */
                    547:        if ( ImageType == FLOPPY_IMAGE_TYPE_IPF )
                    548:        {
                    549:                if ( IPF_Insert ( Drive , EmulationDrives[Drive].pBuffer , nImageBytes ) == false )
                    550:                {
                    551:                        free ( EmulationDrives[Drive].pBuffer );
                    552:                        return false;
                    553:                }
                    554:        }
                    555: 
                    556:        /* For STX, call specific function to handle the inserted image */
                    557:        else if ( ImageType == FLOPPY_IMAGE_TYPE_STX )
                    558:        {
                    559:                if ( STX_Insert ( Drive , filename , EmulationDrives[Drive].pBuffer , nImageBytes ) == false )
                    560:                {
                    561:                        free ( EmulationDrives[Drive].pBuffer );
                    562:                        return false;
                    563:                }
                    564:        }
                    565: 
1.1.1.13  root      566:        /* Store image filename (required for ejecting the disk later!) */
                    567:        strcpy(EmulationDrives[Drive].sFileName, filename);
                    568: 
                    569:        /* Store size and set drive states */
1.1.1.20  root      570:        EmulationDrives[Drive].ImageType = ImageType;
1.1.1.11  root      571:        EmulationDrives[Drive].nImageBytes = nImageBytes;
1.1.1.14  root      572:        EmulationDrives[Drive].bDiskInserted = true;
                    573:        EmulationDrives[Drive].bContentsChanged = false;
1.1.1.20  root      574: 
                    575:        if ( ( ImageType == FLOPPY_IMAGE_TYPE_ST ) || ( ImageType == FLOPPY_IMAGE_TYPE_MSA )
                    576:          || ( ImageType == FLOPPY_IMAGE_TYPE_DIM ) )
                    577:                EmulationDrives[Drive].bOKToSave = Floppy_IsBootSectorOK(Drive);
                    578:        else if ( ImageType == FLOPPY_IMAGE_TYPE_STX )
                    579:                EmulationDrives[Drive].bOKToSave = true;
                    580:        else if ( ImageType == FLOPPY_IMAGE_TYPE_IPF )
                    581:                EmulationDrives[Drive].bOKToSave = false;
                    582:        else
                    583:                EmulationDrives[Drive].bOKToSave = false;
                    584: 
1.1.1.18  root      585:        Floppy_DriveTransitionSetState ( Drive , FLOPPY_DRIVE_TRANSITION_STATE_INSERT );
1.1.1.20  root      586:        FDC_InsertFloppy ( Drive );
                    587: 
1.1.1.12  root      588:        Log_Printf(LOG_INFO, "Inserted disk '%s' to drive %c:.",
                    589:                   filename, 'A'+Drive);
1.1.1.14  root      590:        return true;
1.1       root      591: }
                    592: 
1.1.1.2   root      593: 
                    594: /*-----------------------------------------------------------------------*/
1.1.1.11  root      595: /**
                    596:  * Eject disk from floppy drive, save contents back to PCs hard-drive if
                    597:  * they have been changed.
1.1.1.14  root      598:  * Return true if there was something to eject.
1.1.1.11  root      599:  */
1.1.1.12  root      600: bool Floppy_EjectDiskFromDrive(int Drive)
1.1       root      601: {
1.1.1.14  root      602:        bool bEjected = false;
1.1.1.12  root      603: 
1.1.1.10  root      604:        /* Does our drive have a disk in? */
                    605:        if (EmulationDrives[Drive].bDiskInserted)
                    606:        {
1.1.1.16  root      607:                bool bSaved = false;
1.1.1.13  root      608:                char *psFileName = EmulationDrives[Drive].sFileName;
1.1.1.12  root      609: 
1.1.1.10  root      610:                /* OK, has contents changed? If so, need to save */
                    611:                if (EmulationDrives[Drive].bContentsChanged)
                    612:                {
                    613:                        /* Is OK to save image (if boot-sector is bad, don't allow a save) */
1.1.1.20  root      614:                        if (EmulationDrives[Drive].bOKToSave)
1.1.1.10  root      615:                        {
1.1.1.20  root      616:                                /* Save as .MSA, .ST, .DIM, .IPF or .STX image? */
1.1.1.14  root      617:                                if (MSA_FileNameIsMSA(psFileName, true))
1.1.1.20  root      618:                                        bSaved = MSA_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
1.1.1.14  root      619:                                else if (ST_FileNameIsST(psFileName, true))
1.1.1.20  root      620:                                        bSaved = ST_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
1.1.1.14  root      621:                                else if (DIM_FileNameIsDIM(psFileName, true))
1.1.1.20  root      622:                                        bSaved = DIM_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
                    623:                                else if (IPF_FileNameIsIPF(psFileName, true))
                    624:                                        bSaved = IPF_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
                    625:                                else if (STX_FileNameIsSTX(psFileName, true))
                    626:                                        bSaved = STX_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
1.1.1.12  root      627:                                else if (ZIP_FileNameIsZIP(psFileName))
1.1.1.20  root      628:                                        bSaved = ZIP_WriteDisk(Drive, psFileName, EmulationDrives[Drive].pBuffer, EmulationDrives[Drive].nImageBytes);
1.1.1.16  root      629:                                if (bSaved)
                    630:                                        Log_Printf(LOG_INFO, "Updated the contents of floppy image '%s'.", psFileName);
                    631:                                else
                    632:                                        Log_Printf(LOG_INFO, "Writing of this format failed or not supported, discarded the contents\n of floppy image '%s'.", psFileName);
                    633:                        } else
                    634:                                Log_Printf(LOG_INFO, "Writing not possible, discarded the contents of floppy image\n '%s'.", psFileName);
1.1.1.10  root      635:                }
                    636: 
                    637:                /* Inform user that disk has been ejected! */
1.1.1.12  root      638:                Log_Printf(LOG_INFO, "Floppy %c: has been removed from drive.",
                    639:                           'A'+Drive);
                    640: 
1.1.1.18  root      641:                Floppy_DriveTransitionSetState ( Drive , FLOPPY_DRIVE_TRANSITION_STATE_EJECT );
1.1.1.20  root      642:                FDC_EjectFloppy ( Drive );
1.1.1.14  root      643:                bEjected = true;
1.1.1.10  root      644:        }
                    645: 
1.1.1.20  root      646:        /* Free data used by this IPF image */
                    647:        if ( EmulationDrives[Drive].ImageType == FLOPPY_IMAGE_TYPE_IPF )
                    648:                IPF_Eject ( Drive );
                    649:        /* Free data used by this STX image */
                    650:        else if ( EmulationDrives[Drive].ImageType == FLOPPY_IMAGE_TYPE_STX )
                    651:                STX_Eject ( Drive );
                    652: 
                    653: 
1.1.1.10  root      654:        /* Drive is now empty */
                    655:        if (EmulationDrives[Drive].pBuffer != NULL)
                    656:        {
                    657:                free(EmulationDrives[Drive].pBuffer);
                    658:                EmulationDrives[Drive].pBuffer = NULL;
                    659:        }
1.1.1.12  root      660: 
1.1.1.13  root      661:        EmulationDrives[Drive].sFileName[0] = '\0';
1.1.1.20  root      662:        EmulationDrives[Drive].ImageType = FLOPPY_IMAGE_TYPE_NONE;
1.1.1.10  root      663:        EmulationDrives[Drive].nImageBytes = 0;
1.1.1.14  root      664:        EmulationDrives[Drive].bDiskInserted = false;
                    665:        EmulationDrives[Drive].bContentsChanged = false;
                    666:        EmulationDrives[Drive].bOKToSave = false;
1.1.1.12  root      667: 
                    668:        return bEjected;
1.1       root      669: }
                    670: 
1.1.1.2   root      671: 
                    672: /*-----------------------------------------------------------------------*/
1.1.1.11  root      673: /**
                    674:  * Eject all disk image from floppy drives - call when quit.
1.1.1.14  root      675:  * Return true if there was something to eject.
1.1.1.11  root      676:  */
1.1.1.13  root      677: static bool Floppy_EjectBothDrives(void)
1.1       root      678: {
1.1.1.12  root      679:        bool bEjectedA, bEjectedB;
                    680: 
1.1.1.10  root      681:        /* Eject disk images from drives 'A' and 'B' */
1.1.1.12  root      682:        bEjectedA = Floppy_EjectDiskFromDrive(0);
                    683:        bEjectedB = Floppy_EjectDiskFromDrive(1);
                    684: 
                    685:        return bEjectedA || bEjectedB;
1.1       root      686: }
                    687: 
1.1.1.2   root      688: 
                    689: /*-----------------------------------------------------------------------*/
1.1.1.11  root      690: /**
                    691:  * Double-check information read from boot-sector as this is sometimes found to
1.1.1.17  root      692:  * be incorrect. The .ST image file should be divisible by the sector size,
                    693:  * the sectors per track. the number of tracks and the number of sides.
1.1.1.11  root      694:  * NOTE - Pass information from boot-sector to this function (if we can't
                    695:  * decide we leave it alone).
                    696:  */
1.1.1.17  root      697: static void Floppy_DoubleCheckFormat(long nDiskSize, long nSectorsPerDisk, Uint16 *pnSides, Uint16 *pnSectorsPerTrack)
1.1       root      698: {
1.1.1.17  root      699:        long    TotalSectors;
                    700:        int     Sides_fixed;
                    701:        int     SectorsPerTrack_fixed;
1.1       root      702: 
1.1.1.10  root      703:        /* Now guess at number of sides */
1.1.1.17  root      704:        if ( nDiskSize < (500*1024) )                           /* If size >500k assume 2 sides */
                    705:                Sides_fixed = 1;
1.1.1.10  root      706:        else
1.1.1.17  root      707:                Sides_fixed = 2;
1.1.1.10  root      708: 
1.1.1.17  root      709:        /* Number of 512 bytes sectors for this disk image */
                    710:        TotalSectors = nDiskSize / 512;
                    711: 
                    712:        /* Check some common values */
                    713:        if      ( TotalSectors == 80*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
                    714:        else if ( TotalSectors == 81*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
                    715:        else if ( TotalSectors == 82*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
1.1.1.18  root      716:        else if ( TotalSectors == 83*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
                    717:        else if ( TotalSectors == 84*9*Sides_fixed )    { SectorsPerTrack_fixed = 9; }
1.1.1.17  root      718:        else if ( TotalSectors == 80*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
                    719:        else if ( TotalSectors == 81*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
                    720:        else if ( TotalSectors == 82*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
1.1.1.18  root      721:        else if ( TotalSectors == 83*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
                    722:        else if ( TotalSectors == 84*10*Sides_fixed )   { SectorsPerTrack_fixed = 10; }
1.1.1.17  root      723:        else if ( TotalSectors == 80*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
                    724:        else if ( TotalSectors == 81*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
                    725:        else if ( TotalSectors == 82*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
1.1.1.18  root      726:        else if ( TotalSectors == 83*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
                    727:        else if ( TotalSectors == 84*11*Sides_fixed )   { SectorsPerTrack_fixed = 11; }
1.1.1.17  root      728:        else if ( TotalSectors == 80*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
                    729:        else if ( TotalSectors == 81*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
                    730:        else if ( TotalSectors == 82*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
1.1.1.18  root      731:        else if ( TotalSectors == 83*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
                    732:        else if ( TotalSectors == 84*12*Sides_fixed )   { SectorsPerTrack_fixed = 12; }
1.1.1.17  root      733: 
                    734:        /* unknown combination, assume boot sector is correct */
                    735:        else                                            { SectorsPerTrack_fixed = *pnSectorsPerTrack; }
                    736: 
                    737:        /* Valid new values if necessary */
                    738:        if ( ( *pnSides != Sides_fixed ) || ( *pnSectorsPerTrack != SectorsPerTrack_fixed ) )
1.1.1.10  root      739:        {
1.1.1.17  root      740: #if 0
                    741:                int TracksPerDisk_fixed = TotalSectors / ( SectorsPerTrack_fixed * Sides_fixed );
                    742:                Log_Printf(LOG_WARN, "Floppy_DoubleCheckFormat: boot sector doesn't match disk image's size :"
                    743:                        " total sectors %ld->%ld sides %d->%d sectors %d->%d tracks %d\n",
                    744:                        nSectorsPerDisk , TotalSectors , *pnSides , Sides_fixed , *pnSectorsPerTrack , SectorsPerTrack_fixed , TracksPerDisk_fixed );
                    745: #endif
                    746:                *pnSides = Sides_fixed;
                    747:                *pnSectorsPerTrack = SectorsPerTrack_fixed;
1.1.1.10  root      748:        }
1.1       root      749: }
                    750: 
1.1.1.2   root      751: 
                    752: /*-----------------------------------------------------------------------*/
1.1.1.11  root      753: /**
                    754:  * Find details of disk image. We need to do this via a function as sometimes the boot-block
                    755:  * is not actually correct with the image - some demos/game disks have incorrect bytes in the
                    756:  * boot sector and this attempts to find the correct values.
                    757:  */
1.1.1.9   root      758: void Floppy_FindDiskDetails(const Uint8 *pBuffer, int nImageBytes,
1.1.1.12  root      759:                             Uint16 *pnSectorsPerTrack, Uint16 *pnSides)
1.1       root      760: {
1.1.1.17  root      761:        Uint16 nSectorsPerTrack, nSides, nSectorsPerDisk;
1.1       root      762: 
1.1.1.10  root      763:        /* First do check to find number of sectors and bytes per sector */
                    764:        nSectorsPerTrack = SDL_SwapLE16(*(const Uint16 *)(pBuffer+24));   /* SPT */
                    765:        nSides = SDL_SwapLE16(*(const Uint16 *)(pBuffer+26));             /* SIDE */
1.1.1.17  root      766:        nSectorsPerDisk = pBuffer[19] | (pBuffer[20] << 8);               /* total sectors */
1.1.1.10  root      767: 
                    768:        /* If the number of sectors announced is incorrect, the boot-sector may
                    769:         * contain incorrect information, eg the 'Eat.st' demo, or wrongly imaged
                    770:         * single/double sided floppies... */
1.1.1.17  root      771:        if (nSectorsPerDisk != nImageBytes/512)
                    772:                Floppy_DoubleCheckFormat(nImageBytes, nSectorsPerDisk, &nSides, &nSectorsPerTrack);
1.1.1.10  root      773: 
                    774:        /* And set values */
                    775:        if (pnSectorsPerTrack)
                    776:                *pnSectorsPerTrack = nSectorsPerTrack;
                    777:        if (pnSides)
                    778:                *pnSides = nSides;
1.1       root      779: }
                    780: 
1.1.1.2   root      781: 
                    782: /*-----------------------------------------------------------------------*/
1.1.1.11  root      783: /**
                    784:  * Read sectors from floppy disk image, return TRUE if all OK
                    785:  * NOTE Pass -ve as Count to read whole track
                    786:  */
1.1.1.20  root      787: bool Floppy_ReadSectors(int Drive, Uint8 **pBuffer, Uint16 Sector,
1.1.1.12  root      788:                         Uint16 Track, Uint16 Side, short Count,
1.1.1.18  root      789:                         int *pnSectorsPerTrack, int *pSectorSize)
1.1       root      790: {
1.1.1.10  root      791:        Uint8 *pDiskBuffer;
1.1.1.12  root      792:        Uint16 nSectorsPerTrack, nSides, nBytesPerTrack;
1.1.1.10  root      793:        long Offset;
                    794:        int nImageTracks;
                    795: 
                    796:        /* Do we have a disk in our drive? */
                    797:        if (EmulationDrives[Drive].bDiskInserted)
                    798:        {
                    799:                /* Looks good */
                    800:                pDiskBuffer = EmulationDrives[Drive].pBuffer;
                    801: 
                    802:                /* Find #sides and #sectors per track */
                    803:                Floppy_FindDiskDetails(EmulationDrives[Drive].pBuffer,EmulationDrives[Drive].nImageBytes,&nSectorsPerTrack,&nSides);
                    804:                nImageTracks = ((EmulationDrives[Drive].nImageBytes / NUMBYTESPERSECTOR) / nSectorsPerTrack) / nSides;
                    805: 
                    806:                /* Need to read whole track? */
                    807:                if (Count<0)
                    808:                        Count = nSectorsPerTrack;
                    809:                /* Write back number of sector per track */
                    810:                if (pnSectorsPerTrack)
                    811:                        *pnSectorsPerTrack = nSectorsPerTrack;
                    812: 
1.1.1.18  root      813:                if (pSectorSize)
                    814:                        *pSectorSize = NUMBYTESPERSECTOR;                       /* Size is 512 bytes for ST/MSA */
                    815: 
1.1.1.10  root      816:                /* Debug check as if we read over the end of a track we read into side 2! */
                    817:                if (Count > nSectorsPerTrack)
                    818:                {
                    819:                        Log_Printf(LOG_DEBUG, "Floppy_ReadSectors: reading over single track\n");
                    820:                }
                    821: 
                    822:                /* Check that the side number (0 or 1) does not exceed the amount of sides (1 or 2).
                    823:                 * (E.g. some games like Drakkhen or Bolo can load additional data from the
                    824:                 * second disk side, but they also work with single side floppy drives) */
                    825:                if (Side >= nSides)
                    826:                {
                    827:                        Log_Printf(LOG_DEBUG, "Floppy_ReadSectors: Program tries to read from side %i "
                    828:                                   "of a disk image with %i sides!\n", Side+1, nSides);
1.1.1.14  root      829:                        return false;
1.1.1.10  root      830:                }
                    831: 
                    832:                /* Check if track number is in range */
                    833:                if (Track >= nImageTracks)
                    834:                {
                    835:                        Log_Printf(LOG_DEBUG, "Floppy_ReadSectors: Program tries to read from track %i "
                    836:                                   "of a disk image with only %i tracks!\n", Track, nImageTracks);
1.1.1.14  root      837:                        return false;
1.1.1.10  root      838:                }
                    839: 
                    840:                /* Check if sector number is in range */
                    841:                if (Sector <= 0 || Sector > nSectorsPerTrack)
                    842:                {
                    843:                        Log_Printf(LOG_DEBUG, "Floppy_ReadSectors: Program tries to read from sector %i "
                    844:                                   "of a disk image with %i sectors per track!\n", Sector, nSectorsPerTrack);
1.1.1.14  root      845:                        return false;
1.1.1.10  root      846:                }
                    847: 
                    848:                /* Seek to sector */
                    849:                nBytesPerTrack = NUMBYTESPERSECTOR*nSectorsPerTrack;
                    850:                Offset = nBytesPerTrack*Side;                 /* First seek to side */
                    851:                Offset += (nBytesPerTrack*nSides)*Track;      /* Then seek to track */
                    852:                Offset += (NUMBYTESPERSECTOR*(Sector-1));     /* And finally to sector */
1.1.1.6   root      853: 
1.1.1.20  root      854:                /* Return a pointer to the sectors data (usually 512 bytes per sector) */
                    855:                *pBuffer = pDiskBuffer+Offset;
1.1       root      856: 
1.1.1.14  root      857:                return true;
1.1.1.10  root      858:        }
1.1       root      859: 
1.1.1.14  root      860:        return false;
1.1       root      861: }
                    862: 
1.1.1.2   root      863: 
                    864: /*-----------------------------------------------------------------------*/
1.1.1.11  root      865: /**
                    866:  * Write sectors from floppy disk image, return TRUE if all OK
                    867:  * NOTE Pass -ve as Count to write whole track
                    868:  */
1.1.1.12  root      869: bool Floppy_WriteSectors(int Drive, Uint8 *pBuffer, Uint16 Sector,
                    870:                          Uint16 Track, Uint16 Side, short Count,
1.1.1.18  root      871:                          int *pnSectorsPerTrack, int *pSectorSize)
1.1       root      872: {
1.1.1.10  root      873:        Uint8 *pDiskBuffer;
1.1.1.12  root      874:        Uint16 nSectorsPerTrack, nSides, nBytesPerTrack;
1.1.1.10  root      875:        long Offset;
                    876:        int nImageTracks;
                    877: 
                    878:        /* Do we have a writable disk in our drive? */
                    879:        if (EmulationDrives[Drive].bDiskInserted && !Floppy_IsWriteProtected(Drive))
                    880:        {
                    881:                /* Looks good */
                    882:                pDiskBuffer = EmulationDrives[Drive].pBuffer;
                    883: 
                    884:                /* Find #sides and #sectors per track */
                    885:                Floppy_FindDiskDetails(EmulationDrives[Drive].pBuffer,EmulationDrives[Drive].nImageBytes,&nSectorsPerTrack,&nSides);
                    886:                nImageTracks = ((EmulationDrives[Drive].nImageBytes / NUMBYTESPERSECTOR) / nSectorsPerTrack) / nSides;
                    887: 
                    888:                /* Need to write whole track? */
                    889:                if (Count<0)
                    890:                        Count = nSectorsPerTrack;
                    891:                /* Write back number of sector per track */
                    892:                if (pnSectorsPerTrack)
                    893:                        *pnSectorsPerTrack = nSectorsPerTrack;
                    894: 
1.1.1.18  root      895:                if (pSectorSize)
                    896:                        *pSectorSize = NUMBYTESPERSECTOR;                       /* Size is 512 bytes for ST/MSA */
                    897: 
1.1.1.10  root      898:                /* Debug check as if we write over the end of a track we write into side 2! */
                    899:                if (Count > nSectorsPerTrack)
                    900:                {
                    901:                        Log_Printf(LOG_DEBUG, "Floppy_WriteSectors: writing over single track\n");
                    902:                }
                    903: 
                    904:                /* Check that the side number (0 or 1) does not exceed the amount of sides (1 or 2). */
                    905:                if (Side >= nSides)
                    906:                {
                    907:                        Log_Printf(LOG_DEBUG, "Floppy_WriteSectors: Program tries to write to side %i "
                    908:                                   "of a disk image with %i sides!\n", Side+1, nSides);
1.1.1.14  root      909:                        return false;
1.1.1.10  root      910:                }
                    911: 
                    912:                /* Check if track number is in range */
                    913:                if (Track >= nImageTracks)
                    914:                {
                    915:                        Log_Printf(LOG_DEBUG, "Floppy_WriteSectors: Program tries to write to track %i "
                    916:                                   "of a disk image with only %i tracks!\n", Track, nImageTracks);
1.1.1.14  root      917:                        return false;
1.1.1.10  root      918:                }
                    919: 
                    920:                /* Check if sector number is in range */
                    921:                if (Sector <= 0 || Sector > nSectorsPerTrack)
                    922:                {
                    923:                        Log_Printf(LOG_DEBUG, "Floppy_WriteSectors: Program tries to write to sector %i "
                    924:                                   "of a disk image with %i sectors per track!\n", Sector, nSectorsPerTrack);
1.1.1.14  root      925:                        return false;
1.1.1.10  root      926:                }
                    927: 
                    928:                /* Seek to sector */
                    929:                nBytesPerTrack = NUMBYTESPERSECTOR*nSectorsPerTrack;
                    930:                Offset = nBytesPerTrack*Side;               /* First seek to side */
                    931:                Offset += (nBytesPerTrack*nSides)*Track;    /* Then seek to track */
                    932:                Offset += (NUMBYTESPERSECTOR*(Sector-1));   /* And finally to sector */
                    933: 
                    934:                /* Write sectors (usually 512 bytes per sector) */
                    935:                memcpy(pDiskBuffer+Offset, pBuffer, (int)Count*NUMBYTESPERSECTOR);
                    936:                /* And set 'changed' flag */
1.1.1.14  root      937:                EmulationDrives[Drive].bContentsChanged = true;
1.1       root      938: 
1.1.1.14  root      939:                return true;
1.1.1.10  root      940:        }
1.1       root      941: 
1.1.1.14  root      942:        return false;
1.1       root      943: }

unix.superglobalmegacorp.com

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