all repos — mgba @ 3660246a83a60d756dfabd3264520531843d5edb

mGBA Game Boy Advance Emulator

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->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);
 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);
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(const 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 (savedata->vf && savedata->vf->size(savedata->vf) == SIZE_CART_EEPROM512) ? SIZE_CART_EEPROM512 : 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) {
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);
207		break;
208	case SAVEDATA_EEPROM:
209		GBASavedataInitEEPROM(savedata);
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) {
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	if (end < SIZE_CART_FLASH512) {
248		memset(&savedata->data[end], 0xFF, flashSize - end);
249	}
250}
251
252void GBASavedataInitEEPROM(struct GBASavedata* savedata) {
253	if (savedata->type == SAVEDATA_AUTODETECT) {
254		savedata->type = SAVEDATA_EEPROM;
255	} else {
256		mLOG(GBA_SAVE, WARN, "Can't re-initialize savedata");
257		return;
258	}
259	int32_t eepromSize = SIZE_CART_EEPROM512;
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_EEPROM512) {
267			savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM512);
268		} else if (end > SIZE_CART_EEPROM512) {
269			eepromSize = SIZE_CART_EEPROM;
270		}
271		savedata->data = savedata->vf->map(savedata->vf, eepromSize, savedata->mapMode);
272	}
273	if (end < SIZE_CART_EEPROM512) {
274		memset(&savedata->data[end], 0xFF, SIZE_CART_EEPROM512 - end);
275	}
276}
277
278void GBASavedataInitSRAM(struct GBASavedata* savedata) {
279	if (savedata->type == SAVEDATA_AUTODETECT) {
280		savedata->type = SAVEDATA_SRAM;
281	} else {
282		mLOG(GBA_SAVE, WARN, "Can't re-initialize savedata");
283		return;
284	}
285	off_t end;
286	if (!savedata->vf) {
287		end = 0;
288		savedata->data = anonymousMemoryMap(SIZE_CART_SRAM);
289	} else {
290		end = savedata->vf->size(savedata->vf);
291		if (end < SIZE_CART_SRAM) {
292			savedata->vf->truncate(savedata->vf, SIZE_CART_SRAM);
293		}
294		savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_SRAM, savedata->mapMode);
295	}
296
297	if (end < SIZE_CART_SRAM) {
298		memset(&savedata->data[end], 0xFF, SIZE_CART_SRAM - end);
299	}
300}
301
302uint8_t GBASavedataReadFlash(struct GBASavedata* savedata, uint16_t address) {
303	if (savedata->command == FLASH_COMMAND_ID) {
304		if (savedata->type == SAVEDATA_FLASH512) {
305			if (address < 2) {
306				return FLASH_MFG_PANASONIC >> (address * 8);
307			}
308		} else if (savedata->type == SAVEDATA_FLASH1M) {
309			if (address < 2) {
310				return FLASH_MFG_SANYO >> (address * 8);
311			}
312		}
313	}
314	if (mTimingIsScheduled(savedata->timing, &savedata->dust) && (address >> 12) == savedata->settling) {
315		return 0x5F;
316	}
317	return savedata->currentBank[address];
318}
319
320void GBASavedataWriteFlash(struct GBASavedata* savedata, uint16_t address, uint8_t value) {
321	switch (savedata->flashState) {
322	case FLASH_STATE_RAW:
323		switch (savedata->command) {
324		case FLASH_COMMAND_PROGRAM:
325			savedata->dirty |= SAVEDATA_DIRT_NEW;
326			savedata->currentBank[address] = value;
327			savedata->command = FLASH_COMMAND_NONE;
328			mTimingDeschedule(savedata->timing, &savedata->dust);
329			mTimingSchedule(savedata->timing, &savedata->dust, FLASH_PROGRAM_CYCLES);
330			break;
331		case FLASH_COMMAND_SWITCH_BANK:
332			if (address == 0 && value < 2) {
333				_flashSwitchBank(savedata, value);
334			} else {
335				mLOG(GBA_SAVE, GAME_ERROR, "Bad flash bank switch");
336				savedata->command = FLASH_COMMAND_NONE;
337			}
338			savedata->command = FLASH_COMMAND_NONE;
339			break;
340		default:
341			if (address == FLASH_BASE_HI && value == FLASH_COMMAND_START) {
342				savedata->flashState = FLASH_STATE_START;
343			} else {
344				mLOG(GBA_SAVE, GAME_ERROR, "Bad flash write: %#04x = %#02x", address, value);
345			}
346			break;
347		}
348		break;
349	case FLASH_STATE_START:
350		if (address == FLASH_BASE_LO && value == FLASH_COMMAND_CONTINUE) {
351			savedata->flashState = FLASH_STATE_CONTINUE;
352		} else {
353			mLOG(GBA_SAVE, GAME_ERROR, "Bad flash write: %#04x = %#02x", address, value);
354			savedata->flashState = FLASH_STATE_RAW;
355		}
356		break;
357	case FLASH_STATE_CONTINUE:
358		savedata->flashState = FLASH_STATE_RAW;
359		if (address == FLASH_BASE_HI) {
360			switch (savedata->command) {
361			case FLASH_COMMAND_NONE:
362				switch (value) {
363				case FLASH_COMMAND_ERASE:
364				case FLASH_COMMAND_ID:
365				case FLASH_COMMAND_PROGRAM:
366				case FLASH_COMMAND_SWITCH_BANK:
367					savedata->command = value;
368					break;
369				default:
370					mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash operation: %#02x", value);
371					break;
372				}
373				break;
374			case FLASH_COMMAND_ERASE:
375				switch (value) {
376				case FLASH_COMMAND_ERASE_CHIP:
377					_flashErase(savedata);
378					break;
379				default:
380					mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash erase operation: %#02x", value);
381					break;
382				}
383				savedata->command = FLASH_COMMAND_NONE;
384				break;
385			case FLASH_COMMAND_ID:
386				if (value == FLASH_COMMAND_TERMINATE) {
387					savedata->command = FLASH_COMMAND_NONE;
388				}
389				break;
390			default:
391				mLOG(GBA_SAVE, ERROR, "Flash entered bad state: %#02x", savedata->command);
392				savedata->command = FLASH_COMMAND_NONE;
393				break;
394			}
395		} else if (savedata->command == FLASH_COMMAND_ERASE) {
396			if (value == FLASH_COMMAND_ERASE_SECTOR) {
397				_flashEraseSector(savedata, address);
398				savedata->command = FLASH_COMMAND_NONE;
399			} else {
400				mLOG(GBA_SAVE, GAME_ERROR, "Unsupported flash erase operation: %#02x", value);
401			}
402		}
403		break;
404	}
405}
406
407static void _ensureEeprom(struct GBASavedata* savedata, uint32_t size) {
408	if (size < SIZE_CART_EEPROM512) {
409		return;
410	}
411	if (!savedata->vf || savedata->vf->size(savedata->vf) > SIZE_CART_EEPROM512) {
412		return;
413	}
414	savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_EEPROM512);
415	savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM);
416	savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode);
417	memset(&savedata->data[SIZE_CART_EEPROM512], 0xFF, SIZE_CART_EEPROM - SIZE_CART_EEPROM512);
418}
419
420void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize) {
421	switch (savedata->command) {
422	// Read header
423	case EEPROM_COMMAND_NULL:
424	default:
425		savedata->command = value & 0x1;
426		break;
427	case EEPROM_COMMAND_PENDING:
428		savedata->command <<= 1;
429		savedata->command |= value & 0x1;
430		if (savedata->command == EEPROM_COMMAND_WRITE) {
431			savedata->writeAddress = 0;
432		} else {
433			savedata->readAddress = 0;
434		}
435		break;
436	// Do commands
437	case EEPROM_COMMAND_WRITE:
438		// Write
439		if (writeSize > 65) {
440			savedata->writeAddress <<= 1;
441			savedata->writeAddress |= (value & 0x1) << 6;
442		} else if (writeSize == 1) {
443			savedata->command = EEPROM_COMMAND_NULL;
444		} else if ((savedata->writeAddress >> 3) < SIZE_CART_EEPROM) {
445			_ensureEeprom(savedata, savedata->writeAddress >> 3);
446			uint8_t current = savedata->data[savedata->writeAddress >> 3];
447			current &= ~(1 << (0x7 - (savedata->writeAddress & 0x7)));
448			current |= (value & 0x1) << (0x7 - (savedata->writeAddress & 0x7));
449			savedata->dirty |= SAVEDATA_DIRT_NEW;
450			savedata->data[savedata->writeAddress >> 3] = current;
451			mTimingDeschedule(savedata->timing, &savedata->dust);
452			mTimingSchedule(savedata->timing, &savedata->dust, EEPROM_SETTLE_CYCLES);
453			++savedata->writeAddress;
454		} else {
455			mLOG(GBA_SAVE, GAME_ERROR, "Writing beyond end of EEPROM: %08X", (savedata->writeAddress >> 3));
456		}
457		break;
458	case EEPROM_COMMAND_READ_PENDING:
459		// Read
460		if (writeSize > 1) {
461			savedata->readAddress <<= 1;
462			if (value & 0x1) {
463				savedata->readAddress |= 0x40;
464			}
465		} else {
466			savedata->readBitsRemaining = 68;
467			savedata->command = EEPROM_COMMAND_READ;
468		}
469		break;
470	}
471}
472
473uint16_t GBASavedataReadEEPROM(struct GBASavedata* savedata) {
474	if (savedata->command != EEPROM_COMMAND_READ) {
475		if (!mTimingIsScheduled(savedata->timing, &savedata->dust)) {
476			return 1;
477		} else {
478			return 0;
479		}
480	}
481	--savedata->readBitsRemaining;
482	if (savedata->readBitsRemaining < 64) {
483		int step = 63 - savedata->readBitsRemaining;
484		uint32_t address = (savedata->readAddress + step) >> 3;
485		_ensureEeprom(savedata, address);
486		if (address >= SIZE_CART_EEPROM) {
487			mLOG(GBA_SAVE, GAME_ERROR, "Reading beyond end of EEPROM: %08X", address);
488			return 0xFF;
489		}
490		uint8_t data = savedata->data[address] >> (0x7 - (step & 0x7));
491		if (!savedata->readBitsRemaining) {
492			savedata->command = EEPROM_COMMAND_NULL;
493		}
494		return data & 0x1;
495	}
496	return 0;
497}
498
499void GBASavedataClean(struct GBASavedata* savedata, uint32_t frameCount) {
500	if (!savedata->vf) {
501		return;
502	}
503	if (savedata->dirty & SAVEDATA_DIRT_NEW) {
504		savedata->dirtAge = frameCount;
505		savedata->dirty &= ~SAVEDATA_DIRT_NEW;
506		if (!(savedata->dirty & SAVEDATA_DIRT_SEEN)) {
507			savedata->dirty |= SAVEDATA_DIRT_SEEN;
508		}
509	} else if ((savedata->dirty & SAVEDATA_DIRT_SEEN) && frameCount - savedata->dirtAge > CLEANUP_THRESHOLD) {
510		if (savedata->maskWriteback) {
511			GBASavedataUnmask(savedata);
512		}
513		if (savedata->mapMode & MAP_WRITE) {
514			size_t size = GBASavedataSize(savedata);
515			savedata->dirty = 0;
516			if (savedata->data && savedata->vf->sync(savedata->vf, savedata->data, size)) {
517				mLOG(GBA_SAVE, INFO, "Savedata synced");
518			} else {
519				mLOG(GBA_SAVE, INFO, "Savedata failed to sync!");
520			}
521		}
522	}
523}
524
525void GBASavedataSerialize(const struct GBASavedata* savedata, struct GBASerializedState* state) {
526	state->savedata.type = savedata->type;
527	state->savedata.command = savedata->command;
528	GBASerializedSavedataFlags flags = 0;
529	flags = GBASerializedSavedataFlagsSetFlashState(flags, savedata->flashState);
530	flags = GBASerializedSavedataFlagsTestFillFlashBank(flags, savedata->currentBank == &savedata->data[0x10000]);
531
532	if (mTimingIsScheduled(savedata->timing, &savedata->dust)) {
533		STORE_32(savedata->dust.when - mTimingCurrentTime(savedata->timing), 0, &state->savedata.settlingDust);
534		flags = GBASerializedSavedataFlagsFillDustSettling(flags);
535	}
536
537	state->savedata.flags = flags;
538	state->savedata.readBitsRemaining = savedata->readBitsRemaining;
539	STORE_32(savedata->readAddress, 0, &state->savedata.readAddress);
540	STORE_32(savedata->writeAddress, 0, &state->savedata.writeAddress);
541	STORE_16(savedata->settling, 0, &state->savedata.settlingSector);
542
543}
544
545void GBASavedataDeserialize(struct GBASavedata* savedata, const struct GBASerializedState* state) {
546	if (savedata->type != state->savedata.type) {
547		mLOG(GBA_SAVE, DEBUG, "Switching save types");
548		GBASavedataForceType(savedata, state->savedata.type);
549	}
550	savedata->command = state->savedata.command;
551	GBASerializedSavedataFlags flags = state->savedata.flags;
552	savedata->flashState = GBASerializedSavedataFlagsGetFlashState(flags);
553	savedata->readBitsRemaining = state->savedata.readBitsRemaining;
554	LOAD_32(savedata->readAddress, 0, &state->savedata.readAddress);
555	LOAD_32(savedata->writeAddress, 0, &state->savedata.writeAddress);
556	LOAD_16(savedata->settling, 0, &state->savedata.settlingSector);
557
558	if (savedata->type == SAVEDATA_FLASH1M) {
559		_flashSwitchBank(savedata, GBASerializedSavedataFlagsGetFlashBank(flags));
560	}
561
562	if (GBASerializedSavedataFlagsIsDustSettling(flags)) {
563		uint32_t when;
564		LOAD_32(when, 0, &state->savedata.settlingDust);
565		mTimingSchedule(savedata->timing, &savedata->dust, when);
566	}
567}
568
569void _flashSwitchBank(struct GBASavedata* savedata, int bank) {
570	mLOG(GBA_SAVE, DEBUG, "Performing flash bank switch to bank %i", bank);
571	savedata->currentBank = &savedata->data[bank << 16];
572	if (bank > 0 && savedata->type == SAVEDATA_FLASH512) {
573		mLOG(GBA_SAVE, INFO, "Updating flash chip from 512kb to 1Mb");
574		savedata->type = SAVEDATA_FLASH1M;
575		if (savedata->vf) {
576			savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_FLASH512);
577			if (savedata->vf->size(savedata->vf) == SIZE_CART_FLASH512) {
578				savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M);
579				savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE);
580				memset(&savedata->data[SIZE_CART_FLASH512], 0xFF, SIZE_CART_FLASH512);
581			} else {
582				savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE);
583			}
584		}
585	}
586}
587
588void _flashErase(struct GBASavedata* savedata) {
589	mLOG(GBA_SAVE, DEBUG, "Performing flash chip erase");
590	savedata->dirty |= SAVEDATA_DIRT_NEW;
591	size_t size = SIZE_CART_FLASH512;
592	if (savedata->type == SAVEDATA_FLASH1M) {
593		size = SIZE_CART_FLASH1M;
594	}
595	memset(savedata->data, 0xFF, size);
596}
597
598void _flashEraseSector(struct GBASavedata* savedata, uint16_t sectorStart) {
599	mLOG(GBA_SAVE, DEBUG, "Performing flash sector erase at 0x%04x", sectorStart);
600	savedata->dirty |= SAVEDATA_DIRT_NEW;
601	size_t size = 0x1000;
602	if (savedata->type == SAVEDATA_FLASH1M) {
603		mLOG(GBA_SAVE, DEBUG, "Performing unknown sector-size erase at 0x%04x", sectorStart);
604	}
605	savedata->settling = sectorStart >> 12;
606	mTimingDeschedule(savedata->timing, &savedata->dust);
607	mTimingSchedule(savedata->timing, &savedata->dust, FLASH_ERASE_CYCLES);
608	memset(&savedata->currentBank[sectorStart & ~(size - 1)], 0xFF, size);
609}