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#ifdef _MSC_VER
17#include <time.h>
18#else
19#include <sys/time.h>
20#endif
21
22const uint32_t GBA_SAVESTATE_MAGIC = 0x01000000;
23const uint32_t GBA_SAVESTATE_VERSION = 0x00000002;
24
25mLOG_DEFINE_CATEGORY(GBA_STATE, "GBA Savestate");
26
27struct GBABundledState {
28 struct GBASerializedState* state;
29 struct mStateExtdata* extdata;
30};
31
32void GBASerialize(struct GBA* gba, struct GBASerializedState* state) {
33 STORE_32(GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, 0, &state->versionMagic);
34 STORE_32(gba->biosChecksum, 0, &state->biosChecksum);
35 STORE_32(gba->romCrc32, 0, &state->romCrc32);
36 STORE_32(gba->timing.masterCycles, 0, &state->masterCycles);
37
38 if (gba->memory.rom) {
39 state->id = ((struct GBACartridge*) gba->memory.rom)->id;
40 memcpy(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title));
41 } else {
42 state->id = 0;
43 memset(state->title, 0, sizeof(state->title));
44 }
45
46 int i;
47 for (i = 0; i < 16; ++i) {
48 STORE_32(gba->cpu->gprs[i], i * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
49 }
50 STORE_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
51 STORE_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
52 STORE_32(gba->cpu->cycles, 0, &state->cpu.cycles);
53 STORE_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
54 for (i = 0; i < 6; ++i) {
55 int j;
56 for (j = 0; j < 7; ++j) {
57 STORE_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
58 }
59 STORE_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
60 }
61
62 STORE_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
63 STORE_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
64 STORE_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
65 STORE_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
66
67 GBASerializedMiscFlags miscFlags = 0;
68 miscFlags = GBASerializedMiscFlagsSetHalted(miscFlags, gba->cpu->halted);
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
78#ifndef _MSC_VER
79 struct timeval tv;
80 if (!gettimeofday(&tv, 0)) {
81 uint64_t usec = tv.tv_usec;
82 usec += tv.tv_sec * 1000000LL;
83 STORE_64(usec, 0, &state->creationUsec);
84 }
85#else
86 struct timespec ts;
87 if (timespec_get(&ts, TIME_UTC)) {
88 uint64_t usec = ts.tv_nsec / 1000;
89 usec += ts.tv_sec * 1000000LL;
90 STORE_64(usec, 0, &state->creationUsec);
91 }
92#endif
93 else {
94 state->creationUsec = 0;
95 }
96 state->associatedStreamId = 0;
97 if (gba->rr) {
98 gba->rr->stateSaved(gba->rr, state);
99 }
100}
101
102bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state) {
103 bool error = false;
104 int32_t check;
105 uint32_t ucheck;
106 LOAD_32(ucheck, 0, &state->versionMagic);
107 if (ucheck > GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) {
108 mLOG(GBA_STATE, WARN, "Invalid or too new savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
109 error = true;
110 } else if (ucheck < GBA_SAVESTATE_MAGIC) {
111 mLOG(GBA_STATE, WARN, "Invalid savestate: expected %08X, got %08X", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
112 error = true;
113 } else if (ucheck < GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION) {
114 mLOG(GBA_STATE, WARN, "Old savestate: expected %08X, got %08X, continuing anyway", GBA_SAVESTATE_MAGIC + GBA_SAVESTATE_VERSION, ucheck);
115 }
116 LOAD_32(ucheck, 0, &state->biosChecksum);
117 if (ucheck != gba->biosChecksum) {
118 mLOG(GBA_STATE, WARN, "Savestate created using a different version of the BIOS: expected %08X, got %08X", gba->biosChecksum, ucheck);
119 uint32_t pc;
120 LOAD_32(pc, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
121 if (pc < SIZE_BIOS && pc >= 0x20) {
122 error = true;
123 }
124 }
125 if (gba->memory.rom && (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title)))) {
126 mLOG(GBA_STATE, WARN, "Savestate is for a different game");
127 error = true;
128 } else if (!gba->memory.rom && state->id != 0) {
129 mLOG(GBA_STATE, WARN, "Savestate is for a game, but no game loaded");
130 error = true;
131 }
132 LOAD_32(ucheck, 0, &state->romCrc32);
133 if (ucheck != gba->romCrc32) {
134 mLOG(GBA_STATE, WARN, "Savestate is for a different version of the game");
135 }
136 LOAD_32(check, 0, &state->cpu.cycles);
137 if (check < 0) {
138 mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are negative");
139 error = true;
140 }
141 if (check >= (int32_t) GBA_ARM7TDMI_FREQUENCY) {
142 mLOG(GBA_STATE, WARN, "Savestate is corrupted: CPU cycles are too high");
143 error = true;
144 }
145 LOAD_32(check, ARM_PC * sizeof(state->cpu.gprs[0]), state->cpu.gprs);
146 int region = (check >> BASE_OFFSET);
147 if ((region == REGION_CART0 || region == REGION_CART1 || region == REGION_CART2) && ((check - WORD_SIZE_ARM) & SIZE_CART0) >= gba->memory.romSize - WORD_SIZE_ARM) {
148 mLOG(GBA_STATE, WARN, "Savestate created using a differently sized version of the ROM");
149 error = true;
150 }
151 if (error) {
152 return false;
153 }
154 LOAD_32(gba->timing.masterCycles, 0, &state->masterCycles);
155 size_t i;
156 for (i = 0; i < 16; ++i) {
157 LOAD_32(gba->cpu->gprs[i], i * sizeof(gba->cpu->gprs[0]), state->cpu.gprs);
158 }
159 LOAD_32(gba->cpu->cpsr.packed, 0, &state->cpu.cpsr.packed);
160 LOAD_32(gba->cpu->spsr.packed, 0, &state->cpu.spsr.packed);
161 LOAD_32(gba->cpu->cycles, 0, &state->cpu.cycles);
162 LOAD_32(gba->cpu->nextEvent, 0, &state->cpu.nextEvent);
163 for (i = 0; i < 6; ++i) {
164 int j;
165 for (j = 0; j < 7; ++j) {
166 LOAD_32(gba->cpu->bankedRegisters[i][j], (i * 7 + j) * sizeof(gba->cpu->bankedRegisters[0][0]), state->cpu.bankedRegisters);
167 }
168 LOAD_32(gba->cpu->bankedSPSRs[i], i * sizeof(gba->cpu->bankedSPSRs[0]), state->cpu.bankedSPSRs);
169 }
170 gba->cpu->privilegeMode = gba->cpu->cpsr.priv;
171 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
172 if (state->biosPrefetch) {
173 LOAD_32(gba->memory.biosPrefetch, 0, &state->biosPrefetch);
174 }
175 LOAD_32(gba->memory.lastPrefetchedPc, 0, &state->lastPrefetchedPc);
176 if (gba->cpu->cpsr.t) {
177 gba->cpu->executionMode = MODE_THUMB;
178 if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
179 LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
180 LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
181 gba->cpu->prefetch[0] &= 0xFFFF;
182 gba->cpu->prefetch[1] &= 0xFFFF;
183 } else {
184 // Maintain backwards compat
185 LOAD_16(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_THUMB) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
186 LOAD_16(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
187 }
188 } else {
189 gba->cpu->executionMode = MODE_ARM;
190 if (state->cpuPrefetch[0] && state->cpuPrefetch[1]) {
191 LOAD_32(gba->cpu->prefetch[0], 0, state->cpuPrefetch);
192 LOAD_32(gba->cpu->prefetch[1], 4, state->cpuPrefetch);
193 } else {
194 // Maintain backwards compat
195 LOAD_32(gba->cpu->prefetch[0], (gba->cpu->gprs[ARM_PC] - WORD_SIZE_ARM) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
196 LOAD_32(gba->cpu->prefetch[1], (gba->cpu->gprs[ARM_PC]) & gba->cpu->memory.activeMask, gba->cpu->memory.activeRegion);
197 }
198 }
199 GBASerializedMiscFlags miscFlags = 0;
200 LOAD_32(miscFlags, 0, &state->miscFlags);
201 gba->cpu->halted = GBASerializedMiscFlagsGetHalted(miscFlags);
202
203 GBAVideoDeserialize(&gba->video, state);
204 GBAMemoryDeserialize(&gba->memory, state);
205 GBAIODeserialize(gba, state);
206 GBAAudioDeserialize(&gba->audio, state);
207 GBASavedataDeserialize(&gba->memory.savedata, state);
208
209 if (gba->rr) {
210 gba->rr->stateLoaded(gba->rr, state);
211 }
212 return true;
213}
214
215struct GBASerializedState* GBAAllocateState(void) {
216 return anonymousMemoryMap(sizeof(struct GBASerializedState));
217}
218
219void GBADeallocateState(struct GBASerializedState* state) {
220 mappedMemoryFree(state, sizeof(struct GBASerializedState));
221}