all repos — mgba @ a97276b3a017311198e8aefcb89de50b54604dc5

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