all repos — mgba @ bd4dd8de5ca3cd2c62c778c26114f685be6ac729

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 = 0x00000002;
 16
 17static void GBSGBSerialize(struct GB* gb, struct GBSerializedState* state);
 18static void GBSGBDeserialize(struct GB* gb, const struct GBSerializedState* state);
 19
 20void GBSerialize(struct GB* gb, struct GBSerializedState* state) {
 21	STORE_32LE(GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, 0, &state->versionMagic);
 22	STORE_32LE(gb->romCrc32, 0, &state->romCrc32);
 23	STORE_32LE(gb->timing.masterCycles, 0, &state->masterCycles);
 24
 25	if (gb->memory.rom) {
 26		memcpy(state->title, ((struct GBCartridge*) &gb->memory.rom[0x100])->titleLong, sizeof(state->title));
 27	} else {
 28		memset(state->title, 0, sizeof(state->title));
 29	}
 30
 31	state->model = gb->model;
 32
 33	state->cpu.a = gb->cpu->a;
 34	state->cpu.f = gb->cpu->f.packed;
 35	state->cpu.b = gb->cpu->b;
 36	state->cpu.c = gb->cpu->c;
 37	state->cpu.d = gb->cpu->d;
 38	state->cpu.e = gb->cpu->e;
 39	state->cpu.h = gb->cpu->h;
 40	state->cpu.l = gb->cpu->l;
 41	STORE_16LE(gb->cpu->sp, 0, &state->cpu.sp);
 42	STORE_16LE(gb->cpu->pc, 0, &state->cpu.pc);
 43
 44	STORE_32LE(gb->cpu->cycles, 0, &state->cpu.cycles);
 45	STORE_32LE(gb->cpu->nextEvent, 0, &state->cpu.nextEvent);
 46
 47	STORE_16LE(gb->cpu->index, 0, &state->cpu.index);
 48	state->cpu.bus = gb->cpu->bus;
 49	state->cpu.executionState = gb->cpu->executionState;
 50	STORE_16LE(gb->cpu->irqVector, 0, &state->cpu.irqVector);
 51
 52	GBSerializedCpuFlags flags = 0;
 53	flags = GBSerializedCpuFlagsSetCondition(flags, gb->cpu->condition);
 54	flags = GBSerializedCpuFlagsSetIrqPending(flags, gb->cpu->irqPending);
 55	flags = GBSerializedCpuFlagsSetDoubleSpeed(flags, gb->doubleSpeed);
 56	flags = GBSerializedCpuFlagsSetEiPending(flags, mTimingIsScheduled(&gb->timing, &gb->eiPending));
 57	STORE_32LE(flags, 0, &state->cpu.flags);
 58	STORE_32LE(gb->eiPending.when - mTimingCurrentTime(&gb->timing), 0, &state->cpu.eiPending);
 59
 60	GBMemorySerialize(gb, state);
 61	GBIOSerialize(gb, state);
 62	GBVideoSerialize(&gb->video, state);
 63	GBTimerSerialize(&gb->timer, state);
 64	GBAudioSerialize(&gb->audio, state);
 65
 66	if (gb->model == GB_MODEL_SGB) {
 67		GBSGBSerialize(gb, state);
 68	}
 69}
 70
 71bool GBDeserialize(struct GB* gb, const struct GBSerializedState* state) {
 72	bool error = false;
 73	int32_t check;
 74	uint32_t ucheck;
 75	int16_t check16;
 76	uint16_t ucheck16;
 77	LOAD_32LE(ucheck, 0, &state->versionMagic);
 78	if (ucheck > GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION) {
 79		mLOG(GB_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
 80		error = true;
 81	} else if (ucheck < GB_SAVESTATE_MAGIC) {
 82		mLOG(GB_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
 83		error = true;
 84	} else if (ucheck < GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION) {
 85		mLOG(GB_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
 86	}
 87	bool canSgb = ucheck >= GB_SAVESTATE_MAGIC + 2;
 88
 89	if (gb->memory.rom && memcmp(state->title, ((struct GBCartridge*) &gb->memory.rom[0x100])->titleLong, sizeof(state->title))) {
 90		LOAD_32LE(ucheck, 0, &state->versionMagic);
 91		if (ucheck > GB_SAVESTATE_MAGIC + 2 || memcmp(state->title, ((struct GBCartridge*) gb->memory.rom)->titleLong, sizeof(state->title))) {
 92			// There was a bug in previous versions where the memory address being compared was wrong
 93			mLOG(GB_STATE, WARN, "Savestate is for a different game");
 94			error = true;
 95		}
 96	}
 97	LOAD_32LE(ucheck, 0, &state->romCrc32);
 98	if (ucheck != gb->romCrc32) {
 99		mLOG(GB_STATE, WARN, "Savestate is for a different version of the game");
100	}
101	LOAD_32LE(check, 0, &state->cpu.cycles);
102	if (check < 0) {
103		mLOG(GB_STATE, WARN, "Savestate is corrupted: CPU cycles are negative");
104		error = true;
105	}
106	if (state->cpu.executionState != LR35902_CORE_FETCH) {
107		mLOG(GB_STATE, WARN, "Savestate is corrupted: Execution state is not FETCH");
108		error = true;
109	}
110	if (check >= (int32_t) DMG_LR35902_FREQUENCY) {
111		mLOG(GB_STATE, WARN, "Savestate is corrupted: CPU cycles are too high");
112		error = true;
113	}
114	LOAD_16LE(check16, 0, &state->video.x);
115	if (check16 < 0 || check16 > GB_VIDEO_HORIZONTAL_PIXELS) {
116		mLOG(GB_STATE, WARN, "Savestate is corrupted: video x is out of range");
117		error = true;
118	}
119	LOAD_16LE(check16, 0, &state->video.ly);
120	if (check16 < 0 || check16 > GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
121		mLOG(GB_STATE, WARN, "Savestate is corrupted: video y is out of range");
122		error = true;
123	}
124	LOAD_16LE(ucheck16, 0, &state->memory.dmaDest);
125	if (ucheck16 + state->memory.dmaRemaining > GB_SIZE_OAM) {
126		mLOG(GB_STATE, WARN, "Savestate is corrupted: DMA destination is out of range");
127		error = true;
128	}
129	LOAD_16LE(ucheck16, 0, &state->video.bcpIndex);
130	if (ucheck16 >= 0x40) {
131		mLOG(GB_STATE, WARN, "Savestate is corrupted: BCPS is out of range");
132	}
133	LOAD_16LE(ucheck16, 0, &state->video.ocpIndex);
134	if (ucheck16 >= 0x40) {
135		mLOG(GB_STATE, WARN, "Savestate is corrupted: OCPS is out of range");
136	}
137	if (error) {
138		return false;
139	}
140	gb->timing.root = NULL;
141	LOAD_32LE(gb->timing.masterCycles, 0, &state->masterCycles);
142
143	gb->cpu->a = state->cpu.a;
144	gb->cpu->f.packed = state->cpu.f;
145	gb->cpu->b = state->cpu.b;
146	gb->cpu->c = state->cpu.c;
147	gb->cpu->d = state->cpu.d;
148	gb->cpu->e = state->cpu.e;
149	gb->cpu->h = state->cpu.h;
150	gb->cpu->l = state->cpu.l;
151	LOAD_16LE(gb->cpu->sp, 0, &state->cpu.sp);
152	LOAD_16LE(gb->cpu->pc, 0, &state->cpu.pc);
153
154	LOAD_16LE(gb->cpu->index, 0, &state->cpu.index);
155	gb->cpu->bus = state->cpu.bus;
156	gb->cpu->executionState = state->cpu.executionState;
157	LOAD_16LE(gb->cpu->irqVector, 0, &state->cpu.irqVector);
158
159	GBSerializedCpuFlags flags;
160	LOAD_32LE(flags, 0, &state->cpu.flags);
161	gb->cpu->condition = GBSerializedCpuFlagsGetCondition(flags);
162	gb->cpu->irqPending = GBSerializedCpuFlagsGetIrqPending(flags);
163	gb->doubleSpeed = GBSerializedCpuFlagsGetDoubleSpeed(flags);
164	gb->audio.timingFactor = gb->doubleSpeed + 1;
165
166	LOAD_32LE(gb->cpu->cycles, 0, &state->cpu.cycles);
167	LOAD_32LE(gb->cpu->nextEvent, 0, &state->cpu.nextEvent);
168	gb->timing.root = NULL;
169
170	uint32_t when;
171	LOAD_32LE(when, 0, &state->cpu.eiPending);
172	if (GBSerializedCpuFlagsIsEiPending(flags)) {
173		mTimingSchedule(&gb->timing, &gb->eiPending, when);
174	}
175
176	gb->model = state->model;
177
178	if (gb->model < GB_MODEL_CGB) {
179		gb->audio.style = GB_AUDIO_DMG;
180	} else {
181		gb->audio.style = GB_AUDIO_CGB;
182	}
183
184	GBMemoryDeserialize(gb, state);
185	GBVideoDeserialize(&gb->video, state);
186	GBIODeserialize(gb, state);
187	GBTimerDeserialize(&gb->timer, state);
188	GBAudioDeserialize(&gb->audio, state);
189
190	if (gb->model == GB_MODEL_SGB && canSgb) {
191		GBSGBDeserialize(gb, state);
192	}
193
194	gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
195
196	gb->timing.reroot = gb->timing.root;
197	gb->timing.root = NULL;
198
199	return true;
200}
201
202// TODO: Reorganize SGB into its own file
203void GBSGBSerialize(struct GB* gb, struct GBSerializedState* state) {
204	state->sgb.command = gb->video.sgbCommandHeader;
205	state->sgb.bits = gb->sgbBit;
206
207	GBSerializedSGBFlags flags = 0;
208	flags = GBSerializedSGBFlagsSetP1Bits(flags, gb->currentSgbBits);
209	flags = GBSerializedSGBFlagsSetRenderMode(flags, gb->video.renderer->sgbRenderMode);
210	STORE_32LE(flags, 0, &state->sgb.flags);
211
212	memcpy(state->sgb.packet, gb->sgbPacket, sizeof(state->sgb.packet));
213
214	if (gb->video.renderer->sgbCharRam) {
215		memcpy(state->sgb.charRam, gb->video.renderer->sgbCharRam, sizeof(state->sgb.charRam));
216	}
217	if (gb->video.renderer->sgbMapRam) {
218		memcpy(state->sgb.mapRam, gb->video.renderer->sgbMapRam, sizeof(state->sgb.mapRam));
219	}
220	if (gb->video.renderer->sgbPalRam) {
221		memcpy(state->sgb.palRam, gb->video.renderer->sgbPalRam, sizeof(state->sgb.palRam));
222	}
223	if (gb->video.renderer->sgbAttributeFiles) {
224		memcpy(state->sgb.atfRam, gb->video.renderer->sgbAttributeFiles, sizeof(state->sgb.atfRam));
225	}
226	if (gb->video.renderer->sgbAttributes) {
227		memcpy(state->sgb.attributes, gb->video.renderer->sgbAttributes, sizeof(state->sgb.attributes));
228	}
229}
230
231void GBSGBDeserialize(struct GB* gb, const struct GBSerializedState* state) {
232	gb->video.sgbCommandHeader = state->sgb.command;
233	gb->sgbBit = state->sgb.bits;
234
235	GBSerializedSGBFlags flags;
236	LOAD_32LE(flags, 0, &state->sgb.flags);
237	gb->currentSgbBits = GBSerializedSGBFlagsGetP1Bits(flags);
238	gb->video.renderer->sgbRenderMode = GBSerializedSGBFlagsGetRenderMode(flags);
239
240	memcpy(gb->sgbPacket, state->sgb.packet, sizeof(state->sgb.packet));
241
242	if (gb->video.renderer->sgbCharRam) {
243		memcpy(gb->video.renderer->sgbCharRam, state->sgb.charRam, sizeof(state->sgb.charRam));
244	}
245	if (gb->video.renderer->sgbMapRam) {
246		memcpy(gb->video.renderer->sgbMapRam, state->sgb.mapRam, sizeof(state->sgb.mapRam));
247	}
248	if (gb->video.renderer->sgbPalRam) {
249		memcpy(gb->video.renderer->sgbPalRam, state->sgb.palRam, sizeof(state->sgb.palRam));
250	}
251	if (gb->video.renderer->sgbAttributeFiles) {
252		memcpy(gb->video.renderer->sgbAttributeFiles, state->sgb.atfRam, sizeof(state->sgb.atfRam));
253	}
254	if (gb->video.renderer->sgbAttributes) {
255		memcpy(gb->video.renderer->sgbAttributes, state->sgb.attributes, sizeof(state->sgb.attributes));
256	}
257
258	GBVideoWriteSGBPacket(&gb->video, (uint8_t[16]) { (SGB_ATRC_EN << 3) | 1, 0 });
259	GBVideoWriteSGBPacket(&gb->video, gb->sgbPacket);
260}