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