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