all repos — mgba @ 740f7a0f66a1dc78af39db981dcc332bab8bdff6

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