]> Shamusworld >> Repos - apple2/blobdiff - src/floppy.cpp
Added infrastructure to handle write protecting floppy disks
[apple2] / src / floppy.cpp
index 091467966c8aee4cf1858f922e3a39f3f61f6b86..d4037140d5e652b6474af8a585c0e9889da2934e 100755 (executable)
@@ -37,6 +37,7 @@ uint8 FloppyDrive::doSector[16] = {
        0x0, 0x7, 0xE, 0x6, 0xD, 0x5, 0xC, 0x4, 0xB, 0x3, 0xA, 0x2, 0x9, 0x1, 0x8, 0xF };
 uint8 FloppyDrive::poSector[16] = {
        0x0, 0x8, 0x1, 0x9, 0x2, 0xA, 0x3, 0xB, 0x4, 0xC, 0x5, 0xD, 0x6, 0xE, 0x7, 0xF };
+char FloppyDrive::nameBuf[MAX_PATH];
 
 // FloppyDrive class implementation...
 
@@ -46,6 +47,7 @@ FloppyDrive::FloppyDrive(): motorOn(0), activeDrive(0), ioMode(IO_MODE_READ), ph
        diskSize[0] = diskSize[1] = 0;
        diskType[0] = diskType[1] = DT_UNKNOWN;
        imageDirty[0] = imageDirty[1] = false;
+       writeProtected[0] = writeProtected[1] = false;
        imageName[0][0] = imageName[1][0] = 0;                  // Zero out filenames
 }
 
@@ -145,6 +147,8 @@ bool FloppyDrive::SaveImage(uint8 driveNum/*= 0*/)
        fwrite(disk[driveNum], 1, diskSize[driveNum], fp);
        fclose(fp);
 
+       WriteLog("FLOPPY: Successfully wrote image file '%s'...\n", imageName[driveNum]);
+
        return true;
 }
 
@@ -167,6 +171,7 @@ void FloppyDrive::CreateBlankImage(uint8 driveNum/*= 0*/)
        memset(nybblizedImage[driveNum], 0x00, 232960); // Set it to 0 instead of $FF for proper formatting...
        diskType[driveNum] = DT_DOS33;
        strcpy(imageName[driveNum], "newblank.dsk");
+       writeProtected[driveNum] = false;
 SpawnMessage("New blank image inserted in drive %u...", driveNum);
 }
 
@@ -198,6 +203,10 @@ void FloppyDrive::SwapImages(void)
        uint8 imageDirtyTmp = imageDirty[0];
        imageDirty[0] = imageDirty[1];
        imageDirty[1] = imageDirtyTmp;
+
+       uint8 writeProtectedTmp = writeProtected[0];
+       writeProtected[0] = writeProtected[1];
+       writeProtected[1] = writeProtectedTmp;
 SpawnMessage("Drive 0: %s...", imageName[0]);
 }
 
@@ -462,6 +471,96 @@ void FloppyDrive::DenybblizeImage(uint8 driveNum)
        }
 }
 
