all repos — mgba @ ba2175f5c513288d7c1feba3dad6a964cb7a22b4

mGBA Game Boy Advance Emulator

src/gba/serialize.c (view raw)

  1/* Copyright (c) 2013-2016 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/serialize.h>
  7
  8#include <mgba/internal/arm/macros.h>
  9#include <mgba/internal/gba/bios.h>
 10#include <mgba/internal/gba/io.h>
 11
 12#include <mgba-util/memory.h>
 13#include <mgba-util/vfs.h>
 14
 15#include <fcntl.h>
 16
 17const uint32_t GBA_SAVESTATE_MAGIC = 0x01000000;
 18const uint32_t GBA_SAVESTATE_VERSION = 0x00000003;
 19
 20mLOG_DEFINE_CATEGORY(GBA_STATE, "GBA Savestate", "gba.serialize");
 21
 22struct GBABundledState {
 23	struct GBASerializedState* state;
 24	struct mStateExtdata* extdata;
 25};
 26
 27void GBASerialize(struct GBA* gba, struct GBASerializedState* state) {
 28	STORE_32(GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, 0, &state->versionMagic);
 29	STORE_32(gba->biosChecksum, 0, &state->biosChecksum);
 30	STORE_32(gba->romCrc32, 0, &state->romCrc32);
 31	STORE_32(gba->timing.masterCycles, 0, &state->masterCycles);
 32	STORE_64LE(gba->timing.globalCycles, 0, &state->globalCycles);
 33
 34	if (gba->memory.rom) {
 35		state->id = ((struct GBACartridge*) gba->memory.rom)->id;
 36		memcpy(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title));
 37	} else {
 38		state->id = 0;
 39		memset(state->title, 0, sizeof(state->title));
 40	}
 41
 42	int i;
 43	for (i = 0; i < 16; ++i) {
 44		STORE_32(gba->cpu->gprs[i], i * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
 45	}
 46	STORE_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
 47	STORE_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
 48	STORE_32(gba->cpu->cycles, 0, &state->cpu.cycles);
 49	STORE_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
 50	for (i = 0; i < 6; ++i) {
 51		int j;
 52		for (j = 0; j < 7; ++j) {
 53			STORE_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
 54		}
 55		STORE_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
 56	}
 57
 58	STORE_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
 59	STORE_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
 60	STORE_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
 61	STORE_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
 62
 63	GBASerializedMiscFlags miscFlags = 0;
 64	miscFlags = GBASerializedMiscFlagsSetHalted(miscFlags, gba->cpu->halted);
 65	miscFlags = GBASerializedMiscFlagsSetPOSTFLG(miscFlags, gba->memory.io[REG_POSTFLG >> 1] & 1);
 66	if (mTimingIsScheduled(&gba->timing, &gba->irqEvent)) {
 67		miscFlags = GBASerializedMiscFlagsFillIrqPending(miscFlags);
 68		STORE_32(gba->irqEvent.when - mTimingCurrentTime(&gba->timing), 0, &state->nextIrq);
 69	}
 70	STORE_32(miscFlags, 0, &state->miscFlags);
 71
 72	GBAMemorySerialize(&gba->memory, state);
 73	GBAIOSerialize(gba, state);
 74	GBAVideoSerialize(&gba->video, state);
 75	GBAAudioSerialize(&gba->audio, state);
 76	GBASavedataSerialize(&gba->memory.savedata, state);
 77}
 78
 79bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) {
 80	bool error = false;
 81	int32_t check;
 82	uint32_t ucheck;
 83	LOAD_32(ucheck, 0, &state->versionMagic);
 84	if (ucheck > GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) {
 85		mLOG(GBA_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
 86		error = true;
 87	} else if (ucheck < GBA_SAVESTATE_MAGIC) {
 88		mLOG(GBA_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
 89		error = true;
 90	} else if (ucheck < GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) {
 91		mLOG(GBA_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
 92	}
 93	LOAD_32(ucheck, 0, &state->biosChecksum);
 94	if (ucheck != gba->biosChecksum) {
 95		mLOG(GBA_STATE, WARN, "Savestate created using a different version of the BIOS: expected %08X, got %08X", gba->biosChecksum, ucheck);
 96		uint32_t pc;
 97		LOAD_32(pc, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
 98		if ((ucheck == GBA_BIOS_CHECKSUM || gba->biosChecksum == GBA_BIOS_CHECKSUM) && pc < SIZE_BIOS && pc >= 0x20) {
 99			error = true;
100		}
101	}
102	if (gba->memory.rom && (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title)))) {
103		mLOG(GBA_STATE, WARN, "Savestate is for a different game");
104		error = true;
105	} else if (!gba->memory.rom && state->id != 0) {
106		mLOG(GBA_STATE, WARN, "Savestate is for a game, but no game loaded");
107		error = true;
108	}
109	LOAD_32(ucheck, 0, &state->romCrc32);
110	if (ucheck != gba->romCrc32) {
111		mLOG(GBA_STATE, WARN, "Savestate is for a different version of the game");
112	}
113	LOAD_32(check, 0, &state->cpu.cycles);
114	if (check < 0) {
115		mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are negative");
116		error = true;
117	}
118	if (check >= (int32_t) GBA_ARM7TDMI_FREQUENCY) {
119		mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are too high");
120		error = true;
121	}
122	LOAD_32(check, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
123	int region = (check >> BASE_OFFSET);
124	if ((region == REGION_CART0 || region == REGION_CART1 || region == REGION_CART2) && ((check - WORD_SIZE_ARM) & SIZE_CART0) >= gba->memory.romSize - WORD_SIZE_ARM) {
125		mLOG(GBA_STATE, WARN, "Savestate created using a differently sized version of the ROM");
126		error = true;
127	}
128	if (error) {
129		return false;
130	}
131	mTimingClear(&gba->timing);
132	LOAD_32(gba->timing.masterCycles, 0, &state->masterCycles);
133	LOAD_64LE(gba->timing.globalCycles, 0, &state->globalCycles);
134
135	size_t i;
136	for (i = 0; i < 16; ++i) {
137		LOAD_32(gba->cpu->gprs[i], i * sizeof(gba->cpu->gprs[0]), state->cpu.gprs);
138	}
139	LOAD_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
140	LOAD_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
141	LOAD_32(gba->cpu->cycles, 0, &state->cpu.cycles);
142	LOAD_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
143	for (i = 0; i < 6; ++i) {
144		int j;
145		for (j = 0; j < 7; ++j) {
146			LOAD_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
147		}
148		LOAD_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
149	}
150	gba->cpu->privilegeMode = gba->cpu->cpsr.priv;
151	gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
152	if (state->biosPrefetch) {
153		LOAD_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
154	}
155	LOAD_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
156	if (gba->cpu->cpsr.t) {
157		gba->cpu->executionMode = MODE_THUMB;
158		if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
159			LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
160			LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
161			gba->cpu->prefetch[0] &= 0xFFFF;
162			gba->cpu->prefetch[1] &= 0xFFFF;
163		} else {
164			// Maintain backwards compat
165			LOAD_16(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_THUMB) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
166			LOAD_16(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
167		}
168	} else {
169		gba->cpu->executionMode = MODE_ARM;
170		if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
171			LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
172			LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
173		} else {
174			// Maintain backwards compat
175			LOAD_32(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
176			LOAD_32(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
177		}
178	}
179	GBASerializedMiscFlags miscFlags = 0;
180	LOAD_32(miscFlags, 0, &state->miscFlags);
181	gba->cpu->halted = GBASerializedMiscFlagsGetHalted(miscFlags);
182	gba->memory.io[REG_POSTFLG >> 1] = GBASerializedMiscFlagsGetPOSTFLG(miscFlags);
183	if (GBASerializedMiscFlagsIsIrqPending(miscFlags)) {
184		int32_t when;
185		LOAD_32(when, 0, &state->nextIrq);
186		mTimingSchedule(&gba->timing, &gba->irqEvent, when);		
187	}
188
189	GBAVideoDeserialize(&gba->video, state);
190	GBAMemoryDeserialize(&gba->memory, state);
191	GBAIODeserialize(gba, state);
192	GBAAudioDeserialize(&gba->audio, state);
193	GBASavedataDeserialize(&gba->memory.savedata, state);
194
195	gba->timing.reroot = gba->timing.root;
196	gba->timing.root = NULL;
197
198	return true;
199}