all repos — mgba @ 668c4f68b76a5d9d4ac5345201ef162078d56601

mGBA Game Boy Advance Emulator

src/gba/gba-serialize.c (view raw)

  1#include "gba-serialize.h"
  2
  3#include "gba-io.h"
  4#include "memory.h"
  5
  6#include <fcntl.h>
  7#include <limits.h>
  8#include <stdio.h>
  9#include <string.h>
 10#include <unistd.h>
 11
 12const uint32_t GBA_SAVESTATE_MAGIC = 0x01000000;
 13
 14void GBASerialize(struct GBA* gba, struct GBASerializedState* state) {
 15	state->versionMagic = GBA_SAVESTATE_MAGIC;
 16	state->biosChecksum = gba->biosChecksum;
 17	state->id = ((struct GBACartridge*) gba->memory.rom)->id;
 18	memcpy(state->title, ((struct GBACartridge*) gba->memory.rom)->title, sizeof(state->title));
 19
 20	memcpy(state->cpu.gprs, gba->cpu.gprs, sizeof(state->cpu.gprs));
 21	state->cpu.cpsr = gba->cpu.cpsr;
 22	state->cpu.spsr = gba->cpu.spsr;
 23	state->cpu.cycles = gba->cpu.cycles;
 24	state->cpu.nextEvent = gba->cpu.nextEvent;
 25	memcpy(state->cpu.bankedRegisters, gba->cpu.bankedRegisters, 6 * 7 * sizeof(int32_t));
 26	memcpy(state->cpu.bankedSPSRs, gba->cpu.bankedSPSRs, 6 * sizeof(int32_t));
 27
 28	GBAMemorySerialize(&gba->memory, state);
 29	GBAIOSerialize(gba, state);
 30	GBAVideoSerialize(&gba->video, 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}
 61
 62static int _getStateFd(struct GBA* gba, int slot) {
 63	char path[PATH_MAX];
 64	path[PATH_MAX - 1] = '\0';
 65	snprintf(path, PATH_MAX - 1, "%s.ss%d", gba->activeFile, slot);
 66	int fd = open(path, O_CREAT | O_RDWR, 0777);
 67	if (fd >= 0) {
 68		ftruncate(fd, sizeof(struct GBASerializedState));
 69	}
 70	return fd;
 71}
 72
 73int GBASaveState(struct GBA* gba, int slot) {
 74	int fd = _getStateFd(gba, slot);
 75	if (fd < 0) {
 76		return 0;
 77	}
 78	struct GBASerializedState* state = GBAMapState(fd);
 79	GBASerialize(gba, state);
 80	GBADeallocateState(state);
 81	close(fd);
 82	return 1;
 83}
 84
 85int GBALoadState(struct GBA* gba, int slot) {
 86	int fd = _getStateFd(gba, slot);
 87	if (fd < 0) {
 88		return 0;
 89	}
 90	struct GBASerializedState* state = GBAMapState(fd);
 91	GBADeserialize(gba, state);
 92	GBADeallocateState(state);
 93	close(fd);
 94	return 1;
 95}
 96
 97struct GBASerializedState* GBAMapState(int fd) {
 98	return fileMemoryMap(fd, sizeof(struct GBASerializedState), MEMORY_WRITE);
 99}
100
101struct GBASerializedState* GBAAloocateState(void) {
102	return anonymousMemoryMap(sizeof(struct GBASerializedState));
103}
104
105void GBADeallocateState(struct GBASerializedState* state) {
106	mappedMemoryFree(state, sizeof(struct GBASerializedState));
107}