From e695cb91ab0ddc099a19c690f89c99b609c6a404 Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Tue, 21 Oct 2014 19:41:44 +0100 Subject: [PATCH] checkpointing --- cpu.h | 5 ++++- hardware.cpp | 30 +++++++++++++++++++++++++++++- hardware.h | 4 +++- memory.cpp | 22 ++++++++++------------ memory.h | 6 +++++- ram.h | 2 +- spiram.h | 2 +- utftdisplay.h | 2 +- 8 files changed, 54 insertions(+), 19 deletions(-) diff --git a/cpu.h b/cpu.h index 4d41e47..2e92061 100644 --- a/cpu.h +++ b/cpu.h @@ -8,7 +8,7 @@ #include #endif -class CPU { +class CPU: public Checkpointable { public: virtual void reset () =0; virtual Memory::address run (unsigned instructions) =0; @@ -17,6 +17,9 @@ public: typedef void (*statfn) (const char *, ...); + virtual void checkpoint(Stream &s) = 0; + virtual void restore(Stream &s) = 0; + protected: CPU (Memory *m, jmp_buf *e, statfn s): _memory(m), _err(e), _status(s){} Memory *_memory; diff --git a/hardware.cpp b/hardware.cpp index 4daac86..930fcd7 100644 --- a/hardware.cpp +++ b/hardware.cpp @@ -5,14 +5,17 @@ #include "memory.h" #include "spiram.h" +#include "cpu.h" #include "hardware.h" Memory memory; PS2Driver ps2; spiram sram(SPIRAM_SIZE); UTFT utft(TFT_MODEL, TFT_RS, TFT_WR, TFT_CS, TFT_RST); +static CPU *_cpu; -bool hardware_init() { +bool hardware_init(CPU &cpu) { + _cpu = &cpu; ps2.begin(KBD_DATA, KBD_IRQ); pinMode(SD_CS, OUTPUT); @@ -28,5 +31,30 @@ bool hardware_init() { sram.begin(SPIRAM_CS, SPIRAM_SPI); + cpu.reset(); return sd; } + +void hardware_checkpoint(Stream &s) { + _cpu->checkpoint(s); + Memory::Device *d = 0; + for (unsigned i = 0; i < 0x10000; i += Memory::page_size) { + Memory::Device *dev = memory.get(i); + if (dev && dev != d) { + d = dev; + d->checkpoint(s); + } + } +} + +void hardware_restore(Stream &s) { + _cpu->restore(s); + Memory::Device *d = 0; + for (unsigned i = 0; i < 0x10000; i += Memory::page_size) { + Memory::Device *dev = memory.get(i); + if (dev && dev != d) { + d = dev; + d->restore(s); + } + } +} diff --git a/hardware.h b/hardware.h index dafe411..15c316c 100644 --- a/hardware.h +++ b/hardware.h @@ -32,7 +32,9 @@ #define SD_CS PF_3 #define SD_SPI 1 -bool hardware_init(void); +bool hardware_init(class CPU &); +void hardware_checkpoint(Stream &); // FIXME: make a path? +void hardware_restore(Stream &); extern class PS2Driver ps2; extern class spiram sram; diff --git a/memory.cpp b/memory.cpp index f5395fc..6ccc260 100644 --- a/memory.cpp +++ b/memory.cpp @@ -1,24 +1,22 @@ #include "memory.h" void Memory::put (Device &dev, address b) { - Device **d = _pages + b/page_size; + Device **d = _pages + b/page_size; - int size = dev.pages(); - while (size--) - *d++ = &dev; + int size = dev.pages(); + while (size--) + *d++ = &dev; - dev.base(b); + dev.base(b); } class NullDevice: public Memory::Device { - public: - NullDevice(): Memory::Device(65536) {} - void operator= (byte b) {} - operator byte() { return 0; } +public: + NullDevice(): Memory::Device(65536) {} + void operator= (byte b) {} + operator byte() { return 0; } } nd; Memory::Memory() { - put(nd, 0); + put(nd, 0); } - - diff --git a/memory.h b/memory.h index 1b87d91..0da0065 100644 --- a/memory.h +++ b/memory.h @@ -19,10 +19,11 @@ public: typedef unsigned short address; static const int page_size = 256; - class Device { + class Device: public Checkpointable { public: Device (int bytes): _pages(bytes/page_size) {} virtual ~Device () {} + int pages () const { return _pages; } void access (address a) { _acc=a-_base; } void base (address a) { _base=a; } @@ -31,6 +32,9 @@ public: virtual void operator= (byte) =0; virtual operator byte () =0; + virtual void checkpoint(Stream &s) {}; + virtual void restore(Stream &s) {}; + protected: address _acc, _base; private: diff --git a/ram.h b/ram.h index 2c18d18..49c61d9 100644 --- a/ram.h +++ b/ram.h @@ -1,7 +1,7 @@ #ifndef _RAM_H #define _RAM_H -class ram: public Memory::Device, public Checkpointable { +class ram: public Memory::Device { public: virtual void operator= (byte c) { _mem[_acc] = c; } virtual operator byte () { return _mem[_acc]; } diff --git a/spiram.h b/spiram.h index 82783ab..4314bca 100644 --- a/spiram.h +++ b/spiram.h @@ -1,7 +1,7 @@ #ifndef __SPIRAM_H__ #define __SPIRAM_H__ -class spiram: public Memory::Device, public Checkpointable { +class spiram: public Memory::Device { public: virtual void operator= (byte c); virtual operator byte (); diff --git a/utftdisplay.h b/utftdisplay.h index 9b164a3..9df2593 100644 --- a/utftdisplay.h +++ b/utftdisplay.h @@ -3,7 +3,7 @@ class Stream; -class UTFTDisplay: public Memory::Device, public Checkpointable { +class UTFTDisplay: public Memory::Device { public: void begin(unsigned bg, unsigned fg); void clear();