all repos — mgba @ 6d898542c765f4efc4a094c5ebd3f3465c36f417

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