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