src/gba/savedata.c (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba/internal/gba/savedata.h>
7
8#include <mgba/internal/arm/macros.h>
9#include <mgba/internal/gba/gba.h>
10#include <mgba/internal/gba/serialize.h>
11
12#include <mgba-util/memory.h>
13#include <mgba-util/vfs.h>
14
15#include <errno.h>
16#include <fcntl.h>
17
18// Some testing was done here...
19// Erase cycles can vary greatly.
20// Some games may vary anywhere between about 2000 cycles to up to 30000 cycles. (Observed on a Macronix (09C2) chip).
21// Other games vary from very little, with a fairly solid 20500 cycle count. (Observed on a SST (D4BF) chip).
22// An average estimation is as follows.
23#define FLASH_ERASE_CYCLES 30000
24#define FLASH_PROGRAM_CYCLES 650
25// This needs real testing, and is only an estimation currently
26#define EEPROM_SETTLE_CYCLES 115000
27#define CLEANUP_THRESHOLD 15
28
29mLOG_DEFINE_CATEGORY(GBA_SAVE, "GBA Savedata", "gba.savedata");
30
31static void _flashSwitchBank(struct GBASavedata* savedata, int bank);
32static void _flashErase(struct GBASavedata* savedata);
33static void _flashEraseSector(struct GBASavedata* savedata, uint16_t sectorStart);
34
35static void _ashesToAshes(struct mTiming* timing, void* user, uint32_t cyclesLate) {
36 UNUSED(timing);
37 UNUSED(user);
38 UNUSED(cyclesLate);
39 // Funk to funky
40}
41
42void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf) {
43 savedata->type = SAVEDATA_AUTODETECT;
44 savedata->data = 0;
45 savedata->command = EEPROM_COMMAND_NULL;
46 savedata->flashState = FLASH_STATE_RAW;
47 savedata->vf = vf;
48 savedata->realVf = vf;
49 savedata->mapMode = MAP_WRITE;
50 savedata->maskWriteback = false;
51 savedata->dirty = 0;
52 savedata->dirtAge = 0;
53 savedata->dust.name = "GBA Savedata Settling";
54 savedata->dust.priority = 0x70;
55 savedata->dust.context = savedata;
56 savedata->dust.callback = _ashesToAshes;
57}
58
59void GBASavedataDeinit(struct GBASavedata* savedata) {
60 if (savedata->vf) {
61 size_t size = GBASavedataSize(savedata);
62 if (savedata->data) {
63 savedata->vf->unmap(savedata->vf, savedata->data, size);
64 }
65 savedata->vf = NULL;
66 } else {
67 switch (savedata->type) {
68 case SAVEDATA_SRAM:
69 mappedMemoryFree(savedata->data, SIZE_CART_SRAM);
70 break;
71 case SAVEDATA_FLASH512:
72 mappedMemoryFree(savedata->data, SIZE_CART_FLASH512);
73 break;
74 case SAVEDATA_FLASH1M:
75 mappedMemoryFree(savedata->data, SIZE_CART_FLASH1M);
76 break;
77 case SAVEDATA_EEPROM:
78 mappedMemoryFree(savedata->data, SIZE_CART_EEPROM);
79 break;
80 case SAVEDATA_EEPROM512:
81 mappedMemoryFree(savedata->data, SIZE_CART_EEPROM512);
82 break;
83 case SAVEDATA_FORCE_NONE:
84 case SAVEDATA_AUTODETECT:
85 break;
86 }
87 }
88 savedata->data = 0;
89 savedata->type = SAVEDATA_AUTODETECT;
90}
91
92void GBASavedataMask(struct GBASavedata* savedata, struct VFile* vf, bool writeback) {
93 enum SavedataType type = savedata->type;
94 struct VFile* oldVf = savedata->vf;
95 GBASavedataDeinit(savedata);
96 if (oldVf && oldVf != savedata->realVf) {
97 oldVf->close(oldVf);
98 }
99 savedata->vf = vf;
100 savedata->mapMode = MAP_READ;
101 savedata->maskWriteback = writeback;
102 GBASavedataForceType(savedata, type);
103}
104
105void GBASavedataUnmask(struct GBASavedata* savedata) {
106 if (!savedata->realVf || savedata->vf == savedata->realVf) {
107 return;
108 }
109 enum SavedataType type = savedata->type;
110 struct VFile* vf = savedata->vf;
111 GBASavedataDeinit(savedata);
112 savedata->vf = savedata->realVf;
113 savedata->mapMode = MAP_WRITE;
114 GBASavedataForceType(savedata, type);
115 if (savedata->maskWriteback) {
116 GBASavedataLoad(savedata, vf);
117 savedata->maskWriteback = false;
118 }
119 vf->close(vf);
120}
121
122bool GBASavedataClone(struct GBASavedata* savedata, struct VFile* out) {
123 if (savedata->data) {
124 switch (savedata->type) {
125 case SAVEDATA_SRAM:
126 return out->write(out, savedata->data, SIZE_CART_SRAM) == SIZE_CART_SRAM;
127 case SAVEDATA_FLASH512:
128 return out->write(out, savedata->data, SIZE_CART_FLASH512) == SIZE_CART_FLASH512;
129 case SAVEDATA_FLASH1M:
130 return out->write(out, savedata->data, SIZE_CART_FLASH1M) == SIZE_CART_FLASH1M;
131 case SAVEDATA_EEPROM:
132 return out->write(out, savedata->data, SIZE_CART_EEPROM) == SIZE_CART_EEPROM;
133 case SAVEDATA_EEPROM512:
134 return out->write(out, savedata->data, SIZE_CART_EEPROM512) == SIZE_CART_EEPROM512;
135 case SAVEDATA_AUTODETECT:
136 case SAVEDATA_FORCE_NONE:
137 return true;
138 }
139 } else if (savedata->vf) {
140 off_t read = 0;
141 uint8_t buffer[2048];
142 do {
143 read = savedata->vf->read(savedata->vf, buffer, sizeof(buffer));
144 out->write(out, buffer, read);
145 } while (read == sizeof(buffer));
146 return read >= 0;
147 }
148 return true;
149}
150
151size_t GBASavedataSize(const struct GBASavedata* savedata) {
152 switch (savedata->type) {
153 case SAVEDATA_SRAM:
154 return SIZE_CART_SRAM;
155 case SAVEDATA_FLASH512:
156 return SIZE_CART_FLASH512;
157 case SAVEDATA_FLASH1M:
158 return SIZE_CART_FLASH1M;
159 case SAVEDATA_EEPROM:
160 return SIZE_CART_EEPROM;
161 case SAVEDATA_EEPROM512:
162 return SIZE_CART_EEPROM512;
163 case SAVEDATA_FORCE_NONE:
164 return 0;
165 case SAVEDATA_AUTODETECT:
166 default:
167 if (savedata->vf) {
168 return savedata->vf->size(savedata->vf);
169 }
170 return 0;
171 }
172}
173
174bool GBASavedataLoad(struct GBASavedata* savedata, struct VFile* in) {
175 if (savedata->data) {
176 if (!in && savedata->type != SAVEDATA_FORCE_NONE) {
177 return false;
178 }
179 ssize_t size = GBASavedataSize(savedata);
180 in->seek(in, 0, SEEK_SET);
181 return in->read(in, savedata->data, size) == size;
182 } else if (savedata->vf) {
183 off_t read = 0;
184 uint8_t buffer[2048];
185 savedata->vf->seek(savedata->vf, 0, SEEK_SET);
186 if (in) {
187 in->seek(in, 0, SEEK_SET);
188 do {
189 read = in->read(in, buffer, sizeof(buffer));
190 read = savedata->vf->write(savedata->vf, buffer, read);
191 } while (read == sizeof(buffer));
192 }
193 memset(buffer, 0xFF, sizeof(buffer));
194 ssize_t fsize = savedata->vf->size(savedata->vf);
195 ssize_t pos = savedata->vf->seek(savedata->vf, 0, SEEK_CUR);
196 while (fsize - pos >= (ssize_t) sizeof(buffer)) {
197 savedata->vf->write(savedata->vf, buffer, sizeof(buffer));
198 pos = savedata->vf->seek(savedata->vf, 0, SEEK_CUR);
199 }
200 if (fsize - pos > 0) {
201 savedata->vf->write(savedata->vf, buffer, fsize - pos);
202 }
203 return read >= 0;
204 }
205 return true;
206}
207
208void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type) {
209 if (savedata->type == type) {
210 return;
211 }
212 if (savedata->type != SAVEDATA_AUTODETECT) {
213 struct VFile* vf = savedata->vf;
214 int mapMode = savedata->mapMode;
215 bool maskWriteback = savedata->maskWriteback;
216 GBASavedataDeinit(savedata);
217 GBASavedataInit(savedata, vf);
218 savedata->mapMode = mapMode;
219 savedata->maskWriteback = maskWriteback;
220 }
221 switch (type) {
222 case SAVEDATA_FLASH512:
223 case SAVEDATA_FLASH1M:
224 savedata->type = type;
225 GBASavedataInitFlash(savedata);
226 break;
227 case SAVEDATA_EEPROM:
228 case SAVEDATA_EEPROM512:
229 GBASavedataInitEEPROM(savedata);
230 break;
231 case SAVEDATA_SRAM:
232 GBASavedataInitSRAM(savedata);
233 break;
234 case SAVEDATA_FORCE_NONE:
235 savedata->type = SAVEDATA_FORCE_NONE;
236 break;
237 case SAVEDATA_AUTODETECT:
238 break;
239 }
240}
241
242void GBASavedataInitFlash(struct GBASavedata* savedata) {
243 if (savedata->type == SAVEDATA_AUTODETECT) {
244 savedata->type = SAVEDATA_FLASH512;
245 }
246 if (savedata->type != SAVEDATA_FLASH512 && savedata->type != SAVEDATA_FLASH1M) {
247 mLOG(GBA_SAVE, WARN, "Can't re-initialize savedata");
248 return;
249 }
250 int32_t flashSize = SIZE_CART_FLASH512;
251 if (savedata->type == SAVEDATA_FLASH1M) {
252 flashSize = SIZE_CART_FLASH1M;
253 }
254 off_t end;
255 if (!savedata->vf) {
256 end = 0;
257 savedata->data = anonymousMemoryMap(SIZE_CART_FLASH1M);
258 } else {
259 end = savedata->vf->size(savedata->vf);
260 if (end < flashSize) {
261 savedata->vf->truncate(savedata->vf, flashSize);
262 }
263 savedata->data = savedata->vf->map(savedata->vf, flashSize, savedata->mapMode);
264 }
265
266 savedata->currentBank = savedata->data;
267 if (end < SIZE_CART_FLASH512) {
268 memset(&savedata->data[end], 0xFF, flashSize - end);
269 }
270}
271
272void GBASavedataInitEEPROM(struct GBASavedata* savedata) {
273 if (savedata->type == SAVEDATA_AUTODETECT) {
274 savedata->type = SAVEDATA_EEPROM512;
275 } else if (savedata->type != SAVEDATA_EEPROM512 && savedata->type != SAVEDATA_EEPROM) {
276 mLOG(GBA_SAVE, WARN, "Can't re-initialize savedata");
277 return;
278 }
279 int32_t eepromSize = SIZE_CART_EEPROM512;
280 if (savedata->type == SAVEDATA_EEPROM) {
281 eepromSize = SIZE_CART_EEPROM;
282 }
283 off_t end;
284 if (!savedata->vf) {
285 end = 0;
286 savedata->data = anonymousMemoryMap(SIZE_CART_EEPROM);
287 } else {
288 end = savedata->vf->size(savedata->vf);
289 if (end < eepromSize) {
290 savedata->vf->truncate(savedata->vf, eepromSize);
291 }
292 savedata->data = savedata->vf->map(savedata->vf, eepromSize, savedata->mapMode);
293 }
294 if (end < SIZE_CART_EEPROM512) {
295 memset(&savedata->data[end], 0xFF, SIZE_CART_EEPROM512 - end);
296 }
297}
298
299void GBASavedataInitSRAM(struct GBASavedata* savedata) {
300 if (savedata->type == SAVEDATA_AUTODETECT) {
301 savedata->type = SAVEDATA_SRAM;
302 } else {
303 mLOG(GBA_SAVE, WARN, "Can't re-initialize savedata");
304 return;
305 }
306 off_t end;
307 if (!savedata->vf) {
308 end = 0;
309 savedata->data = anonymousMemoryMap(SIZE_CART_SRAM);
310 } else {
311 end = savedata->vf->size(savedata->vf);
312 if (end < SIZE_CART_SRAM) {
313 savedata->vf->truncate(savedata->vf, SIZE_CART_SRAM);
314 }
315 savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_SRAM, savedata->mapMode);
316 }
317
318 if (end < SIZE_CART_SRAM) {
319 memset(&savedata->data[end], 0xFF, SIZE_CART_SRAM - end);
320 }
321}
322
323uint8_t GBASavedataReadFlash(struct GBASavedata* savedata, uint16_t address) {
324 if (savedata->command == FLASH_COMMAND_ID) {
325 if (savedata->type == SAVEDATA_FLASH512) {
326 if (address < 2) {
327 return FLASH_MFG_PANASONIC >> (address * 8);
328 }
329 } else if (savedata->type == SAVEDATA_FLASH1M) {
330 if (address < 2) {
331 return FLASH_MFG_SANYO >> (address * 8);
332 }
333 }
334 }
335 if (mTimingIsScheduled(savedata->timing, &savedata->dust) && (address >> 12) == savedata->settling) {
336 return 0x5F;
337 }
338 return savedata->currentBank[address];
339}
340
341void GBASavedataWriteFlash(struct GBASavedata* savedata, uint16_t address, uint8_t value) {
342 switch (savedata->flashState) {
343 case FLASH_STATE_RAW:
344 switch (savedata->command) {
345 case FLASH_COMMAND_PROGRAM:
346 savedata->dirty |= SAVEDATA_DIRT_NEW;
347 savedata->currentBank[address] = value;
348 savedata->command = FLASH_COMMAND_NONE;
349 mTimingDeschedule(savedata->timing, &savedata->dust);
350 mTimingSchedule(savedata->timing, &savedata->dust, FLASH_PROGRAM_CYCLES);
351 break;
352 case FLASH_COMMAND_SWITCH_BANK:
353 if (address == 0 && value < 2) {
354 _flashSwitchBank(savedata, value);
355 } else {
356 mLOG(GBA_SAVE, GAME_ERROR, "Bad flash bank switch");
357 savedata->command = FLASH_COMMAND_NONE;
358 }
359 savedata->command = FLASH_COMMAND_NONE;
360 break;
361 default:
362 if (address == FLASH_BASE_HI && value == FLASH_COMMAND_START) {
363 savedata->flashState = FLASH_STATE_START;
364 } else {
365 mLOG(GBA_SAVE, GAME_ERROR, "Bad flash write: %#04x = %#02x", address, value);
366 }
367 break;
368 }
369 break;
370 case FLASH_STATE_START:
371 if (address == FLASH_BASE_LO && value == FLASH_COMMAND_CONTINUE) {
372 savedata->flashState = FLASH_STATE_CONTINUE;
373 } else {
374 mLOG(GBA_SAVE, GAME_ERROR, "Bad flash write: %#04x = %#02x", address, value);
375 savedata->flashState = FLASH_STATE_RAW;
376 }
377 break;
378 case FLASH_STATE_CONTINUE:
379 savedata->flashState = FLASH_STATE_RAW;
380 if (address == FLASH_BASE_HI) {
381 switch (savedata->command) {
382 case FLASH_COMMAND_NONE:
383 switch (value) {
384 case FLASH_COMMAND_ERASE:
385 case FLASH_COMMAND_ID:
386 case FLASH_COMMAND_PROGRAM:
387 case FLASH_COMMAND_SWITCH_BANK:
388 savedata->command = value;
389 break;
390 default:
391 mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash operation: %#02x", value);
392 break;
393 }
394 break;
395 case FLASH_COMMAND_ERASE:
396 switch (value) {
397 case FLASH_COMMAND_ERASE_CHIP:
398 _flashErase(savedata);
399 break;
400 default:
401 mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash erase operation: %#02x", value);
402 break;
403 }
404 savedata->command = FLASH_COMMAND_NONE;
405 break;
406 case FLASH_COMMAND_ID:
407 if (value == FLASH_COMMAND_TERMINATE) {
408 savedata->command = FLASH_COMMAND_NONE;
409 }
410 break;
411 default:
412 mLOG(GBA_SAVE, ERROR, "Flash entered bad state: %#02x", savedata->command);
413 savedata->command = FLASH_COMMAND_NONE;
414 break;
415 }
416 } else if (savedata->command == FLASH_COMMAND_ERASE) {
417 if (value == FLASH_COMMAND_ERASE_SECTOR) {
418 _flashEraseSector(savedata, address);
419 savedata->command = FLASH_COMMAND_NONE;
420 } else {
421 mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash erase operation: %#02x", value);
422 }
423 }
424 break;
425 }
426}
427
428static void _ensureEeprom(struct GBASavedata* savedata, uint32_t size) {
429 if (size < SIZE_CART_EEPROM512) {
430 return;
431 }
432 if (savedata->type == SAVEDATA_EEPROM) {
433 return;
434 }
435 savedata->type = SAVEDATA_EEPROM;
436 if (!savedata->vf) {
437 return;
438 }
439 savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_EEPROM512);
440 if (savedata->vf->size(savedata->vf) < SIZE_CART_EEPROM) {
441 savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM);
442 savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode);
443 memset(&savedata->data[SIZE_CART_EEPROM512], 0xFF, SIZE_CART_EEPROM - SIZE_CART_EEPROM512);
444 } else {
445 savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode);
446 }
447}
448
449void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize) {
450 switch (savedata->command) {
451 // Read header
452 case EEPROM_COMMAND_NULL:
453 default:
454 savedata->command = value & 0x1;
455 break;
456 case EEPROM_COMMAND_PENDING:
457 savedata->command <<= 1;
458 savedata->command |= value & 0x1;
459 if (savedata->command == EEPROM_COMMAND_WRITE) {
460 savedata->writeAddress = 0;
461 } else {
462 savedata->readAddress = 0;
463 }
464 break;
465 // Do commands
466 case EEPROM_COMMAND_WRITE:
467 // Write
468 if (writeSize > 65) {
469 savedata->writeAddress <<= 1;
470 savedata->writeAddress |= (value & 0x1) << 6;
471 } else if (writeSize == 1) {
472 savedata->command = EEPROM_COMMAND_NULL;
473 } else if ((savedata->writeAddress >> 3) < SIZE_CART_EEPROM) {
474 _ensureEeprom(savedata, savedata->writeAddress >> 3);
475 uint8_t current = savedata->data[savedata->writeAddress >> 3];
476 current &= ~(1 << (0x7 - (savedata->writeAddress & 0x7)));
477 current |= (value & 0x1) << (0x7 - (savedata->writeAddress & 0x7));
478 savedata->dirty |= SAVEDATA_DIRT_NEW;
479 savedata->data[savedata->writeAddress >> 3] = current;
480 mTimingDeschedule(savedata->timing, &savedata->dust);
481 mTimingSchedule(savedata->timing, &savedata->dust, EEPROM_SETTLE_CYCLES);
482 ++savedata->writeAddress;
483 } else {
484 mLOG(GBA_SAVE, GAME_ERROR, "Writing beyond end of EEPROM: %08X", (savedata->writeAddress >> 3));
485 }
486 break;
487 case EEPROM_COMMAND_READ_PENDING:
488 // Read
489 if (writeSize > 1) {
490 savedata->readAddress <<= 1;
491 if (value & 0x1) {
492 savedata->readAddress |= 0x40;
493 }
494 } else {
495 savedata->readBitsRemaining = 68;
496 savedata->command = EEPROM_COMMAND_READ;
497 }
498 break;
499 }
500}
501
502uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata) {
503 if (savedata->command != EEPROM_COMMAND_READ) {
504 if (!mTimingIsScheduled(savedata->timing, &savedata->dust)) {
505 return 1;
506 } else {
507 return 0;
508 }
509 }
510 --savedata->readBitsRemaining;
511 if (savedata->readBitsRemaining < 64) {
512 int step = 63 - savedata->readBitsRemaining;
513 uint32_t address = (savedata->readAddress + step) >> 3;
514 _ensureEeprom(savedata, address);
515 if (address >= SIZE_CART_EEPROM) {
516 mLOG(GBA_SAVE, GAME_ERROR, "Reading beyond end of EEPROM: %08X", address);
517 return 0xFF;
518 }
519 uint8_t data = savedata->data[address] >> (0x7 - (step & 0x7));
520 if (!savedata->readBitsRemaining) {
521 savedata->command = EEPROM_COMMAND_NULL;
522 }
523 return data & 0x1;
524 }
525 return 0;
526}
527
528void GBASavedataClean(struct GBASavedata* savedata, uint32_t frameCount) {
529 if (!savedata->vf) {
530 return;
531 }
532 if (savedata->dirty & SAVEDATA_DIRT_NEW) {
533 savedata->dirtAge = frameCount;
534 savedata->dirty &= ~SAVEDATA_DIRT_NEW;
535 if (!(savedata->dirty & SAVEDATA_DIRT_SEEN)) {
536 savedata->dirty |= SAVEDATA_DIRT_SEEN;
537 }
538 } else if ((savedata->dirty & SAVEDATA_DIRT_SEEN) && frameCount - savedata->dirtAge > CLEANUP_THRESHOLD) {
539 if (savedata->maskWriteback) {
540 GBASavedataUnmask(savedata);
541 }
542 if (savedata->mapMode & MAP_WRITE) {
543 size_t size = GBASavedataSize(savedata);
544 savedata->dirty = 0;
545 if (savedata->data && savedata->vf->sync(savedata->vf, savedata->data, size)) {
546 mLOG(GBA_SAVE, INFO, "Savedata synced");
547 } else {
548 mLOG(GBA_SAVE, INFO, "Savedata failed to sync!");
549 }
550 }
551 }
552}
553
554void GBASavedataSerialize(const struct GBASavedata* savedata, struct GBASerializedState* state) {
555 state->savedata.type = savedata->type;
556 state->savedata.command = savedata->command;
557 GBASerializedSavedataFlags flags = 0;
558 flags = GBASerializedSavedataFlagsSetFlashState(flags, savedata->flashState);
559 flags = GBASerializedSavedataFlagsTestFillFlashBank(flags, savedata->currentBank == &savedata->data[0x10000]);
560
561 if (mTimingIsScheduled(savedata->timing, &savedata->dust)) {
562 STORE_32(savedata->dust.when - mTimingCurrentTime(savedata->timing), 0, &state->savedata.settlingDust);
563 flags = GBASerializedSavedataFlagsFillDustSettling(flags);
564 }
565
566 state->savedata.flags = flags;
567 state->savedata.readBitsRemaining = savedata->readBitsRemaining;
568 STORE_32(savedata->readAddress, 0, &state->savedata.readAddress);
569 STORE_32(savedata->writeAddress, 0, &state->savedata.writeAddress);
570 STORE_16(savedata->settling, 0, &state->savedata.settlingSector);
571
572}
573
574void GBASavedataDeserialize(struct GBASavedata* savedata, const struct GBASerializedState* state) {
575 if (savedata->type != state->savedata.type) {
576 mLOG(GBA_SAVE, DEBUG, "Switching save types");
577 GBASavedataForceType(savedata, state->savedata.type);
578 }
579 savedata->command = state->savedata.command;
580 GBASerializedSavedataFlags flags = state->savedata.flags;
581 savedata->flashState = GBASerializedSavedataFlagsGetFlashState(flags);
582 savedata->readBitsRemaining = state->savedata.readBitsRemaining;
583 LOAD_32(savedata->readAddress, 0, &state->savedata.readAddress);
584 LOAD_32(savedata->writeAddress, 0, &state->savedata.writeAddress);
585 LOAD_16(savedata->settling, 0, &state->savedata.settlingSector);
586
587 if (savedata->type == SAVEDATA_FLASH1M) {
588 _flashSwitchBank(savedata, GBASerializedSavedataFlagsGetFlashBank(flags));
589 }
590
591 if (GBASerializedSavedataFlagsIsDustSettling(flags)) {
592 uint32_t when;
593 LOAD_32(when, 0, &state->savedata.settlingDust);
594 mTimingSchedule(savedata->timing, &savedata->dust, when);
595 }
596}
597
598void _flashSwitchBank(struct GBASavedata* savedata, int bank) {
599 mLOG(GBA_SAVE, DEBUG, "Performing flash bank switch to bank %i", bank);
600 savedata->currentBank = &savedata->data[bank << 16];
601 if (bank > 0 && savedata->type == SAVEDATA_FLASH512) {
602 mLOG(GBA_SAVE, INFO, "Updating flash chip from 512kb to 1Mb");
603 savedata->type = SAVEDATA_FLASH1M;
604 if (savedata->vf) {
605 savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_FLASH512);
606 if (savedata->vf->size(savedata->vf) < SIZE_CART_FLASH1M) {
607 savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M);
608 savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE);
609 memset(&savedata->data[SIZE_CART_FLASH512], 0xFF, SIZE_CART_FLASH512);
610 } else {
611 savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE);
612 }
613 }
614 }
615}
616
617void _flashErase(struct GBASavedata* savedata) {
618 mLOG(GBA_SAVE, DEBUG, "Performing flash chip erase");
619 savedata->dirty |= SAVEDATA_DIRT_NEW;
620 size_t size = SIZE_CART_FLASH512;
621 if (savedata->type == SAVEDATA_FLASH1M) {
622 size = SIZE_CART_FLASH1M;
623 }
624 memset(savedata->data, 0xFF, size);
625}
626
627void _flashEraseSector(struct GBASavedata* savedata, uint16_t sectorStart) {
628 mLOG(GBA_SAVE, DEBUG, "Performing flash sector erase at 0x%04x", sectorStart);
629 savedata->dirty |= SAVEDATA_DIRT_NEW;
630 size_t size = 0x1000;
631 if (savedata->type == SAVEDATA_FLASH1M) {
632 mLOG(GBA_SAVE, DEBUG, "Performing unknown sector-size erase at 0x%04x", sectorStart);
633 }
634 savedata->settling = sectorStart >> 12;
635 mTimingDeschedule(savedata->timing, &savedata->dust);
636 mTimingSchedule(savedata->timing, &savedata->dust, FLASH_ERASE_CYCLES);
637 memset(&savedata->currentBank[sectorStart & ~(size - 1)], 0xFF, size);
638}