src/gb/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 GB_SERIALIZE_H
7#define GB_SERIALIZE_H
8
9#include "util/common.h"
10
11#include "core/core.h"
12#include "gb/gb.h"
13
14extern const uint32_t GB_SAVESTATE_MAGIC;
15extern const uint32_t GB_SAVESTATE_VERSION;
16
17mLOG_DECLARE_CATEGORY(GB_STATE);
18
19/* Savestate format:
20 * 0x00000 - 0x00003: Version Magic (0x01000001)
21 * 0x00004 - 0x00007: ROM CRC32
22 * 0x00008: Game Boy model
23 * 0x00009 - 0x0000F: Reserved (leave zero)
24 * 0x00010 - 0x0001F: Game title/code (e.g. PM_CRYSTALBYTE)
25 * 0x00020 - 0x00047: CPU state:
26 * | 0x00020: A register
27 * | 0x00021: F register
28 * | 0x00022: B register
29 * | 0x00023: C register
30 * | 0x00024: D register
31 * | 0x00025: E register
32 * | 0x00026: H register
33 * | 0x00027: L register
34 * | 0x00028 - 0z00029: SP register
35 * | 0x0002A - 0z0002B: PC register
36 * | 0x0002C - 0x0002F: Cycles since last event
37 * | 0x00030 - 0x00033: Cycles until next event
38 * | 0x00034 - 0x00035: Reserved (current instruction)
39 * | 0x00036 - 0x00037: Index address
40 * | 0x00038: Bus value
41 * | 0x00039: Execution state
42 * | 0x0003A - 0x0003B: IRQ vector
43 * | 0x0003C - 0x0003F: EI pending cycles
44 * | 0x00040 - 0x00043: Reserved (DI pending cycles)
45 * | 0x00044 - 0x00047: Flags
46 * | bit 0: Is condition met?
47 * | bit 1: Is condition IRQ pending?
48 * | bit 2: Double speed
49 * | bits 3 - 31: Reserved
50 * 0x00048 - 0x0005B: Audio channel 1/framer state
51 * | 0x00048 - 0x0004B: Envelepe timing
52 * | bits 0 - 6: Remaining length
53 * | bits 7 - 9: Next step
54 * | bits 10 - 20: Shadow frequency register
55 * | bits 21 - 31: Reserved
56 * | 0x0004C - 0x0004F: Next frame
57 * | 0x00050 - 0x00057: Reserved
58 * | 0x00058 - 0x0005B: Next event
59 * 0x0005C - 0x0006B: Audio channel 2 state
60 * | 0x0005C - 0x0005F: Envelepe timing
61 * | bits 0 - 2: Remaining length
62 * | bits 3 - 5: Next step
63 * | bits 6 - 31: Reserved
64 * | 0x00060 - 0x00067: Reserved
65 * | 0x00068 - 0x0006B: Next event
66 * 0x0006C - 0x00093: Audio channel 3 state
67 * | 0x0006C - 0x0008B: Wave banks
68 * | 0x0008C - 0x0008D: Remaining length
69 * | 0x0008E - 0x0008F: Reserved
70 * | 0x00090 - 0x00093: Next event
71 * 0x00094 - 0x000A3: Audio channel 4 state
72 * | 0x00094 - 0x00097: Linear feedback shift register state
73 * | 0x00098 - 0x0009B: Envelepe timing
74 * | bits 0 - 2: Remaining length
75 * | bits 3 - 5: Next step
76 * | bits 6 - 31: Reserved
77 * | 0x00098 - 0x0009F: Reserved
78 * | 0x000A0 - 0x000A3: Next event
79 * 0x000A4 - 0x000B7: Audio miscellaneous state
80 * | TODO: Fix this, they're in big-endian order, but field is little-endian
81 * | 0x000A4: Channel 1 envelope state
82 * | bits 0 - 3: Current volume
83 * | bits 4 - 5: Is dead?
84 * | bit 6: Is high?
85 * | 0x000A5: Channel 2 envelope state
86 * | bits 0 - 3: Current volume
87 * | bits 4 - 5: Is dead?
88 * | bit 6: Is high?
89* | bits 7: Reserved
90 * | 0x000A6: Channel 4 envelope state
91 * | bits 0 - 3: Current volume
92 * | bits 4 - 5: Is dead?
93 * | bit 6: Is high?
94* | bits 7: Reserved
95 * | 0x000A7: Miscellaneous audio flags
96 * | bits 0 - 3: Current frame
97 * | bit 4: Is channel 1 sweep enabled?
98 * | bit 5: Has channel 1 sweep occurred?
99 * | bits 6 - 7: Reserved
100 * | 0x000A8 - 0x000AB: Next event
101 * | 0x000AC - 0x000AF: Event diff
102 * | 0x000B0 - 0x000B3: Next sample
103 * 0x000B4 - 0x000153: Video state
104 * | 0x000B4 - 0x000B5: Current x
105 * | 0x000B6 - 0x000B7: Current y (ly)
106 * | 0x000B8 - 0x000BB: Next event
107 * | 0x000BC - 0x000BF: Event diff
108 * | 0x000C0 - 0x000C3: Next mode
109 * | 0x000C4 - 0x000C7: Dot cycle counter
110 * | 0x000C8 - 0x000CB: Frame counter
111 * | 0x000CC: Current VRAM bank
112 * | 0x000CD: Palette flags
113 * | bit 0: BCP increment
114 * | bit 1: OCP increment
115 * | bits 2 - 7: Reserved
116 * | 0x000CE - 0x000CF: Reserved
117 * | 0x000D0 - 0x000D1: BCP index
118 * | 0x000D1 - 0x000D3: OCP index
119 * | 0x000D4 - 0x00153: Palette entries
120 * 0x00154 - 0x000167: Timer state
121 * | 0x00154 - 0x00157: Next event
122 * | 0x00158 - 0x0015B: Event diff
123 * | 0x0015C - 0x0015F: Next DIV
124 * | 0x00160 - 0x00163: Next TIMA
125 * | 0x00164 - 0x00167: TIMA period
126 * 0x000168 - 0x000197: Memory state
127 * | 0x00168 - 0x00169: Current ROM bank
128 * | 0x0016A: Current WRAM bank
129 * | 0x0016B: Current SRAM bank
130 * | 0x0016C - 0x0016F: Next DMA
131 * | 0x00170 - 0x00171: Next DMA source
132 * | 0x00172 - 0x00173: Next DMA destination
133 * | 0x00174 - 0x00177: Next HDMA
134 * | 0x00178 - 0x00179: Next HDMA source
135 * | 0x0017A - 0x0017B: Next HDMA destination
136 * | 0x0017C - 0x0017D: HDMA remaining
137 * | 0x0017E: DMA remaining
138 * | 0x0017F - 0x00183: RTC registers
139 * | 0x00184 - 0x00193: MBC state (TODO)
140 * | 0x00194 - 0x00195: Flags
141 * | bit 0: SRAM accessable
142 * | bit 1: RTC accessible
143 * | bit 2: RTC latched
144 * | bit 3: IME
145 * | bit 4: Is HDMA active?
146 * | bits 5 - 7: Active RTC register
147 * | 0x00196 - 0x00197: Reserved (leave zero)
148 * 0x00198 - 0x0019F: Savestate creation time (usec since 1970)
149 * 0x001A0 - 0x0025F: Reserved (leave zero)
150 * 0x00260 - 0x002FF: OAM
151 * 0x00300 - 0x0037F: I/O memory
152 * 0x00380 - 0x003FE: HRAM
153 * 0x003FF: Interrupts enabled
154 * 0x00400 - 0x043FF: VRAM
155 * 0x04400 - 0x0C3FF: WRAM
156 * Total size: 0xC400 (50,176) bytes
157*/
158
159DECL_BITFIELD(GBSerializedAudioFlags, uint32_t);
160DECL_BITS(GBSerializedAudioFlags, Ch1Volume, 0, 4);
161DECL_BITS(GBSerializedAudioFlags, Ch1Dead, 4, 2);
162DECL_BIT(GBSerializedAudioFlags, Ch1Hi, 6);
163DECL_BITS(GBSerializedAudioFlags, Ch2Volume, 8, 4);
164DECL_BITS(GBSerializedAudioFlags, Ch2Dead, 12, 2);
165DECL_BIT(GBSerializedAudioFlags, Ch2Hi, 14);
166DECL_BITS(GBSerializedAudioFlags, Ch4Volume, 16, 4);
167DECL_BITS(GBSerializedAudioFlags, Ch4Dead, 20, 2);
168DECL_BITS(GBSerializedAudioFlags, Frame, 22, 3);
169DECL_BIT(GBSerializedAudioFlags, Ch1SweepEnabled, 25);
170DECL_BIT(GBSerializedAudioFlags, Ch1SweepOccurred, 26);
171
172DECL_BITFIELD(GBSerializedAudioEnvelope, uint32_t);
173DECL_BITS(GBSerializedAudioEnvelope, Length, 0, 7);
174DECL_BITS(GBSerializedAudioEnvelope, NextStep, 7, 3);
175DECL_BITS(GBSerializedAudioEnvelope, Frequency, 10, 11);
176
177struct GBSerializedPSGState {
178 struct {
179 GBSerializedAudioEnvelope envelope;
180 int32_t nextFrame;
181 int32_t reserved[2];
182 int32_t nextEvent;
183 } ch1;
184 struct {
185 GBSerializedAudioEnvelope envelope;
186 int32_t reserved[2];
187 int32_t nextEvent;
188 } ch2;
189 struct {
190 uint32_t wavebanks[8];
191 int16_t length;
192 int16_t reserved;
193 int32_t nextEvent;
194 } ch3;
195 struct {
196 int32_t lfsr;
197 GBSerializedAudioEnvelope envelope;
198 int32_t reserved;
199 int32_t nextEvent;
200 } ch4;
201};
202
203DECL_BITFIELD(GBSerializedCpuFlags, uint32_t);
204DECL_BIT(GBSerializedCpuFlags, Condition, 0);
205DECL_BIT(GBSerializedCpuFlags, IrqPending, 1);
206DECL_BIT(GBSerializedCpuFlags, DoubleSpeed, 2);
207
208
209DECL_BITFIELD(GBSerializedVideoFlags, uint8_t);
210DECL_BIT(GBSerializedVideoFlags, BcpIncrement, 0);
211DECL_BIT(GBSerializedVideoFlags, OcpIncrement, 1);
212
213DECL_BITFIELD(GBSerializedMBC7Flags, uint8_t);
214DECL_BITS(GBSerializedMBC7Flags, Command, 0, 2);
215DECL_BIT(GBSerializedMBC7Flags, Writable, 2);
216
217DECL_BITFIELD(GBSerializedMemoryFlags, uint16_t);
218DECL_BIT(GBSerializedMemoryFlags, SramAccess, 0);
219DECL_BIT(GBSerializedMemoryFlags, RtcAccess, 1);
220DECL_BIT(GBSerializedMemoryFlags, RtcLatched, 2);
221DECL_BIT(GBSerializedMemoryFlags, Ime, 3);
222DECL_BIT(GBSerializedMemoryFlags, IsHdma, 4);
223DECL_BITS(GBSerializedMemoryFlags, ActiveRtcReg, 5, 3);
224
225#pragma pack(push, 1)
226struct GBSerializedState {
227 uint32_t versionMagic;
228 uint32_t romCrc32;
229 uint8_t model;
230 uint8_t reservedHeader[7];
231
232 char title[16];
233
234 struct {
235 uint8_t a;
236 uint8_t f;
237 uint8_t b;
238 uint8_t c;
239 uint8_t d;
240 uint8_t e;
241 uint8_t h;
242 uint8_t l;
243 uint16_t sp;
244 uint16_t pc;
245
246 int32_t cycles;
247 int32_t nextEvent;
248
249 uint16_t reservedInstruction;
250 uint16_t index;
251 uint8_t bus;
252 uint8_t executionState;
253
254 uint16_t irqVector;
255
256 int32_t eiPending;
257 int32_t reservedDiPending;
258 GBSerializedCpuFlags flags;
259 } cpu;
260
261 struct {
262 struct GBSerializedPSGState psg;
263 GBSerializedAudioFlags flags;
264 int32_t nextEvent;
265 int32_t eventDiff;
266 int32_t nextSample;
267 } audio;
268
269 struct {
270 int16_t x;
271 int16_t ly;
272 int32_t nextEvent;
273 int32_t eventDiff;
274 int32_t nextMode;
275 int32_t dotCounter;
276 int32_t frameCounter;
277
278 uint8_t vramCurrentBank;
279 GBSerializedVideoFlags flags;
280 uint16_t reserved;
281
282 uint16_t bcpIndex;
283 uint16_t ocpIndex;
284
285 uint16_t palette[64];
286 } video;
287
288 struct {
289 int32_t nextEvent;
290 int32_t eventDiff;
291
292 int32_t nextDiv;
293 int32_t nextTima;
294 int32_t timaPeriod;
295 } timer;
296
297 struct {
298 uint16_t currentBank;
299 uint8_t wramCurrentBank;
300 uint8_t sramCurrentBank;
301
302 int32_t dmaNext;
303 uint16_t dmaSource;
304 uint16_t dmaDest;
305
306 int32_t hdmaNext;
307 uint16_t hdmaSource;
308 uint16_t hdmaDest;
309
310 uint16_t hdmaRemaining;
311 uint8_t dmaRemaining;
312 uint8_t rtcRegs[5];
313
314 union {
315 struct {
316 uint32_t mode;
317 } mbc1;
318 struct {
319 int8_t machineState;
320 GBMBC7Field field;
321 int8_t address;
322 uint8_t srBits;
323 uint32_t sr;
324 GBSerializedMBC7Flags flags;
325 } mbc7;
326 struct {
327 uint8_t reserved[16];
328 } padding;
329 };
330
331 GBSerializedMemoryFlags flags;
332 uint16_t reserved;
333 } memory;
334
335 uint64_t creationUsec;
336
337 uint32_t reserved[48];
338
339 uint8_t oam[GB_SIZE_OAM];
340
341 uint8_t io[GB_SIZE_IO];
342 uint8_t hram[GB_SIZE_HRAM];
343 uint8_t ie;
344
345 uint8_t vram[GB_SIZE_VRAM];
346 uint8_t wram[GB_SIZE_WORKING_RAM];
347};
348#pragma pack(pop)
349
350bool GBDeserialize(struct GB* gb, const struct GBSerializedState* state);
351void GBSerialize(struct GB* gb, struct GBSerializedState* state);
352
353#endif