+const char * FloppyDrive::GetImageName(uint8 driveNum/*= 0*/)
+{
+       // Set up a zero-length string for return value
+       nameBuf[0] = 0;
+
+       if (driveNum > 1)
+       {
+               WriteLog("FLOPPY: Attempted to get image name for drive #%u!\n", driveNum);
+               return nameBuf;
+       }
+
+       // Now we attempt to strip out extraneous paths/extensions to get just the filename
+       const char * startOfFile = strrchr(imageName[driveNum], '/');
+       const char * startOfExt = strrchr(imageName[driveNum], '.');
+
+       // If there isn't a path, assume we're starting at the beginning
+       if (startOfFile == NULL)
+               startOfFile = &imageName[driveNum][0];
+       else
+               startOfFile++;
+
+       // If there isn't an extension, assume it's at the terminating NULL
+       if (startOfExt == NULL)
+               startOfExt = &imageName[driveNum][0] + strlen(imageName[driveNum]);
+
+       // Now copy the filename (may copy nothing!)
+       int j = 0;
+
+       for(const char * i=startOfFile; i<startOfExt; i++)
+               nameBuf[j++] = *i;
+
+       nameBuf[j] = 0;
+
+       return nameBuf;
+}
+
+void FloppyDrive::EjectImage(uint8 driveNum/*= 0*/)
+{
+       // Probably want to save a dirty image... ;-)
+       SaveImage(driveNum);
+
+       WriteLog("FLOPPY: Ejected image file '%s' from drive %u...\n", imageName[driveNum], driveNum);
+
+       if (disk[driveNum])
+               delete[] disk[driveNum];
+
+       disk[driveNum] = NULL;
+       diskSize[driveNum] = 0;
+       diskType[driveNum] = DT_UNKNOWN;
+       imageDirty[driveNum] = false;
+       writeProtected[driveNum] = false;
+       imageName[driveNum][0] = 0;                     // Zero out filenames
+       memset(nybblizedImage[driveNum], 0xFF, 232960); // Doesn't matter if 00s or FFs...
+}
+
+bool FloppyDrive::DriveIsEmpty(uint8 driveNum/*= 0*/)
+{
+       if (driveNum > 1)
+       {
+               WriteLog("FLOPPY: Attempted DriveIsEmtpy() for drive #%u!\n", driveNum);
+               return true;
+       }
+
+       // This is kinda gay, but it works
+       return (imageName[driveNum][0] == 0 ? true : false);
+}
+
+bool FloppyDrive::DiskIsWriteProtected(uint8 driveNum/*= 0*/)
+{
+       if (driveNum > 1)
+       {
+               WriteLog("FLOPPY: Attempted DiskIsWriteProtected() for drive #%u!\n", driveNum);
+               return true;
+       }
+
+       return writeProtected[driveNum];
+}
+
+void FloppyDrive::SetWriteProtect(bool state, uint8 driveNum/*= 0*/)
+{
+       if (driveNum > 1)
+       {
+               WriteLog("FLOPPY: Attempted set write protect for drive #%u!\n", driveNum);
+               return;
+       }
+
+       writeProtected[driveNum] = state;
+}
+
+
 // Memory mapped I/O functions
 
 /*
@@ -532,8 +631,13 @@ Which we now do. :-)
 */
        if (ioMode == IO_MODE_WRITE && (latchValue & 0x80))
        {
-               nybblizedImage[activeDrive][(track * 6656) + currentPos] = latchValue;
-               imageDirty[activeDrive] = true;
+               // Does it behave like this?
+#warning "Write protection kludged in--investigate real behavior!"
+               if (!writeProtected[activeDrive])
+               {
+                       nybblizedImage[activeDrive][(track * 6656) + currentPos] = latchValue;
+                       imageDirty[activeDrive] = true;
+               }
        }
 
        uint8 diskByte = nybblizedImage[activeDrive][(track * 6656) + currentPos];
@@ -566,3 +670,38 @@ void FloppyDrive::SetWriteMode(void)
        // $C0EF
        ioMode = IO_MODE_WRITE;
 }
+
+/*
+PRODOS 8 MLI ERROR CODES
+
+$00:    No error
+$01:    Bad system call number
+$04:    Bad system call parameter count
+$25:    Interrupt table full
+$27:    I/O error
+$28:    No device connected
+$2B:    Disk write protected
+$2E:    Disk switched
+$40:    Invalid pathname
+$42:    Maximum number of files open
+$43:    Invalid reference number
+$44:    Directory not found
+$45:    Volume not found
+$46:    File not found
+$47:    Duplicate filename
+$48:    Volume full
+$49:    Volume directory full
+$4A:    Incompatible file format, also a ProDOS directory
+$4B:    Unsupported storage_type
+$4C:    End of file encountered
+$4D:    Position out of range
+$4E:    File access error, also file locked
+$50:    File is open
+$51:    Directory structure damaged
+$52:    Not a ProDOS volume
+$53:    Invalid system call parameter
+$55:    Volume Control Block table full
+$56:    Bad buffer address
+$57:    Duplicate volume
+$5A:    File structure damaged
+*/