all repos — mgba @ 0bf0975a5d040bcc00741b25ebe8f18110ba08a0

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