all repos — mgba @ 346f5dc0b53afa8ea1e11d8c94a0090b5d49abc9

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 = 0x00000004;
 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	miscFlags = GBASerializedMiscFlagsSetBlocked(miscFlags, gba->cpuBlocked);
 71	STORE_32(miscFlags, 0, &state->miscFlags);
 72	STORE_32(gba->biosStall, 0, &state->biosStall);
 73
 74	GBAMemorySerialize(&gba->memory, state);
 75	GBAIOSerialize(gba, state);
 76	GBAVideoSerialize(&gba->video, state);
 77	GBAAudioSerialize(&gba->audio, state);
 78	GBASavedataSerialize(&gba->memory.savedata, state);
 79
 80	if (gba->memory.matrix.size) {
 81		GBAMatrixSerialize(gba, state);
 82	}
 83}
 84
 85bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) {
 86	bool error = false;
 87	int32_t check;
 88	uint32_t ucheck;
 89	LOAD_32(ucheck, 0, &state->versionMagic);
 90	if (ucheck > GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) {
 91		mLOG(GBA_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
 92		error = true;
 93	} else if (ucheck < GBA_SAVESTATE_MAGIC) {
 94		mLOG(GBA_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
 95		error = true;
 96	} else if (ucheck < GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) {
 97		mLOG(GBA_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
 98	}
 99	LOAD_32(ucheck, 0, &state->biosChecksum);
100	if (ucheck != gba->biosChecksum) {
101		mLOG(GBA_STATE, WARN, "Savestate created using a different version of the BIOS: expected %08X, got %08X", gba->biosChecksum, ucheck);
102		uint32_t pc;
103		LOAD_32(pc, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
104		if ((ucheck == GBA_BIOS_CHECKSUM || gba->biosChecksum == GBA_BIOS_CHECKSUM) && pc < SIZE_BIOS && pc >= 0x20) {
105			error = true;
106		}
107	}
108	if (gba->memory.rom && (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title)))) {
109		mLOG(GBA_STATE, WARN, "Savestate is for a different game");
110		error = true;
111	} else if (!gba->memory.rom && state->id != 0) {
112		mLOG(GBA_STATE, WARN, "Savestate is for a game, but no game loaded");
113		error = true;
114	}
115	LOAD_32(ucheck, 0, &state->romCrc32);
116	if (ucheck != gba->romCrc32) {
117		mLOG(GBA_STATE, WARN, "Savestate is for a different version of the game");
118	}
119	LOAD_32(check, 0, &state->cpu.cycles);
120	if (check < 0) {
121		mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are negative");
122		error = true;
123	}
124	if (check >= (int32_t) GBA_ARM7TDMI_FREQUENCY) {
125		mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are too high");
126		error = true;
127	}
128	LOAD_32(check, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
129	int region = (check >> BASE_OFFSET);
130	if ((region == REGION_CART0 || region == REGION_CART1 || region == REGION_CART2) && ((check - WORD_SIZE_ARM) & SIZE_CART0) >= gba->memory.romSize - WORD_SIZE_ARM) {
131		mLOG(GBA_STATE, WARN, "Savestate created using a differently sized version of the ROM");
132		error = true;
133	}
134	if (error) {
135		return false;
136	}
137	mTimingClear(&gba->timing);
138	LOAD_32(gba->timing.masterCycles, 0, &state->masterCycles);
139	LOAD_64LE(gba->timing.globalCycles, 0, &state->globalCycles);
140
141	size_t i;
142	for (i = 0; i < 16; ++i) {
143		LOAD_32(gba->cpu->gprs[i], i * sizeof(gba->cpu->gprs[0]), state->cpu.gprs);
144	}
145	LOAD_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
146	LOAD_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
147	LOAD_32(gba->cpu->cycles, 0, &state->cpu.cycles);
148	LOAD_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
149	for (i = 0; i < 6; ++i) {
150		int j;
151		for (j = 0; j < 7; ++j) {
152			LOAD_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
153		}
154		LOAD_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
155	}
156	gba->cpu->privilegeMode = gba->cpu->cpsr.priv;
157	uint32_t pcMask = (gba->cpu->cpsr.t ? WORD_SIZE_THUMB : WORD_SIZE_ARM) - 1;
158	if (gba->cpu->gprs[ARM_PC] & pcMask) {
159		mLOG(GBA_STATE, WARN, "Savestate has unaligned PC and is probably corrupted");
160		gba->cpu->gprs[ARM_PC] &= ~pcMask;
161	}
162	gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
163	if (state->biosPrefetch) {
164		LOAD_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
165	}
166	LOAD_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
167	if (gba->cpu->cpsr.t) {
168		gba->cpu->executionMode = MODE_THUMB;
169		if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
170			LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
171			LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
172			gba->cpu->prefetch[0] &= 0xFFFF;
173			gba->cpu->prefetch[1] &= 0xFFFF;
174		} else {
175			// Maintain backwards compat
176			LOAD_16(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_THUMB) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
177			LOAD_16(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
178		}
179	} else {
180		gba->cpu->executionMode = MODE_ARM;
181		if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
182			LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
183			LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
184		} else {
185			// Maintain backwards compat
186			LOAD_32(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
187			LOAD_32(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
188		}
189	}
190	GBASerializedMiscFlags miscFlags = 0;
191	LOAD_32(miscFlags, 0, &state->miscFlags);
192	gba->cpu->halted = GBASerializedMiscFlagsGetHalted(miscFlags);
193	gba->memory.io[REG_POSTFLG >> 1] = GBASerializedMiscFlagsGetPOSTFLG(miscFlags);
194	if (GBASerializedMiscFlagsIsIrqPending(miscFlags)) {
195		int32_t when;
196		LOAD_32(when, 0, &state->nextIrq);
197		mTimingSchedule(&gba->timing, &gba->irqEvent, when);		
198	}
199	gba->cpuBlocked = GBASerializedMiscFlagsGetBlocked(miscFlags);
200	LOAD_32(gba->biosStall, 0, &state->biosStall);
201
202	GBAVideoDeserialize(&gba->video, state);
203	GBAMemoryDeserialize(&gba->memory, state);
204	GBAIODeserialize(gba, state);
205	GBAAudioDeserialize(&gba->audio, state);
206	GBASavedataDeserialize(&gba->memory.savedata, state);
207
208	if (gba->memory.matrix.size) {
209		GBAMatrixDeserialize(gba, state);
210	}
211
212	gba->timing.reroot = gba->timing.root;
213	gba->timing.root = NULL;
214
215	return true;
216}