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#include "gb/serialize.h"
14
15extern const uint32_t GBA_SAVESTATE_MAGIC;
16extern const uint32_t GBA_SAVESTATE_VERSION;
17
18mLOG_DECLARE_CATEGORY(GBA_STATE);
19
20/* Savestate format:
21 * 0x00000 - 0x00003: Version Magic (0x01000001)
22 * 0x00004 - 0x00007: BIOS checksum (e.g. 0xBAAE187F for official BIOS)
23 * 0x00008 - 0x0000B: ROM CRC32
24 * 0x0000C - 0x0000F: Master cycles
25 * 0x00010 - 0x0001B: Game title (e.g. METROID4USA)
26 * 0x0001C - 0x0001F: Game code (e.g. AMTE)
27 * 0x00020 - 0x0012F: CPU state:
28 * | 0x00020 - 0x0005F: GPRs
29 * | 0x00060 - 0x00063: CPSR
30 * | 0x00064 - 0x00067: SPSR
31 * | 0x00068 - 0x0006B: Cycles since last event
32 * | 0x0006C - 0x0006F: Cycles until next event
33 * | 0x00070 - 0x00117: Banked registers
34 * | 0x00118 - 0x0012F: Banked SPSRs
35 * 0x00130 - 0x00143: Audio channel 1/framer state
36 * | 0x00130 - 0x00133: Envelepe timing
37 * | bits 0 - 6: Remaining length
38 * | bits 7 - 9: Next step
39 * | bits 10 - 20: Shadow frequency register
40 * | bits 21 - 31: Reserved
41 * | 0x00134 - 0x00137: Next frame
42 * | 0x00138 - 0x0013F: Reserved
43 * | 0x00140 - 0x00143: Next event
44 * 0x00144 - 0x00153: Audio channel 2 state
45 * | 0x00144 - 0x00147: Envelepe timing
46 * | bits 0 - 2: Remaining length
47 * | bits 3 - 5: Next step
48 * | bits 6 - 31: Reserved
49 * | 0x00148 - 0x0014F: Reserved
50 * | 0x00150 - 0x00153: Next event
51 * 0x00154 - 0x0017B: Audio channel 3 state
52 * | 0x00154 - 0x00173: Wave banks
53 * | 0x00174 - 0x00175: Remaining length
54 * | 0x00176 - 0x00177: Reserved
55 * | 0x00178 - 0x0017B: Next event
56 * 0x0017C - 0x0018B: Audio channel 4 state
57 * | 0x0017C - 0x0017F: Linear feedback shift register state
58 * | 0x00180 - 0x00183: Envelepe timing
59 * | bits 0 - 2: Remaining length
60 * | bits 3 - 5: Next step
61 * | bits 6 - 31: Reserved
62 * | 0x00184 - 0x00187: Reserved
63 * | 0x00188 - 0x0018B: Next event
64 * 0x0018C - 0x001AB: Audio FIFO 1
65 * 0x001AC - 0x001CB: Audio FIFO 2
66 * 0x001CC - 0x001DF: Audio miscellaneous state
67 * | 0x001CC - 0x001D3: Reserved
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 - 0x001FB: Reserved
93 * | 0x001FC - 0x001FF: Frame counter
94 * 0x00200 - 0x00213: Timer 0
95 * | 0x00200 - 0x00201: Reload value
96 * | 0x00202 - 0x00203: Old reload value
97 * | 0x00204 - 0x00207: Last event
98 * | 0x00208 - 0x0020B: Next event
99 * | 0x0020C - 0x0020F: Overflow interval
100 * | 0x00210 - 0x00213: Miscellaneous flags
101 * 0x00214 - 0x00227: Timer 1
102 * | 0x00214 - 0x00215: Reload value
103 * | 0x00216 - 0x00217: Old reload value
104 * | 0x00218 - 0x0021B: Last event
105 * | 0x0021C - 0x0021F: Next event
106 * | 0x00220 - 0x00223: Overflow interval
107 * | 0x00224 - 0x00227: Miscellaneous flags
108 * 0x00228 - 0x0023B: Timer 2
109 * | 0x00228 - 0x00229: Reload value
110 * | 0x0022A - 0x0022B: Old reload value
111 * | 0x0022C - 0x0022F: Last event
112 * | 0x00230 - 0x00233: Next event
113 * | 0x00234 - 0x00237: Overflow interval
114 * | 0x00238 - 0x0023B: Miscellaneous flags
115 * 0x0023C - 0x00250: Timer 3
116 * | 0x0023C - 0x0023D: Reload value
117 * | 0x0023E - 0x0023F: Old reload value
118 * | 0x00240 - 0x00243: Last event
119 * | 0x00244 - 0x00247: Next event
120 * | 0x00248 - 0x0024B: Overflow interval
121 * | 0x0024C - 0x0024F: Miscellaneous flags
122 * 0x00250 - 0x0025F: DMA 0
123 * | 0x00250 - 0x00253: DMA next source
124 * | 0x00254 - 0x00257: DMA next destination
125 * | 0x00258 - 0x0025B: DMA next count
126 * | 0x0025C - 0x0025F: DMA next event
127 * 0x00260 - 0x0026F: DMA 1
128 * | 0x00260 - 0x00263: DMA next source
129 * | 0x00264 - 0x00267: DMA next destination
130 * | 0x00268 - 0x0026B: DMA next count
131 * | 0x0026C - 0x0026F: DMA next event
132 * 0x00270 - 0x0027F: DMA 2
133 * | 0x00270 - 0x00273: DMA next source
134 * | 0x00274 - 0x00277: DMA next destination
135 * | 0x00278 - 0x0027B: DMA next count
136 * | 0x0027C - 0x0027F: DMA next event
137 * 0x00280 - 0x0028F: DMA 3
138 * | 0x00280 - 0x00283: DMA next source
139 * | 0x00284 - 0x00287: DMA next destination
140 * | 0x00288 - 0x0028B: DMA next count
141 * | 0x0028C - 0x0028F: DMA next event
142 * 0x00290 - 0x002C3: GPIO state
143 * | 0x00290 - 0x00291: Pin state
144 * | 0x00292 - 0x00293: Direction state
145 * | 0x00294 - 0x002B6: RTC state (see hardware.h for format)
146 * | 0x002B7 - 0x002B7: GPIO devices
147 * | bit 0: Has RTC values
148 * | bit 1: Has rumble value (reserved)
149 * | bit 2: Has light sensor value
150 * | bit 3: Has gyroscope value
151 * | bit 4: Has tilt values
152 * | bit 5: Has Game Boy Player attached
153 * | bits 6 - 7: Reserved
154 * | 0x002B8 - 0x002B9: Gyroscope sample
155 * | 0x002BA - 0x002BB: Tilt x sample
156 * | 0x002BC - 0x002BD: Tilt y sample
157 * | 0x002BE - 0x002BF: Flags
158 * | bit 0: Is read enabled
159 * | bit 1: Gyroscope sample is edge
160 * | bit 2: Light sample is edge
161 * | bit 3: Reserved
162 * | bits 4 - 15: Light counter
163 * | 0x002C0 - 0x002C0: Light sample
164 * | 0x002C1 - 0x002C3: Flags
165 * | bits 0 - 1: Tilt state machine
166 * | bits 2 - 3: GB Player inputs posted
167 * | bits 4 - 8: GB Player transmit position
168 * | bits 9 - 23: Reserved
169 * 0x002C4 - 0x002C7: Game Boy Player next event
170 * 0x002C8 - 0x002DF: Reserved (leave zero)
171 * 0x002E0 - 0x002EF: Savedata state
172 * | 0x002E0 - 0x002E0: Savedata type
173 * | 0x002E1 - 0x002E1: Savedata command (see savedata.h)
174 * | 0x002E2 - 0x002E2: Flags
175 * | bits 0 - 1: Flash state machine
176 * | bits 2 - 3: Reserved
177 * | bit 4: Flash bank
178 * | bits 5 - 7: Reserved
179 * | 0x002E3 - 0x002E3: Reserved
180 * | 0x002E4 - 0x002E7: EEPROM read bits remaining
181 * | 0x002E8 - 0x002EB: EEPROM read address
182 * | 0x002EC - 0x002EF: EEPROM write address
183 * | 0x002F0 - 0x002F1: Flash settling sector
184 * | 0x002F2 - 0x002F3: Flash settling remaining
185 * 0x002F4 - 0x002FF: Prefetch
186 * | 0x002F4 - 0x002F7: GBA BIOS bus prefetch
187 * | 0x002F8 - 0x002FB: CPU prefecth (decode slot)
188 * | 0x002FC - 0x002FF: CPU prefetch (fetch slot)
189 * 0x00300 - 0x00303: Associated movie stream ID for record/replay (or 0 if no stream)
190 * 0x00304 - 0x0030F: Reserved (leave zero)
191 * 0x00310 - 0x00317: Savestate creation time (usec since 1970)
192 * 0x00318 - 0x0031B: Last prefetched program counter
193 * 0x0031C - 0x0031F: Miscellaneous flags
194 * | bit 0: Is CPU halted?
195 * 0x00320 - 0x003FF: Reserved (leave zero)
196 * 0x00400 - 0x007FF: I/O memory
197 * 0x00800 - 0x00BFF: Palette
198 * 0x00C00 - 0x00FFF: OAM
199 * 0x01000 - 0x18FFF: VRAM
200 * 0x19000 - 0x20FFF: IWRAM
201 * 0x21000 - 0x60FFF: WRAM
202 * Total size: 0x61000 (397,312) bytes
203 */
204
205DECL_BITFIELD(GBASerializedHWFlags1, uint16_t);
206DECL_BIT(GBASerializedHWFlags1, ReadWrite, 0);
207DECL_BIT(GBASerializedHWFlags1, GyroEdge, 1);
208DECL_BIT(GBASerializedHWFlags1, LightEdge, 2);
209DECL_BITS(GBASerializedHWFlags1, LightCounter, 4, 12);
210
211DECL_BITFIELD(GBASerializedHWFlags2, uint8_t);
212DECL_BITS(GBASerializedHWFlags2, TiltState, 0, 2);
213DECL_BITS(GBASerializedHWFlags2, GbpInputsPosted, 2, 2);
214DECL_BITS(GBASerializedHWFlags2, GbpTxPosition, 4, 5);
215
216DECL_BITFIELD(GBASerializedHWFlags3, uint16_t);
217
218DECL_BITFIELD(GBASerializedSavedataFlags, uint8_t);
219DECL_BITS(GBASerializedSavedataFlags, FlashState, 0, 2);
220DECL_BIT(GBASerializedSavedataFlags, FlashBank, 4);
221
222DECL_BITFIELD(GBASerializedMiscFlags, uint32_t);
223DECL_BIT(GBASerializedMiscFlags, Halted, 0);
224
225struct GBASerializedState {
226 uint32_t versionMagic;
227 uint32_t biosChecksum;
228 uint32_t romCrc32;
229 uint32_t masterCycles;
230
231 char title[12];
232 uint32_t id;
233
234 struct {
235 int32_t gprs[16];
236 union PSR cpsr;
237 union PSR spsr;
238
239 int32_t cycles;
240 int32_t nextEvent;
241
242 int32_t bankedRegisters[6][7];
243 int32_t bankedSPSRs[6];
244 } cpu;
245
246 struct {
247 struct GBSerializedPSGState psg;
248 uint8_t fifoA[32];
249 uint8_t fifoB[32];
250 int32_t reserved[2];
251 int32_t nextSample;
252 uint32_t fifoSize;
253 GBSerializedAudioFlags flags;
254 } audio;
255
256 struct {
257 int32_t nextEvent;
258 int32_t reserved[6];
259 int32_t frameCounter;
260 } video;
261
262 struct {
263 uint16_t reload;
264 uint16_t oldReload;
265 uint32_t lastEvent;
266 uint32_t nextEvent;
267 int32_t overflowInterval;
268 GBATimerFlags flags;
269 } timers[4];
270
271 struct {
272 uint32_t nextSource;
273 uint32_t nextDest;
274 int32_t nextCount;
275 int32_t when;
276 } dma[4];
277
278 struct {
279 uint16_t pinState;
280 uint16_t pinDirection;
281 struct GBARTC rtc;
282 uint8_t devices;
283 uint16_t gyroSample;
284 uint16_t tiltSampleX;
285 uint16_t tiltSampleY;
286 GBASerializedHWFlags1 flags1;
287 uint8_t lightSample;
288 GBASerializedHWFlags2 flags2;
289 GBASerializedHWFlags3 flags3;
290 uint32_t gbpNextEvent;
291 } hw;
292
293 uint32_t reservedHardware[6];
294
295 struct {
296 uint8_t type;
297 uint8_t command;
298 GBASerializedSavedataFlags flags;
299 uint8_t reserved;
300 int32_t readBitsRemaining;
301 uint32_t readAddress;
302 uint32_t writeAddress;
303 uint16_t settlingSector;
304 uint16_t settlingDust;
305 } savedata;
306
307 uint32_t biosPrefetch;
308 uint32_t cpuPrefetch[2];
309
310 uint32_t associatedStreamId;
311 uint32_t reservedRr[3];
312
313 uint64_t creationUsec;
314
315 uint32_t lastPrefetchedPc;
316 GBASerializedMiscFlags miscFlags;
317
318 uint32_t reserved[56];
319
320 uint16_t io[SIZE_IO >> 1];
321 uint16_t pram[SIZE_PALETTE_RAM >> 1];
322 uint16_t oam[SIZE_OAM >> 1];
323 uint16_t vram[SIZE_VRAM >> 1];
324 uint8_t iwram[SIZE_WORKING_IRAM];
325 uint8_t wram[SIZE_WORKING_RAM];
326};
327
328struct VDir;
329
330void GBASerialize(struct GBA* gba, struct GBASerializedState* state);
331bool GBADeserialize(struct GBA* gba, const struct GBASerializedState* state);
332
333struct GBASerializedState* GBAAllocateState(void);
334void GBADeallocateState(struct GBASerializedState* state);
335
336#endif