GBA Savedata: Fix various filesize edge cases (fixes #1221)

This commit is contained in:
Vicki Pfau 2018-11-09 20:31:30 -08:00
parent 32279c37b1
commit 9262475cb4
2 changed files with 10 additions and 11 deletions

View File

@ -259,9 +259,6 @@ void GBASavedataInitFlash(struct GBASavedata* savedata) {
end = savedata->vf->size(savedata->vf);
if (end < flashSize) {
savedata->vf->truncate(savedata->vf, flashSize);
} else if (end >= SIZE_CART_FLASH1M) {
flashSize = SIZE_CART_FLASH1M;
savedata->type = SAVEDATA_FLASH1M;
}
savedata->data = savedata->vf->map(savedata->vf, flashSize, savedata->mapMode);
}
@ -291,9 +288,6 @@ void GBASavedataInitEEPROM(struct GBASavedata* savedata) {
end = savedata->vf->size(savedata->vf);
if (end < eepromSize) {
savedata->vf->truncate(savedata->vf, eepromSize);
} else if (end >= SIZE_CART_EEPROM) {
eepromSize = SIZE_CART_EEPROM;
savedata->type = SAVEDATA_EEPROM;
}
savedata->data = savedata->vf->map(savedata->vf, eepromSize, savedata->mapMode);
}
@ -439,13 +433,17 @@ static void _ensureEeprom(struct GBASavedata* savedata, uint32_t size) {
return;
}
savedata->type = SAVEDATA_EEPROM;
if (!savedata->vf || savedata->vf->size(savedata->vf) > SIZE_CART_EEPROM512) {
if (!savedata->vf) {
return;
}
savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_EEPROM512);
savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM);
savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode);
memset(&savedata->data[SIZE_CART_EEPROM512], 0xFF, SIZE_CART_EEPROM - SIZE_CART_EEPROM512);
if (savedata->vf->size(savedata->vf) < SIZE_CART_EEPROM) {
savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM);
savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode);
memset(&savedata->data[SIZE_CART_EEPROM512], 0xFF, SIZE_CART_EEPROM - SIZE_CART_EEPROM512);
} else {
savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, savedata->mapMode);
}
}
void GBASavedataWriteEEPROM(struct GBASavedata* savedata, uint16_t value, uint32_t writeSize) {
@ -605,7 +603,7 @@ void _flashSwitchBank(struct GBASavedata* savedata, int bank) {
savedata->type = SAVEDATA_FLASH1M;
if (savedata->vf) {
savedata->vf->unmap(savedata->vf, savedata->data, SIZE_CART_FLASH512);
if (savedata->vf->size(savedata->vf) == SIZE_CART_FLASH512) {
if (savedata->vf->size(savedata->vf) < SIZE_CART_FLASH1M) {
savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M);
savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE);
memset(&savedata->data[SIZE_CART_FLASH512], 0xFF, SIZE_CART_FLASH512);

View File

@ -489,6 +489,7 @@ bool retro_load_game(const struct retro_game_info* game) {
core->setPeripheral(core, mPERIPH_RUMBLE, &rumble);
savedata = anonymousMemoryMap(SIZE_CART_FLASH1M);
memset(savedata, 0xFF, SIZE_CART_FLASH1M);
struct VFile* save = VFileFromMemory(savedata, SIZE_CART_FLASH1M);
_reloadSettings();