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