From b7a9efe914f9dc1d1fa1f87d0a4b7c26a59d5e7c Mon Sep 17 00:00:00 2001 From: Stephen Crane Date: Fri, 14 Nov 2014 18:27:54 +0000 Subject: [PATCH] checkpointing utilities --- checkpoint.cpp | 29 +++++++++++++++++++++++++++++ checkpoint.h | 8 ++++++++ r65emu.h | 1 + 3 files changed, 38 insertions(+) create mode 100644 checkpoint.cpp create mode 100644 checkpoint.h diff --git a/checkpoint.cpp b/checkpoint.cpp new file mode 100644 index 0000000..11f211f --- /dev/null +++ b/checkpoint.cpp @@ -0,0 +1,29 @@ +#include +#include "sdtape.h" +#include "checkpoint.h" +#include "hardware.h" + +static char buf[32]; +static char chkpt[] = { "CHKPOINT" }; +static int cpid = 0; + +const char *checkpoint(sdtape &tape, const char *dir) { + tape.stop(); + snprintf(buf, sizeof(buf), "%s%s.%03d", dir, chkpt, cpid++); + File file = SD.open(buf, O_WRITE | O_CREAT | O_TRUNC); + hardware_checkpoint(file); + file.close(); + tape.start(dir); + return buf; +} + +void restore(sdtape &tape, const char *dir, const char *filename) { + tape.stop(); + snprintf(buf, sizeof(buf), "%s%s", dir, filename); + File file = SD.open(buf, O_READ); + hardware_restore(file); + file.close(); + int n = sscanf(buf + strlen(dir), "%[A-Z0-9].%d", chkpt, &cpid); + cpid = (n == 1)? 0: cpid+1; + tape.start(dir); +} diff --git a/checkpoint.h b/checkpoint.h new file mode 100644 index 0000000..7297ae9 --- /dev/null +++ b/checkpoint.h @@ -0,0 +1,8 @@ +#ifndef __CHECKPOINT_H__ +#define __CHECKPOINT_H__ + +// utility checkpoint functions +const char *checkpoint(sdtape &tape, const char *dir); +void restore(sdtape &tape, const char *dir, const char *filename); + +#endif diff --git a/r65emu.h b/r65emu.h index ed8eb9c..71ef289 100644 --- a/r65emu.h +++ b/r65emu.h @@ -13,5 +13,6 @@ #include "sdtape.h" #include "timed.h" #include "hardware.h" +#include "checkpoint.h" #endif