]> Shamusworld >> Repos - apple2/blobdiff - src/floppy.cpp
Changes to sound system relating to the new threaded CPU core. It works,
[apple2] / src / floppy.cpp
index 59c9e6dfc1a872f938b33deae7f8af4a93b64920..810960448331fe2067885d033e17c29373972f9b 100755 (executable)
@@ -21,7 +21,7 @@
 #include "log.h"
 #include "applevideo.h"                                        // For message spawning... Though there's probably a better approach than this!
 
-using namespace std;
+//using namespace std;
 
 // Useful enums
 
@@ -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
 }
 
@@ -60,6 +62,8 @@ FloppyDrive::~FloppyDrive()
 
 bool FloppyDrive::LoadImage(const char * filename, uint8 driveNum/*= 0*/)
 {
+       WriteLog("FLOPPY: Attempting to load image '%s' in drive #%u.\n", filename, driveNum);
+
        if (driveNum > 1)
        {
                WriteLog("FLOPPY: Attempted to load image to drive #%u!\n", driveNum);
@@ -90,6 +94,21 @@ bool FloppyDrive::LoadImage(const char * filename, uint8 driveNum/*= 0*/)
        DetectImageType(filename, driveNum);
        strcpy(imageName[driveNum], filename);
 
+#if 0
+       WriteLog("FLOPPY: Opening image for drive #%u.\n", driveNum);
+       FILE * fp2 = fopen("bt-nybblized.nyb", "wb");
+
+       if (fp2 == NULL)
+               WriteLog("FLOPPY: Failed to open image file 'bt-nybblized.nyb' for writing...\n");
+       else
+       {
+               fwrite(nybblizedImage[driveNum], 1, 232960, fp2);
+               fclose(fp2);
+       }
+#endif
+//writeProtected[driveNum] = true;
+       WriteLog("FLOPPY: Loaded image '%s' for drive #%u.\n", filename, driveNum);
+
        return true;
 }
 
@@ -129,6 +148,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;
 }
 
@@ -151,6 +172,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);
 }
 
@@ -182,6 +204,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]);
 }
 
@@ -227,8 +253,16 @@ WRT to the disk image itself.
 //*/
                }
 
+// Actually, it just might matter WRT to nybblyzing/denybblyzing
+// Here, we check for BT3
+//Nope, no change...
+//diskType[driveNum] = DT_PRODOS;
+
                NybblizeImage(driveNum);
        }
+
+#warning "Should we attempt to nybblize unknown images here? Definitely SHOULD issue a warning!"
+
 WriteLog("FLOPPY: Detected image type %s...\n", (diskType[driveNum] == DT_NYBBLE ?
        "Nybble image" : (diskType[driveNum] == DT_DOS33 ?
        "DOS 3.3 image" : (diskType[driveNum] == DT_PRODOS ? "ProDOS image" : "unknown"))));
@@ -317,8 +351,16 @@ void FloppyDrive::NybblizeImage(uint8 driveNum)
                        // Using a lookup table, convert the 6-bit bytes into disk bytes.
 
                        for(uint16 i=0; i<343; i++)
+//#define TEST_NYBBLIZATION
+#ifdef TEST_NYBBLIZATION
+{
+WriteLog("FL: i = %u, img[i] = %02X, diskbyte = %02X\n", i, img[i], diskbyte[img[i] >> 2]);
+#endif
                                img[i] = diskbyte[img[i] >> 2];
-
+#ifdef TEST_NYBBLIZATION
+//WriteLog("            img[i] = %02X\n", img[i]);
+}
+#endif
                        img += 343;
 
                        // Done with the nybblization, now for the epilogue...
@@ -430,6 +472,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
 
 /*
@@ -488,7 +620,8 @@ void FloppyDrive::DriveEnable(uint8 addr)
 
 uint8 FloppyDrive::ReadWrite(void)
 {
-SpawnMessage("%sing %s track %u, sector %u...", (ioMode == IO_MODE_READ ? "Read" : "Write"),
+SpawnMessage("%u:%sing %s track %u, sector %u...", activeDrive,
+       (ioMode == IO_MODE_READ ? "Read" : "Write"),
        (ioMode == IO_MODE_READ ? "from" : "to"), track, currentPos / 396);
        // $C0EC
 /*
@@ -499,13 +632,22 @@ 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;
+               }
+               else
+//doesn't seem to do anything
+                       return 0;//is this more like it?
        }
 
        uint8 diskByte = nybblizedImage[activeDrive][(track * 6656) + currentPos];
        currentPos = (currentPos + 1) % 6656;
 
+//WriteLog("FL: diskByte=%02X, currentPos=%u\n", diskByte, currentPos);
        return diskByte;
 }
 
@@ -532,3 +674,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
+*/