all repos — mgba @ 4e98546cd655405a88fc8670ba0a37d21455b8fc

mGBA Game Boy Advance Emulator

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

  1#ifndef GBA_SERIALIZE_H
  2#define GBA_SERIALIZE_H
  3
  4#include "gba.h"
  5
  6const uint32_t GBA_SAVESTATE_MAGIC;
  7
  8/* Savestate format:
  9 * 0x00000 - 0x00003: Version Magic (0x01000000)
 10 * 0x00004 - 0x00007: BIOS checksum (e.g. 0xBAAE187F for official BIOS)
 11 * 0x00008 - 0x0000F: Reserved (leave zero)
 12 * 0x00010 - 0x0001B: Game title (e.g. METROID4USA)
 13 * 0x0001C - 0x0001F: Game code (e.g. AMTE)
 14 * 0x00020 - 0x0012F: CPU state:
 15 * | 0x00020 - 0x0005F: GPRs
 16 * | 0x00060 - 0x00063: CPSR
 17 * | 0x00064 - 0x00067: SPSR
 18 * | 0x00068 - 0x0006B: Cycles since last event
 19 * | 0x0006C - 0x0006F: Cycles until next event
 20 * | 0x00070 - 0x00117: Banked registers
 21 * | 0x00118 - 0x0012F: Banked SPSRs
 22 * 0x00130 - 0x00147: Audio channel 1 state
 23 * | 0x00130 - 0x00130: Current volume
 24 * | 0x00131 - 0x00131: Is channel dead?
 25 * | 0x00132 - 0x00132: Is channel high?
 26 * | 0x00133 - 0x00133: Reserved
 27 * | 0x00134 - 0x00137: Next envelope step
 28 * | 0x00137 - 0x0013B: Next square wave step
 29 * | 0x0013C - 0x0013G: Next sweep step
 30 * | 0x00140 - 0x00143: Channel end cycle
 31 * | 0x00144 - 0x00147: Next event
 32 * 0x00148 - 0x0015F: Audio channel 2/4 state
 33 * | 0x00148 - 0x00148: Current volume
 34 * | 0x00149 - 0x00149: Is channel dead?
 35 * | 0x0014A - 0x0014A: Is channel high?
 36 * | 0x0014B - 0x0014B: Reserved
 37 * | 0x0014C - 0x0014F: Next envelope step
 38 * | 0x00150 - 0x00153: Next square wave step
 39 * | 0x00154 - 0x00157: Audio channel 4 LFSR
 40 * | 0x00158 - 0x0015B: Channel end cycle
 41 * | 0x0015C - 0x0015F: Next Event
 42 * 0x00160 - 0x0017F: Audio channel 3 wave banks
 43 * 0x00180 - 0x0019F: Audio FIFO 1
 44 * 0x001A0 - 0x001BF: Audio FIFO 2
 45 * 0x001C0 - 0x001DF: Audio miscellaneous state
 46 * | 0x001C0 - 0x001C3: Next event
 47 * | 0x001C4 - 0x001C7: Event diff
 48 * | 0x001C8 - 0x001CB: Next channel 3 event
 49 * | 0x001CC - 0x001CF: Next channel 4 event
 50 * | 0x001D0 - 0x001D3: Next sample
 51 * | 0x001D4 - 0x001D7: FIFO size
 52 * | 0x001D8 - 0x001DF: Reserved
 53 * 0x001E0 - 0x001FF: Video miscellaneous state
 54 * | 0x001E0 - 0x001E3: Next event
 55 * | 0x001E4 - 0x001E7: Event diff
 56 * | 0x001E8 - 0x001EB: Last hblank
 57 * | 0x001EC - 0x001EF: Next hblank
 58 * | 0x001F0 - 0x001F3: Next hblank IRQ
 59 * | 0x001F4 - 0x001F7: Next vblank IRQ
 60 * | 0x001F8 - 0x001FB: Next vcounter IRQ
 61 * | 0x001FC - 0x001FF: Reserved
 62 * 0x00200 - 0x003FF: Reserved (leave zero)
 63 * 0x00400 - 0x007FF: I/O memory
 64 * 0x00800 - 0x00BFF: Palette
 65 * 0x00C00 - 0x00FFF: OAM
 66 * 0x01000 - 0x18FFF: VRAM
 67 * 0x19000 - 0x20FFF: IWRAM
 68 * 0x21000 - 0x60FFF: WRAM
 69 * Total size: 0x61000 (397,312) bytes
 70 */
 71
 72struct GBASerializedState {
 73	uint32_t versionMagic;
 74	uint32_t biosChecksum;
 75	uint32_t reservedHeader[2];
 76
 77	char title[12];
 78	uint32_t id;
 79
 80	struct {
 81		int32_t gprs[16];
 82		union PSR cpsr;
 83		union PSR spsr;
 84
 85		int32_t cycles;
 86		int32_t nextEvent;
 87
 88		int32_t bankedRegisters[6][7];
 89		int32_t bankedSPSRs[6];
 90	} cpu;
 91
 92	struct {
 93		struct {
 94			int8_t volume;
 95			int8_t dead;
 96			int8_t hi;
 97			int8_t : 8;
 98			int32_t envelopeNextStep;
 99			int32_t waveNextStep;
100			int32_t sweepNextStep;
101			int32_t endTime;
102			int32_t nextEvent;
103		} ch1;
104		struct {
105			int8_t volume;
106			int8_t dead;
107			int8_t hi;
108			int8_t : 8;
109			int32_t envelopeNextStep;
110			int32_t waveNextStep;
111			int32_t ch4Lfsr;
112			int32_t endTime;
113			int32_t nextEvent;
114		} ch2;
115		uint32_t ch3[8];
116		uint32_t fifoA[8];
117		uint32_t fifoB[8];
118		int32_t nextEvent;
119		int32_t eventDiff;
120		int32_t nextCh3;
121		int32_t nextCh4;
122		int32_t nextSample;
123		int32_t fifoSize;
124		int32_t : 32;
125		int32_t : 32;
126	} audio;
127
128	struct {
129		int32_t nextEvent;
130		int32_t eventDiff;
131		int32_t lastHblank;
132		int32_t nextHblank;
133		int32_t nextHblankIRQ;
134		int32_t nextVblankIRQ;
135		int32_t nextVcounterIRQ;
136		int32_t : 32;
137	} video;
138
139	uint32_t reservedGpio[128];
140
141	uint16_t io[SIZE_IO >> 1];
142	uint16_t pram[SIZE_PALETTE_RAM >> 1];
143	uint16_t oam[SIZE_OAM >> 1];
144	uint16_t vram[SIZE_VRAM >> 1];
145	uint8_t iwram[SIZE_WORKING_IRAM];
146	uint8_t wram[SIZE_WORKING_RAM];
147};
148
149void GBASerialize(struct GBA* gba, struct GBASerializedState* state);
150void GBADeserialize(struct GBA* gba, struct GBASerializedState* state);
151
152int GBASaveState(struct GBA* gba, int slot);
153int GBALoadState(struct GBA* gba, int slot);
154
155struct GBASerializedState* GBAMapState(int fd);
156struct GBASerializedState* GBAAllocateState(void);
157void GBADeallocateState(struct GBASerializedState* state);
158
159#endif