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
7#include "util/memory.h"
8
9#include <fcntl.h>
10
11const uint32_t GBA_SAVESTATE_MAGIC = 0x01000000;
12
13void GBASerialize(struct GBA* gba, struct GBASerializedState* state) {
14 state->versionMagic = GBA_SAVESTATE_MAGIC;
15 state->biosChecksum = gba->biosChecksum;
16 state->id = ((struct GBACartridge*) gba->memory.rom)->id;
17 memcpy(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title));
18
19 memcpy(state->cpu.gprs, gba->cpu->gprs, sizeof(state->cpu.gprs));
20 state->cpu.cpsr = gba->cpu->cpsr;
21 state->cpu.spsr = gba->cpu->spsr;
22 state->cpu.cycles = gba->cpu->cycles;
23 state->cpu.nextEvent = gba->cpu->nextEvent;
24 memcpy(state->cpu.bankedRegisters, gba->cpu->bankedRegisters, 6 * 7 * sizeof(int32_t));
25 memcpy(state->cpu.bankedSPSRs, gba->cpu->bankedSPSRs, 6 * sizeof(int32_t));
26
27 GBAMemorySerialize(&gba->memory, state);
28 GBAIOSerialize(gba, state);
29 GBAVideoSerialize(&gba->video, state);
30 GBAAudioSerialize(&gba->audio, state);
31}
32
33void GBADeserialize(struct GBA* gba, struct GBASerializedState* state) {
34 if (state->versionMagic != GBA_SAVESTATE_MAGIC) {
35 GBALog(gba, GBA_LOG_WARN, "Invalid or too new savestate");
36 return;
37 }
38 if (state->biosChecksum != gba->biosChecksum) {
39 GBALog(gba, GBA_LOG_WARN, "Savestate created using a different version of the BIOS");
40 if (state->cpu.gprs[ARM_PC] < SIZE_BIOS && state->cpu.gprs[ARM_PC] >= 0x20) {
41 return;
42 }
43 }
44 if (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title))) {
45 GBALog(gba, GBA_LOG_WARN, "Savestate is for a different game");
46 return;
47 }
48 memcpy(gba->cpu->gprs, state->cpu.gprs, sizeof(gba->cpu->gprs));
49 gba->cpu->cpsr = state->cpu.cpsr;
50 gba->cpu->spsr = state->cpu.spsr;
51 gba->cpu->cycles = state->cpu.cycles;
52 gba->cpu->nextEvent = state->cpu.nextEvent;
53 memcpy(gba->cpu->bankedRegisters, state->cpu.bankedRegisters, 6 * 7 * sizeof(int32_t));
54 memcpy(gba->cpu->bankedSPSRs, state->cpu.bankedSPSRs, 6 * sizeof(int32_t));
55 gba->cpu->executionMode = gba->cpu->cpsr.t ? MODE_THUMB : MODE_ARM;
56 gba->cpu->privilegeMode = gba->cpu->cpsr.priv;
57 gba->cpu->memory.setActiveRegion(gba->cpu, gba->cpu->gprs[ARM_PC]);
58
59 GBAMemoryDeserialize(&gba->memory, state);
60 GBAIODeserialize(gba, state);
61 GBAVideoDeserialize(&gba->video, state);
62 GBAAudioDeserialize(&gba->audio, state);
63}
64
65static int _getStateFd(struct GBA* gba, int slot) {
66 char path[PATH_MAX];
67 path[PATH_MAX - 1] = '\0';
68 snprintf(path, PATH_MAX - 1, "%s.ss%d", gba->activeFile, slot);
69 int fd = open(path, O_CREAT | O_RDWR, 0777);
70 if (fd >= 0) {
71 ftruncate(fd, sizeof(struct GBASerializedState));
72 }
73 return fd;
74}
75
76bool GBASaveState(struct GBA* gba, int slot) {
77 int fd = _getStateFd(gba, slot);
78 if (fd < 0) {
79 return false;
80 }
81 struct GBASerializedState* state = GBAMapState(fd);
82 GBASerialize(gba, state);
83 GBADeallocateState(state);
84 close(fd);
85 return true;
86}
87
88bool GBALoadState(struct GBA* gba, int slot) {
89 int fd = _getStateFd(gba, slot);
90 if (fd < 0) {
91 return false;
92 }
93 struct GBASerializedState* state = GBAMapState(fd);
94 GBADeserialize(gba, state);
95 GBADeallocateState(state);
96 close(fd);
97 return true;
98}
99
100struct GBASerializedState* GBAMapState(int fd) {
101 return fileMemoryMap(fd, sizeof(struct GBASerializedState), MEMORY_WRITE);
102}
103
104struct GBASerializedState* GBAAllocateState(void) {
105 return anonymousMemoryMap(sizeof(struct GBASerializedState));
106}
107
108void GBADeallocateState(struct GBASerializedState* state) {
109 mappedMemoryFree(state, sizeof(struct GBASerializedState));
110}
111
112void GBARecordFrame(struct GBAThread* thread) {
113 int offset = thread->rewindBufferWriteOffset;
114 struct GBASerializedState* state = thread->rewindBuffer[offset];
115 if (!state) {
116 state = GBAAllocateState();
117 thread->rewindBuffer[offset] = state;
118 }
119 GBASerialize(thread->gba, state);
120 thread->rewindBufferSize = thread->rewindBufferSize == thread->rewindBufferCapacity ? thread->rewindBufferCapacity : thread->rewindBufferSize + 1;
121 thread->rewindBufferWriteOffset = (offset + 1) % thread->rewindBufferCapacity;
122}
123
124void GBARewind(struct GBAThread* thread, int nStates) {
125 if (nStates > thread->rewindBufferSize || nStates < 0) {
126 nStates = thread->rewindBufferSize;
127 }
128 if (nStates == 0) {
129 return;
130 }
131 int offset = thread->rewindBufferWriteOffset - nStates;
132 if (offset < 0) {
133 offset += thread->rewindBufferSize;
134 }
135 struct GBASerializedState* state = thread->rewindBuffer[offset];
136 if (!state) {
137 return;
138 }
139 thread->rewindBufferSize -= nStates;
140 thread->rewindBufferWriteOffset = (offset + thread->rewindBufferCapacity - nStates) % thread->rewindBufferCapacity;
141 GBADeserialize(thread->gba, state);
142}