-
Notifications
You must be signed in to change notification settings - Fork 1
/
persistence.ino
308 lines (266 loc) · 8.02 KB
/
persistence.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "FS.h"
#include "SPIFFS.h"
#include <M5Stack.h>
#define FORMAT_SPIFFS_IF_FAILED true
struct CPUSTATUS {
uint16_t pc;
uint8_t sp;
uint8_t a;
uint8_t x;
uint8_t y;
uint8_t cpustatus;
};
extern CPUSTATUS getCPUSTATUS();
extern void setCPUSTATUS(CPUSTATUS cs);
void listDir(fs::FS &fs, const char * dirname, uint8_t levels) {
Serial.printf("Listing directory: %s\r\n", dirname);
M5.Lcd.printf("Listing directory: %s\r\n", dirname);
File root = fs.open(dirname);
if (!root) {
Serial.println("- failed to open directory");
M5.Lcd.println("- failed to open directory");
return;
}
if (!root.isDirectory()) {
Serial.println(" - not a directory");
M5.Lcd.println(" - not a directory");
return;
}
File file = root.openNextFile();
while (file) {
if (file.isDirectory()) {
Serial.print(" DIR : ");
M5.Lcd.println(" DIR : ");
Serial.println(file.name());
M5.Lcd.println(file.name());
if (levels) {
listDir(fs, file.name(), levels - 1);
}
} else {
Serial.print(" FILE: ");
M5.Lcd.print(" FILE: ");
Serial.print(file.name());
M5.Lcd.print(file.name());
Serial.print("\t SIZE: ");
M5.Lcd.print("\t SIZE: ");
Serial.println(file.size());
M5.Lcd.println(file.size());
}
file = root.openNextFile();
}
}
void pushMemory(fs::FS &fs, const char * path) {
Serial.printf("Memory file I/O with %s\r\n", path);
M5.Lcd.printf("Memory file I/O with %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("- failed to open file for writing");
M5.Lcd.println("- failed to open file for writing");
return;
}
// memory array data: uint8_t RAM[RAM_SIZE];
Serial.print("- writing" );
M5.Lcd.print("- writing" );
uint32_t start = millis();
//file.write(RAM, RAM_SIZE);
size_t i = 0;
for (i = 0; i < RAM_SIZE; i++) {
file.write(RAM[i]);
}
Serial.println("");
M5.Lcd.println("");
uint32_t end = millis() - start;
Serial.printf(" - %u bytes written in %u ms\r\n", RAM_SIZE, end);
M5.Lcd.println("");
file.close();
}
void pullMemory(fs::FS &fs, const char * path) {
Serial.printf("file I/O with %s\r\n", path);
M5.Lcd.printf("file I/O with %s\r\n", path);
File file = fs.open(path, FILE_READ);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
M5.Lcd.println("- failed to open file for reading");
return;
}
size_t len = 0;
uint32_t start = millis();
uint32_t end = start;
size_t i = 0;
// memory array data: uint8_t RAM[RAM_SIZE];
start = millis();
len = file.size();
size_t flen = len;
Serial.printf("- %u bytes\r\n", flen);
M5.Lcd.printf("- %u bytes\r\n", flen);
static uint8_t buf[RAM_SIZE];
Serial.print("- reading" );
M5.Lcd.print("- reading" );
for (i = 0; i < file.size(); i++) //Read upto complete file size
{
buf[i] = ((uint8_t)file.read());
}
Serial.println("");
M5.Lcd.println("");
end = millis() - start;
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
M5.Lcd.printf("- %u bytes read in %u ms\r\n", flen, end);
file.close();
Serial.println("Memory push");
M5.Lcd.println("Memory push");
for (i = 0; i < RAM_SIZE; i++) //Read upto complete file size
{
uint8_t tmp = buf[i];
RAM[i] = tmp;
}
}
void pushCPU(fs::FS &fs, const char * path) {
Serial.printf("CPU file I/O with %s\r\n", path);
M5.Lcd.printf("CPU file I/O with %s\r\n", path);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("- failed to open file for writing");
M5.Lcd.println("- failed to open file for writing");
return;
}
// memory array data: uint8_t RAM[RAM_SIZE];
Serial.print("- writing" );
M5.Lcd.print("- writing" );
uint32_t start = millis();
//file.write(RAM, RAM_SIZE);
size_t i = 0;
CPUSTATUS cs = getCPUSTATUS();
Serial.println("");
M5.Lcd.println("");
Serial.printf("%u %u %u %u %u %u", cs.pc, cs.sp, cs.a, cs.x, cs.y, cs.cpustatus);
M5.Lcd.printf("%u %u %u %u %u %u", cs.pc, cs.sp, cs.a, cs.x, cs.y, cs.cpustatus);
uint8_t lowbit = ((cs.pc >> 8) & 0xFF);
uint8_t highbit = (cs.pc & 0xFF);
//file.write(cs.pc);
file.write(lowbit);
file.write(highbit);
file.write(cs.sp);
file.write(cs.a);
file.write(cs.x);
file.write(cs.y);
file.write(cs.cpustatus);
Serial.println("");
M5.Lcd.println("");
uint32_t end = millis() - start;
Serial.printf(" - %u bytes written in %u ms\r\n", sizeof(cs), end);
M5.Lcd.println("");
file.close();
}
void pullCPU(fs::FS &fs, const char * path) {
Serial.printf("CPU with %s\r\n", path);
M5.Lcd.printf("CPU I/O with %s\r\n", path);
File file = fs.open(path, FILE_READ);
if (!file || file.isDirectory()) {
Serial.println("- failed to open file for reading");
M5.Lcd.println("- failed to open file for reading");
return;
}
size_t len = 0;
uint32_t start = millis();
uint32_t end = start;
size_t i = 0;
// memory array data: uint8_t RAM[RAM_SIZE];
start = millis();
len = file.size();
size_t flen = len;
Serial.printf("- %u bytes\r\n", flen);
M5.Lcd.printf("- %u bytes\r\n", flen);
// static uint8_t buf[RAM_SIZE];
Serial.print("- reading" );
M5.Lcd.print("- reading" );
CPUSTATUS cs;
uint8_t lowbit;
uint8_t highbit;
//cs.pc=file.read();
lowbit = file.read();
highbit = file.read();
cs.pc = ((uint16_t)lowbit << 8) | highbit;
cs.sp = file.read();
cs.a = file.read();
cs.x = file.read();
cs.y = file.read();
cs.cpustatus = file.read();
Serial.println("");
M5.Lcd.println("");
Serial.printf("%u %u %u %u %u %u", cs.pc, cs.sp, cs.a, cs.x, cs.y, cs.cpustatus);
M5.Lcd.printf("%u %u %u %u %u %u", cs.pc, cs.sp, cs.a, cs.x, cs.y, cs.cpustatus);
Serial.println("");
M5.Lcd.println("");
end = millis() - start;
Serial.printf("- %u bytes read in %u ms\r\n", flen, end);
M5.Lcd.printf("- %u bytes read in %u ms\r\n", flen, end);
file.close();
Serial.println("CPU Pull");
M5.Lcd.println("CPU Pull");
setCPUSTATUS(cs);
}
uint8_t getSlotNumber(fs::FS &fs) {
uint8_t myid;
char* path = "/slot.c64";
// sprintf(text, "%d", myid);
//writeFile(SPIFFS, "/slot.c64", text);
//void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Reading file: %s\r\n", path);
//M5.Lcd.printf("uint8_t sid; file: %s\r\n", path);
File file = fs.open(path, FILE_READ);
if (!file) {
Serial.println("- failed to open file for reading");
//M5.Lcd.println("- failed to open file for reading");
return 0;
}
if (myid = (uint8_t)file.read()) {
Serial.println("- file read");
//M5.Lcd.println("- file read");
} else {
Serial.println("- read failed");
//M5.Lcd.println("- read failed");
}
Serial.printf("slot: %i\r\n", myid);
//M5.Lcd.printf("slot: %i\r\n", myid);
return myid;
}
void setSlotNumber(fs::FS &fs, uint8_t myid) {
char text[1];
char* path = "/slot.c64";
// sprintf(text, "%d", myid);
//writeFile(SPIFFS, "/slot.c64", text);
//void writeFile(fs::FS &fs, const char * path, const char * message) {
Serial.printf("Writing file: %s\r\n", path);
//M5.Lcd.printf("Writing file: %s\r\n", path);
Serial.printf("set slot: %u\r\n", myid);
//M5.Lcd.printf("set slot: %u\r\n", myid);
File file = fs.open(path, FILE_WRITE);
if (!file) {
Serial.println("- failed to open file for writing");
//M5.Lcd.println("- failed to open file for writing");
return;
}
if (file.write(myid)) {
Serial.println("- file written");
//M5.Lcd.println("- file written");
} else {
Serial.println("- frite failed");
//M5.Lcd.println("- frite failed");
}
}
void persistenceinit() {
if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) {
Serial.println("SPIFFS Mount Failed");
M5.Lcd.println("SPIFFS Mount Failed");
return;
}
uint8_t sid;
//writeFile(SPIFFS, "/hello.txt", "Hello ");
//pushMemory(SPIFFS, "/memory.c64");
//pullMemory(SPIFFS, "/memory.c64");
listDir(SPIFFS, "/", 0);
//setSlotNumber(SPIFFS, 4);
sid = getSlotNumber(SPIFFS);
Serial.printf("slot: %u\r\n", sid);
M5.Lcd.printf("slot: %u\r\n", sid);
}