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