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