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>
#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;

View File

@ -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);
}
}
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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:

2
ram.h
View File

@ -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]; }

View File

@ -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 ();

View File

@ -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();