checkpointing

This commit is contained in:
Stephen Crane 2014-10-21 19:41:44 +01:00
parent 0744c03f88
commit e695cb91ab
8 changed files with 54 additions and 19 deletions

5
cpu.h
View File

@ -8,7 +8,7 @@
#include <setjmp.h> #include <setjmp.h>
#endif #endif
class CPU { class CPU: public Checkpointable {
public: public:
virtual void reset () =0; virtual void reset () =0;
virtual Memory::address run (unsigned instructions) =0; virtual Memory::address run (unsigned instructions) =0;
@ -17,6 +17,9 @@ public:
typedef void (*statfn) (const char *, ...); typedef void (*statfn) (const char *, ...);
virtual void checkpoint(Stream &s) = 0;
virtual void restore(Stream &s) = 0;
protected: protected:
CPU (Memory *m, jmp_buf *e, statfn s): _memory(m), _err(e), _status(s){} CPU (Memory *m, jmp_buf *e, statfn s): _memory(m), _err(e), _status(s){}
Memory *_memory; Memory *_memory;

View File

@ -5,14 +5,17 @@
#include "memory.h" #include "memory.h"
#include "spiram.h" #include "spiram.h"
#include "cpu.h"
#include "hardware.h" #include "hardware.h"
Memory memory; Memory memory;
PS2Driver ps2; PS2Driver ps2;
spiram sram(SPIRAM_SIZE); spiram sram(SPIRAM_SIZE);
UTFT utft(TFT_MODEL, TFT_RS, TFT_WR, TFT_CS, TFT_RST); 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); ps2.begin(KBD_DATA, KBD_IRQ);
pinMode(SD_CS, OUTPUT); pinMode(SD_CS, OUTPUT);
@ -28,5 +31,30 @@ bool hardware_init() {
sram.begin(SPIRAM_CS, SPIRAM_SPI); sram.begin(SPIRAM_CS, SPIRAM_SPI);
cpu.reset();
return sd; 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);
}
}
}

View File

@ -32,7 +32,9 @@
#define SD_CS PF_3 #define SD_CS PF_3
#define SD_SPI 1 #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 PS2Driver ps2;
extern class spiram sram; extern class spiram sram;

View File

@ -1,24 +1,22 @@
#include "memory.h" #include "memory.h"
void Memory::put (Device &dev, address b) { void Memory::put (Device &dev, address b) {
Device **d = _pages + b/page_size; Device **d = _pages + b/page_size;
int size = dev.pages(); int size = dev.pages();
while (size--) while (size--)
*d++ = &dev; *d++ = &dev;
dev.base(b); dev.base(b);
} }
class NullDevice: public Memory::Device { class NullDevice: public Memory::Device {
public: public:
NullDevice(): Memory::Device(65536) {} NullDevice(): Memory::Device(65536) {}
void operator= (byte b) {} void operator= (byte b) {}
operator byte() { return 0; } operator byte() { return 0; }
} nd; } nd;
Memory::Memory() { Memory::Memory() {
put(nd, 0); put(nd, 0);
} }

View File

@ -19,10 +19,11 @@ public:
typedef unsigned short address; typedef unsigned short address;
static const int page_size = 256; static const int page_size = 256;
class Device { class Device: public Checkpointable {
public: public:
Device (int bytes): _pages(bytes/page_size) {} Device (int bytes): _pages(bytes/page_size) {}
virtual ~Device () {} virtual ~Device () {}
int pages () const { return _pages; } int pages () const { return _pages; }
void access (address a) { _acc=a-_base; } void access (address a) { _acc=a-_base; }
void base (address a) { _base=a; } void base (address a) { _base=a; }
@ -31,6 +32,9 @@ public:
virtual void operator= (byte) =0; virtual void operator= (byte) =0;
virtual operator byte () =0; virtual operator byte () =0;
virtual void checkpoint(Stream &s) {};
virtual void restore(Stream &s) {};
protected: protected:
address _acc, _base; address _acc, _base;
private: private:

2
ram.h
View File

@ -1,7 +1,7 @@
#ifndef _RAM_H #ifndef _RAM_H
#define _RAM_H #define _RAM_H
class ram: public Memory::Device, public Checkpointable { class ram: public Memory::Device {
public: public:
virtual void operator= (byte c) { _mem[_acc] = c; } virtual void operator= (byte c) { _mem[_acc] = c; }
virtual operator byte () { return _mem[_acc]; } virtual operator byte () { return _mem[_acc]; }

View File

@ -1,7 +1,7 @@
#ifndef __SPIRAM_H__ #ifndef __SPIRAM_H__
#define __SPIRAM_H__ #define __SPIRAM_H__
class spiram: public Memory::Device, public Checkpointable { class spiram: public Memory::Device {
public: public:
virtual void operator= (byte c); virtual void operator= (byte c);
virtual operator byte (); virtual operator byte ();

View File

@ -3,7 +3,7 @@
class Stream; class Stream;
class UTFTDisplay: public Memory::Device, public Checkpointable { class UTFTDisplay: public Memory::Device {
public: public:
void begin(unsigned bg, unsigned fg); void begin(unsigned bg, unsigned fg);
void clear(); void clear();