all repos — mgba @ 2bb16fd0a8cc7536085bc9172c515a613c2333e1

mGBA Game Boy Advance Emulator

GBA: Better savestate error reporting
Jeffrey Pfau jeffrey@endrift.com
Sat, 20 Jun 2015 00:49:50 -0700
commit

2bb16fd0a8cc7536085bc9172c515a613c2333e1

parent

83dfbe61236b128bfdd09f29a34c486fe620e861

3 files changed, 32 insertions(+), 23 deletions(-)

jump to
M src/gba/serialize.csrc/gba/serialize.c

@@ -61,67 +61,71 @@ gba->rr->stateSaved(gba->rr, state);

} } -void GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) { +bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) { + bool error = false; if (state->versionMagic != GBA_SAVESTATE_MAGIC) { GBALog(gba, GBA_LOG_WARN, "Invalid or too new savestate"); - return; + error = true; } if (state->biosChecksum != gba->biosChecksum) { GBALog(gba, GBA_LOG_WARN, "Savestate created using a different version of the BIOS"); if (state->cpu.gprs[ARM_PC] < SIZE_BIOS && state->cpu.gprs[ARM_PC] >= 0x20) { - return; + error = true; } } if (gba->memory.rom && (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title)))) { GBALog(gba, GBA_LOG_WARN, "Savestate is for a different game"); - return; + error = true; } else if (!gba->memory.rom && state->id != 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is for a game, but no game loaded"); - return; + error = true; } if (state->romCrc32 != gba->romCrc32) { GBALog(gba, GBA_LOG_WARN, "Savestate is for a different version of the game"); } if (state->cpu.cycles < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: CPU cycles are negative"); - return; + error = true; } if (state->video.eventDiff < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: video eventDiff is negative"); - return; + error = true; } if (state->video.nextHblank - state->video.eventDiff < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: nextHblank is negative"); - return; + error = true; } if (state->timers[0].overflowInterval < 0 || state->timers[1].overflowInterval < 0 || state->timers[2].overflowInterval < 0 || state->timers[3].overflowInterval < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: overflowInterval is negative"); - return; + error = true; } if (state->audio.eventDiff < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: audio eventDiff is negative"); - return; + error = true; } if (state->audio.ch1.envelopeNextStep < 0 || state->audio.ch1.waveNextStep < 0 || state->audio.ch1.sweepNextStep < 0 || state->audio.ch1.nextEvent < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: audio channel 1 register is negative"); - return; + error = true; } if (state->audio.ch2.envelopeNextStep < 0 || state->audio.ch2.waveNextStep < 0 || state->audio.ch2.nextEvent < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: audio channel 2 register is negative"); - return; + error = true; } if (state->audio.ch3.nextEvent < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: audio channel 3 register is negative"); - return; + error = true; } if (state->audio.ch4.envelopeNextStep < 0 || state->audio.ch4.nextEvent < 0) { GBALog(gba, GBA_LOG_WARN, "Savestate is corrupted: audio channel 4 register is negative"); - return; + error = true; } int region = (state->cpu.gprs[ARM_PC] >> BASE_OFFSET); if ((region == REGION_CART0 || region == REGION_CART1 || region == REGION_CART2) && ((state->cpu.gprs[ARM_PC] - WORD_SIZE_ARM) & SIZE_CART0) >= gba->memory.romSize - WORD_SIZE_ARM) { GBALog(gba, GBA_LOG_WARN, "Savestate created using a differently sized version of the ROM"); - return; + error = true; + } + if (error) { + return false; } memcpy(gba->cpu->gprs, state->cpu.gprs, sizeof(gba->cpu->gprs)); gba->cpu->cpsr = state->cpu.cpsr;

@@ -166,6 +170,7 @@

if (gba->rr) { gba->rr->stateLoaded(gba->rr, state); } + return true; } struct VFile* GBAGetState(struct GBA* gba, struct VDir* dir, int slot, bool write) {

@@ -218,8 +223,7 @@ }

struct GBASerializedState state; uLongf len = sizeof(state); uncompress((Bytef*) &state, &len, chunk->data, chunk->size); - GBADeserialize(png_get_user_chunk_ptr(png), &state); - return 1; + return GBADeserialize(png_get_user_chunk_ptr(png), &state); } static bool _loadPNGState(struct GBA* gba, struct VFile* vf) {

@@ -254,6 +258,8 @@ bool success = GBASaveStateNamed(threadContext->gba, vf, screenshot);

vf->close(vf); if (success) { GBALog(threadContext->gba, GBA_LOG_STATUS, "State %i saved", slot); + } else { + GBALog(threadContext->gba, GBA_LOG_STATUS, "State %i failed to save", slot); } return success; }

@@ -268,6 +274,8 @@ bool success = GBALoadStateNamed(threadContext->gba, vf);

vf->close(vf); if (success) { GBALog(threadContext->gba, GBA_LOG_STATUS, "State %i loaded", slot); + } else { + GBALog(threadContext->gba, GBA_LOG_STATUS, "State %i failed to load", slot); } return success; }

@@ -304,9 +312,9 @@ struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_READ);

if (!state) { return false; } - GBADeserialize(gba, state); + bool success = GBADeserialize(gba, state); vf->unmap(vf, state, sizeof(struct GBASerializedState)); - return true; + return success; } struct GBASerializedState* GBAAllocateState(void) {
M src/gba/serialize.hsrc/gba/serialize.h

@@ -321,7 +321,7 @@ struct VDir;

struct GBAThread; void GBASerialize(struct GBA* gba, struct GBASerializedState* state); -void GBADeserialize(struct GBA* gba, const struct GBASerializedState* state); +bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state); bool GBASaveState(struct GBAThread* thread, struct VDir* dir, int slot, bool screenshot); bool GBALoadState(struct GBAThread* thread, struct VDir* dir, int slot);
M src/platform/qt/GameController.cppsrc/platform/qt/GameController.cpp

@@ -573,9 +573,10 @@ m_stateSlot = slot;

} GBARunOnThread(&m_threadContext, [](GBAThread* context) { GameController* controller = static_cast<GameController*>(context->userData); - GBALoadState(context, context->stateDir, controller->m_stateSlot); - controller->stateLoaded(context); - controller->frameAvailable(controller->m_drawContext); + if (GBALoadState(context, context->stateDir, controller->m_stateSlot)) { + controller->stateLoaded(context); + controller->frameAvailable(controller->m_drawContext); + } }); }