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/sm83/sm83.h>
11
12#include <mgba-util/memory.h>
13
14mLOG_DEFINE_CATEGORY(GB_STATE, "GB Savestate", "gb.serialize");
15
16const uint32_t GB_SAVESTATE_MAGIC = 0x00400000;
17const uint32_t GB_SAVESTATE_VERSION = 0x00000002;
18
19static void GBSGBSerialize(struct GB* gb, struct GBSerializedState* state);
20static void GBSGBDeserialize(struct GB* gb, const struct GBSerializedState* state);
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 STORE_64LE(gb->timing.globalCycles, 0, &state->globalCycles);
27
28 if (gb->memory.rom) {
29 memcpy(state->title, ((struct GBCartridge*) &gb->memory.rom[0x100])->titleLong, sizeof(state->title));
30 } else {
31 memset(state->title, 0, sizeof(state->title));
32 }
33
34 state->model = gb->model;
35
36 state->cpu.a = gb->cpu->a;
37 state->cpu.f = gb->cpu->f.packed;
38 state->cpu.b = gb->cpu->b;
39 state->cpu.c = gb->cpu->c;
40 state->cpu.d = gb->cpu->d;
41 state->cpu.e = gb->cpu->e;
42 state->cpu.h = gb->cpu->h;
43 state->cpu.l = gb->cpu->l;
44 STORE_16LE(gb->cpu->sp, 0, &state->cpu.sp);
45 STORE_16LE(gb->cpu->pc, 0, &state->cpu.pc);
46
47 STORE_32LE(gb->cpu->cycles, 0, &state->cpu.cycles);
48 STORE_32LE(gb->cpu->nextEvent, 0, &state->cpu.nextEvent);
49
50 STORE_16LE(gb->cpu->index, 0, &state->cpu.index);
51 state->cpu.bus = gb->cpu->bus;
52 state->cpu.executionState = gb->cpu->executionState;
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 if (gb->model & GB_MODEL_SGB) {
69 GBSGBSerialize(gb, state);
70 }
71}
72
73bool GBDeserialize(struct GB* gb, const struct GBSerializedState* state) {
74 bool error = false;
75 int32_t check;
76 uint32_t ucheck;
77 int16_t check16;
78 uint16_t ucheck16;
79 LOAD_32LE(ucheck, 0, &state->versionMagic);
80 if (ucheck > GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION) {
81 mLOG(GB_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
82 error = true;
83 } else if (ucheck < GB_SAVESTATE_MAGIC) {
84 mLOG(GB_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
85 error = true;
86 } else if (ucheck < GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION) {
87 mLOG(GB_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GB_SAVESTATE_MAGIC + GB_SAVESTATE_VERSION, ucheck);
88 }
89 bool canSgb = ucheck >= GB_SAVESTATE_MAGIC + 2;
90
91 if (gb->memory.rom && memcmp(state->title, ((struct GBCartridge*) &gb->memory.rom[0x100])->titleLong, sizeof(state->title))) {
92 LOAD_32LE(ucheck, 0, &state->versionMagic);
93 if (ucheck > GB_SAVESTATE_MAGIC + 2 || memcmp(state->title, ((struct GBCartridge*) gb->memory.rom)->titleLong, sizeof(state->title))) {
94 // There was a bug in previous versions where the memory address being compared was wrong
95 mLOG(GB_STATE, WARN, "Savestate is for a different game");
96 error = true;
97 }
98 }
99 LOAD_32LE(ucheck, 0, &state->romCrc32);
100 if (ucheck != gb->romCrc32) {
101 mLOG(GB_STATE, WARN, "Savestate is for a different version of the game");
102 }
103 LOAD_32LE(check, 0, &state->cpu.cycles);
104 if (check < 0) {
105 mLOG(GB_STATE, WARN, "Savestate is corrupted: CPU cycles are negative");
106 error = true;
107 }
108 if (state->cpu.executionState != SM83_CORE_FETCH) {
109 mLOG(GB_STATE, WARN, "Savestate is corrupted: Execution state is not FETCH");
110 error = true;
111 }
112 if (check >= (int32_t) DMG_SM83_FREQUENCY) {
113 mLOG(GB_STATE, WARN, "Savestate is corrupted: CPU cycles are too high");
114 error = true;
115 }
116 LOAD_16LE(check16, 0, &state->video.x);
117 if (check16 < -7 || check16 > GB_VIDEO_HORIZONTAL_PIXELS) {
118 mLOG(GB_STATE, WARN, "Savestate is corrupted: video x is out of range");
119 error = true;
120 }
121 LOAD_16LE(check16, 0, &state->video.ly);
122 if (check16 < 0 || check16 > GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
123 mLOG(GB_STATE, WARN, "Savestate is corrupted: video y is out of range");
124 error = true;
125 }
126 LOAD_16LE(ucheck16, 0, &state->memory.dmaDest);
127 if (ucheck16 + state->memory.dmaRemaining > GB_SIZE_OAM) {
128 mLOG(GB_STATE, WARN, "Savestate is corrupted: DMA destination is out of range");
129 error = true;
130 }
131 LOAD_16LE(ucheck16, 0, &state->video.bcpIndex);
132 if (ucheck16 >= 0x40) {
133 mLOG(GB_STATE, WARN, "Savestate is corrupted: BCPS is out of range");
134 }
135 LOAD_16LE(ucheck16, 0, &state->video.ocpIndex);
136 if (ucheck16 >= 0x40) {
137 mLOG(GB_STATE, WARN, "Savestate is corrupted: OCPS is out of range");
138 }
139 bool differentBios = !gb->biosVf || gb->model != state->model;
140 if (state->io[0x50] == 0xFF) {
141 if (differentBios) {
142 mLOG(GB_STATE, WARN, "Incompatible savestate, please restart with correct BIOS in %s mode", GBModelToName(state->model));
143 error = true;
144 } else {
145 // TODO: Make it work correctly
146 mLOG(GB_STATE, WARN, "Loading savestate in BIOS. This may not work correctly");
147 }
148 }
149 if (error) {
150 return false;
151 }
152 mTimingClear(&gb->timing);
153 LOAD_32LE(gb->timing.masterCycles, 0, &state->masterCycles);
154 LOAD_64LE(gb->timing.globalCycles, 0, &state->globalCycles);
155
156 gb->cpu->a = state->cpu.a;
157 gb->cpu->f.packed = state->cpu.f;
158 gb->cpu->b = state->cpu.b;
159 gb->cpu->c = state->cpu.c;
160 gb->cpu->d = state->cpu.d;
161 gb->cpu->e = state->cpu.e;
162 gb->cpu->h = state->cpu.h;
163 gb->cpu->l = state->cpu.l;
164 LOAD_16LE(gb->cpu->sp, 0, &state->cpu.sp);
165 LOAD_16LE(gb->cpu->pc, 0, &state->cpu.pc);
166
167 LOAD_16LE(gb->cpu->index, 0, &state->cpu.index);
168 gb->cpu->bus = state->cpu.bus;
169 gb->cpu->executionState = state->cpu.executionState;
170
171 GBSerializedCpuFlags flags;
172 LOAD_32LE(flags, 0, &state->cpu.flags);
173 gb->cpu->condition = GBSerializedCpuFlagsGetCondition(flags);
174 gb->cpu->irqPending = GBSerializedCpuFlagsGetIrqPending(flags);
175 gb->doubleSpeed = GBSerializedCpuFlagsGetDoubleSpeed(flags);
176 gb->audio.timingFactor = gb->doubleSpeed + 1;
177
178 LOAD_32LE(gb->cpu->cycles, 0, &state->cpu.cycles);
179 LOAD_32LE(gb->cpu->nextEvent, 0, &state->cpu.nextEvent);
180 gb->timing.root = NULL;
181
182 uint32_t when;
183 LOAD_32LE(when, 0, &state->cpu.eiPending);
184 if (GBSerializedCpuFlagsIsEiPending(flags)) {
185 mTimingSchedule(&gb->timing, &gb->eiPending, when);
186 }
187
188 gb->model = state->model;
189
190 if (gb->model < GB_MODEL_CGB) {
191 gb->audio.style = GB_AUDIO_DMG;
192 } else {
193 gb->audio.style = GB_AUDIO_CGB;
194 }
195
196 GBMemoryDeserialize(gb, state);
197 GBVideoDeserialize(&gb->video, state);
198 GBIODeserialize(gb, state);
199 GBTimerDeserialize(&gb->timer, state);
200 GBAudioDeserialize(&gb->audio, state);
201
202 if (gb->memory.io[0x50] == 0xFF) {
203 GBMapBIOS(gb);
204 } else {
205 GBUnmapBIOS(gb);
206 }
207
208 if (gb->model & GB_MODEL_SGB && canSgb) {
209 GBSGBDeserialize(gb, state);
210 }
211
212 gb->cpu->memory.setActiveRegion(gb->cpu, gb->cpu->pc);
213
214 gb->timing.reroot = gb->timing.root;
215 gb->timing.root = NULL;
216
217 return true;
218}
219
220// TODO: Reorganize SGB into its own file
221void GBSGBSerialize(struct GB* gb, struct GBSerializedState* state) {
222 state->sgb.command = gb->video.sgbCommandHeader;
223 state->sgb.bits = gb->sgbBit;
224
225 GBSerializedSGBFlags flags = 0;
226 flags = GBSerializedSGBFlagsSetP1Bits(flags, gb->currentSgbBits);
227 flags = GBSerializedSGBFlagsSetRenderMode(flags, gb->video.renderer->sgbRenderMode);
228 flags = GBSerializedSGBFlagsSetBufferIndex(flags, gb->video.sgbBufferIndex);
229 flags = GBSerializedSGBFlagsSetReqControllers(flags, gb->sgbControllers);
230 flags = GBSerializedSGBFlagsSetIncrement(flags, gb->sgbIncrement);
231 flags = GBSerializedSGBFlagsSetCurrentController(flags, gb->sgbCurrentController);
232 STORE_32LE(flags, 0, &state->sgb.flags);
233
234 memcpy(state->sgb.packet, gb->video.sgbPacketBuffer, sizeof(state->sgb.packet));
235 memcpy(state->sgb.inProgressPacket, gb->sgbPacket, sizeof(state->sgb.inProgressPacket));
236
237 if (gb->video.renderer->sgbCharRam) {
238 memcpy(state->sgb.charRam, gb->video.renderer->sgbCharRam, sizeof(state->sgb.charRam));
239 }
240 if (gb->video.renderer->sgbMapRam) {
241 memcpy(state->sgb.mapRam, gb->video.renderer->sgbMapRam, sizeof(state->sgb.mapRam));
242 }
243 if (gb->video.renderer->sgbPalRam) {
244 memcpy(state->sgb.palRam, gb->video.renderer->sgbPalRam, sizeof(state->sgb.palRam));
245 }
246 if (gb->video.renderer->sgbAttributeFiles) {
247 memcpy(state->sgb.atfRam, gb->video.renderer->sgbAttributeFiles, sizeof(state->sgb.atfRam));
248 }
249 if (gb->video.renderer->sgbAttributes) {
250 memcpy(state->sgb.attributes, gb->video.renderer->sgbAttributes, sizeof(state->sgb.attributes));
251 }
252 gb->video.renderer->enableSGBBorder(gb->video.renderer, gb->video.sgbBorders);
253}
254
255void GBSGBDeserialize(struct GB* gb, const struct GBSerializedState* state) {
256 gb->video.sgbCommandHeader = state->sgb.command;
257 gb->sgbBit = state->sgb.bits;
258
259 GBSerializedSGBFlags flags;
260 LOAD_32LE(flags, 0, &state->sgb.flags);
261 gb->currentSgbBits = GBSerializedSGBFlagsGetP1Bits(flags);
262 gb->video.renderer->sgbRenderMode = GBSerializedSGBFlagsGetRenderMode(flags);
263 gb->video.sgbBufferIndex = GBSerializedSGBFlagsGetBufferIndex(flags);
264 gb->sgbControllers = GBSerializedSGBFlagsGetReqControllers(flags);
265 gb->sgbCurrentController = GBSerializedSGBFlagsGetCurrentController(flags);
266 gb->sgbIncrement = GBSerializedSGBFlagsGetIncrement(flags);
267
268 // Old versions of mGBA stored the increment bits here
269 if (gb->sgbBit > 129 && gb->sgbBit & 2) {
270 gb->sgbIncrement = true;
271 }
272
273 memcpy(gb->video.sgbPacketBuffer, state->sgb.packet, sizeof(state->sgb.packet));
274 memcpy(gb->sgbPacket, state->sgb.inProgressPacket, sizeof(state->sgb.inProgressPacket));
275
276 if (!gb->video.renderer->sgbCharRam) {
277 gb->video.renderer->sgbCharRam = anonymousMemoryMap(SGB_SIZE_CHAR_RAM);
278 }
279 if (!gb->video.renderer->sgbMapRam) {
280 gb->video.renderer->sgbMapRam = anonymousMemoryMap(SGB_SIZE_MAP_RAM);
281 }
282 if (!gb->video.renderer->sgbPalRam) {
283 gb->video.renderer->sgbPalRam = anonymousMemoryMap(SGB_SIZE_PAL_RAM);
284 }
285 if (!gb->video.renderer->sgbAttributeFiles) {
286 gb->video.renderer->sgbAttributeFiles = anonymousMemoryMap(SGB_SIZE_ATF_RAM);
287 }
288 if (!gb->video.renderer->sgbAttributes) {
289 gb->video.renderer->sgbAttributes = malloc(90 * 45);
290 }
291
292 memcpy(gb->video.renderer->sgbCharRam, state->sgb.charRam, sizeof(state->sgb.charRam));
293 memcpy(gb->video.renderer->sgbMapRam, state->sgb.mapRam, sizeof(state->sgb.mapRam));
294 memcpy(gb->video.renderer->sgbPalRam, state->sgb.palRam, sizeof(state->sgb.palRam));
295 memcpy(gb->video.renderer->sgbAttributeFiles, state->sgb.atfRam, sizeof(state->sgb.atfRam));
296 memcpy(gb->video.renderer->sgbAttributes, state->sgb.attributes, sizeof(state->sgb.attributes));
297
298 GBVideoWriteSGBPacket(&gb->video, (uint8_t[16]) { (SGB_ATRC_EN << 3) | 1, 0 });
299}