all repos — mgba @ 775e417cc6781ceb30520c85c968d198efb87429

mGBA Game Boy Advance Emulator

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		return;
 41	}
 42	if (state->id != ((struct GBACartridge*) gba->memory.rom)->id || memcmp(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title))) {
 43		GBALog(gba, GBA_LOG_WARN, "Savestate is for a different game");
 44		return;
 45	}
 46	memcpy(gba->cpu.gprs, state->cpu.gprs, sizeof(gba->cpu.gprs));
 47	gba->cpu.cpsr = state->cpu.cpsr;
 48	gba->cpu.spsr = state->cpu.spsr;
 49	gba->cpu.cycles = state->cpu.cycles;
 50	gba->cpu.nextEvent = state->cpu.nextEvent;
 51	memcpy(gba->cpu.bankedRegisters, state->cpu.bankedRegisters, 6 * 7 * sizeof(int32_t));
 52	memcpy(gba->cpu.bankedSPSRs, state->cpu.bankedSPSRs, 6 * sizeof(int32_t));
 53	gba->cpu.executionMode = gba->cpu.cpsr.t ? MODE_THUMB : MODE_ARM;
 54	gba->cpu.privilegeMode = gba->cpu.cpsr.priv;
 55	gba->cpu.memory->setActiveRegion(gba->cpu.memory, gba->cpu.gprs[ARM_PC]);
 56
 57	GBAMemoryDeserialize(&gba->memory, state);
 58	GBAIODeserialize(gba, state);
 59	GBAVideoDeserialize(&gba->video, state);
 60	GBAAudioDeserialize(&gba->audio, state);
 61}
 62
 63static int _getStateFd(struct GBA* gba, int slot) {
 64	char path[PATH_MAX];
 65	path[PATH_MAX - 1] = '\0';
 66	snprintf(path, PATH_MAX - 1, "%s.ss%d", gba->activeFile, slot);
 67	int fd = open(path, O_CREAT | O_RDWR, 0777);
 68	if (fd >= 0) {
 69		ftruncate(fd, sizeof(struct GBASerializedState));
 70	}
 71	return fd;
 72}
 73
 74int GBASaveState(struct GBA* gba, int slot) {
 75	int fd = _getStateFd(gba, slot);
 76	if (fd < 0) {
 77		return 0;
 78	}
 79	struct GBASerializedState* state = GBAMapState(fd);
 80	GBASerialize(gba, state);
 81	GBADeallocateState(state);
 82	close(fd);
 83	return 1;
 84}
 85
 86int GBALoadState(struct GBA* gba, int slot) {
 87	int fd = _getStateFd(gba, slot);
 88	if (fd < 0) {
 89		return 0;
 90	}
 91	struct GBASerializedState* state = GBAMapState(fd);
 92	GBADeserialize(gba, state);
 93	GBADeallocateState(state);
 94	close(fd);
 95	return 1;
 96}
 97
 98struct GBASerializedState* GBAMapState(int fd) {
 99	return fileMemoryMap(fd, sizeof(struct GBASerializedState), MEMORY_WRITE);
100}
101
102struct GBASerializedState* GBAAllocateState(void) {
103	return anonymousMemoryMap(sizeof(struct GBASerializedState));
104}
105
106void GBADeallocateState(struct GBASerializedState* state) {
107	mappedMemoryFree(state, sizeof(struct GBASerializedState));
108}
109
110void GBARecordFrame(struct GBAThread* thread) {
111	int offset = thread->rewindBufferWriteOffset;
112	struct GBASerializedState* state = thread->rewindBuffer[offset];
113	if (!state) {
114		state = GBAAllocateState();
115		thread->rewindBuffer[offset] = state;
116	}
117	GBASerialize(thread->gba, state);
118	thread->rewindBufferSize = thread->rewindBufferSize == thread->rewindBufferCapacity ? thread->rewindBufferCapacity : thread->rewindBufferSize + 1;
119	thread->rewindBufferWriteOffset = (offset + 1) % thread->rewindBufferCapacity;
120}
121
122void GBARewind(struct GBAThread* thread, int nStates) {
123	if (nStates > thread->rewindBufferSize || nStates < 0) {
124		nStates = thread->rewindBufferSize;
125	}
126	if (nStates == 0) {
127		return;
128	}
129	int offset = thread->rewindBufferWriteOffset - nStates;
130	if (offset < 0) {
131		offset += thread->rewindBufferSize;
132	}
133	struct GBASerializedState* state = thread->rewindBuffer[offset];
134	if (!state) {
135		return;
136	}
137	thread->rewindBufferSize -= nStates;
138	thread->rewindBufferWriteOffset = (offset + thread->rewindBufferCapacity - nStates) % thread->rewindBufferCapacity;
139	GBADeserialize(thread->gba, state);
140}