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