all repos — mgba @ 37f5feb713d5768f2bf5405e5a9db8765248efc2

mGBA Game Boy Advance Emulator

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