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