src/gba/gba-serialize.c (view raw)
1#include "gba-serialize.h"
2
3#include "gba-audio.h"
4#include "gba-io.h"
5#include "gba-thread.h"
6#include "gba-video.h"
7
8#include "util/memory.h"
9#include "util/png-io.h"
10#include "util/vfs.h"
11
12#include <fcntl.h>
13#include <png.h>
14#include <zlib.h>
15
16const uint32_t GBA_SAVESTATE_MAGIC = 0x01000000;
17
18void GBASerialize(struct GBA* gba, struct GBASerializedState* state) {
19 state->versionMagic = GBA_SAVESTATE_MAGIC;
20 state->biosChecksum = gba->biosChecksum;
21 state->romCrc32 = gba->romCrc32;
22
23 state->id = ((struct GBACartridge*) gba->memory.rom)->id;
24 memcpy(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title));
25
26 memcpy(state->cpu.gprs, gba->cpu->gprs, sizeof(state->cpu.gprs));
27 state->cpu.cpsr = gba->cpu->cpsr;
28 state->cpu.spsr = gba->cpu->spsr;
29 state->cpu.cycles = gba->cpu->cycles;
30 state->cpu.nextEvent = gba->cpu->nextEvent;
31 memcpy(state->cpu.bankedRegisters, gba->cpu->bankedRegisters, 6 * 7 * sizeof(int32_t));
32 memcpy(state->cpu.bankedSPSRs, gba->cpu->bankedSPSRs, 6 * sizeof(int32_t));
33
34 GBAMemorySerialize(&gba->memory, state);
35 GBAIOSerialize(gba, state);
36 GBAVideoSerialize(&gba->video, state);
37 GBAAudioSerialize(&gba->audio, state);
38}
39
40void GBADeserialize(struct GBA* gba, struct GBASerializedState* state) {
41 if (state->versionMagic != GBA_SAVESTATE_MAGIC) {
42 GBALog(gba, GBA_LOG_WARN, "Invalid or too new savestate");
43 return;
44 }
45 if (state->biosChecksum != gba->biosChecksum) {
46 GBALog(gba, GBA_LOG_WARN, "Savestate created using a different version of the BIOS");
47 if (state->cpu.gprs[ARM_PC] < SIZE_BIOS && state->cpu.gprs[ARM_PC] >= 0x20) {
48 return;
49 }
50 }
51 if (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title))) {
52 GBALog(gba, GBA_LOG_WARN, "Savestate is for a different game");
53 return;
54 }
55 if (state->romCrc32 != gba->romCrc32) {
56 GBALog(gba, GBA_LOG_WARN, "Savestate is for a different version of the game");
57 }
58 memcpy(gba->cpu->gprs, state->cpu.gprs, sizeof(gba->cpu->gprs));
59 gba->cpu->cpsr = state->cpu.cpsr;
60 gba->cpu->spsr = state->cpu.spsr;
61 gba->cpu->cycles = state->cpu.cycles;
62 gba->cpu->nextEvent = state->cpu.nextEvent;
63 memcpy(gba->cpu->bankedRegisters, state->cpu.bankedRegisters, 6 * 7 * sizeof(int32_t));
64 memcpy(gba->cpu->bankedSPSRs, state->cpu.bankedSPSRs, 6 * sizeof(int32_t));
65 gba->cpu->executionMode = gba->cpu->cpsr.t ? MODE_THUMB : MODE_ARM;
66 gba->cpu->privilegeMode = gba->cpu->cpsr.priv;
67 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
68
69 GBAMemoryDeserialize(&gba->memory, state);
70 GBAIODeserialize(gba, state);
71 GBAVideoDeserialize(&gba->video, state);
72 GBAAudioDeserialize(&gba->audio, state);
73}
74
75static struct VFile* _getStateVf(struct GBA* gba, struct VDir* dir, int slot, bool write) {
76 char path[PATH_MAX];
77 path[PATH_MAX - 1] = '\0';
78 struct VFile* vf;
79 if (!dir) {
80 snprintf(path, PATH_MAX - 1, "%s.ss%d", gba->activeFile, slot);
81 vf = VFileOpen(path, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
82 } else {
83 snprintf(path, PATH_MAX - 1, "savestate.ss%d", slot);
84 vf = dir->openFile(dir, path, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
85 }
86 return vf;
87}
88
89static bool _savePNGState(struct GBA* gba, struct VFile* vf) {
90 unsigned stride;
91 void* pixels = 0;
92 gba->video.renderer->getPixels(gba->video.renderer, &stride, &pixels);
93 if (!pixels) {
94 return false;
95 }
96
97 struct GBASerializedState* state = GBAAllocateState();
98 png_structp png = PNGWriteOpen(vf);
99 png_infop info = PNGWriteHeader(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS);
100 uLongf len = compressBound(sizeof(*state));
101 void* buffer = malloc(len);
102 if (state && png && info && buffer) {
103 GBASerialize(gba, state);
104 compress(buffer, &len, (const Bytef*) state, sizeof(*state));
105 PNGWritePixels(png, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, stride, pixels);
106 PNGWriteCustomChunk(png, "gbAs", len, buffer);
107 }
108 PNGWriteClose(png, info);
109 free(buffer);
110 GBADeallocateState(state);
111 return state && png && info && buffer;
112}
113
114static int _loadPNGChunkHandler(png_structp png, png_unknown_chunkp chunk) {
115 if (strcmp((const char*) chunk->name, "gbAs") != 0) {
116 return 0;
117 }
118 struct GBASerializedState state;
119 uLongf len = sizeof(state);
120 uncompress((Bytef*) &state, &len, chunk->data, chunk->size);
121 GBADeserialize(png_get_user_chunk_ptr(png), &state);
122 return 1;
123}
124
125static bool _loadPNGState(struct GBA* gba, struct VFile* vf) {
126 png_structp png = PNGReadOpen(vf, PNG_HEADER_BYTES);
127 png_infop info = png_create_info_struct(png);
128 png_infop end = png_create_info_struct(png);
129 if (!png || !info || !end) {
130 PNGReadClose(png, info, end);
131 return false;
132 }
133 PNGInstallChunkHandler(png, gba, _loadPNGChunkHandler, "gbAs");
134 PNGReadHeader(png, info);
135 PNGIgnorePixels(png, info);
136 PNGReadFooter(png, end);
137 PNGReadClose(png, info, end);
138 return true;
139}
140
141bool GBASaveState(struct GBA* gba, struct VDir* dir, int slot, bool screenshot) {
142 struct VFile* vf = _getStateVf(gba, dir, slot, true);
143 if (!vf) {
144 return false;
145 }
146 bool success = true;
147 if (!screenshot) {
148 vf->truncate(vf, sizeof(struct GBASerializedState));
149 struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_WRITE);
150 GBASerialize(gba, state);
151 vf->unmap(vf, state, sizeof(struct GBASerializedState));
152 } else {
153 _savePNGState(gba, vf);
154 }
155 vf->close(vf);
156 return success;
157}
158
159bool GBALoadState(struct GBA* gba, struct VDir* dir, int slot) {
160 struct VFile* vf = _getStateVf(gba, dir, slot, false);
161 if (!vf) {
162 return false;
163 }
164 if (!isPNG(vf)) {
165 struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_READ);
166 GBADeserialize(gba, state);
167 vf->unmap(vf, state, sizeof(struct GBASerializedState));
168 } else {
169 _loadPNGState(gba, vf);
170 }
171 vf->close(vf);
172 return true;
173}
174
175struct GBASerializedState* GBAAllocateState(void) {
176 return anonymousMemoryMap(sizeof(struct GBASerializedState));
177}
178
179void GBADeallocateState(struct GBASerializedState* state) {
180 mappedMemoryFree(state, sizeof(struct GBASerializedState));
181}
182
183void GBARecordFrame(struct GBAThread* thread) {
184 int offset = thread->rewindBufferWriteOffset;
185 struct GBASerializedState* state = thread->rewindBuffer[offset];
186 if (!state) {
187 state = GBAAllocateState();
188 thread->rewindBuffer[offset] = state;
189 }
190 GBASerialize(thread->gba, state);
191 thread->rewindBufferSize = thread->rewindBufferSize == thread->rewindBufferCapacity ? thread->rewindBufferCapacity : thread->rewindBufferSize + 1;
192 thread->rewindBufferWriteOffset = (offset + 1) % thread->rewindBufferCapacity;
193}
194
195void GBARewind(struct GBAThread* thread, int nStates) {
196 if (nStates > thread->rewindBufferSize || nStates < 0) {
197 nStates = thread->rewindBufferSize;
198 }
199 if (nStates == 0) {
200 return;
201 }
202 int offset = thread->rewindBufferWriteOffset - nStates;
203 if (offset < 0) {
204 offset += thread->rewindBufferSize;
205 }
206 struct GBASerializedState* state = thread->rewindBuffer[offset];
207 if (!state) {
208 return;
209 }
210 thread->rewindBufferSize -= nStates;
211 thread->rewindBufferWriteOffset = (offset + thread->rewindBufferCapacity - nStates) % thread->rewindBufferCapacity;
212 GBADeserialize(thread->gba, state);
213}