diff --git a/LRingBuffer.cpp b/LRingBuffer.cpp index 18cc14a..7b7034a 100644 --- a/LRingBuffer.cpp +++ b/LRingBuffer.cpp @@ -20,60 +20,50 @@ LRingBuffer::~LRingBuffer() bool LRingBuffer::Serialize(int8_t fd) { - g_filemanager->writeByte(fd, RINGBUFFERMAGIC); + uint8_t buf[9] = { RINGBUFFERMAGIC, + (max >> 8) & 0xFF, + (max ) & 0xFF, + (ptr >> 8) & 0xFF, + (ptr ) & 0xFF, + (fill >> 8) & 0xFF, + (fill ) & 0xFF, + (cursor >> 8) & 0xFF, + (cursor ) & 0xFF }; + if (g_filemanager->write(fd, buf, 9) != 9) + return false; - g_filemanager->writeByte(fd, (max >> 8) & 0xFF); - g_filemanager->writeByte(fd, (max ) & 0xFF); + if (g_filemanager->write(fd, buffer, max) != max) + return false; - g_filemanager->writeByte(fd, (ptr >> 8) & 0xFF); - g_filemanager->writeByte(fd, (ptr ) & 0xFF); - - g_filemanager->writeByte(fd, (fill >> 8) & 0xFF); - g_filemanager->writeByte(fd, (fill ) & 0xFF); - - g_filemanager->writeByte(fd, (cursor >> 8) & 0xFF); - g_filemanager->writeByte(fd, (cursor ) & 0xFF); - - for (uint16_t i=0; iwriteByte(fd, buffer[i]); - } - - g_filemanager->writeByte(fd, RINGBUFFERMAGIC); + if (g_filemanager->write(fd, buf, 1) != 1) + return false; return true; } bool LRingBuffer::Deserialize(int8_t fd) { - if (g_filemanager->readByte(fd) != RINGBUFFERMAGIC) + uint8_t buf[9]; + if (g_filemanager->read(fd, buf, 9) != 9) + return false; + if (buf[0] != RINGBUFFERMAGIC) return false; - max = g_filemanager->readByte(fd); - max <<= 8; - max |= g_filemanager->readByte(fd); - - ptr = g_filemanager->readByte(fd); - ptr <<= 8; - ptr |= g_filemanager->readByte(fd); - - fill = g_filemanager->readByte(fd); - fill <<= 8; - fill |= g_filemanager->readByte(fd); - - cursor = g_filemanager->readByte(fd); - cursor <<= 8; - cursor |= g_filemanager->readByte(fd); + max = (buf[1] << 8) | buf[2]; + ptr = (buf[3] << 8) | buf[4]; + fill = (buf[5] << 8) | buf[6]; + cursor = (buf[7] << 8) | buf[8]; if (buffer) free(buffer); buffer = (uint8_t *)malloc(max); - - for (uint16_t i=0; ireadByte(fd); - } - if (g_filemanager->readByte(fd) != RINGBUFFERMAGIC) + if (g_filemanager->read(fd, buffer, max) != max) + return false; + + if (g_filemanager->read(fd, buf, 1) != 1 || + buf[0] != RINGBUFFERMAGIC) return false; return true; diff --git a/apple/applemmu.cpp b/apple/applemmu.cpp index c5f4f5f..46b74b8 100644 --- a/apple/applemmu.cpp +++ b/apple/applemmu.cpp @@ -162,22 +162,22 @@ AppleMMU::~AppleMMU() bool AppleMMU::Serialize(int8_t fd) { - g_filemanager->writeByte(fd, MMUMAGIC); - - g_filemanager->writeByte(fd, (switches >> 8) & 0xFF); - g_filemanager->writeByte(fd, (switches ) & 0xFF); - - g_filemanager->writeByte(fd, auxRamRead ? 1 : 0); - g_filemanager->writeByte(fd, auxRamWrite ? 1 : 0); - g_filemanager->writeByte(fd, bank2 ? 1 : 0); - g_filemanager->writeByte(fd, readbsr ? 1 : 0); - g_filemanager->writeByte(fd, writebsr ? 1 : 0); - g_filemanager->writeByte(fd, altzp ? 1 : 0); - g_filemanager->writeByte(fd, intcxrom ? 1 : 0); - g_filemanager->writeByte(fd, slot3rom ? 1 : 0); - g_filemanager->writeByte(fd, slotLatch); - g_filemanager->writeByte(fd, preWriteFlag ? 1 : 0); - + uint8_t buf[13] = { MMUMAGIC, + (switches >> 8) & 0xFF, + (switches ) & 0xFF, + auxRamRead ? 1 : 0, + auxRamWrite ? 1 : 0, + bank2 ? 1 : 0, + readbsr ? 1 : 0, + writebsr ? 1 : 0, + altzp ? 1 : 0, + intcxrom ? 1 : 0, + slot3rom ? 1 : 0, + slotLatch, + preWriteFlag ? 1 : 0 }; + if (g_filemanager->write(fd, buf, 13) != 13) + return false; + if (!g_ram.Serialize(fd)) return false; @@ -187,36 +187,41 @@ bool AppleMMU::Serialize(int8_t fd) // Not suspending/resuming slots b/c they're a fixed configuration // in this project. Should probably checksum them though. FIXME. - g_filemanager->writeByte(fd, MMUMAGIC); + if (g_filemanager->write(fd, buf, 1) != 1) + return false; + return true; } bool AppleMMU::Deserialize(int8_t fd) { - if (g_filemanager->readByte(fd) != MMUMAGIC) { + uint8_t buf[13]; + + if (g_filemanager->read(fd, buf, 13) != 13) return false; - } - switches = g_filemanager->readByte(fd); - switches <<= 8; - switches |= g_filemanager->readByte(fd); - - auxRamRead = g_filemanager->readByte(fd); - auxRamWrite = g_filemanager->readByte(fd); - bank2 = g_filemanager->readByte(fd); - readbsr = g_filemanager->readByte(fd); - writebsr = g_filemanager->readByte(fd); - altzp = g_filemanager->readByte(fd); - intcxrom = g_filemanager->readByte(fd); - slot3rom = g_filemanager->readByte(fd); - slotLatch = g_filemanager->readByte(fd); - preWriteFlag = g_filemanager->readByte(fd); + if (buf[0] != MMUMAGIC) + return false; + switches = (buf[1] << 8) | buf[2]; + auxRamRead = buf[3]; + auxRamWrite = buf[4]; + bank2 = buf[5]; + readbsr = buf[6]; + writebsr = buf[7]; + altzp = buf[8]; + intcxrom = buf[9]; + slot3rom = buf[10]; + slotLatch = buf[11]; + preWriteFlag = buf[12]; + if (!g_ram.Deserialize(fd)) { return false; } - if (g_filemanager->readByte(fd) != MMUMAGIC) + if (g_filemanager->read(fd, buf, 1) != 1) + return false; + if (buf[0] != MMUMAGIC) return false; // Reset readPages[] and writePages[] and the display diff --git a/apple/applevm.cpp b/apple/applevm.cpp index ab209c1..b7655fe 100644 --- a/apple/applevm.cpp +++ b/apple/applevm.cpp @@ -9,7 +9,7 @@ #include "globals.h" #include -const char *suspendHdr = "Sus1"; +const char *suspendHdr = "Sus2"; AppleVM::AppleVM() { @@ -48,19 +48,18 @@ void AppleVM::Suspend(const char *fn) } /* Header */ - for (int i=0; iwriteByte(fh, suspendHdr[i]); - } + if (g_filemanager->write(fh, suspendHdr, strlen(suspendHdr)) != strlen(suspendHdr)) + return; /* Tell all of the peripherals to suspend */ if (g_cpu->Serialize(fh) && disk6->Serialize(fh) && hd32->Serialize(fh) ) { -#ifndef TEENSYDUINO - printf("All serialized successfully\n"); -#else +#ifdef TEENSYDUINO Serial.println("All serialized successfully"); +#else + printf("All serialized successfully\n"); #endif } @@ -86,8 +85,10 @@ void AppleVM::Resume(const char *fn) } /* Header */ + uint8_t c; for (int i=0; ireadByte(fh) != suspendHdr[i]) { + if (g_filemanager->read(fh, &c, 1) != 1 || + c != suspendHdr[i]) { /* Failed to read correct header; abort */ g_filemanager->closeFile(fh); return; diff --git a/apple/diskii.cpp b/apple/diskii.cpp index 1ad2c35..d50c79d 100644 --- a/apple/diskii.cpp +++ b/apple/diskii.cpp @@ -53,144 +53,144 @@ DiskII::~DiskII() bool DiskII::Serialize(int8_t fd) { - g_filemanager->writeByte(fd, DISKIIMAGIC); - - g_filemanager->writeByte(fd, readWriteLatch); - g_filemanager->writeByte(fd, sequencer); - g_filemanager->writeByte(fd, dataRegister); - g_filemanager->writeByte(fd, writeMode); - g_filemanager->writeByte(fd, writeProt); - g_filemanager->writeByte(fd, selectedDisk); + uint8_t buf[23] = { DISKIIMAGIC, + readWriteLatch, + sequencer, + dataRegister, + writeMode, + writeProt, + selectedDisk }; + + if (g_filemanager->write(fd, buf, 7) != 7) { + return false; + } for (int i=0; i<2; i++) { - g_filemanager->writeByte(fd, curHalfTrack[i]); - g_filemanager->writeByte(fd, curWozTrack[i]); - g_filemanager->writeByte(fd, curPhase[i]); - - g_filemanager->writeByte(fd, - ((driveSpinupCycles[i] >> 56) & 0xFF)); - g_filemanager->writeByte(fd, - ((driveSpinupCycles[i] >> 48) & 0xFF)); - g_filemanager->writeByte(fd, - ((driveSpinupCycles[i] >> 40) & 0xFF)); - g_filemanager->writeByte(fd, - ((driveSpinupCycles[i] >> 32) & 0xFF)); - g_filemanager->writeByte(fd, - ((driveSpinupCycles[i] >> 24) & 0xFF)); - g_filemanager->writeByte(fd, - ((driveSpinupCycles[i] >> 16) & 0xFF)); - g_filemanager->writeByte(fd, - ((driveSpinupCycles[i] >> 8) & 0xFF)); - g_filemanager->writeByte(fd, - ((driveSpinupCycles[i] ) & 0xFF)); - - g_filemanager->writeByte(fd, - ((deliveredDiskBits[i] >> 56) & 0xFF)); - g_filemanager->writeByte(fd, - ((deliveredDiskBits[i] >> 48) & 0xFF)); - g_filemanager->writeByte(fd, - ((deliveredDiskBits[i] >> 40) & 0xFF)); - g_filemanager->writeByte(fd, - ((deliveredDiskBits[i] >> 32) & 0xFF)); - g_filemanager->writeByte(fd, - ((deliveredDiskBits[i] >> 24) & 0xFF)); - g_filemanager->writeByte(fd, - ((deliveredDiskBits[i] >> 16) & 0xFF)); - g_filemanager->writeByte(fd, - ((deliveredDiskBits[i] >> 8) & 0xFF)); - g_filemanager->writeByte(fd, - ((deliveredDiskBits[i] ) & 0xFF)); - - g_filemanager->writeByte(fd, - (diskIsSpinningUntil[i] >> 24) & 0xFF); - g_filemanager->writeByte(fd, - (diskIsSpinningUntil[i] >> 16) & 0xFF); - g_filemanager->writeByte(fd, - (diskIsSpinningUntil[i] >> 8) & 0xFF); - g_filemanager->writeByte(fd, - (diskIsSpinningUntil[i] ) & 0xFF); + uint8_t ptr = 0; + buf[ptr++] = curHalfTrack[i]; + buf[ptr++] = curWozTrack[i]; + buf[ptr++] = curPhase[i]; + buf[ptr++] = ((driveSpinupCycles[i] >> 56) & 0xFF); + buf[ptr++] = ((driveSpinupCycles[i] >> 48) & 0xFF); + buf[ptr++] = ((driveSpinupCycles[i] >> 40) & 0xFF); + buf[ptr++] = ((driveSpinupCycles[i] >> 32) & 0xFF); + buf[ptr++] = ((driveSpinupCycles[i] >> 24) & 0xFF); + buf[ptr++] = ((driveSpinupCycles[i] >> 16) & 0xFF); + buf[ptr++] = ((driveSpinupCycles[i] >> 8) & 0xFF); + buf[ptr++] = ((driveSpinupCycles[i] ) & 0xFF); + buf[ptr++] = ((deliveredDiskBits[i] >> 56) & 0xFF); + buf[ptr++] = ((deliveredDiskBits[i] >> 48) & 0xFF); + buf[ptr++] = ((deliveredDiskBits[i] >> 40) & 0xFF); + buf[ptr++] = ((deliveredDiskBits[i] >> 32) & 0xFF); + buf[ptr++] = ((deliveredDiskBits[i] >> 24) & 0xFF); + buf[ptr++] = ((deliveredDiskBits[i] >> 16) & 0xFF); + buf[ptr++] = ((deliveredDiskBits[i] >> 8) & 0xFF); + buf[ptr++] = ((deliveredDiskBits[i] ) & 0xFF); + buf[ptr++] = (diskIsSpinningUntil[i] >> 24) & 0xFF; + buf[ptr++] = (diskIsSpinningUntil[i] >> 16) & 0xFF; + buf[ptr++] = (diskIsSpinningUntil[i] >> 8) & 0xFF; + buf[ptr++] = (diskIsSpinningUntil[i] ) & 0xFF; + // Safety check: keeping the hard-coded 23 and comparing against ptr. + // If we change the 23, also need to change the size of buf[] above + if (g_filemanager->write(fd, buf, 23) != ptr) { + return false; + } if (disk[i]) { // Make sure we have flushed the disk images disk[i]->flush(); flushAt[i] = 0; // and there's no need to re-flush them now - g_filemanager->writeByte(fd, 1); + buf[0] = 1; + if (g_filemanager->write(fd, buf, 1) != 1) + return false; + // FIXME: this ONLY works for builds using the filemanager to read // the disk image, so it's broken until we port Woz to do that! const char *fn = disk[i]->diskName(); - for (int j=0; jwriteByte(fd, fn[j]); - } - g_filemanager->writeByte(fd, 0); + if (g_filemanager->write(fd, fn, strlen(fn)+1) != strlen(fn)+1) // include null terminator + return false; if (!disk[i]->Serialize(fd)) return false; } else { - g_filemanager->writeByte(fd, 0); + buf[0] = 0; + if (g_filemanager->write(fd, buf, 0) != 1) + return false; } } - - g_filemanager->writeByte(fd, DISKIIMAGIC); + + buf[0] = DISKIIMAGIC; + if (g_filemanager->write(fd, buf, 1) != 1) + return false; return true; } bool DiskII::Deserialize(int8_t fd) { - if (g_filemanager->readByte(fd) != DISKIIMAGIC) { + uint8_t buf[23]; + if (g_filemanager->read(fd, buf, 7) != 7) return false; - } - - readWriteLatch = g_filemanager->readByte(fd); - sequencer = g_filemanager->readByte(fd); - dataRegister = g_filemanager->readByte(fd); - writeMode = g_filemanager->readByte(fd); - writeProt = g_filemanager->readByte(fd); - selectedDisk = g_filemanager->readByte(fd); + if (buf[0] != DISKIIMAGIC) + return false; + + readWriteLatch = buf[1]; + sequencer = buf[2]; + dataRegister = buf[3]; + writeMode = buf[4]; + writeProt = buf[5]; + selectedDisk = buf[6]; for (int i=0; i<2; i++) { - curHalfTrack[i] = g_filemanager->readByte(fd); - curWozTrack[i] = g_filemanager->readByte(fd); - curPhase[i] = g_filemanager->readByte(fd); + uint8_t ptr = 0; + if (g_filemanager->read(fd, buf, 23) != 23) + return false; - driveSpinupCycles[i] = g_filemanager->readByte(fd); - driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= g_filemanager->readByte(fd); - driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= g_filemanager->readByte(fd); - driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= g_filemanager->readByte(fd); - driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= g_filemanager->readByte(fd); - driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= g_filemanager->readByte(fd); - driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= g_filemanager->readByte(fd); - driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= g_filemanager->readByte(fd); + curHalfTrack[i] = buf[ptr++]; + curWozTrack[i] = buf[ptr++]; + curPhase[i] = buf[ptr++]; - deliveredDiskBits[i] = g_filemanager->readByte(fd); - deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= g_filemanager->readByte(fd); - deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= g_filemanager->readByte(fd); - deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= g_filemanager->readByte(fd); - deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= g_filemanager->readByte(fd); - deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= g_filemanager->readByte(fd); - deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= g_filemanager->readByte(fd); - deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= g_filemanager->readByte(fd); + driveSpinupCycles[i] = buf[ptr++]; + driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= buf[ptr++]; + driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= buf[ptr++]; + driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= buf[ptr++]; + driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= buf[ptr++]; + driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= buf[ptr++]; + driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= buf[ptr++]; + driveSpinupCycles[i] <<= 8; driveSpinupCycles[i] |= buf[ptr++]; - diskIsSpinningUntil[i] = g_filemanager->readByte(fd); - diskIsSpinningUntil[i] <<= 8; diskIsSpinningUntil[i] |= g_filemanager->readByte(fd); - diskIsSpinningUntil[i] <<= 8; diskIsSpinningUntil[i] |= g_filemanager->readByte(fd); - diskIsSpinningUntil[i] <<= 8; diskIsSpinningUntil[i] |= g_filemanager->readByte(fd); + deliveredDiskBits[i] = buf[ptr++]; + deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= buf[ptr++]; + deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= buf[ptr++]; + deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= buf[ptr++]; + deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= buf[ptr++]; + deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= buf[ptr++]; + deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= buf[ptr++]; + deliveredDiskBits[i] <<= 8; deliveredDiskBits[i] |= buf[ptr++]; + + diskIsSpinningUntil[i] = buf[ptr++]; + diskIsSpinningUntil[i] <<= 8; diskIsSpinningUntil[i] |= buf[ptr++]; + diskIsSpinningUntil[i] <<= 8; diskIsSpinningUntil[i] |= buf[ptr++]; + diskIsSpinningUntil[i] <<= 8; diskIsSpinningUntil[i] |= buf[ptr++]; if (disk[i]) delete disk[i]; - if (g_filemanager->readByte(fd) == 1) { + if (g_filemanager->read(fd, buf, 1) != 1) + return false; + if (buf[0]) { disk[i] = new WozSerializer(); - char buf[MAXPATH]; - char c; - int ptr = 0; - while ( (c = g_filemanager->readByte(fd) != 0) ) { - buf[ptr++] = c; + + ptr = 0; + while (1) { + if (g_filemanager->read(fd, &buf[ptr++], 1) != 1) + return false; + if (buf[ptr-1] == 0) + break; } - buf[ptr] = 0; if (buf[0]) { // Important we don't read all the tracks, so we can also flush // writes back to the fd... - disk[i]->readFile(buf, false, T_AUTO); // FIXME error checking + disk[i]->readFile((char *)buf, false, T_AUTO); // FIXME error checking } else { // ERROR: there's a disk but we don't have the path to its image? return false; @@ -203,9 +203,10 @@ bool DiskII::Deserialize(int8_t fd) } } - if (g_filemanager->readByte(fd) != DISKIIMAGIC) { + if (g_filemanager->read(fd, buf, 1) != 1) + return false; + if (buf[0] != DISKIIMAGIC) return false; - } return true; } diff --git a/apple/hd32.cpp b/apple/hd32.cpp index 8ff4e65..f2cd894 100644 --- a/apple/hd32.cpp +++ b/apple/hd32.cpp @@ -212,9 +212,9 @@ uint8_t HD32::readNextByteFromSelectedDrive() // FIXME: assumes file is open & cursor is valid uint8_t v; - - v = g_filemanager->readByteAt(fd[driveSelected], cursor[driveSelected]); // FIXME: error handling + g_filemanager->lseek(fd[driveSelected], cursor[driveSelected], SEEK_SET); + g_filemanager->read(fd[driveSelected], &v, 1); cursor[driveSelected]++; @@ -224,17 +224,20 @@ uint8_t HD32::readNextByteFromSelectedDrive() bool HD32::writeBlockToSelectedDrive() { // FIXME: assumes file is open & cursor is valid - // FIXME: need a better filemanager interface that holds the file open + + // FIXME: is there a better static 512-char buf somewhere we can reuse instead of allocing new? (The teensy is low on ram.) + uint8_t buf[512]; for (uint16_t i=0; i<512; i++) { - uint8_t b = mmu->read(memBlock[driveSelected] + i); - if (!g_filemanager->writeByteAt(fd[driveSelected], b, diskBlock[driveSelected] * 512 + i)) { - // FIXME + buf[i] = mmu->read(memBlock[driveSelected] + i); + } + if (g_filemanager->lseek(fd[driveSelected], diskBlock[driveSelected]*512, SEEK_SET) == -1 || + g_filemanager->write(fd[driveSelected], buf, 512) != 512) { + // FIXME #ifndef TEENSYDUINO - printf("ERROR: failed to write to hd file? errno %d\n", errno); + printf("ERROR: failed to write to hd file? errno %d\n", errno); #endif - return false; - } + return false; } return true; diff --git a/apple/woz-serializer.cpp b/apple/woz-serializer.cpp index faba5d7..aff97b7 100644 --- a/apple/woz-serializer.cpp +++ b/apple/woz-serializer.cpp @@ -19,57 +19,50 @@ bool WozSerializer::Serialize(int8_t fd) { // If we're being asked to serialize, make sure we've flushed any data first flush(); - - g_filemanager->writeByte(fd, WOZMAGIC); - // We need the internal state about data but not much else - g_filemanager->writeByte(fd, - (trackPointer >> 24) & 0xFF); - g_filemanager->writeByte(fd, - (trackPointer >> 16) & 0xFF); - g_filemanager->writeByte(fd, - (trackPointer >> 8) & 0xFF); - g_filemanager->writeByte(fd, - (trackPointer ) & 0xFF); + uint8_t buf[13] = { WOZMAGIC, + (trackPointer >> 24) & 0xFF, + (trackPointer >> 16) & 0xFF, + (trackPointer >> 8) & 0xFF, + (trackPointer ) & 0xFF, + (trackBitCounter >> 24) & 0xFF, + (trackBitCounter >> 16) & 0xFF, + (trackBitCounter >> 8) & 0xFF, + (trackBitCounter ) & 0xFF, + trackByte, + trackBitIdx, + trackLoopCounter, + WOZMAGIC }; + if (g_filemanager->write(fd, buf, 13) != 13) + return false; - g_filemanager->writeByte(fd, - (trackBitCounter >> 24) & 0xFF); - g_filemanager->writeByte(fd, - (trackBitCounter >> 16) & 0xFF); - g_filemanager->writeByte(fd, - (trackBitCounter >> 8) & 0xFF); - g_filemanager->writeByte(fd, - (trackBitCounter ) & 0xFF); - - g_filemanager->writeByte(fd, trackByte); - g_filemanager->writeByte(fd, trackBitIdx); - g_filemanager->writeByte(fd, trackLoopCounter); - - g_filemanager->writeByte(fd, WOZMAGIC); return true; } bool WozSerializer::Deserialize(int8_t fd) { // Before deserializing, the caller has to re-load the right disk image! - if (g_filemanager->readByte(fd) != WOZMAGIC) + uint8_t buf[13]; + if (g_filemanager->read(fd, buf, 13) != 13) + return false; + + if (buf[0] != WOZMAGIC) return false; - trackPointer = g_filemanager->readByte(fd); - trackPointer <<= 8; trackPointer |= g_filemanager->readByte(fd); - trackPointer <<= 8; trackPointer |= g_filemanager->readByte(fd); - trackPointer <<= 8; trackPointer |= g_filemanager->readByte(fd); + trackPointer = buf[1]; + trackPointer <<= 8; trackPointer |= buf[2]; + trackPointer <<= 8; trackPointer |= buf[3]; + trackPointer <<= 8; trackPointer |= buf[4]; - trackBitCounter = g_filemanager->readByte(fd); - trackBitCounter <<= 8; trackBitCounter |= g_filemanager->readByte(fd); - trackBitCounter <<= 8; trackBitCounter |= g_filemanager->readByte(fd); - trackBitCounter <<= 8; trackBitCounter |= g_filemanager->readByte(fd); - - trackByte = g_filemanager->readByte(fd); - trackBitIdx = g_filemanager->readByte(fd); - trackLoopCounter = g_filemanager->readByte(fd); - - if (g_filemanager->readByte(fd) != WOZMAGIC) + trackBitCounter = buf[5]; + trackBitCounter <<= 8; trackBitCounter |= buf[6]; + trackBitCounter <<= 8; trackBitCounter |= buf[7]; + trackBitCounter <<= 8; trackBitCounter |= buf[8]; + + trackByte = buf[9]; + trackBitIdx = buf[10]; + trackLoopCounter = buf[11]; + if (buf[12] != WOZMAGIC) return false; return true; diff --git a/cpu.cpp b/cpu.cpp index e0257b6..b542e42 100644 --- a/cpu.cpp +++ b/cpu.cpp @@ -318,23 +318,22 @@ Cpu::~Cpu() bool Cpu::Serialize(int8_t fh) { - g_filemanager->writeByte(fh, CPUMAGIC); + uint8_t buf[13] = { CPUMAGIC, + (pc >> 8) & 0xFF, + (pc ) & 0xFF, + sp, + a, + x, + y, + flags, + (cycles >> 24) & 0xFF, + (cycles >> 16) & 0xFF, + (cycles >> 8) & 0xFF, + (cycles ) & 0xFF, + irqPending ? 1 : 0 }; - g_filemanager->writeByte(fh, (pc >> 8) & 0xFF); - g_filemanager->writeByte(fh, (pc ) & 0xFF); - - g_filemanager->writeByte(fh, sp); - g_filemanager->writeByte(fh, a); - g_filemanager->writeByte(fh, x); - g_filemanager->writeByte(fh, y); - g_filemanager->writeByte(fh, flags); - - g_filemanager->writeByte(fh, (cycles >> 24) & 0xFF); - g_filemanager->writeByte(fh, (cycles >> 16) & 0xFF); - g_filemanager->writeByte(fh, (cycles >> 8) & 0xFF); - g_filemanager->writeByte(fh, (cycles ) & 0xFF); - - g_filemanager->writeByte(fh, irqPending ? 1 : 0); + if (g_filemanager->write(fh, buf, 13) != 13) + return false; if (!mmu->Serialize(fh)) { #ifndef TEENSYDUINO @@ -345,35 +344,29 @@ bool Cpu::Serialize(int8_t fh) return false; } - g_filemanager->writeByte(fh, CPUMAGIC); + if (g_filemanager->write(fh, buf, 1) != 1) + return false; - return true; // FIXME: no error checking on writes + return true; } bool Cpu::Deserialize(int8_t fh) { - if (g_filemanager->readByte(fh) != CPUMAGIC) + uint8_t buf[13]; + if (g_filemanager->read(fh, buf, 13) != 13) return false; + if (buf[0] != CPUMAGIC) + return false; + pc = (buf[1] << 8) | buf[2]; + sp = buf[3]; + a = buf[4]; + x = buf[5]; + y = buf[6]; + flags = buf[7]; - pc = g_filemanager->readByte(fh); - pc <<= 8; - pc |= g_filemanager->readByte(fh); + cycles = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11]; - sp = g_filemanager->readByte(fh); - a = g_filemanager->readByte(fh); - x = g_filemanager->readByte(fh); - y = g_filemanager->readByte(fh); - flags = g_filemanager->readByte(fh); - - cycles = g_filemanager->readByte(fh); - cycles <<= 8; - cycles |= g_filemanager->readByte(fh); - cycles <<= 8; - cycles |= g_filemanager->readByte(fh); - cycles <<= 8; - cycles |= g_filemanager->readByte(fh); - - irqPending = g_filemanager->readByte(fh) ? true : false; + irqPending = buf[12]; if (!mmu->Deserialize(fh)) { #ifndef TEENSYDUINO @@ -382,7 +375,9 @@ bool Cpu::Deserialize(int8_t fh) return false; } - if (g_filemanager->readByte(fh) != CPUMAGIC) + if (g_filemanager->read(fh, buf, 1) != 1) + return false; + if (buf[0] != CPUMAGIC) return false; #ifndef TEENSYDUINO diff --git a/filemanager.h b/filemanager.h index 690eac2..0ab4ab1 100644 --- a/filemanager.h +++ b/filemanager.h @@ -22,6 +22,8 @@ class FileManager { public: virtual ~FileManager() {}; +#define writeByte(fd,x) {static uint8_t c = x; write(outfd, &c, 1);} + virtual bool SerializeFile(int8_t outfd, int8_t fd) { writeByte(outfd, FMMAGIC); @@ -61,13 +63,21 @@ class FileManager { } virtual int8_t DeserializeFile(int8_t infd) { - if (readByte(infd) != FMMAGIC) + uint8_t b; + if (read(infd, &b, 1) != 1) + return -1; + if (b != FMMAGIC) return -1; - if (readByte(infd) == 0) { + if (read(infd, &b, 1) != 1) + return -1; + + if (b == 0) { // No file was cached. Verify footer and we're done without error - - if (readByte(infd) != FMMAGIC) { + + if (read(infd, &b, 1) != 1) + return -1; + if (b != FMMAGIC) { // FIXME: no way to raise this error. return -1; } @@ -76,25 +86,36 @@ class FileManager { } char buf[MAXPATH]; - int8_t l = readByte(infd); - for (int i=0; i= numCached) - return -1; // FIXME: error handling? - - if (cachedNames[fd][0] == 0) - return -1; // FIXME: error handling? - - uint8_t v = 0; - - // open, seek, read, close. - bool ret = false; - int ffd = open(cachedNames[fd], O_RDONLY); - if (ffd != -1) { - lseek(ffd, pos, SEEK_SET); - ret = (read(ffd, &v, 1) == 1); - close(ffd); - } - - if (!ret) { - printf("ERROR reading byte at %u: %d\n", pos, errno); - } - - // FIXME: error handling? - return v; -} - -bool NixFileManager::writeByteAt(int8_t fd, uint8_t v, uint32_t pos) -{ - if (fd < 0 || fd >= numCached) - return false; - - if (cachedNames[fd][0] == 0) - return false; - - // open, seek, write, close. - bool ret = false; - int ffd = open(cachedNames[fd], O_WRONLY|O_CREAT, 0644); - if (ffd != -1) { - lseek(ffd, pos, SEEK_SET); - ret = (write(ffd, &v, 1) == 1); - close(ffd); - } - - return ret; -} - -bool NixFileManager::writeByte(int8_t fd, uint8_t v) -{ - if (fd < 0 || fd >= numCached) - return false; - - if (cachedNames[fd][0] == 0) - return false; - - uint32_t pos = fileSeekPositions[fd]; - - // open, seek, write, close. - bool ret = false; - int ffd = open(cachedNames[fd], O_WRONLY|O_CREAT, 0644); - if (ffd != -1) { - lseek(ffd, pos, SEEK_SET); - ret = (write(ffd, &v, 1) == 1); - if (!ret) { - printf("error writing: %d\n", errno); - } - close(ffd); - } else { - printf("Failed to open '%s' for writing: %d\n", - cachedNames[fd], errno); - } - fileSeekPositions[fd]++; - return ret; -} - -uint8_t NixFileManager::readByte(int8_t fd) -{ - if (fd < 0 || fd >= numCached) - return -1; // FIXME: error handling? - - if (cachedNames[fd][0] == 0) - return -1; // FIXME: error handling? - - uint8_t v = 0; - - uint32_t pos = fileSeekPositions[fd]; - - // open, seek, read, close. - bool ret = false; - int ffd = open(cachedNames[fd], O_RDONLY); - if (ffd != -1) { - lseek(ffd, pos, SEEK_SET); - ret = (read(ffd, &v, 1) == 1); - close(ffd); - } - fileSeekPositions[fd]++; - - if (!ret) { - printf("ERROR reading from pos %d: %d\n", pos, errno); - } - - // FIXME: error handling? - return v; -} - void NixFileManager::getRootPath(char *toWhere, int8_t maxLen) { strcpy(toWhere, ROOTDIR); diff --git a/nix/nix-filemanager.h b/nix/nix-filemanager.h index 91f3eca..6cd8ed7 100644 --- a/nix/nix-filemanager.h +++ b/nix/nix-filemanager.h @@ -16,12 +16,6 @@ class NixFileManager : public FileManager { virtual int8_t readDir(const char *where, const char *suffix, char *outputFN, int8_t startIdx, uint16_t maxlen); - virtual uint8_t readByteAt(int8_t fd, uint32_t pos); - virtual bool writeByteAt(int8_t fd, uint8_t v, uint32_t pos); - - virtual uint8_t readByte(int8_t fd); - virtual bool writeByte(int8_t fd, uint8_t v); - void getRootPath(char *toWhere, int8_t maxLen); virtual bool setSeekPosition(int8_t fd, uint32_t pos); diff --git a/teensy/teensy-filemanager.cpp b/teensy/teensy-filemanager.cpp index 2903403..1e6d45f 100644 --- a/teensy/teensy-filemanager.cpp +++ b/teensy/teensy-filemanager.cpp @@ -182,102 +182,6 @@ bool TeensyFileManager::_prepCache(int8_t fd) return true; // FIXME error handling } -uint8_t TeensyFileManager::readByteAt(int8_t fd, uint32_t pos) -{ - // open, seek, read, close. - if (fd < 0 || fd >= numCached) - return false; - - if (cachedNames[fd][0] == 0) - return false; - - _prepCache(fd); - - if (!rawFile.seek(pos)) { - Serial.print("readByteAt: seek failed to pos "); - Serial.println(pos); - Serial.println("Trying to continue anyway"); - // return false; - } - uint8_t b; - return (rawFile.read(&b, 1) == 1); -} - -bool TeensyFileManager::writeByteAt(int8_t fd, uint8_t v, uint32_t pos) -{ - // open, seek, write, close. - if (fd < 0 || fd >= numCached) - return false; - - if (cachedNames[fd][0] == 0) - return false; - - _prepCache(fd); - - if (!rawFile.seek(pos)) - return false; - - return (rawFile.write(&v, 1) == 1); -} - -// FIXME: the semantics of this are wrong - lots of 'return false' for a uint8_t -uint8_t TeensyFileManager::readByte(int8_t fd) -{ - // open, seek, read, close. - if (fd < 0 || fd >= numCached) - return false; - - if (cachedNames[fd][0] == 0) - return false; - - _prepCache(fd); - - uint32_t pos = fileSeekPositions[fd]; - - if (!rawFile.seek(pos)) { - Serial.print("readByte: seek failed to byte "); - Serial.println(pos); - return false; - } - - uint8_t b; - rawFile.read(&b, 1); - fileSeekPositions[fd]++; - - // FIXME: check v == 1 & handle error - return b; -} - -bool TeensyFileManager::writeByte(int8_t fd, uint8_t v) -{ - // open, seek, write, close. - if (fd < 0 || fd >= numCached) { - Serial.println("failed writeByte - invalid fd"); - return false; - } - - if (cachedNames[fd][0] == 0) { - Serial.println("failed writeByte - no cache name"); - return false; - } - - _prepCache(fd); - - uint32_t pos = fileSeekPositions[fd]; - - if (!rawFile.seek(pos)) { - return false; - } - - if (rawFile.write(&v, 1) != 1) { - return false; - } - - fileSeekPositions[fd]++; - - return true; -} - void TeensyFileManager::getRootPath(char *toWhere, int8_t maxLen) { strcpy(toWhere, "/A2DISKS/"); diff --git a/teensy/teensy-filemanager.h b/teensy/teensy-filemanager.h index aea4f77..a98db9d 100644 --- a/teensy/teensy-filemanager.h +++ b/teensy/teensy-filemanager.h @@ -16,12 +16,6 @@ class TeensyFileManager : public FileManager { virtual int8_t readDir(const char *where, const char *suffix, char *outputFN, int8_t startIdx, uint16_t maxlen); - virtual uint8_t readByteAt(int8_t fd, uint32_t pos); - virtual bool writeByteAt(int8_t fd, uint8_t v, uint32_t pos); - - virtual uint8_t readByte(int8_t fd); - virtual bool writeByte(int8_t fd, uint8_t v); - virtual void getRootPath(char *toWhere, int8_t maxLen); virtual bool setSeekPosition(int8_t fd, uint32_t pos); diff --git a/vmram.cpp b/vmram.cpp index f0f1182..88e44a8 100644 --- a/vmram.cpp +++ b/vmram.cpp @@ -39,48 +39,45 @@ void VMRam::writeByte(uint32_t addr, uint8_t value) bool VMRam::Serialize(int8_t fd) { - g_filemanager->writeByte(fd, RAMMAGIC); uint32_t size = sizeof(preallocatedRam); - g_filemanager->writeByte(fd, (size >> 24) & 0xFF); - g_filemanager->writeByte(fd, (size >> 16) & 0xFF); - g_filemanager->writeByte(fd, (size >> 8) & 0xFF); - g_filemanager->writeByte(fd, (size ) & 0xFF); + uint8_t buf[5] = { RAMMAGIC, + (size >> 24) & 0xFF, + (size >> 16) & 0xFF, + (size >> 8) & 0xFF, + (size ) & 0xFF }; + if (g_filemanager->write(fd, buf, 5) != 5) + return false; - for (uint32_t pos = 0; pos < sizeof(preallocatedRam); pos++) { - g_filemanager->writeByte(fd, preallocatedRam[pos]); - } - - g_filemanager->writeByte(fd, RAMMAGIC); + if (g_filemanager->write(fd, preallocatedRam, sizeof(preallocatedRam)) != sizeof(preallocatedRam)) + return false; + + if (g_filemanager->write(fd, buf, 1) != 1) + return false; return true; } bool VMRam::Deserialize(int8_t fd) { - if (g_filemanager->readByte(fd) != RAMMAGIC) { + uint8_t buf[5]; + if (g_filemanager->read(fd, buf, 5) != 5) return false; - } - uint32_t size = 0; - size = g_filemanager->readByte(fd); - size <<= 8; - size |= g_filemanager->readByte(fd); - size <<= 8; - size |= g_filemanager->readByte(fd); - size <<= 8; - size |= g_filemanager->readByte(fd); - - if (size != sizeof(preallocatedRam)) { + if (buf[0] != RAMMAGIC) return false; - } + + uint32_t size = (buf[1] << 24) | (buf[2] << 16) | (buf[3] << 8) | buf[4]; - for (uint32_t pos = 0; pos < sizeof(preallocatedRam); pos++) { - preallocatedRam[pos] = g_filemanager->readByte(fd); - } - - if (g_filemanager->readByte(fd) != RAMMAGIC) { + if (size != sizeof(preallocatedRam)) + return false; + + if (g_filemanager->read(fd, preallocatedRam, size) != size) + return false; + + if (g_filemanager->read(fd, buf, 1) != 1) + return false; + if (buf[0] != RAMMAGIC) return false; - } return true; }