1
0
mirror of https://github.com/jscrane/r65emu.git synced 2024-06-08 07:29:37 +00:00
r65emu/checkpoint.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

#include <stdint.h>
2018-11-10 09:32:33 +00:00
#include <stdio.h>
#include <string.h>
#include "hardware.h"
2014-11-14 18:27:54 +00:00
#include "sdtape.h"
#include "checkpoint.h"
2018-11-10 09:32:33 +00:00
#if defined(USE_SD)
#include <SD.h>
#elif defined(USE_SPIFFS)
#include <SPIFFS.h>
2018-11-10 13:35:41 +00:00
#elif defined(ESP8266)
#include <FS.h>
#endif
2014-11-14 18:27:54 +00:00
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++);
2018-11-10 13:35:41 +00:00
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(ESP8266)
2018-11-10 09:32:33 +00:00
#if defined(USE_SD)
2014-11-14 18:27:54 +00:00
File file = SD.open(buf, O_WRITE | O_CREAT | O_TRUNC);
2018-11-10 13:35:41 +00:00
#elif defined(USE_SPIFFS)
File file = SPIFFS.open(buf, FILE_WRITE);
2018-11-10 13:35:41 +00:00
#else
File file = SPIFFS.open(buf, "w");
#endif
2014-11-14 18:27:54 +00:00
hardware_checkpoint(file);
file.close();
2018-11-10 09:32:33 +00:00
#endif
2014-11-14 18:27:54 +00:00
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);
2018-11-10 13:35:41 +00:00
#if defined(USE_SD) || defined(USE_SPIFFS) || defined(ESP8266)
2018-11-10 09:32:33 +00:00
#if defined(USE_SD)
2014-11-14 18:27:54 +00:00
File file = SD.open(buf, O_READ);
2018-11-10 13:35:41 +00:00
#elif defined(USE_SPIFFS)
File file = SPIFFS.open(buf, FILE_READ);
2018-11-10 13:35:41 +00:00
#else
File file = SPIFFS.open(buf, "r");
#endif
2014-11-14 18:27:54 +00:00
hardware_restore(file);
file.close();
2018-11-10 09:32:33 +00:00
#endif
2014-11-14 18:27:54 +00:00
int n = sscanf(buf + strlen(dir), "%[A-Z0-9].%d", chkpt, &cpid);
cpid = (n == 1)? 0: cpid+1;
tape.start(dir);
}