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");
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->dirty = 0;
51 savedata->dirtAge = 0;
52 savedata->dust.name = "GBA Savedata Settling";
53 savedata->dust.priority = 0x70;
54 savedata->dust.context = savedata;
55 savedata->dust.callback = _ashesToAshes;
56}
57
58void GBASavedataDeinit(struct GBASavedata* savedata) {
59 if (savedata->vf) {
60 size_t size = GBASavedataSize(savedata);
61 if (savedata->data) {
62 savedata->vf->unmap(savedata->vf, savedata->data, size);
63 }
64 savedata->vf = NULL;
65 } else {
66 switch (savedata->type) {
67 case SAVEDATA_SRAM:
68 mappedMemoryFree(savedata->data, SIZE_CART_SRAM);
69 break;
70 case SAVEDATA_FLASH512:
71 mappedMemoryFree(savedata->data, SIZE_CART_FLASH512);
72 break;
73 case SAVEDATA_FLASH1M:
74 mappedMemoryFree(savedata->data, SIZE_CART_FLASH1M);
75 break;
76 case SAVEDATA_EEPROM:
77 mappedMemoryFree(savedata->data, SIZE_CART_EEPROM);
78 break;
79 case SAVEDATA_FORCE_NONE:
80 case SAVEDATA_AUTODETECT:
81 break;
82 }
83 }
84 savedata->data = 0;
85 savedata->type = SAVEDATA_AUTODETECT;
86}
87
88void GBASavedataMask(struct GBASavedata* savedata, struct VFile* vf, bool writeback) {
89 enum SavedataType type = savedata->type;
90 GBASavedataDeinit(savedata);
91 savedata->vf = vf;
92 savedata->mapMode = MAP_READ;
93 savedata->maskWriteback = writeback;
94 GBASavedataForceType(savedata, type, savedata->realisticTiming);
95}
96
97void GBASavedataUnmask(struct GBASavedata* savedata) {
98 if (savedata->vf == savedata->realVf) {
99 return;
100 }
101 enum SavedataType type = savedata->type;
102 struct VFile* vf = savedata->vf;
103 GBASavedataDeinit(savedata);
104 savedata->vf = savedata->realVf;
105 savedata->mapMode = MAP_WRITE;
106 GBASavedataForceType(savedata, type, savedata->realisticTiming);
107 if (savedata->maskWriteback) {
108 GBASavedataLoad(savedata, vf);
109 savedata->maskWriteback = false;
110 }
111 vf->close(vf);
112}
113
114bool GBASavedataClone(struct GBASavedata* savedata, struct VFile* out) {
115 if (savedata->data) {
116 switch (savedata->type) {
117 case SAVEDATA_SRAM:
118 return out->write(out, savedata->data, SIZE_CART_SRAM) == SIZE_CART_SRAM;
119 case SAVEDATA_FLASH512:
120 return out->write(out, savedata->data, SIZE_CART_FLASH512) == SIZE_CART_FLASH512;
121 case SAVEDATA_FLASH1M:
122 return out->write(out, savedata->data, SIZE_CART_FLASH1M) == SIZE_CART_FLASH1M;
123 case SAVEDATA_EEPROM:
124 return out->write(out, savedata->data, SIZE_CART_EEPROM) == SIZE_CART_EEPROM;
125 case SAVEDATA_AUTODETECT:
126 case SAVEDATA_FORCE_NONE:
127 return true;
128 }
129 } else if (savedata->vf) {
130 off_t read = 0;
131 uint8_t buffer[2048];
132 do {
133 read = savedata->vf->read(savedata->vf, buffer, sizeof(buffer));
134 out->write(out, buffer, read);
135 } while (read == sizeof(buffer));
136 return read >= 0;
137 }
138 return true;
139}
140
141size_t GBASavedataSize(struct GBASavedata* savedata) {
142 switch (savedata->type) {
143 case SAVEDATA_SRAM:
144 return SIZE_CART_SRAM;
145 case SAVEDATA_FLASH512:
146 return SIZE_CART_FLASH512;
147 case SAVEDATA_FLASH1M:
148 return SIZE_CART_FLASH1M;
149 case SAVEDATA_EEPROM:
150 return SIZE_CART_EEPROM;
151 case SAVEDATA_FORCE_NONE:
152 return 0;
153 case SAVEDATA_AUTODETECT:
154 default:
155 if (savedata->vf) {
156 return savedata->vf->size(savedata->vf);
157 }
158 return 0;
159 }
160}
161
162bool GBASavedataLoad(struct GBASavedata* savedata, struct VFile* in) {
163 if (savedata->data) {
164 if (!in && savedata->type != SAVEDATA_FORCE_NONE) {
165 return false;
166 }
167 ssize_t size = GBASavedataSize(savedata);
168 in->seek(in, 0, SEEK_SET);
169 return in->read(in, savedata->data, size) == size;
170 } else if (savedata->vf) {
171 off_t read = 0;
172 uint8_t buffer[2048];
173 savedata->vf->seek(savedata->vf, 0, SEEK_SET);
174 if (in) {
175 in->seek(in, 0, SEEK_SET);
176 do {
177 read = in->read(in, buffer, sizeof(buffer));
178 read = savedata->vf->write(savedata->vf, buffer, read);
179 } while (read == sizeof(buffer));
180 }
181 memset(buffer, 0xFF, sizeof(buffer));
182 ssize_t fsize = savedata->vf->size(savedata->vf);
183 ssize_t pos = savedata->vf->seek(savedata->vf, 0, SEEK_CUR);
184 while (fsize - pos >= (ssize_t) sizeof(buffer)) {
185 savedata->vf->write(savedata->vf, buffer, sizeof(buffer));
186 pos = savedata->vf->seek(savedata->vf, 0, SEEK_CUR);
187 }
188 if (fsize - pos > 0) {
189 savedata->vf->write(savedata->vf, buffer, fsize - pos);
190 }
191 return read >= 0;
192 }
193 return true;
194}
195
196void GBASavedataForceType(struct GBASavedata* savedata, enum SavedataType type, bool realisticTiming) {
197 if (savedata->type != SAVEDATA_AUTODETECT) {
198 struct VFile* vf = savedata->vf;
199 GBASavedataDeinit(savedata);
200 GBASavedataInit(savedata, vf);
201 }
202 switch (type) {
203 case SAVEDATA_FLASH512:
204 case SAVEDATA_FLASH1M:
205 savedata->type = type;
206 GBASavedataInitFlash(savedata, realisticTiming);
207 break;
208 case SAVEDATA_EEPROM:
209 GBASavedataInitEEPROM(savedata, realisticTiming);
210 break;
211 case SAVEDATA_SRAM:
212 GBASavedataInitSRAM(savedata);
213 break;
214 case SAVEDATA_FORCE_NONE:
215 savedata->type = SAVEDATA_FORCE_NONE;
216 break;
217 case SAVEDATA_AUTODETECT:
218 break;
219 }
220}
221
222void GBASavedataInitFlash(struct GBASavedata* savedata, bool realisticTiming) {
223 if (savedata->type == SAVEDATA_AUTODETECT) {
224 savedata->type = SAVEDATA_FLASH512;
225 }
226 if (savedata->type != SAVEDATA_FLASH512 && savedata->type != SAVEDATA_FLASH1M) {
227 mLOG(GBA_SAVE, WARN, "Can't re-initialize savedata");
228 return;
229 }
230 int32_t flashSize = SIZE_CART_FLASH512;
231 if (savedata->type == SAVEDATA_FLASH1M) {
232 flashSize = SIZE_CART_FLASH1M;
233 }
234 off_t end;
235 if (!savedata->vf) {
236 end = 0;
237 savedata->data = anonymousMemoryMap(SIZE_CART_FLASH1M);
238 } else {
239 end = savedata->vf->size(savedata->vf);
240 if (end < flashSize) {
241 savedata->vf->truncate(savedata->vf, flashSize);
242 }
243 savedata->data = savedata->vf->map(savedata->vf, flashSize, savedata->mapMode);
244 }
245
246 savedata->currentBank = savedata->data;
247 savedata->realisticTiming = realisticTiming;
248 if (end < SIZE_CART_FLASH512) {
249 memset(&savedata->data[end], 0xFF, flashSize - end);
250 }
251}
252
253void GBASavedataInitEEPROM(struct GBASavedata* savedata, bool realisticTiming) {
254 if (savedata->type == SAVEDATA_AUTODETECT) {
255 savedata->type = SAVEDATA_EEPROM;
256 } else {
257 mLOG(GBA_SAVE, WARN, "Can't re-initialize savedata");
258 return;
259 }
260 off_t end;
261 if (!savedata->vf) {
262 end = 0;
263 savedata->data = anonymousMemoryMap(SIZE_CART_EEPROM);
264 } else {
265 end = savedata->vf->size(savedata->vf);
266 if (end < SIZE_CART_EEPROM) {
267 savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM);
268 }
269 savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode);
270 }
271 savedata->realisticTiming = realisticTiming;
272 if (end < SIZE_CART_EEPROM) {
273 memset(&savedata->data[end], 0xFF, SIZE_CART_EEPROM - end);
274 }
275}
276
277void GBASavedataInitSRAM(struct GBASavedata* savedata) {
278 if (savedata->type == SAVEDATA_AUTODETECT) {
279 savedata->type = SAVEDATA_SRAM;
280 } else {
281 mLOG(GBA_SAVE, WARN, "Can't re-initialize savedata");
282 return;
283 }
284 off_t end;
285 if (!savedata->vf) {
286 end = 0;
287 savedata->data = anonymousMemoryMap(SIZE_CART_SRAM);
288 } else {
289 end = savedata->vf->size(savedata->vf);
290 if (end < SIZE_CART_SRAM) {
291 savedata->vf->truncate(savedata->vf, SIZE_CART_SRAM);
292 }
293 savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_SRAM, savedata->mapMode);
294 }
295
296 if (end < SIZE_CART_SRAM) {
297 memset(&savedata->data[end], 0xFF, SIZE_CART_SRAM - end);
298 }
299}
300
301uint8_t GBASavedataReadFlash(struct GBASavedata* savedata, uint16_t address) {
302 if (savedata->command == FLASH_COMMAND_ID) {
303 if (savedata->type == SAVEDATA_FLASH512) {
304 if (address < 2) {
305 return FLASH_MFG_PANASONIC >> (address * 8);
306 }
307 } else if (savedata->type == SAVEDATA_FLASH1M) {
308 if (address < 2) {
309 return FLASH_MFG_SANYO >> (address * 8);
310 }
311 }
312 }
313 if (mTimingIsScheduled(savedata->timing, &savedata->dust) && (address >> 12) == savedata->settling) {
314 return 0x5F;
315 }
316 return savedata->currentBank[address];
317}
318
319void GBASavedataWriteFlash(struct GBASavedata* savedata, uint16_t address, uint8_t value) {
320 switch (savedata->flashState) {
321 case FLASH_STATE_RAW:
322 switch (savedata->command) {
323 case FLASH_COMMAND_PROGRAM:
324 savedata->dirty |= SAVEDATA_DIRT_NEW;
325 savedata->currentBank[address] = value;
326 savedata->command = FLASH_COMMAND_NONE;
327 if (savedata->realisticTiming) {
328 mTimingDeschedule(savedata->timing, &savedata->dust);
329 mTimingSchedule(savedata->timing, &savedata->dust, FLASH_PROGRAM_CYCLES);
330 }
331 break;
332 case FLASH_COMMAND_SWITCH_BANK:
333 if (address == 0 && value < 2) {
334 _flashSwitchBank(savedata, value);
335 } else {
336 mLOG(GBA_SAVE, GAME_ERROR, "Bad flash bank switch");
337 savedata->command = FLASH_COMMAND_NONE;
338 }
339 savedata->command = FLASH_COMMAND_NONE;
340 break;
341 default:
342 if (address == FLASH_BASE_HI && value == FLASH_COMMAND_START) {
343 savedata->flashState = FLASH_STATE_START;
344 } else {
345 mLOG(GBA_SAVE, GAME_ERROR, "Bad flash write: %#04x = %#02x", address, value);
346 }
347 break;
348 }
349 break;
350 case FLASH_STATE_START:
351 if (address == FLASH_BASE_LO && value == FLASH_COMMAND_CONTINUE) {
352 savedata->flashState = FLASH_STATE_CONTINUE;
353 } else {
354 mLOG(GBA_SAVE, GAME_ERROR, "Bad flash write: %#04x = %#02x", address, value);
355 savedata->flashState = FLASH_STATE_RAW;
356 }
357 break;
358 case FLASH_STATE_CONTINUE:
359 savedata->flashState = FLASH_STATE_RAW;
360 if (address == FLASH_BASE_HI) {
361 switch (savedata->command) {
362 case FLASH_COMMAND_NONE:
363 switch (value) {
364 case FLASH_COMMAND_ERASE:
365 case FLASH_COMMAND_ID:
366 case FLASH_COMMAND_PROGRAM:
367 case FLASH_COMMAND_SWITCH_BANK:
368 savedata->command = value;
369 break;
370 default:
371 mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash operation: %#02x", value);
372 break;
373 }
374 break;
375 case FLASH_COMMAND_ERASE:
376 switch (value) {
377 case FLASH_COMMAND_ERASE_CHIP:
378 _flashErase(savedata);
379 break;
380 default:
381 mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash erase operation: %#02x", value);
382 break;
383 }
384 savedata->command = FLASH_COMMAND_NONE;
385 break;
386 case FLASH_COMMAND_ID:
387 if (value == FLASH_COMMAND_TERMINATE) {
388 savedata->command = FLASH_COMMAND_NONE;
389 }
390 break;
391 default:
392 mLOG(GBA_SAVE, ERROR, "Flash entered bad state: %#02x", savedata->command);
393 savedata->command = FLASH_COMMAND_NONE;
394 break;
395 }
396 } else if (savedata->command == FLASH_COMMAND_ERASE) {
397 if (value == FLASH_COMMAND_ERASE_SECTOR) {
398 _flashEraseSector(savedata, address);
399 savedata->command = FLASH_COMMAND_NONE;
400 } else {
401 mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash erase operation: %#02x", value);
402 }
403 }
404 break;
405 }
406}
407
408void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize) {
409 switch (savedata->command) {
410 // Read header
411 case EEPROM_COMMAND_NULL:
412 default:
413 savedata->command = value & 0x1;
414 break;
415 case EEPROM_COMMAND_PENDING:
416 savedata->command <<= 1;
417 savedata->command |= value & 0x1;
418 if (savedata->command == EEPROM_COMMAND_WRITE) {
419 savedata->writeAddress = 0;
420 } else {
421 savedata->readAddress = 0;
422 }
423 break;
424 // Do commands
425 case EEPROM_COMMAND_WRITE:
426 // Write
427 if (writeSize > 65) {
428 savedata->writeAddress <<= 1;
429 savedata->writeAddress |= (value & 0x1) << 6;
430 } else if (writeSize == 1) {
431 savedata->command = EEPROM_COMMAND_NULL;
432 } else if ((savedata->writeAddress >> 3) < SIZE_CART_EEPROM) {
433 uint8_t current = savedata->data[savedata->writeAddress >> 3];
434 current &= ~(1 << (0x7 - (savedata->writeAddress & 0x7)));
435 current |= (value & 0x1) << (0x7 - (savedata->writeAddress & 0x7));
436 savedata->dirty |= SAVEDATA_DIRT_NEW;
437 savedata->data[savedata->writeAddress >> 3] = current;
438 if (savedata->realisticTiming) {
439 mTimingDeschedule(savedata->timing, &savedata->dust);
440 mTimingSchedule(savedata->timing, &savedata->dust, EEPROM_SETTLE_CYCLES);
441 }
442 ++savedata->writeAddress;
443 } else {
444 mLOG(GBA_SAVE, GAME_ERROR, "Writing beyond end of EEPROM: %08X", (savedata->writeAddress >> 3));
445 }
446 break;
447 case EEPROM_COMMAND_READ_PENDING:
448 // Read
449 if (writeSize > 1) {
450 savedata->readAddress <<= 1;
451 if (value & 0x1) {
452 savedata->readAddress |= 0x40;
453 }
454 } else {
455 savedata->readBitsRemaining = 68;
456 savedata->command = EEPROM_COMMAND_READ;
457 }
458 break;
459 }
460}
461
462uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata) {
463 if (savedata->command != EEPROM_COMMAND_READ) {
464 if (!savedata->realisticTiming || !mTimingIsScheduled(savedata->timing, &savedata->dust)) {
465 return 1;
466 } else {
467 return 0;
468 }
469 }
470 --savedata->readBitsRemaining;
471 if (savedata->readBitsRemaining < 64) {
472 int step = 63 - savedata->readBitsRemaining;
473 uint32_t address = (savedata->readAddress + step) >> 3;
474 if (address >= SIZE_CART_EEPROM) {
475 mLOG(GBA_SAVE, GAME_ERROR, "Reading beyond end of EEPROM: %08X", address);
476 return 0xFF;
477 }
478 uint8_t data = savedata->data[address] >> (0x7 - (step & 0x7));
479 if (!savedata->readBitsRemaining) {
480 savedata->command = EEPROM_COMMAND_NULL;
481 }
482 return data & 0x1;
483 }
484 return 0;
485}
486
487void GBASavedataClean(struct GBASavedata* savedata, uint32_t frameCount) {
488 if (!savedata->vf) {
489 return;
490 }
491 if (savedata->dirty & SAVEDATA_DIRT_NEW) {
492 savedata->dirtAge = frameCount;
493 savedata->dirty &= ~SAVEDATA_DIRT_NEW;
494 if (!(savedata->dirty & SAVEDATA_DIRT_SEEN)) {
495 savedata->dirty |= SAVEDATA_DIRT_SEEN;
496 }
497 } else if ((savedata->dirty & SAVEDATA_DIRT_SEEN) && frameCount - savedata->dirtAge > CLEANUP_THRESHOLD) {
498 if (savedata->maskWriteback) {
499 GBASavedataUnmask(savedata);
500 }
501 size_t size = GBASavedataSize(savedata);
502 savedata->dirty = 0;
503 if (savedata->data && savedata->vf->sync(savedata->vf, savedata->data, size)) {
504 mLOG(GBA_SAVE, INFO, "Savedata synced");
505 } else {
506 mLOG(GBA_SAVE, INFO, "Savedata failed to sync!");
507 }
508 }
509}
510
511void GBASavedataSerialize(const struct GBASavedata* savedata, struct GBASerializedState* state) {
512 state->savedata.type = savedata->type;
513 state->savedata.command = savedata->command;
514 GBASerializedSavedataFlags flags = 0;
515 flags = GBASerializedSavedataFlagsSetFlashState(flags, savedata->flashState);
516 flags = GBASerializedSavedataFlagsTestFillFlashBank(flags, savedata->currentBank == &savedata->data[0x10000]);
517
518 if (mTimingIsScheduled(savedata->timing, &savedata->dust)) {
519 STORE_32(savedata->dust.when - mTimingCurrentTime(savedata->timing), 0, &state->savedata.settlingDust);
520 flags = GBASerializedSavedataFlagsFillDustSettling(flags);
521 }
522
523 state->savedata.flags = flags;
524 state->savedata.readBitsRemaining = savedata->readBitsRemaining;
525 STORE_32(savedata->readAddress, 0, &state->savedata.readAddress);
526 STORE_32(savedata->writeAddress, 0, &state->savedata.writeAddress);
527 STORE_16(savedata->settling, 0, &state->savedata.settlingSector);
528
529}
530
531void GBASavedataDeserialize(struct GBASavedata* savedata, const struct GBASerializedState* state) {
532 if (savedata->type != state->savedata.type) {
533 mLOG(GBA_SAVE, DEBUG, "Switching save types");
534 GBASavedataForceType(savedata, state->savedata.type, savedata->realisticTiming);
535 }
536 savedata->command = state->savedata.command;
537 GBASerializedSavedataFlags flags = state->savedata.flags;
538 savedata->flashState = GBASerializedSavedataFlagsGetFlashState(flags);
539 savedata->readBitsRemaining = state->savedata.readBitsRemaining;
540 LOAD_32(savedata->readAddress, 0, &state->savedata.readAddress);
541 LOAD_32(savedata->writeAddress, 0, &state->savedata.writeAddress);
542 LOAD_16(savedata->settling, 0, &state->savedata.settlingSector);
543
544 if (savedata->type == SAVEDATA_FLASH1M) {
545 _flashSwitchBank(savedata, GBASerializedSavedataFlagsGetFlashBank(flags));
546 }
547
548 if (GBASerializedSavedataFlagsIsDustSettling(flags)) {
549 uint32_t when;
550 LOAD_32(when, 0, &state->savedata.settlingDust);
551 mTimingDeschedule(savedata->timing, &savedata->dust);
552 mTimingSchedule(savedata->timing, &savedata->dust, when);
553 }
554}
555
556void _flashSwitchBank(struct GBASavedata* savedata, int bank) {
557 mLOG(GBA_SAVE, DEBUG, "Performing flash bank switch to bank %i", bank);
558 savedata->currentBank = &savedata->data[bank << 16];
559 if (bank > 0 && savedata->type == SAVEDATA_FLASH512) {
560 mLOG(GBA_SAVE, INFO, "Updating flash chip from 512kb to 1Mb");
561 savedata->type = SAVEDATA_FLASH1M;
562 if (savedata->vf && savedata->vf->size(savedata->vf) == SIZE_CART_FLASH512) {
563 savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M);
564 memset(&savedata->data[SIZE_CART_FLASH512], 0xFF, SIZE_CART_FLASH512);
565 }
566 }
567}
568
569void _flashErase(struct GBASavedata* savedata) {
570 mLOG(GBA_SAVE, DEBUG, "Performing flash chip erase");
571 savedata->dirty |= SAVEDATA_DIRT_NEW;
572 size_t size = SIZE_CART_FLASH512;
573 if (savedata->type == SAVEDATA_FLASH1M) {
574 size = SIZE_CART_FLASH1M;
575 }
576 memset(savedata->data, 0xFF, size);
577}
578
579void _flashEraseSector(struct GBASavedata* savedata, uint16_t sectorStart) {
580 mLOG(GBA_SAVE, DEBUG, "Performing flash sector erase at 0x%04x", sectorStart);
581 savedata->dirty |= SAVEDATA_DIRT_NEW;
582 size_t size = 0x1000;
583 if (savedata->type == SAVEDATA_FLASH1M) {
584 mLOG(GBA_SAVE, DEBUG, "Performing unknown sector-size erase at 0x%04x", sectorStart);
585 }
586 savedata->settling = sectorStart >> 12;
587 if (savedata->realisticTiming) {
588 mTimingDeschedule(savedata->timing, &savedata->dust);
589 mTimingSchedule(savedata->timing, &savedata->dust, FLASH_ERASE_CYCLES);
590 }
591 memset(&savedata->currentBank[sectorStart & ~(size - 1)], 0xFF, size);
592}