From 3890fc3dbd133b9f28bddabbed7e1b69660111f9 Mon Sep 17 00:00:00 2001 From: Jorj Bauer Date: Wed, 10 Jan 2018 08:05:49 -0500 Subject: [PATCH] suspend/resume fixed for new memory model --- apple/applemmu.cpp | 63 +++++----------------------------------------- apple/applemmu.h | 2 -- vmram.cpp | 52 ++++++++++++++++++++++++++++++++++++++ vmram.h | 3 +++ 4 files changed, 61 insertions(+), 59 deletions(-) diff --git a/apple/applemmu.cpp b/apple/applemmu.cpp index f854b4d..df807a8 100644 --- a/apple/applemmu.cpp +++ b/apple/applemmu.cpp @@ -103,8 +103,6 @@ AppleMMU::AppleMMU(AppleDisplay *display) slots[i] = NULL; } - allocateMemory(); - this->display = display; this->display->setSwitches(&switches); resetRAM(); // initialize RAM, load ROM @@ -113,7 +111,6 @@ AppleMMU::AppleMMU(AppleDisplay *display) AppleMMU::~AppleMMU() { delete display; - // FIXME: clean up the memory we allocated } bool AppleMMU::Serialize(int8_t fd) @@ -134,27 +131,14 @@ bool AppleMMU::Serialize(int8_t fd) g_filemanager->writeByte(fd, slotLatch); g_filemanager->writeByte(fd, preWriteFlag ? 1 : 0); - // FIXME: write the RAM -#if 0 - for (uint16_t i=0; i<0x100; i++) { - for (uint8_t j=0; j<5; j++) { - g_filemanager->writeByte(fd, MMUMAGIC); - if (ramPages[i][j]) { - g_filemanager->writeByte(fd, 1); - for (uint16_t k=0; k<0x100; k++) { - g_filemanager->writeByte(fd, ramPages[i][j][k]); - } - } else { - g_filemanager->writeByte(fd, 0); - } - } - } -#endif + if (!g_ram.Serialize(fd)) + return false; + // readPages & writePages don't need suspending, but we will need to // recalculate after resume // Not suspending/resuming slots b/c they're a fixed configuration - // in this project. + // in this project. Should probably checksum them though. FIXME. g_filemanager->writeByte(fd, MMUMAGIC); return true; @@ -181,38 +165,8 @@ bool AppleMMU::Deserialize(int8_t fd) slotLatch = g_filemanager->readByte(fd); preWriteFlag = g_filemanager->readByte(fd); - for (uint16_t i=0; i<0x100; i++) { - for (uint8_t j=0; j<5; j++) { - if (g_filemanager->readByte(fd) != MMUMAGIC) { -#ifndef TEENSYDUINO - printf("Page %d/%d bad magic\n", i, j); -#endif - return false; - } - - // FIXME: deserialize RAM -#if 0 - if (g_filemanager->readByte(fd)) { - // This page has data -#ifndef TEENSYDUINO - if (!ramPages[i][j]) { - printf("ERROR: shouldn't be writing to this page\n"); - exit(1); - } -#endif - for (uint16_t k=0; k<0x100; k++) { - ramPages[i][j][k] = g_filemanager->readByte(fd); - } - } else { -#ifndef TEENSYDUINO - if (ramPages[i][j]) { - printf("ERROR: this page exists but wasn't serialized?\n"); - exit(1); - } -#endif - } -#endif - } + if (!g_ram.Deserialize(fd)) { + return false; } if (g_filemanager->readByte(fd) != MMUMAGIC) @@ -963,11 +917,6 @@ void AppleMMU::setSlot(int8_t slotnum, Slot *peripheral) } } -void AppleMMU::allocateMemory() -{ - // FIXME: don't need this any more, I don't think, since it's allocated by g_ram -} - void AppleMMU::updateMemoryPages() { if (auxRamRead) { diff --git a/apple/applemmu.h b/apple/applemmu.h index 1296a7d..0473648 100644 --- a/apple/applemmu.h +++ b/apple/applemmu.h @@ -56,8 +56,6 @@ class AppleMMU : public MMU { void setAppleKey(int8_t which, bool isDown); protected: - void allocateMemory(); - void resetDisplay(); uint8_t readSwitches(uint16_t address); void writeSwitches(uint16_t address, uint8_t v); diff --git a/vmram.cpp b/vmram.cpp index 1a27eea..655854b 100644 --- a/vmram.cpp +++ b/vmram.cpp @@ -1,5 +1,6 @@ #include "vmram.h" #include +#include "globals.h" #ifndef TEENSYDUINO #include @@ -7,6 +8,9 @@ #define assert(x) #endif +// Serializing token for RAM data +#define RAMMAGIC 'R' + VMRam::VMRam() {memset(preallocatedRam, 0, sizeof(preallocatedRam)); } VMRam::~VMRam() { } @@ -21,3 +25,51 @@ void VMRam::init() uint8_t VMRam::readByte(uint32_t addr) { assert(addr < sizeof(preallocatedRam)); return preallocatedRam[addr]; } void VMRam::writeByte(uint32_t addr, uint8_t value) { assert(addr < sizeof(preallocatedRam)); preallocatedRam[addr] = 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); + + for (uint32_t pos = 0; pos < sizeof(preallocatedRam); pos++) { + g_filemanager->writeByte(fd, preallocatedRam[pos]); + } + + g_filemanager->writeByte(fd, RAMMAGIC); + + return true; +} + +bool VMRam::Deserialize(int8_t fd) +{ + if (g_filemanager->readByte(fd) != RAMMAGIC) { + 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)) { + return false; + } + + for (uint32_t pos = 0; pos < sizeof(preallocatedRam); pos++) { + preallocatedRam[pos] = g_filemanager->readByte(fd); + } + + if (g_filemanager->readByte(fd) != RAMMAGIC) { + return false; + } + + return true; +} diff --git a/vmram.h b/vmram.h index b18c1c2..a3ddd29 100644 --- a/vmram.h +++ b/vmram.h @@ -15,6 +15,9 @@ class VMRam { uint8_t readByte(uint32_t addr); void writeByte(uint32_t addr, uint8_t value); + bool Serialize(int8_t fd); + bool Deserialize(int8_t fd); + private: uint8_t preallocatedRam[591*256]; // 591 pages of RAM };