|
|
1.1 ! root 1: /* ! 2: Hatari tool: Magic Shadow Archiver - floppy.c ! 3: ! 4: This file is distributed under the GNU Public License, version 2 or at ! 5: your option any later version. Read the file gpl.txt for details. ! 6: ! 7: Check for valid floppy disk geometry. ! 8: */ ! 9: ! 10: #include <stdio.h> ! 11: #include <SDL_endian.h> ! 12: ! 13: #include "hmsa.h" ! 14: #include "floppy.h" ! 15: ! 16: ! 17: /** ! 18: * Double-check information read from boot-sector as this is sometimes found to ! 19: * be incorrect. The .ST image file should be divisible by the sector size and ! 20: * sectors per track. ! 21: * NOTE - Pass information from boot-sector to this function (if we can't ! 22: * decide we leave it alone). ! 23: */ ! 24: static void Floppy_DoubleCheckFormat(long nDiskSize, Uint16 *pnSides, Uint16 *pnSectorsPerTrack) ! 25: { ! 26: int nSectorsPerTrack; ! 27: long TotalSectors; ! 28: ! 29: /* Now guess at number of sides */ ! 30: if (nDiskSize < (500*1024)) /* Is size is >500k assume 2 sides to disk! */ ! 31: *pnSides = 1; ! 32: else ! 33: *pnSides = 2; ! 34: ! 35: /* And Sectors Per Track(always 512 bytes per sector) */ ! 36: TotalSectors = nDiskSize/512; /* # Sectors on disk image */ ! 37: /* Does this match up with what we've read from boot-sector? */ ! 38: nSectorsPerTrack = *pnSectorsPerTrack; ! 39: if (nSectorsPerTrack==0) /* Check valid, default to 9 */ ! 40: nSectorsPerTrack = 9; ! 41: if ((TotalSectors%nSectorsPerTrack)!=0) ! 42: { ! 43: /* No, we have an invalid boot-sector - re-calculate from disk size */ ! 44: if ((TotalSectors%9)==0) /* Work in this order.... */ ! 45: *pnSectorsPerTrack = 9; ! 46: else if ((TotalSectors%10)==0) ! 47: *pnSectorsPerTrack = 10; ! 48: else if ((TotalSectors%11)==0) ! 49: *pnSectorsPerTrack = 11; ! 50: else if ((TotalSectors%12)==0) ! 51: *pnSectorsPerTrack = 12; ! 52: } ! 53: /* else unknown, assume boot-sector is correct!!! */ ! 54: } ! 55: ! 56: ! 57: /** ! 58: * Find details of disk image. We need to do this via a function as sometimes the boot-block ! 59: * is not actually correct with the image - some demos/game disks have incorrect bytes in the ! 60: * boot sector and this attempts to find the correct values. ! 61: */ ! 62: void Floppy_FindDiskDetails(const Uint8 *pBuffer, int nImageBytes, ! 63: unsigned short *pnSectorsPerTrack, unsigned short *pnSides) ! 64: { ! 65: Uint16 nSectorsPerTrack, nSides, nSectors; ! 66: ! 67: /* First do check to find number of sectors and bytes per sector */ ! 68: nSectorsPerTrack = SDL_SwapLE16(*(const Uint16 *)(pBuffer+24)); /* SPT */ ! 69: nSides = SDL_SwapLE16(*(const Uint16 *)(pBuffer+26)); /* SIDE */ ! 70: nSectors = pBuffer[19] | (pBuffer[20] << 8); /* total sectors */ ! 71: ! 72: /* If the number of sectors announced is incorrect, the boot-sector may ! 73: * contain incorrect information, eg the 'Eat.st' demo, or wrongly imaged ! 74: * single/double sided floppies... */ ! 75: if (nSectors != nImageBytes/512) ! 76: Floppy_DoubleCheckFormat(nImageBytes, &nSides, &nSectorsPerTrack); ! 77: ! 78: /* And set values */ ! 79: if (pnSectorsPerTrack) ! 80: *pnSectorsPerTrack = nSectorsPerTrack; ! 81: if (pnSides) ! 82: *pnSides = nSides; ! 83: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.