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 - 0x00213: Timer 0
63 * | 0x00200 - 0x00201: Reload value
64 * | 0x00202 - 0x00203: Old reload value
65 * | 0x00204 - 0x00207: Last event
66 * | 0x00208 - 0x0020B: Next event
67 * | 0x0020C - 0x0020F: Overflow interval
68 * | 0x00210 - 0x00213: Miscellaenous flags
69 * 0x00214 - 0x00227: Timer 1
70 * | 0x00214 - 0x00215: Reload value
71 * | 0x00216 - 0x00217: Old reload value
72 * | 0x00218 - 0x0021B: Last event
73 * | 0x0021C - 0x0021F: Next event
74 * | 0x00220 - 0x00223: Overflow interval
75 * | 0x00224 - 0x00227: Miscellaenous flags
76 * 0x00228 - 0x0023B: Timer 2
77 * | 0x00228 - 0x00229: Reload value
78 * | 0x0022A - 0x0022B: Old reload value
79 * | 0x0022C - 0x0022F: Last event
80 * | 0x00230 - 0x00233: Next event
81 * | 0x00234 - 0x00237: Overflow interval
82 * | 0x00238 - 0x0023B: Miscellaenous flags
83 * 0x0023C - 0x00250: Timer 3
84 * | 0x0023C - 0x0023D: Reload value
85 * | 0x0023E - 0x0023F: Old reload value
86 * | 0x00240 - 0x00243: Last event
87 * | 0x00244 - 0x00247: Next event
88 * | 0x00248 - 0x0024B: Overflow interval
89 * | 0x0024C - 0x0024F: Miscellaenous flags
90 * 0x00250 - 0x0025F: DMA 0
91 * | 0x00250 - 0x00253: DMA next source
92 * | 0x00254 - 0x00257: DMA next destination
93 * | 0x00258 - 0x0025B: DMA next count
94 * | 0x0025C - 0x0025F: DMA next event
95 * 0x00260 - 0x0026F: DMA 1
96 * | 0x00260 - 0x00263: DMA next source
97 * | 0x00264 - 0x00267: DMA next destination
98 * | 0x00268 - 0x0026B: DMA next count
99 * | 0x0026C - 0x0026F: DMA next event
100 * 0x00270 - 0x0027F: DMA 2
101 * | 0x00270 - 0x00273: DMA next source
102 * | 0x00274 - 0x00277: DMA next destination
103 * | 0x00278 - 0x0027B: DMA next count
104 * | 0x0027C - 0x0027F: DMA next event
105 * 0x00280 - 0x0028F: DMA 3
106 * | 0x00280 - 0x00283: DMA next source
107 * | 0x00284 - 0x00287: DMA next destination
108 * | 0x00288 - 0x0028B: DMA next count
109 * | 0x0028C - 0x0028F: DMA next event
110 * 0x00290 - 0x003FF: Reserved (leave zero)
111 * 0x00400 - 0x007FF: I/O memory
112 * 0x00800 - 0x00BFF: Palette
113 * 0x00C00 - 0x00FFF: OAM
114 * 0x01000 - 0x18FFF: VRAM
115 * 0x19000 - 0x20FFF: IWRAM
116 * 0x21000 - 0x60FFF: WRAM
117 * Total size: 0x61000 (397,312) bytes
118 */
119
120struct GBASerializedState {
121 uint32_t versionMagic;
122 uint32_t biosChecksum;
123 uint32_t reservedHeader[2];
124
125 char title[12];
126 uint32_t id;
127
128 struct {
129 int32_t gprs[16];
130 union PSR cpsr;
131 union PSR spsr;
132
133 int32_t cycles;
134 int32_t nextEvent;
135
136 int32_t bankedRegisters[6][7];
137 int32_t bankedSPSRs[6];
138 } cpu;
139
140 struct {
141 struct {
142 int8_t volume;
143 int8_t dead;
144 int8_t hi;
145 int8_t : 8;
146 int32_t envelopeNextStep;
147 int32_t waveNextStep;
148 int32_t sweepNextStep;
149 int32_t endTime;
150 int32_t nextEvent;
151 } ch1;
152 struct {
153 int8_t volume;
154 int8_t dead;
155 int8_t hi;
156 int8_t : 8;
157 int32_t envelopeNextStep;
158 int32_t waveNextStep;
159 int32_t ch4Lfsr;
160 int32_t endTime;
161 int32_t nextEvent;
162 } ch2;
163 uint32_t ch3[8];
164 uint32_t fifoA[8];
165 uint32_t fifoB[8];
166 int32_t nextEvent;
167 int32_t eventDiff;
168 int32_t nextCh3;
169 int32_t nextCh4;
170 int32_t nextSample;
171 int32_t fifoSize;
172 int32_t : 32;
173 int32_t : 32;
174 } audio;
175
176 struct {
177 int32_t nextEvent;
178 int32_t eventDiff;
179 int32_t lastHblank;
180 int32_t nextHblank;
181 int32_t nextHblankIRQ;
182 int32_t nextVblankIRQ;
183 int32_t nextVcounterIRQ;
184 int32_t : 32;
185 } video;
186
187 struct GBATimer timers[4];
188
189 struct {
190 uint32_t nextSource;
191 uint32_t nextDest;
192 int32_t nextCount;
193 int32_t nextEvent;
194 } dma[4];
195
196 uint32_t reservedGpio[92];
197
198 uint16_t io[SIZE_IO >> 1];
199 uint16_t pram[SIZE_PALETTE_RAM >> 1];
200 uint16_t oam[SIZE_OAM >> 1];
201 uint16_t vram[SIZE_VRAM >> 1];
202 uint8_t iwram[SIZE_WORKING_IRAM];
203 uint8_t wram[SIZE_WORKING_RAM];
204};
205
206void GBASerialize(struct GBA* gba, struct GBASerializedState* state);
207void GBADeserialize(struct GBA* gba, struct GBASerializedState* state);
208
209int GBASaveState(struct GBA* gba, int slot);
210int GBALoadState(struct GBA* gba, int slot);
211
212struct GBASerializedState* GBAMapState(int fd);
213struct GBASerializedState* GBAAllocateState(void);
214void GBADeallocateState(struct GBASerializedState* state);
215
216#endif