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 uint32_t pixels[VIDEO_HORIZONTAL_PIXELS * VIDEO_VERTICAL_PIXELS * 4];
134
135 PNGInstallChunkHandler(png, gba, _loadPNGChunkHandler, "gbAs");
136 PNGReadHeader(png, info);
137 PNGReadPixels(png, info, &pixels, VIDEO_HORIZONTAL_PIXELS, VIDEO_VERTICAL_PIXELS, VIDEO_HORIZONTAL_PIXELS);
138 PNGReadFooter(png, end);
139 PNGReadClose(png, info, end);
140 gba->video.renderer->putPixels(gba->video.renderer, VIDEO_HORIZONTAL_PIXELS, pixels);
141 GBASyncPostFrame(gba->sync);
142 return true;
143}
144
145bool GBASaveState(struct GBA* gba, struct VDir* dir, int slot, bool screenshot) {
146 struct VFile* vf = _getStateVf(gba, dir, slot, true);
147 if (!vf) {
148 return false;
149 }
150 bool success = true;
151 if (!screenshot) {
152 vf->truncate(vf, sizeof(struct GBASerializedState));
153 struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_WRITE);
154 GBASerialize(gba, state);
155 vf->unmap(vf, state, sizeof(struct GBASerializedState));
156 } else {
157 _savePNGState(gba, vf);
158 }
159 vf->close(vf);
160 return success;
161}
162
163bool GBALoadState(struct GBA* gba, struct VDir* dir, int slot) {
164 struct VFile* vf = _getStateVf(gba, dir, slot, false);
165 if (!vf) {
166 return false;
167 }
168 if (!isPNG(vf)) {
169 struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_READ);
170 GBADeserialize(gba, state);
171 vf->unmap(vf, state, sizeof(struct GBASerializedState));
172 } else {
173 _loadPNGState(gba, vf);
174 }
175 vf->close(vf);
176 return true;
177}
178
179struct GBASerializedState* GBAAllocateState(void) {
180 return anonymousMemoryMap(sizeof(struct GBASerializedState));
181}
182
183void GBADeallocateState(struct GBASerializedState* state) {
184 mappedMemoryFree(state, sizeof(struct GBASerializedState));
185}
186
187void GBARecordFrame(struct GBAThread* thread) {
188 int offset = thread->rewindBufferWriteOffset;
189 struct GBASerializedState* state = thread->rewindBuffer[offset];
190 if (!state) {
191 state = GBAAllocateState();
192 thread->rewindBuffer[offset] = state;
193 }
194 GBASerialize(thread->gba, state);
195 thread->rewindBufferSize = thread->rewindBufferSize == thread->rewindBufferCapacity ? thread->rewindBufferCapacity : thread->rewindBufferSize + 1;
196 thread->rewindBufferWriteOffset = (offset + 1) % thread->rewindBufferCapacity;
197}
198
199void GBARewind(struct GBAThread* thread, int nStates) {
200 if (nStates > thread->rewindBufferSize || nStates < 0) {
201 nStates = thread->rewindBufferSize;
202 }
203 if (nStates == 0) {
204 return;
205 }
206 int offset = thread->rewindBufferWriteOffset - nStates;
207 if (offset < 0) {
208 offset += thread->rewindBufferSize;
209 }
210 struct GBASerializedState* state = thread->rewindBuffer[offset];
211 if (!state) {
212 return;
213 }
214 thread->rewindBufferSize -= nStates;
215 thread->rewindBufferWriteOffset = (offset + thread->rewindBufferCapacity - nStates) % thread->rewindBufferCapacity;
216 GBADeserialize(thread->gba, state);
217}