src/gba/serialize.h (view raw)
1/* Copyright (c) 2013-2015 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef GBA_SERIALIZE_H
7#define GBA_SERIALIZE_H
8
9#include "util/common.h"
10
11#include "gba/gba.h"
12
13extern const uint32_t GBA_SAVESTATE_MAGIC;
14
15/* Savestate format:
16 * 0x00000 - 0x00003: Version Magic (0x01000000)
17 * 0x00004 - 0x00007: BIOS checksum (e.g. 0xBAAE187F for official BIOS)
18 * 0x00008 - 0x0000B: ROM CRC32
19 * 0x0000C - 0x0000F: Reserved (leave zero)
20 * 0x00010 - 0x0001B: Game title (e.g. METROID4USA)
21 * 0x0001C - 0x0001F: Game code (e.g. AMTE)
22 * 0x00020 - 0x0012F: CPU state:
23 * | 0x00020 - 0x0005F: GPRs
24 * | 0x00060 - 0x00063: CPSR
25 * | 0x00064 - 0x00067: SPSR
26 * | 0x00068 - 0x0006B: Cycles since last event
27 * | 0x0006C - 0x0006F: Cycles until next event
28 * | 0x00070 - 0x00117: Banked registers
29 * | 0x00118 - 0x0012F: Banked SPSRs
30 * 0x00130 - 0x00143: Audio channel 1 state
31 * | 0x00130 - 0x00133: Next envelope step
32 * | 0x00134 - 0x00137: Next square wave step
33 * | 0x00138 - 0x0013B: Next sweep step
34 * | 0x0013C - 0x0013F: Channel end cycle
35 * | 0x00140 - 0x00143: Next event
36 * 0x00144 - 0x00153: Audio channel 2 state
37 * | 0x00144 - 0x00147: Next envelope step
38 * | 0x00148 - 0x0014B: Next square wave step
39 * | 0x0014C - 0x0014F: Channel end cycle
40 * | 0x00150 - 0x00153: Next event
41 * 0x00154 - 0x0017B: Audio channel 3 state
42 * | 0x00154 - 0x00173: Wave banks
43 * | 0x00174 - 0x00177: Channel end cycle
44 * | 0x00178 - 0x0017B: Next event
45 * 0x0017C - 0x0018B: Audio channel 4 state
46 * | 0x0017C - 0x0017F: Linear feedback shift register state
47 * | 0x00180 - 0x00183: Next enveleope step
48 * | 0x00184 - 0x00187: Channel end cycle
49 * | 0x00188 - 0x0018B: Next event
50 * 0x0018C - 0x001AB: Audio FIFO 1
51 * 0x001AC - 0x001CB: Audio FIFO 2
52 * 0x001CC - 0x001DF: Audio miscellaneous state
53 * | 0x001CC - 0x001CF: Next event
54 * | 0x001D0 - 0x001D3: Event diff
55 * | 0x001D4 - 0x001D7: Next sample
56 * | 0x001D8 - 0x001DB: FIFO size
57 * | 0x001DC - 0x001DC: Channel 1 envelope state
58 * | bits 0 - 3: Current volume
59 * | bit 4: Is dead?
60 * | bit 5: Is high?
61 * | bits 6 - 7: Reserved
62 * | 0x001DD - 0x001DD: Channel 2 envelope state
63 * | bits 0 - 3: Current volume
64 * | bit 4: Is dead?
65 * | bit 5: Is high?
66 * | bits 6 - 7: Reserved
67 * | 0x001DE - 0x001DE: Channel 4 envelope state
68 * | bits 0 - 3: Current volume
69 * | bit 4: Is dead?
70 * | bits 5 - 7: Reserved
71 * | 0x001DF - 0x001DF: Reserved
72 * 0x001E0 - 0x001FF: Video miscellaneous state
73 * | 0x001E0 - 0x001E3: Next event
74 * | 0x001E4 - 0x001E7: Event diff
75 * | 0x001E8 - 0x001EB: Last hblank
76 * | 0x001EC - 0x001EF: Next hblank
77 * | 0x001F0 - 0x001F3: Next hblank IRQ
78 * | 0x001F4 - 0x001F7: Next vblank IRQ
79 * | 0x001F8 - 0x001FB: Next vcounter IRQ
80 * | 0x001FC - 0x001FF: Frame counter
81 * 0x00200 - 0x00213: Timer 0
82 * | 0x00200 - 0x00201: Reload value
83 * | 0x00202 - 0x00203: Old reload value
84 * | 0x00204 - 0x00207: Last event
85 * | 0x00208 - 0x0020B: Next event
86 * | 0x0020C - 0x0020F: Overflow interval
87 * | 0x00210 - 0x00213: Miscellaneous flags
88 * 0x00214 - 0x00227: Timer 1
89 * | 0x00214 - 0x00215: Reload value
90 * | 0x00216 - 0x00217: Old reload value
91 * | 0x00218 - 0x0021B: Last event
92 * | 0x0021C - 0x0021F: Next event
93 * | 0x00220 - 0x00223: Overflow interval
94 * | 0x00224 - 0x00227: Miscellaneous flags
95 * 0x00228 - 0x0023B: Timer 2
96 * | 0x00228 - 0x00229: Reload value
97 * | 0x0022A - 0x0022B: Old reload value
98 * | 0x0022C - 0x0022F: Last event
99 * | 0x00230 - 0x00233: Next event
100 * | 0x00234 - 0x00237: Overflow interval
101 * | 0x00238 - 0x0023B: Miscellaneous flags
102 * 0x0023C - 0x00250: Timer 3
103 * | 0x0023C - 0x0023D: Reload value
104 * | 0x0023E - 0x0023F: Old reload value
105 * | 0x00240 - 0x00243: Last event
106 * | 0x00244 - 0x00247: Next event
107 * | 0x00248 - 0x0024B: Overflow interval
108 * | 0x0024C - 0x0024F: Miscellaneous flags
109 * 0x00250 - 0x0025F: DMA 0
110 * | 0x00250 - 0x00253: DMA next source
111 * | 0x00254 - 0x00257: DMA next destination
112 * | 0x00258 - 0x0025B: DMA next count
113 * | 0x0025C - 0x0025F: DMA next event
114 * 0x00260 - 0x0026F: DMA 1
115 * | 0x00260 - 0x00263: DMA next source
116 * | 0x00264 - 0x00267: DMA next destination
117 * | 0x00268 - 0x0026B: DMA next count
118 * | 0x0026C - 0x0026F: DMA next event
119 * 0x00270 - 0x0027F: DMA 2
120 * | 0x00270 - 0x00273: DMA next source
121 * | 0x00274 - 0x00277: DMA next destination
122 * | 0x00278 - 0x0027B: DMA next count
123 * | 0x0027C - 0x0027F: DMA next event
124 * 0x00280 - 0x0028F: DMA 3
125 * | 0x00280 - 0x00283: DMA next source
126 * | 0x00284 - 0x00287: DMA next destination
127 * | 0x00288 - 0x0028B: DMA next count
128 * | 0x0028C - 0x0028F: DMA next event
129 * 0x00290 - 0x002C3: GPIO state
130 * | 0x00290 - 0x00291: Pin state
131 * | 0x00292 - 0x00293: Direction state
132 * | 0x00294 - 0x002B6: RTC state (see hardware.h for format)
133 * | 0x002B7 - 0x002B7: GPIO devices
134 * | bit 0: Has RTC values
135 * | bit 1: Has rumble value (reserved)
136 * | bit 2: Has light sensor value
137 * | bit 3: Has gyroscope value
138 * | bit 4: Has tilt values
139 * | bit 5: Has Game Boy Player attached
140 * | bits 6 - 7: Reserved
141 * | 0x002B8 - 0x002B9: Gyroscope sample
142 * | 0x002BA - 0x002BB: Tilt x sample
143 * | 0x002BC - 0x002BD: Tilt y sample
144 * | 0x002BE - 0x002BF: Flags
145 * | bit 0: Is read enabled
146 * | bit 1: Gyroscope sample is edge
147 * | bit 2: Light sample is edge
148 * | bit 3: Reserved
149 * | bits 4 - 15: Light counter
150 * | 0x002C0 - 0x002C0: Light sample
151 * | 0x002C1 - 0x002C3: Flags
152 * | bits 0 - 1: Tilt state machine
153 * | bits 2 - 3: GB Player inputs posted
154 * | bits 4 - 8: GB Player transmit position
155 * | bits 9 - 23: Reserved
156 * 0x002C4 - 0x002C7: Game Boy Player next event
157 * 0x002C8 - 0x002DF: Reserved (leave zero)
158 * 0x002E0 - 0x002EF: Savedata state
159 * | 0x002E0 - 0x002E0: Savedata type
160 * | 0x002E1 - 0x002E1: Savedata command (see savedata.h)
161 * | 0x002E2 - 0x002E2: Flags
162 * | bits 0 - 1: Flash state machine
163 * | bits 2 - 3: Reserved
164 * | bit 4: Flash bank
165 * | bits 5 - 7: Reserved
166 * | 0x002E3 - 0x002E3: Reserved
167 * | 0x002E4 - 0x002E7: EEPROM read bits remaining
168 * | 0x002E8 - 0x002EB: EEPROM read address
169 * | 0x002EC - 0x002EF: EEPROM write address
170 * | 0x002F0 - 0x002F1: Flash settling sector
171 * | 0x002F2 - 0x002F3: Flash settling remaining
172 * 0x002F4 - 0x002FF: Prefetch
173 * | 0x002F4 - 0x002F7: GBA BIOS bus prefetch
174 * | 0x002F8 - 0x002FB: CPU prefecth (decode slot)
175 * | 0x002FC - 0x002FF: CPU prefetch (fetch slot)
176 * 0x00300 - 0x00303: Associated movie stream ID for record/replay (or 0 if no stream)
177 * 0x00304 - 0x0030F: Reserved (leave zero)
178 * 0x00310 - 0x00317: Savestate creation time (usec since 1970)
179 * 0x00318 - 0x003FF: Reserved (leave zero)
180 * 0x00400 - 0x007FF: I/O memory
181 * 0x00800 - 0x00BFF: Palette
182 * 0x00C00 - 0x00FFF: OAM
183 * 0x01000 - 0x18FFF: VRAM
184 * 0x19000 - 0x20FFF: IWRAM
185 * 0x21000 - 0x60FFF: WRAM
186 * Total size: 0x61000 (397,312) bytes
187 */
188
189DECL_BITFIELD(GBASerializedAudioFlags, uint32_t);
190DECL_BITS(GBASerializedAudioFlags, Ch1Volume, 0, 4);
191DECL_BIT(GBASerializedAudioFlags, Ch1Dead, 4);
192DECL_BIT(GBASerializedAudioFlags, Ch1Hi, 5);
193DECL_BITS(GBASerializedAudioFlags, Ch2Volume, 8, 4);
194DECL_BIT(GBASerializedAudioFlags, Ch2Dead, 12);
195DECL_BIT(GBASerializedAudioFlags, Ch2Hi, 13);
196DECL_BITS(GBASerializedAudioFlags, Ch4Volume, 16, 4);
197DECL_BIT(GBASerializedAudioFlags, Ch4Dead, 20);
198
199DECL_BITFIELD(GBASerializedHWFlags1, uint16_t);
200DECL_BIT(GBASerializedHWFlags1, ReadWrite, 0);
201DECL_BIT(GBASerializedHWFlags1, GyroEdge, 1);
202DECL_BIT(GBASerializedHWFlags1, LightEdge, 2);
203DECL_BITS(GBASerializedHWFlags1, LightCounter, 4, 12);
204
205DECL_BITFIELD(GBASerializedHWFlags2, uint8_t);
206DECL_BITS(GBASerializedHWFlags2, TiltState, 0, 2);
207DECL_BITS(GBASerializedHWFlags2, GbpInputsPosted, 2, 2);
208DECL_BITS(GBASerializedHWFlags2, GbpTxPosition, 4, 5);
209
210DECL_BITFIELD(GBASerializedHWFlags3, uint16_t);
211
212DECL_BITFIELD(GBASerializedSavedataFlags, uint8_t);
213DECL_BITS(GBASerializedSavedataFlags, FlashState, 0, 2);
214DECL_BIT(GBASerializedSavedataFlags, FlashBank, 4);
215
216struct GBASerializedState {
217 uint32_t versionMagic;
218 uint32_t biosChecksum;
219 uint32_t romCrc32;
220 uint32_t reservedHeader;
221
222 char title[12];
223 uint32_t id;
224
225 struct {
226 int32_t gprs[16];
227 union PSR cpsr;
228 union PSR spsr;
229
230 int32_t cycles;
231 int32_t nextEvent;
232
233 int32_t bankedRegisters[6][7];
234 int32_t bankedSPSRs[6];
235 } cpu;
236
237 struct {
238 struct {
239 int32_t envelopeNextStep;
240 int32_t waveNextStep;
241 int32_t sweepNextStep;
242 int32_t endTime;
243 int32_t nextEvent;
244 } ch1;
245 struct {
246 int32_t envelopeNextStep;
247 int32_t waveNextStep;
248 int32_t endTime;
249 int32_t nextEvent;
250 } ch2;
251 struct {
252 uint32_t wavebanks[8];
253 int32_t endTime;
254 int32_t nextEvent;
255 } ch3;
256 struct {
257 int32_t lfsr;
258 int32_t envelopeNextStep;
259 int32_t endTime;
260 int32_t nextEvent;
261 } ch4;
262 uint8_t fifoA[32];
263 uint8_t fifoB[32];
264 int32_t nextEvent;
265 int32_t eventDiff;
266 int32_t nextSample;
267 uint32_t fifoSize;
268 GBASerializedAudioFlags flags;
269 } audio;
270
271 struct {
272 int32_t nextEvent;
273 int32_t eventDiff;
274 int32_t lastHblank;
275 int32_t nextHblank;
276 int32_t nextHblankIRQ;
277 int32_t nextVblankIRQ;
278 int32_t nextVcounterIRQ;
279 int32_t frameCounter;
280 } video;
281
282 struct GBATimer timers[4];
283
284 struct {
285 uint32_t nextSource;
286 uint32_t nextDest;
287 int32_t nextCount;
288 int32_t nextEvent;
289 } dma[4];
290
291 struct {
292 uint16_t pinState;
293 uint16_t pinDirection;
294 struct GBARTC rtc;
295 uint8_t devices;
296 uint16_t gyroSample;
297 uint16_t tiltSampleX;
298 uint16_t tiltSampleY;
299 GBASerializedHWFlags1 flags1;
300 uint8_t lightSample;
301 GBASerializedHWFlags2 flags2;
302 GBASerializedHWFlags3 flags3;
303 uint32_t gbpNextEvent;
304 } hw;
305
306 uint32_t reservedHardware[6];
307
308 struct {
309 uint8_t type;
310 uint8_t command;
311 GBASerializedSavedataFlags flags;
312 uint8_t reserved;
313 int32_t readBitsRemaining;
314 uint32_t readAddress;
315 uint32_t writeAddress;
316 uint16_t settlingSector;
317 uint16_t settlingDust;
318 } savedata;
319
320 uint32_t biosPrefetch;
321 uint32_t cpuPrefetch[2];
322
323 uint32_t associatedStreamId;
324 uint32_t reservedRr[3];
325
326 uint64_t creationUsec;
327
328 uint32_t reserved[58];
329
330 uint16_t io[SIZE_IO >> 1];
331 uint16_t pram[SIZE_PALETTE_RAM >> 1];
332 uint16_t oam[SIZE_OAM >> 1];
333 uint16_t vram[SIZE_VRAM >> 1];
334 uint8_t iwram[SIZE_WORKING_IRAM];
335 uint8_t wram[SIZE_WORKING_RAM];
336};
337
338enum GBAExtdataTag {
339 EXTDATA_NONE = 0,
340 EXTDATA_SCREENSHOT = 1,
341 EXTDATA_SAVEDATA = 2,
342 EXTDATA_CHEATS = 3,
343 EXTDATA_MAX
344};
345
346#define SAVESTATE_SCREENSHOT 1
347#define SAVESTATE_SAVEDATA 2
348#define SAVESTATE_CHEATS 4
349
350struct GBAExtdataItem {
351 int32_t size;
352 void* data;
353 void (*clean)(void*);
354};
355
356struct GBAExtdata {
357 struct GBAExtdataItem data[EXTDATA_MAX];
358};
359
360struct VDir;
361struct GBAThread;
362
363void GBASerialize(struct GBA* gba, struct GBASerializedState* state);
364bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state);
365
366bool GBASaveState(struct GBAThread* thread, struct VDir* dir, int slot, int flags);
367bool GBALoadState(struct GBAThread* thread, struct VDir* dir, int slot, int flags);
368struct VFile* GBAGetState(struct GBA* gba, struct VDir* dir, int slot, bool write);
369void GBADeleteState(struct GBA* thread, struct VDir* dir, int slot);
370
371bool GBASaveStateNamed(struct GBA* gba, struct VFile* vf, int flags);
372bool GBALoadStateNamed(struct GBA* gba, struct VFile* vf, int flags);
373
374bool GBAExtdataInit(struct GBAExtdata*);
375void GBAExtdataDeinit(struct GBAExtdata*);
376void GBAExtdataPut(struct GBAExtdata*, enum GBAExtdataTag, struct GBAExtdataItem*);
377bool GBAExtdataGet(struct GBAExtdata*, enum GBAExtdataTag, struct GBAExtdataItem*);
378bool GBAExtdataSerialize(struct GBAExtdata* extpdata, struct VFile* vf);
379bool GBAExtdataDeserialize(struct GBAExtdata* extdata, struct VFile* vf);
380
381struct GBASerializedState* GBAExtractState(struct VFile* vf, struct GBAExtdata* extdata);
382struct GBASerializedState* GBAAllocateState(void);
383void GBADeallocateState(struct GBASerializedState* state);
384
385void GBARecordFrame(struct GBAThread* thread);
386void GBARewindSettingsChanged(struct GBAThread* thread, int newCapacity, int newInterval);
387int GBARewind(struct GBAThread* thread, int nStates);
388void GBARewindAll(struct GBAThread* thread);
389
390void GBATakeScreenshot(struct GBA* gba, struct VDir* dir);
391
392#endif