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