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 "memory.h"
6
7#include <fcntl.h>
8#include <limits.h>
9#include <stdio.h>
10#include <string.h>
11#include <unistd.h>
12
13const uint32_t GBA_SAVESTATE_MAGIC = 0x01000000;
14
15void GBASerialize(struct GBA* gba, struct GBASerializedState* state) {
16 state->versionMagic = GBA_SAVESTATE_MAGIC;
17 state->biosChecksum = gba->biosChecksum;
18 state->id = ((struct GBACartridge*) gba->memory.rom)->id;
19 memcpy(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title));
20
21 memcpy(state->cpu.gprs, gba->cpu.gprs, sizeof(state->cpu.gprs));
22 state->cpu.cpsr = gba->cpu.cpsr;
23 state->cpu.spsr = gba->cpu.spsr;
24 state->cpu.cycles = gba->cpu.cycles;
25 state->cpu.nextEvent = gba->cpu.nextEvent;
26 memcpy(state->cpu.bankedRegisters, gba->cpu.bankedRegisters, 6 * 7 * sizeof(int32_t));
27 memcpy(state->cpu.bankedSPSRs, gba->cpu.bankedSPSRs, 6 * sizeof(int32_t));
28
29 GBAMemorySerialize(&gba->memory, state);
30 GBAIOSerialize(gba, state);
31 GBAVideoSerialize(&gba->video, state);
32 GBAAudioSerialize(&gba->audio, state);
33}
34
35void GBADeserialize(struct GBA* gba, struct GBASerializedState* state) {
36 if (state->versionMagic != GBA_SAVESTATE_MAGIC) {
37 GBALog(gba, GBA_LOG_WARN, "Invalid or too new savestate");
38 return;
39 }
40 if (state->biosChecksum != gba->biosChecksum) {
41 GBALog(gba, GBA_LOG_WARN, "Savestate created using a different version of the BIOS");
42 return;
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.memory, 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
76int GBASaveState(struct GBA* gba, int slot) {
77 int fd = _getStateFd(gba, slot);
78 if (fd < 0) {
79 return 0;
80 }
81 struct GBASerializedState* state = GBAMapState(fd);
82 GBASerialize(gba, state);
83 GBADeallocateState(state);
84 close(fd);
85 return 1;
86}
87
88int GBALoadState(struct GBA* gba, int slot) {
89 int fd = _getStateFd(gba, slot);
90 if (fd < 0) {
91 return 0;
92 }
93 struct GBASerializedState* state = GBAMapState(fd);
94 GBADeserialize(gba, state);
95 GBADeallocateState(state);
96 close(fd);
97 return 1;
98}
99
100struct GBASerializedState* GBAMapState(int fd) {
101 return fileMemoryMap(fd, sizeof(struct GBASerializedState), MEMORY_WRITE);
102}
103
104struct GBASerializedState* GBAAloocateState(void) {
105 return anonymousMemoryMap(sizeof(struct GBASerializedState));
106}
107
108void GBADeallocateState(struct GBASerializedState* state) {
109 mappedMemoryFree(state, sizeof(struct GBASerializedState));
110}