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
11#include <mgba-util/memory.h>
12#include <mgba-util/vfs.h>
13
14#include <fcntl.h>
15
16const uint32_t GBA_SAVESTATE_MAGIC = 0x01000000;
17const uint32_t GBA_SAVESTATE_VERSION = 0x00000003;
18
19mLOG_DEFINE_CATEGORY(GBA_STATE, "GBA Savestate", "gba.serialize");
20
21struct GBABundledState {
22 struct GBASerializedState* state;
23 struct mStateExtdata* extdata;
24};
25
26void GBASerialize(struct GBA* gba, struct GBASerializedState* state) {
27 STORE_32(GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, 0, &state->versionMagic);
28 STORE_32(gba->biosChecksum, 0, &state->biosChecksum);
29 STORE_32(gba->romCrc32, 0, &state->romCrc32);
30 STORE_32(gba->timing.masterCycles, 0, &state->masterCycles);
31 STORE_64LE(gba->timing.globalCycles, 0, &state->globalCycles);
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
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 mTimingClear(&gba->timing);
131 LOAD_32(gba->timing.masterCycles, 0, &state->masterCycles);
132 LOAD_64LE(gba->timing.globalCycles, 0, &state->globalCycles);
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 if (GBASerializedMiscFlagsIsIrqPending(miscFlags)) {
183 int32_t when;
184 LOAD_32(when, 0, &state->nextIrq);
185 mTimingSchedule(&gba->timing, &gba->irqEvent, when);
186 }
187
188 GBAVideoDeserialize(&gba->video, state);
189 GBAMemoryDeserialize(&gba->memory, state);
190 GBAIODeserialize(gba, state);
191 GBAAudioDeserialize(&gba->audio, state);
192 GBASavedataDeserialize(&gba->memory.savedata, state);
193
194 gba->timing.reroot = gba->timing.root;
195 gba->timing.root = NULL;
196
197 return true;
198}