all repos — mgba @ 94001b113311cad1cce263f14afa2334a5ba4249

mGBA Game Boy Advance Emulator

src/gba/gba-serialize.h (view raw)

  1#ifndef GBA_SERIALIZE_H
  2#define GBA_SERIALIZE_H
  3
  4#include "common.h"
  5
  6#include "gba.h"
  7
  8const uint32_t GBA_SAVESTATE_MAGIC;
  9
 10/* Savestate format:
 11 * 0x00000 - 0x00003: Version Magic (0x01000000)
 12 * 0x00004 - 0x00007: BIOS checksum (e.g. 0xBAAE187F for official BIOS)
 13 * 0x00008 - 0x0000B: ROM CRC32
 14 * 0x0000C - 0x0000F: Reserved (leave zero)
 15 * 0x00010 - 0x0001B: Game title (e.g. METROID4USA)
 16 * 0x0001C - 0x0001F: Game code (e.g. AMTE)
 17 * 0x00020 - 0x0012F: CPU state:
 18 * | 0x00020 - 0x0005F: GPRs
 19 * | 0x00060 - 0x00063: CPSR
 20 * | 0x00064 - 0x00067: SPSR
 21 * | 0x00068 - 0x0006B: Cycles since last event
 22 * | 0x0006C - 0x0006F: Cycles until next event
 23 * | 0x00070 - 0x00117: Banked registers
 24 * | 0x00118 - 0x0012F: Banked SPSRs
 25 * 0x00130 - 0x00143: Audio channel 1 state
 26 * | 0x00130 - 0x00133: Next envelope step
 27 * | 0x00134 - 0x00137: Next square wave step
 28 * | 0x00138 - 0x0013B: Next sweep step
 29 * | 0x0013C - 0x0013F: Channel end cycle
 30 * | 0x00140 - 0x00143: Next event
 31 * 0x00144 - 0x00153: Audio channel 2 state
 32 * | 0x00144 - 0x00147: Next envelope step
 33 * | 0x00148 - 0x0014B: Next square wave step
 34 * | 0x0014C - 0x0014F: Channel end cycle
 35 * | 0x00150 - 0x00153: Next event
 36 * 0x00154 - 0x0017B: Audio channel 3 state
 37 * | 0x00154 - 0x00173: Wave banks
 38 * | 0x00174 - 0x00177: Channel end cycle
 39 * | 0x00178 - 0x0017B: Next event
 40 * 0x0017C - 0x0018B: Audio channel 4 state
 41 * | 0x0017C - 0x0017F: Linear feedback shift register state
 42 * | 0x00180 - 0x00183: Next enveleope step
 43 * | 0x00184 - 0x00187: Channel end cycle
 44 * | 0x00188 - 0x0018B: Next event
 45 * 0x0018C - 0x001AB: Audio FIFO 1
 46 * 0x001AC - 0x001CB: Audio FIFO 2
 47 * 0x001CC - 0x001DF: Audio miscellaneous state
 48 * | 0x001CC - 0x001CF: Next event
 49 * | 0x001D0 - 0x001D3: Event diff
 50 * | 0x001D4 - 0x001D7: Next sample
 51 * | 0x001D8 - 0x001DB: FIFO size
 52 * | 0x001DC - 0x001DC: Channel 1 envelope state
 53 *   | bits 0 - 3: Current volume
 54 *   | bit 4: Is dead?
 55 *   | bit 5: Is high?
 56 *   | bits 6 - 7: Reserved
 57 * | 0x001DD - 0x001DD: Channel 2 envelope state
 58 *   | bits 0 - 3: Current volume
 59 *   | bit 4: Is dead?
 60 *   | bit 5: Is high?
 61 *   | bits 6 - 7: Reserved
 62 * | 0x001DE - 0x001DE: Channel 4 envelope state
 63 *   | bits 0 - 3: Current volume
 64 *   | bit 4: Is dead?
 65 *   | bits 5 - 7: Reserved
 66 * | 0x001DF - 0x001DF: Reserved
 67 * 0x001E0 - 0x001FF: Video miscellaneous state
 68 * | 0x001E0 - 0x001E3: Next event
 69 * | 0x001E4 - 0x001E7: Event diff
 70 * | 0x001E8 - 0x001EB: Last hblank
 71 * | 0x001EC - 0x001EF: Next hblank
 72 * | 0x001F0 - 0x001F3: Next hblank IRQ
 73 * | 0x001F4 - 0x001F7: Next vblank IRQ
 74 * | 0x001F8 - 0x001FB: Next vcounter IRQ
 75 * | 0x001FC - 0x001FF: Reserved
 76 * 0x00200 - 0x00213: Timer 0
 77 * | 0x00200 - 0x00201: Reload value
 78 * | 0x00202 - 0x00203: Old reload value
 79 * | 0x00204 - 0x00207: Last event
 80 * | 0x00208 - 0x0020B: Next event
 81 * | 0x0020C - 0x0020F: Overflow interval
 82 * | 0x00210 - 0x00213: Miscellaneous flags
 83 * 0x00214 - 0x00227: Timer 1
 84 * | 0x00214 - 0x00215: Reload value
 85 * | 0x00216 - 0x00217: Old reload value
 86 * | 0x00218 - 0x0021B: Last event
 87 * | 0x0021C - 0x0021F: Next event
 88 * | 0x00220 - 0x00223: Overflow interval
 89 * | 0x00224 - 0x00227: Miscellaneous flags
 90 * 0x00228 - 0x0023B: Timer 2
 91 * | 0x00228 - 0x00229: Reload value
 92 * | 0x0022A - 0x0022B: Old reload value
 93 * | 0x0022C - 0x0022F: Last event
 94 * | 0x00230 - 0x00233: Next event
 95 * | 0x00234 - 0x00237: Overflow interval
 96 * | 0x00238 - 0x0023B: Miscellaneous flags
 97 * 0x0023C - 0x00250: Timer 3
 98 * | 0x0023C - 0x0023D: Reload value
 99 * | 0x0023E - 0x0023F: Old reload value
100 * | 0x00240 - 0x00243: Last event
101 * | 0x00244 - 0x00247: Next event
102 * | 0x00248 - 0x0024B: Overflow interval
103 * | 0x0024C - 0x0024F: Miscellaneous flags
104 * 0x00250 - 0x0025F: DMA 0
105 * | 0x00250 - 0x00253: DMA next source
106 * | 0x00254 - 0x00257: DMA next destination
107 * | 0x00258 - 0x0025B: DMA next count
108 * | 0x0025C - 0x0025F: DMA next event
109 * 0x00260 - 0x0026F: DMA 1
110 * | 0x00260 - 0x00263: DMA next source
111 * | 0x00264 - 0x00267: DMA next destination
112 * | 0x00268 - 0x0026B: DMA next count
113 * | 0x0026C - 0x0026F: DMA next event
114 * 0x00270 - 0x0027F: DMA 2
115 * | 0x00270 - 0x00273: DMA next source
116 * | 0x00274 - 0x00277: DMA next destination
117 * | 0x00278 - 0x0027B: DMA next count
118 * | 0x0027C - 0x0027F: DMA next event
119 * 0x00280 - 0x0028F: DMA 3
120 * | 0x00280 - 0x00283: DMA next source
121 * | 0x00284 - 0x00287: DMA next destination
122 * | 0x00288 - 0x0028B: DMA next count
123 * | 0x0028C - 0x0028F: DMA next event
124 * 0x00290 - 0x002BF: GPIO state
125 * | 0x00290 - 0x00291: Pin state
126 * | 0x00292 - 0x00293: Direction state
127 * | 0x00294 - 0x002B6: RTC state (see gba-gpio.h for format)
128 * | 0x002B7 - 0x002B7: GPIO devices
129 *   | bit 0: Has RTC values
130 *   | bit 1: Has rumble value (reserved)
131 *   | bit 2: Has light sensor value (reserved)
132 *   | bit 3: Has gyroscope value
133 *   | bit 4: Has tilt values (reserved)
134 *   | bits 5 - 7: Reserved
135 * | 0x002B8 - 0x002B9: Gyroscope sample
136 * | 0x002BA - 0x002BB: Tilt x sample (reserved)
137 * | 0x002BC - 0x002BD: Tilt y sample (reserved)
138 * | 0x002BE - 0x002BF: Flags
139 *   | bit 0: Is read enabled
140 *   | bit 1: Gyroscope sample is edge
141 *   | bits 2 - 15: Reserved
142 * 0x002C0 - 0x003FF: Reserved (leave zero)
143 * 0x00400 - 0x007FF: I/O memory
144 * 0x00800 - 0x00BFF: Palette
145 * 0x00C00 - 0x00FFF: OAM
146 * 0x01000 - 0x18FFF: VRAM
147 * 0x19000 - 0x20FFF: IWRAM
148 * 0x21000 - 0x60FFF: WRAM
149 * Total size: 0x61000 (397,312) bytes
150 */
151
152struct GBASerializedState {
153	uint32_t versionMagic;
154	uint32_t biosChecksum;
155	uint32_t romCrc32;
156	uint32_t reservedHeader;
157
158	char title[12];
159	uint32_t id;
160
161	struct {
162		int32_t gprs[16];
163		union PSR cpsr;
164		union PSR spsr;
165
166		int32_t cycles;
167		int32_t nextEvent;
168
169		int32_t bankedRegisters[6][7];
170		int32_t bankedSPSRs[6];
171	} cpu;
172
173	struct {
174		struct {
175			int32_t envelopeNextStep;
176			int32_t waveNextStep;
177			int32_t sweepNextStep;
178			int32_t endTime;
179			int32_t nextEvent;
180		} ch1;
181		struct {
182			int32_t envelopeNextStep;
183			int32_t waveNextStep;
184			int32_t endTime;
185			int32_t nextEvent;
186		} ch2;
187		struct {
188			uint32_t wavebanks[8];
189			int32_t endTime;
190			int32_t nextEvent;
191		} ch3;
192		struct {
193			int32_t lfsr;
194			int32_t envelopeNextStep;
195			int32_t endTime;
196			int32_t nextEvent;
197		} ch4;
198		uint8_t fifoA[32];
199		uint8_t fifoB[32];
200		int32_t nextEvent;
201		int32_t eventDiff;
202		int32_t nextSample;
203		int32_t fifoSize;
204		unsigned ch1Volume : 4;
205		unsigned ch1Dead : 1;
206		unsigned ch1Hi : 1;
207		unsigned : 2;
208		unsigned ch2Volume : 4;
209		unsigned ch2Dead : 1;
210		unsigned ch2Hi : 1;
211		unsigned : 2;
212		unsigned ch4Volume : 4;
213		unsigned ch4Dead : 1;
214		unsigned : 3;
215		unsigned : 8;
216	} audio;
217
218	struct {
219		int32_t nextEvent;
220		int32_t eventDiff;
221		int32_t lastHblank;
222		int32_t nextHblank;
223		int32_t nextHblankIRQ;
224		int32_t nextVblankIRQ;
225		int32_t nextVcounterIRQ;
226		int32_t : 32;
227	} video;
228
229	struct GBATimer timers[4];
230
231	struct {
232		uint32_t nextSource;
233		uint32_t nextDest;
234		int32_t nextCount;
235		int32_t nextEvent;
236	} dma[4];
237
238	struct {
239		uint16_t pinState;
240		uint16_t pinDirection;
241		struct GBARTC rtc;
242		uint8_t devices;
243		uint16_t gyroSample;
244		uint16_t tiltSampleX;
245		uint16_t tiltSampleY;
246		enum GPIODirection readWrite : 1;
247		unsigned gyroEdge : 1;
248		unsigned reserved : 14;
249	} gpio;
250
251	uint32_t reserved[80];
252
253	uint16_t io[SIZE_IO >> 1];
254	uint16_t pram[SIZE_PALETTE_RAM >> 1];
255	uint16_t oam[SIZE_OAM >> 1];
256	uint16_t vram[SIZE_VRAM >> 1];
257	uint8_t iwram[SIZE_WORKING_IRAM];
258	uint8_t wram[SIZE_WORKING_RAM];
259};
260
261struct VFile;
262
263void GBASerialize(struct GBA* gba, struct GBASerializedState* state);
264void GBADeserialize(struct GBA* gba, struct GBASerializedState* state);
265
266bool GBASaveState(struct GBA* gba, int slot);
267bool GBALoadState(struct GBA* gba, int slot);
268
269struct GBASerializedState* GBAMapState(struct VFile* vf);
270void GBAUnmapState(struct VFile* vf, struct GBASerializedState* state);
271
272struct GBASerializedState* GBAAllocateState(void);
273void GBADeallocateState(struct GBASerializedState* state);
274
275struct GBAThread;
276void GBARecordFrame(struct GBAThread* thread);
277void GBARewind(struct GBAThread* thread, int nStates);
278
279#endif