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