all repos — mgba @ a56fd6016ddd862bcfd22f4378019176d84a80bf

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/io.h>
 10#include <mgba/internal/gba/rr/rr.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 = 0x00000002;
 19
 20mLOG_DEFINE_CATEGORY(GBA_STATE, "GBA Savestate");
 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
 33	if (gba->memory.rom) {
 34		state->id = ((struct GBACartridge*) gba->memory.rom)->id;
 35		memcpy(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title));
 36	} else {
 37		state->id = 0;
 38		memset(state->title, 0, sizeof(state->title));
 39	}
 40
 41	int i;
 42	for (i = 0; i < 16; ++i) {
 43		STORE_32(gba->cpu->gprs[i], i * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
 44	}
 45	STORE_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
 46	STORE_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
 47	STORE_32(gba->cpu->cycles, 0, &state->cpu.cycles);
 48	STORE_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
 49	for (i = 0; i < 6; ++i) {
 50		int j;
 51		for (j = 0; j < 7; ++j) {
 52			STORE_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
 53		}
 54		STORE_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
 55	}
 56
 57	STORE_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
 58	STORE_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
 59	STORE_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
 60	STORE_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
 61
 62	GBASerializedMiscFlags miscFlags = 0;
 63	miscFlags = GBASerializedMiscFlagsSetHalted(miscFlags, gba->cpu->halted);
 64	STORE_32(miscFlags, 0, &state->miscFlags);
 65
 66	GBAMemorySerialize(&gba->memory, state);
 67	GBAIOSerialize(gba, state);
 68	GBAVideoSerialize(&gba->video, state);
 69	GBAAudioSerialize(&gba->audio, state);
 70	GBASavedataSerialize(&gba->memory.savedata, state);
 71
 72
 73#ifndef _MSC_VER
 74	struct timeval tv;
 75	if (!gettimeofday(&tv, 0)) {
 76		uint64_t usec = tv.tv_usec;
 77		usec += tv.tv_sec * 1000000LL;
 78		STORE_64(usec, 0, &state->creationUsec);
 79	}
 80#else
 81	struct timespec ts;
 82	if (timespec_get(&ts, TIME_UTC)) {
 83		uint64_t usec = ts.tv_nsec / 1000;
 84		usec += ts.tv_sec * 1000000LL;
 85		STORE_64(usec, 0, &state->creationUsec);
 86	}
 87#endif
 88	else {
 89		state->creationUsec = 0;
 90	}
 91	state->associatedStreamId = 0;
 92	if (gba->rr) {
 93		gba->rr->stateSaved(gba->rr, state);
 94	}
 95}
 96
 97bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) {
 98	bool error = false;
 99	int32_t check;
100	uint32_t ucheck;
101	LOAD_32(ucheck, 0, &state->versionMagic);
102	if (ucheck > GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) {
103		mLOG(GBA_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
104		error = true;
105	} else if (ucheck < GBA_SAVESTATE_MAGIC) {
106		mLOG(GBA_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
107		error = true;
108	} else if (ucheck < GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) {
109		mLOG(GBA_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
110	}
111	LOAD_32(ucheck, 0, &state->biosChecksum);
112	if (ucheck != gba->biosChecksum) {
113		mLOG(GBA_STATE, WARN, "Savestate created using a different version of the BIOS: expected %08X, got %08X", gba->biosChecksum, ucheck);
114		uint32_t pc;
115		LOAD_32(pc, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
116		if (pc < SIZE_BIOS && pc >= 0x20) {
117			error = true;
118		}
119	}
120	if (gba->memory.rom && (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title)))) {
121		mLOG(GBA_STATE, WARN, "Savestate is for a different game");
122		error = true;
123	} else if (!gba->memory.rom && state->id != 0) {
124		mLOG(GBA_STATE, WARN, "Savestate is for a game, but no game loaded");
125		error = true;
126	}
127	LOAD_32(ucheck, 0, &state->romCrc32);
128	if (ucheck != gba->romCrc32) {
129		mLOG(GBA_STATE, WARN, "Savestate is for a different version of the game");
130	}
131	LOAD_32(check, 0, &state->cpu.cycles);
132	if (check < 0) {
133		mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are negative");
134		error = true;
135	}
136	if (check >= (int32_t) GBA_ARM7TDMI_FREQUENCY) {
137		mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are too high");
138		error = true;
139	}
140	LOAD_32(check, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
141	int region = (check >> BASE_OFFSET);
142	if ((region == REGION_CART0 || region == REGION_CART1 || region == REGION_CART2) && ((check - WORD_SIZE_ARM) & SIZE_CART0) >= gba->memory.romSize - WORD_SIZE_ARM) {
143		mLOG(GBA_STATE, WARN, "Savestate created using a differently sized version of the ROM");
144		error = true;
145	}
146	if (error) {
147		return false;
148	}
149	LOAD_32(gba->timing.masterCycles, 0, &state->masterCycles);
150	size_t i;
151	for (i = 0; i < 16; ++i) {
152		LOAD_32(gba->cpu->gprs[i], i * sizeof(gba->cpu->gprs[0]), state->cpu.gprs);
153	}
154	LOAD_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
155	LOAD_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
156	LOAD_32(gba->cpu->cycles, 0, &state->cpu.cycles);
157	LOAD_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
158	for (i = 0; i < 6; ++i) {
159		int j;
160		for (j = 0; j < 7; ++j) {
161			LOAD_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
162		}
163		LOAD_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
164	}
165	gba->cpu->privilegeMode = gba->cpu->cpsr.priv;
166	gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
167	if (state->biosPrefetch) {
168		LOAD_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
169	}
170	LOAD_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
171	if (gba->cpu->cpsr.t) {
172		gba->cpu->executionMode = MODE_THUMB;
173		if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
174			LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
175			LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
176			gba->cpu->prefetch[0] &= 0xFFFF;
177			gba->cpu->prefetch[1] &= 0xFFFF;
178		} else {
179			// Maintain backwards compat
180			LOAD_16(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_THUMB) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
181			LOAD_16(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
182		}
183	} else {
184		gba->cpu->executionMode = MODE_ARM;
185		if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
186			LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
187			LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
188		} else {
189			// Maintain backwards compat
190			LOAD_32(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
191			LOAD_32(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
192		}
193	}
194	GBASerializedMiscFlags miscFlags = 0;
195	LOAD_32(miscFlags, 0, &state->miscFlags);
196	gba->cpu->halted = GBASerializedMiscFlagsGetHalted(miscFlags);
197
198	GBAVideoDeserialize(&gba->video, state);
199	GBAMemoryDeserialize(&gba->memory, state);
200	GBAIODeserialize(gba, state);
201	GBAAudioDeserialize(&gba->audio, state);
202	GBASavedataDeserialize(&gba->memory.savedata, state);
203
204	if (gba->rr) {
205		gba->rr->stateLoaded(gba->rr, state);
206	}
207	return true;
208}
209
210struct GBASerializedState* GBAAllocateState(void) {
211	return anonymousMemoryMap(sizeof(struct GBASerializedState));
212}
213
214void GBADeallocateState(struct GBASerializedState* state) {
215	mappedMemoryFree(state, sizeof(struct GBASerializedState));
216}