all repos — mgba @ 000f232c582b0981b66d48d7af799e97faffad8e

mGBA Game Boy Advance Emulator

src/gb/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/gb/serialize.h>
  7
  8#include <mgba/internal/gb/io.h>
  9#include <mgba/internal/gb/timer.h>
 10#include <mgba/internal/lr35902/lr35902.h>
 11
 12mLOG_DEFINE_CATEGORY(GB_STATE, "GB Savestate", "gb.serialize");
 13
 14const uint32_t GB_SAVESTATE_MAGIC = 0x00400000;
 15const uint32_t GB_SAVESTATE_VERSION = 0x00000001;
 16
 17void GBSerialize(struct GB* gb, struct GBSerializedState* state) {
 18	STORE_32LE(GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, 0, &state->versionMagic);
 19	STORE_32LE(gb->romCrc32, 0, &state->romCrc32);
 20	STORE_32LE(gb->timing.masterCycles, 0, &state->masterCycles);
 21
 22	if (gb->memory.rom) {
 23		memcpy(state->title, ((struct GBCartridge*) gb->memory.rom)->titleLong, sizeof(state->title));
 24	} else {
 25		memset(state->title, 0, sizeof(state->title));
 26	}
 27
 28	state->model = gb->model;
 29
 30	state->cpu.a = gb->cpu->a;
 31	state->cpu.f = gb->cpu->f.packed;
 32	state->cpu.b = gb->cpu->b;
 33	state->cpu.c = gb->cpu->c;
 34	state->cpu.d = gb->cpu->d;
 35	state->cpu.e = gb->cpu->e;
 36	state->cpu.h = gb->cpu->h;
 37	state->cpu.l = gb->cpu->l;
 38	STORE_16LE(gb->cpu->sp, 0, &state->cpu.sp);
 39	STORE_16LE(gb->cpu->pc, 0, &state->cpu.pc);
 40
 41	STORE_32LE(gb->cpu->cycles, 0, &state->cpu.cycles);
 42	STORE_32LE(gb->cpu->nextEvent, 0, &state->cpu.nextEvent);
 43
 44	STORE_16LE(gb->cpu->index, 0, &state->cpu.index);
 45	state->cpu.bus = gb->cpu->bus;
 46	state->cpu.executionState = gb->cpu->executionState;
 47	STORE_16LE(gb->cpu->irqVector, 0, &state->cpu.irqVector);
 48
 49	GBSerializedCpuFlags flags = 0;
 50	flags = GBSerializedCpuFlagsSetCondition(flags, gb->cpu->condition);
 51	flags = GBSerializedCpuFlagsSetIrqPending(flags, gb->cpu->irqPending);
 52	flags = GBSerializedCpuFlagsSetDoubleSpeed(flags, gb->doubleSpeed);
 53	flags = GBSerializedCpuFlagsSetEiPending(flags, mTimingIsScheduled(&gb->timing, &gb->eiPending));
 54	STORE_32LE(flags, 0, &state->cpu.flags);
 55	STORE_32LE(gb->eiPending.when - mTimingCurrentTime(&gb->timing), 0, &state->cpu.eiPending);
 56
 57	GBMemorySerialize(gb, state);
 58	GBIOSerialize(gb, state);
 59	GBVideoSerialize(&gb->video, state);
 60	GBTimerSerialize(&gb->timer, state);
 61	GBAudioSerialize(&gb->audio, state);
 62
 63#ifndef _MSC_VER
 64	struct timeval tv;
 65	if (!gettimeofday(&tv, 0)) {
 66		uint64_t usec = tv.tv_usec;
 67		usec += tv.tv_sec * 1000000LL;
 68		STORE_64LE(usec, 0, &state->creationUsec);
 69	}
 70#else
 71	struct timespec ts;
 72	if (timespec_get(&ts, TIME_UTC)) {
 73		uint64_t usec = ts.tv_nsec / 1000;
 74		usec += ts.tv_sec * 1000000LL;
 75		STORE_64LE(usec, 0, &state->creationUsec);
 76	}
 77#endif
 78	else {
 79		state->creationUsec = 0;
 80	}
 81}
 82
 83bool GBDeserialize(struct GB* gb, const struct GBSerializedState* state) {
 84	bool error = false;
 85	int32_t check;
 86	uint32_t ucheck;
 87	int16_t check16;
 88	uint16_t ucheck16;
 89	LOAD_32LE(ucheck, 0, &state->versionMagic);
 90	if (ucheck > GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION) {
 91		mLOG(GB_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
 92		error = true;
 93	} else if (ucheck < GB_SAVESTATE_MAGIC) {
 94		mLOG(GB_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
 95		error = true;
 96	} else if (ucheck < GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION) {
 97		mLOG(GB_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
 98	}
 99
100	if (gb->memory.rom && memcmp(state->title, ((struct GBCartridge*) gb->memory.rom)->titleLong, sizeof(state->title))) {
101		mLOG(GB_STATE, WARN, "Savestate is for a different game");
102		error = true;
103	}
104	LOAD_32LE(ucheck, 0, &state->romCrc32);
105	if (ucheck != gb->romCrc32) {
106		mLOG(GB_STATE, WARN, "Savestate is for a different version of the game");
107	}
108	LOAD_32LE(check, 0, &state->cpu.cycles);
109	if (check < 0) {
110		mLOG(GB_STATE, WARN, "Savestate is corrupted: CPU cycles are negative");
111		error = true;
112	}
113	if (state->cpu.executionState != LR35902_CORE_FETCH) {
114		mLOG(GB_STATE, WARN, "Savestate is corrupted: Execution state is not FETCH");
115		error = true;
116	}
117	if (check >= (int32_t) DMG_LR35902_FREQUENCY) {
118		mLOG(GB_STATE, WARN, "Savestate is corrupted: CPU cycles are too high");
119		error = true;
120	}
121	LOAD_16LE(check16, 0, &state->video.x);
122	if (check16 < 0 || check16 > GB_VIDEO_HORIZONTAL_PIXELS) {
123		mLOG(GB_STATE, WARN, "Savestate is corrupted: video x is out of range");
124		error = true;
125	}
126	LOAD_16LE(check16, 0, &state->video.ly);
127	if (check16 < 0 || check16 > GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
128		mLOG(GB_STATE, WARN, "Savestate is corrupted: video y is out of range");
129		error = true;
130	}
131	LOAD_16LE(ucheck16, 0, &state->memory.dmaDest);
132	if (ucheck16 + state->memory.dmaRemaining > GB_SIZE_OAM) {
133		mLOG(GB_STATE, WARN, "Savestate is corrupted: DMA destination is out of range");
134		error = true;
135	}
136	LOAD_16LE(ucheck16, 0, &state->video.bcpIndex);
137	if (ucheck16 >= 0x40) {
138		mLOG(GB_STATE, WARN, "Savestate is corrupted: BCPS is out of range");
139	}
140	LOAD_16LE(ucheck16, 0, &state->video.ocpIndex);
141	if (ucheck16 >= 0x40) {
142		mLOG(GB_STATE, WARN, "Savestate is corrupted: OCPS is out of range");
143	}
144	if (error) {
145		return false;
146	}
147
148	gb->cpu->a = state->cpu.a;
149	gb->cpu->f.packed = state->cpu.f;
150	gb->cpu->b = state->cpu.b;
151	gb->cpu->c = state->cpu.c;
152	gb->cpu->d = state->cpu.d;
153	gb->cpu->e = state->cpu.e;
154	gb->cpu->h = state->cpu.h;
155	gb->cpu->l = state->cpu.l;
156	LOAD_16LE(gb->cpu->sp, 0, &state->cpu.sp);
157	LOAD_16LE(gb->cpu->pc, 0, &state->cpu.pc);
158
159	LOAD_16LE(gb->cpu->index, 0, &state->cpu.index);
160	gb->cpu->bus = state->cpu.bus;
161	gb->cpu->executionState = state->cpu.executionState;
162	LOAD_16LE(gb->cpu->irqVector, 0, &state->cpu.irqVector);
163
164	GBSerializedCpuFlags flags;
165	LOAD_32LE(flags, 0, &state->cpu.flags);
166	gb->cpu->condition = GBSerializedCpuFlagsGetCondition(flags);
167	gb->cpu->irqPending = GBSerializedCpuFlagsGetIrqPending(flags);
168	gb->doubleSpeed = GBSerializedCpuFlagsGetDoubleSpeed(flags);
169	gb->audio.timingFactor = gb->doubleSpeed + 1;
170
171	uint32_t when;
172	LOAD_32LE(when, 0, &state->cpu.eiPending);
173	if (GBSerializedCpuFlagsIsEiPending(flags)) {
174		mTimingSchedule(&gb->timing, &gb->eiPending, when);
175	}
176
177	LOAD_32LE(gb->cpu->cycles, 0, &state->cpu.cycles);
178	LOAD_32LE(gb->cpu->nextEvent, 0, &state->cpu.nextEvent);
179	gb->timing.root = NULL;
180
181	gb->model = state->model;
182
183	if (gb->model < GB_MODEL_CGB) {
184		gb->audio.style = GB_AUDIO_DMG;
185	} else {
186		gb->audio.style = GB_AUDIO_CGB;
187	}
188
189	GBMemoryDeserialize(gb, state);
190	GBIODeserialize(gb, state);
191	GBVideoDeserialize(&gb->video, state);
192	GBTimerDeserialize(&gb->timer, state);
193	GBAudioDeserialize(&gb->audio, state);
194
195	gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
196
197	return true;
198}