all repos — mgba @ ff637c84d1f229b0891e0d7f9a40ad5c7a8b12f0

mGBA Game Boy Advance Emulator

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