all repos — mgba @ a3a380d9f551bbc5ad726ef07c11d20c0263d1e5

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