src/gba/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/gba/serialize.h>
7
8#include <mgba/internal/arm/macros.h>
9#include <mgba/internal/gba/bios.h>
10#include <mgba/internal/gba/io.h>
11
12#include <mgba-util/memory.h>
13#include <mgba-util/vfs.h>
14
15#include <fcntl.h>
16
17MGBA_EXPORT const uint32_t GBASavestateMagic = 0x01000000;
18MGBA_EXPORT const uint32_t GBASavestateVersion = 0x00000004;
19
20mLOG_DEFINE_CATEGORY(GBA_STATE, "GBA Savestate", "gba.serialize");
21
22struct GBABundledState {
23 struct GBASerializedState* state;
24 struct mStateExtdata* extdata;
25};
26
27void GBASerialize(struct GBA* gba, struct GBASerializedState* state) {
28 STORE_32(GBASavestateMagic + GBASavestateVersion, 0, &state->versionMagic);
29 STORE_32(gba->biosChecksum, 0, &state->biosChecksum);
30 STORE_32(gba->romCrc32, 0, &state->romCrc32);
31 STORE_32(gba->timing.masterCycles, 0, &state->masterCycles);
32 STORE_64LE(gba->timing.globalCycles, 0, &state->globalCycles);
33
34 if (gba->memory.rom) {
35 state->id = ((struct GBACartridge*) gba->memory.rom)->id;
36 memcpy(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title));
37 } else {
38 state->id = 0;
39 memset(state->title, 0, sizeof(state->title));
40 }
41
42 int i;
43 for (i = 0; i < 16; ++i) {
44 STORE_32(gba->cpu->gprs[i], i * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
45 }
46 STORE_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
47 STORE_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
48 STORE_32(gba->cpu->cycles, 0, &state->cpu.cycles);
49 STORE_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
50 for (i = 0; i < 6; ++i) {
51 int j;
52 for (j = 0; j < 7; ++j) {
53 STORE_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
54 }
55 STORE_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
56 }
57
58 STORE_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
59 STORE_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
60 STORE_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
61 STORE_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
62
63 GBASerializedMiscFlags miscFlags = 0;
64 miscFlags = GBASerializedMiscFlagsSetHalted(miscFlags, gba->cpu->halted);
65 miscFlags = GBASerializedMiscFlagsSetPOSTFLG(miscFlags, gba->memory.io[REG_POSTFLG >> 1] & 1);
66 if (mTimingIsScheduled(&gba->timing, &gba->irqEvent)) {
67 miscFlags = GBASerializedMiscFlagsFillIrqPending(miscFlags);
68 STORE_32(gba->irqEvent.when - mTimingCurrentTime(&gba->timing), 0, &state->nextIrq);
69 }
70 miscFlags = GBASerializedMiscFlagsSetBlocked(miscFlags, gba->cpuBlocked);
71 STORE_32(miscFlags, 0, &state->miscFlags);
72 STORE_32(gba->biosStall, 0, &state->biosStall);
73
74 GBAMemorySerialize(&gba->memory, state);
75 GBAIOSerialize(gba, state);
76 GBAVideoSerialize(&gba->video, state);
77 GBAAudioSerialize(&gba->audio, state);
78 GBASavedataSerialize(&gba->memory.savedata, state);
79
80 if (gba->memory.matrix.size) {
81 GBAMatrixSerialize(gba, state);
82 }
83}
84
85bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) {
86 bool error = false;
87 int32_t check;
88 uint32_t ucheck;
89 LOAD_32(ucheck, 0, &state->versionMagic);
90 if (ucheck > GBASavestateMagic + GBASavestateVersion) {
91 mLOG(GBA_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GBASavestateMagic + GBASavestateVersion, ucheck);
92 error = true;
93 } else if (ucheck < GBASavestateMagic) {
94 mLOG(GBA_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GBASavestateMagic + GBASavestateVersion, ucheck);
95 error = true;
96 } else if (ucheck < GBASavestateMagic + GBASavestateVersion) {
97 mLOG(GBA_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GBASavestateMagic + GBASavestateVersion, ucheck);
98 }
99 LOAD_32(ucheck, 0, &state->biosChecksum);
100 if (ucheck != gba->biosChecksum) {
101 mLOG(GBA_STATE, WARN, "Savestate created using a different version of the BIOS: expected %08X, got %08X", gba->biosChecksum, ucheck);
102 uint32_t pc;
103 LOAD_32(pc, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
104 if ((ucheck == GBA_BIOS_CHECKSUM || gba->biosChecksum == GBA_BIOS_CHECKSUM) && pc < SIZE_BIOS && pc >= 0x20) {
105 error = true;
106 }
107 }
108 if (gba->memory.rom && (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title)))) {
109 mLOG(GBA_STATE, WARN, "Savestate is for a different game");
110 error = true;
111 } else if (!gba->memory.rom && state->id != 0) {
112 mLOG(GBA_STATE, WARN, "Savestate is for a game, but no game loaded");
113 error = true;
114 }
115 LOAD_32(ucheck, 0, &state->romCrc32);
116 if (ucheck != gba->romCrc32) {
117 mLOG(GBA_STATE, WARN, "Savestate is for a different version of the game");
118 }
119 LOAD_32(check, 0, &state->cpu.cycles);
120 if (check < 0) {
121 mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are negative");
122 error = true;
123 }
124 if (check >= (int32_t) GBA_ARM7TDMI_FREQUENCY) {
125 mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are too high");
126 error = true;
127 }
128 LOAD_32(check, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
129 int region = (check >> BASE_OFFSET);
130 if ((region == REGION_CART0 || region == REGION_CART1 || region == REGION_CART2) && ((check - WORD_SIZE_ARM) & SIZE_CART0) >= gba->memory.romSize - WORD_SIZE_ARM) {
131 mLOG(GBA_STATE, WARN, "Savestate created using a differently sized version of the ROM");
132 error = true;
133 }
134 if (error) {
135 return false;
136 }
137 mTimingClear(&gba->timing);
138 LOAD_32(gba->timing.masterCycles, 0, &state->masterCycles);
139 LOAD_64LE(gba->timing.globalCycles, 0, &state->globalCycles);
140
141 size_t i;
142 for (i = 0; i < 16; ++i) {
143 LOAD_32(gba->cpu->gprs[i], i * sizeof(gba->cpu->gprs[0]), state->cpu.gprs);
144 }
145 LOAD_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
146 LOAD_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
147 LOAD_32(gba->cpu->cycles, 0, &state->cpu.cycles);
148 LOAD_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
149 for (i = 0; i < 6; ++i) {
150 int j;
151 for (j = 0; j < 7; ++j) {
152 LOAD_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
153 }
154 LOAD_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
155 }
156 gba->cpu->privilegeMode = gba->cpu->cpsr.priv;
157 if (gba->cpu->gprs[ARM_PC] & 1) {
158 mLOG(GBA_STATE, WARN, "Savestate has unaligned PC and is probably corrupted");
159 gba->cpu->gprs[ARM_PC] &= ~1;
160 }
161 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
162 if (state->biosPrefetch) {
163 LOAD_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
164 }
165 LOAD_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
166 if (gba->cpu->cpsr.t) {
167 gba->cpu->executionMode = MODE_THUMB;
168 if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
169 LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
170 LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
171 gba->cpu->prefetch[0] &= 0xFFFF;
172 gba->cpu->prefetch[1] &= 0xFFFF;
173 } else {
174 // Maintain backwards compat
175 LOAD_16(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_THUMB) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
176 LOAD_16(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
177 }
178 } else {
179 gba->cpu->executionMode = MODE_ARM;
180 if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
181 LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
182 LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
183 } else {
184 // Maintain backwards compat
185 LOAD_32(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
186 LOAD_32(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
187 }
188 }
189 GBASerializedMiscFlags miscFlags = 0;
190 LOAD_32(miscFlags, 0, &state->miscFlags);
191 gba->cpu->halted = GBASerializedMiscFlagsGetHalted(miscFlags);
192 gba->memory.io[REG_POSTFLG >> 1] = GBASerializedMiscFlagsGetPOSTFLG(miscFlags);
193 if (GBASerializedMiscFlagsIsIrqPending(miscFlags)) {
194 int32_t when;
195 LOAD_32(when, 0, &state->nextIrq);
196 mTimingSchedule(&gba->timing, &gba->irqEvent, when);
197 }
198 gba->cpuBlocked = GBASerializedMiscFlagsGetBlocked(miscFlags);
199 LOAD_32(gba->biosStall, 0, &state->biosStall);
200
201 GBAVideoDeserialize(&gba->video, state);
202 GBAMemoryDeserialize(&gba->memory, state);
203 GBAIODeserialize(gba, state);
204 GBAAudioDeserialize(&gba->audio, state);
205 GBASavedataDeserialize(&gba->memory.savedata, state);
206
207 if (gba->memory.matrix.size) {
208 GBAMatrixDeserialize(gba, state);
209 }
210
211 gba->timing.reroot = gba->timing.root;
212 gba->timing.root = NULL;
213
214 return true;
215}