src/gb/video.c (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#include "video.h"
7
8#include "core/sync.h"
9#include "core/thread.h"
10#include "core/tile-cache.h"
11#include "gb/gb.h"
12#include "gb/io.h"
13#include "gb/serialize.h"
14
15#include "util/memory.h"
16
17static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
18static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
19static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
20static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
21static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
22static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
23static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
24static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
25static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
26static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
27
28static void _cleanOAM(struct GBVideo* video, int y);
29
30static void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate);
31static void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate);
32static void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate);
33static void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate);
34static void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate);
35
36static struct GBVideoRenderer dummyRenderer = {
37 .init = GBVideoDummyRendererInit,
38 .deinit = GBVideoDummyRendererDeinit,
39 .writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
40 .writeVRAM = GBVideoDummyRendererWriteVRAM,
41 .writePalette = GBVideoDummyRendererWritePalette,
42 .drawRange = GBVideoDummyRendererDrawRange,
43 .finishScanline = GBVideoDummyRendererFinishScanline,
44 .finishFrame = GBVideoDummyRendererFinishFrame,
45 .getPixels = GBVideoDummyRendererGetPixels,
46 .putPixels = GBVideoDummyRendererPutPixels,
47};
48
49void GBVideoInit(struct GBVideo* video) {
50 video->renderer = &dummyRenderer;
51 video->renderer->cache = NULL;
52 video->vram = 0;
53 video->frameskip = 0;
54
55 video->modeEvent.context = video;
56 video->modeEvent.name = "GB Video Mode";
57 video->modeEvent.callback = NULL;
58 video->modeEvent.priority = 8;
59 video->frameEvent.context = video;
60 video->frameEvent.name = "GB Video Frame";
61 video->frameEvent.callback = _updateFrameCount;
62 video->frameEvent.priority = 9;
63}
64
65void GBVideoReset(struct GBVideo* video) {
66 video->ly = 0;
67 video->x = 0;
68 video->mode = 1;
69 video->stat = 1;
70
71 video->frameCounter = 0;
72 video->frameskipCounter = 0;
73
74 if (video->vram) {
75 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
76 }
77 video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
78 GBVideoSwitchBank(video, 0);
79 video->renderer->vram = video->vram;
80 memset(&video->oam, 0, sizeof(video->oam));
81 video->renderer->oam = &video->oam;
82 memset(&video->palette, 0, sizeof(video->palette));
83
84 video->renderer->deinit(video->renderer);
85 video->renderer->init(video->renderer, video->p->model);
86}
87
88void GBVideoDeinit(struct GBVideo* video) {
89 GBVideoAssociateRenderer(video, &dummyRenderer);
90 mappedMemoryFree(video->vram, GB_SIZE_VRAM);
91}
92
93void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
94 video->renderer->deinit(video->renderer);
95 renderer->cache = video->renderer->cache;
96 video->renderer = renderer;
97 renderer->vram = video->vram;
98 video->renderer->init(video->renderer, video->p->model);
99}
100
101void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate) {
102 struct GBVideo* video = context;
103 if (video->frameskipCounter <= 0) {
104 video->renderer->finishScanline(video->renderer, video->ly);
105 }
106 int lyc = video->p->memory.io[REG_LYC];
107 int32_t next;
108 ++video->ly;
109 video->p->memory.io[REG_LY] = video->ly;
110 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
111 if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
112 // TODO: Cache SCX & 7 in case it changes during mode 2
113 next = GB_VIDEO_MODE_2_LENGTH + (video->p->memory.io[REG_SCX] & 7);
114 video->mode = 2;
115 video->modeEvent.callback = _endMode2;
116 if (!GBRegisterSTATIsHblankIRQ(video->stat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
117 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
118 }
119 } else {
120 next = GB_VIDEO_HORIZONTAL_LENGTH;
121 video->mode = 1;
122 video->modeEvent.callback = _endMode1;
123
124 _updateFrameCount(timing, video, cyclesLate);
125
126 if (GBRegisterSTATIsVblankIRQ(video->stat) || GBRegisterSTATIsOAMIRQ(video->stat)) {
127 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
128 }
129 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
130
131 struct mCoreCallbacks* callbacks = video->p->coreCallbacks;
132 if (callbacks && callbacks->videoFrameEnded) {
133 callbacks->videoFrameEnded(callbacks->context);
134 }
135 }
136 if (!GBRegisterSTATIsHblankIRQ(video->stat) && GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
137 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
138 }
139 GBUpdateIRQs(video->p);
140 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
141 video->p->memory.io[REG_STAT] = video->stat;
142 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
143}
144
145void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
146 struct GBVideo* video = context;
147 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
148 return;
149 }
150 int lyc = video->p->memory.io[REG_LYC];
151 // TODO: One M-cycle delay
152 ++video->ly;
153 int32_t next;
154 if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
155 video->ly = 0;
156 video->p->memory.io[REG_LY] = video->ly;
157 next = GB_VIDEO_MODE_2_LENGTH + (video->p->memory.io[REG_SCX] & 7);
158 video->mode = 2;
159 video->modeEvent.callback = _endMode2;
160 if (GBRegisterSTATIsOAMIRQ(video->stat)) {
161 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
162 GBUpdateIRQs(video->p);
163 }
164 video->renderer->finishFrame(video->renderer);
165 if (video->p->memory.mbcType == GB_MBC7 && video->p->memory.rotation && video->p->memory.rotation->sample) {
166 video->p->memory.rotation->sample(video->p->memory.rotation);
167 }
168 } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
169 video->p->memory.io[REG_LY] = 0;
170 next = GB_VIDEO_HORIZONTAL_LENGTH - 8;
171 } else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
172 video->p->memory.io[REG_LY] = video->ly;
173 next = 8;
174 } else {
175 video->p->memory.io[REG_LY] = video->ly;
176 next = GB_VIDEO_HORIZONTAL_LENGTH;
177 }
178
179 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
180 video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
181 if (video->ly && GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->p->memory.io[REG_LY]) {
182 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
183 GBUpdateIRQs(video->p);
184 }
185 video->p->memory.io[REG_STAT] = video->stat;
186 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
187}
188
189void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
190 struct GBVideo* video = context;
191 _cleanOAM(video, video->ly);
192 video->x = 0;
193 video->dotClock = timing->masterCycles - cyclesLate;
194 int32_t next = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 11 - (video->p->memory.io[REG_SCX] & 7);
195 video->mode = 3;
196 video->modeEvent.callback = _endMode3;
197 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
198 video->p->memory.io[REG_STAT] = video->stat;
199 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
200}
201
202void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
203 struct GBVideo* video = context;
204 GBVideoProcessDots(video);
205 if (GBRegisterSTATIsHblankIRQ(video->stat)) {
206 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
207 GBUpdateIRQs(video->p);
208 }
209 if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
210 video->p->memory.hdmaRemaining = 0x10;
211 mTimingDeschedule(timing, &video->p->memory.hdmaEvent);
212 mTimingSchedule(timing, &video->p->memory.hdmaEvent, 0);
213 }
214 video->mode = 0;
215 video->modeEvent.callback = _endMode0;
216 video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
217 video->p->memory.io[REG_STAT] = video->stat;
218 int32_t next = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 11;
219 mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
220}
221
222void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate) {
223 UNUSED(cyclesLate);
224 struct GBVideo* video = context;
225 if (video->p->cpu->executionState != LR35902_CORE_FETCH) {
226 mTimingSchedule(timing, &video->frameEvent, 4 - ((video->p->cpu->executionState + 1) & 3));
227 return;
228 }
229
230 GBFrameEnded(video->p);
231 --video->frameskipCounter;
232 if (video->frameskipCounter < 0) {
233 mCoreSyncPostFrame(video->p->sync);
234 video->frameskipCounter = video->frameskip;
235 }
236 ++video->frameCounter;
237
238 // TODO: Move to common code
239 if (video->p->stream && video->p->stream->postVideoFrame) {
240 const color_t* pixels;
241 size_t stride;
242 video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);
243 video->p->stream->postVideoFrame(video->p->stream, pixels, stride);
244 }
245
246 struct mCoreCallbacks* callbacks = video->p->coreCallbacks;
247 if (callbacks && callbacks->videoFrameStarted) {
248 callbacks->videoFrameStarted(callbacks->context);
249 }
250
251 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
252 mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
253 }
254}
255
256static void _cleanOAM(struct GBVideo* video, int y) {
257 // TODO: GBC differences
258 // TODO: Optimize
259 video->objMax = 0;
260 int spriteHeight = 8;
261 if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
262 spriteHeight = 16;
263 }
264 int o = 0;
265 int i;
266 for (i = 0; i < 40; ++i) {
267 uint8_t oy = video->oam.obj[i].y;
268 if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
269 continue;
270 }
271 // TODO: Sort
272 video->objThisLine[o] = video->oam.obj[i];
273 ++o;
274 if (o == 10) {
275 break;
276 }
277 }
278 video->objMax = o;
279}
280
281void GBVideoProcessDots(struct GBVideo* video) {
282 if (video->mode != 3) {
283 return;
284 }
285 int oldX = video->x;
286 video->x = (video->p->timing.masterCycles - video->dotClock + video->p->cpu->cycles) >> video->p->doubleSpeed;
287 if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
288 video->x = GB_VIDEO_HORIZONTAL_PIXELS;
289 } else if (video->x < 0) {
290 mLOG(GB, FATAL, "Video dot clock went negative!");
291 video->x = oldX;
292 }
293 if (video->frameskipCounter <= 0) {
294 video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
295 }
296}
297
298void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
299 if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
300 video->mode = 2;
301 video->modeEvent.callback = _endMode2;
302 int32_t next = GB_VIDEO_MODE_2_LENGTH - 5; // TODO: Why is this fudge factor needed? Might be related to T-cycles for load/store differing
303 mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
304
305 video->ly = 0;
306 video->p->memory.io[REG_LY] = 0;
307 // TODO: Does this read as 0 for 4 T-cycles?
308 video->stat = GBRegisterSTATSetMode(video->stat, 2);
309 video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
310 if (GBRegisterSTATIsLYCIRQ(video->stat) && video->ly == video->p->memory.io[REG_LYC]) {
311 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
312 GBUpdateIRQs(video->p);
313 }
314 video->p->memory.io[REG_STAT] = video->stat;
315 mTimingDeschedule(&video->p->timing, &video->frameEvent);
316 }
317 if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
318 // TODO: Fix serialization; this gets internal and visible modes out of sync
319 video->stat = GBRegisterSTATSetMode(video->stat, 0);
320 video->p->memory.io[REG_STAT] = video->stat;
321 video->ly = 0;
322 video->p->memory.io[REG_LY] = 0;
323 mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
324 }
325 video->p->memory.io[REG_STAT] = video->stat;
326}
327
328void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
329 video->stat = (video->stat & 0x7) | (value & 0x78);
330 if (video->p->model == GB_MODEL_DMG && video->mode == 1) {
331 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
332 GBUpdateIRQs(video->p);
333 }
334}
335
336void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
337 if (video->mode == 2) {
338 video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
339 if (GBRegisterSTATIsLYCIRQ(video->stat) && value == video->ly) {
340 video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
341 GBUpdateIRQs(video->p);
342 }
343 }
344}
345
346void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
347 static const uint16_t dmgPalette[4] = { 0x7FFF, 0x56B5, 0x294A, 0x0000};
348 if (video->p->model < GB_MODEL_CGB) {
349 switch (address) {
350 case REG_BGP:
351 video->palette[0] = dmgPalette[value & 3];
352 video->palette[1] = dmgPalette[(value >> 2) & 3];
353 video->palette[2] = dmgPalette[(value >> 4) & 3];
354 video->palette[3] = dmgPalette[(value >> 6) & 3];
355 video->renderer->writePalette(video->renderer, 0, video->palette[0]);
356 video->renderer->writePalette(video->renderer, 1, video->palette[1]);
357 video->renderer->writePalette(video->renderer, 2, video->palette[2]);
358 video->renderer->writePalette(video->renderer, 3, video->palette[3]);
359 break;
360 case REG_OBP0:
361 video->palette[8 * 4 + 0] = dmgPalette[value & 3];
362 video->palette[8 * 4 + 1] = dmgPalette[(value >> 2) & 3];
363 video->palette[8 * 4 + 2] = dmgPalette[(value >> 4) & 3];
364 video->palette[8 * 4 + 3] = dmgPalette[(value >> 6) & 3];
365 video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
366 video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
367 video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
368 video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
369 break;
370 case REG_OBP1:
371 video->palette[9 * 4 + 0] = dmgPalette[value & 3];
372 video->palette[9 * 4 + 1] = dmgPalette[(value >> 2) & 3];
373 video->palette[9 * 4 + 2] = dmgPalette[(value >> 4) & 3];
374 video->palette[9 * 4 + 3] = dmgPalette[(value >> 6) & 3];
375 video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
376 video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
377 video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
378 video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
379 break;
380 }
381 } else {
382 switch (address) {
383 case REG_BCPD:
384 if (video->bcpIndex & 1) {
385 video->palette[video->bcpIndex >> 1] &= 0x00FF;
386 video->palette[video->bcpIndex >> 1] |= value << 8;
387 } else {
388 video->palette[video->bcpIndex >> 1] &= 0xFF00;
389 video->palette[video->bcpIndex >> 1] |= value;
390 }
391 video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
392 if (video->bcpIncrement) {
393 ++video->bcpIndex;
394 video->bcpIndex &= 0x3F;
395 video->p->memory.io[REG_BCPS] &= 0x80;
396 video->p->memory.io[REG_BCPS] |= video->bcpIndex;
397 }
398 video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
399 break;
400 case REG_OCPD:
401 if (video->ocpIndex & 1) {
402 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
403 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
404 } else {
405 video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
406 video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
407 }
408 video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
409 if (video->ocpIncrement) {
410 ++video->ocpIndex;
411 video->ocpIndex &= 0x3F;
412 video->p->memory.io[REG_OCPS] &= 0x80;
413 video->p->memory.io[REG_OCPS] |= video->ocpIndex;
414 }
415 video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
416 break;
417 }
418 }
419}
420
421void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
422 value &= 1;
423 video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
424 video->vramCurrentBank = value;
425}
426
427static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
428 UNUSED(renderer);
429 UNUSED(model);
430 // Nothing to do
431}
432
433static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
434 UNUSED(renderer);
435 // Nothing to do
436}
437
438static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
439 UNUSED(renderer);
440 UNUSED(address);
441 return value;
442}
443
444static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
445 if (renderer->cache) {
446 mTileCacheWriteVRAM(renderer->cache, address);
447 }
448}
449
450static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
451 UNUSED(value);
452 if (renderer->cache) {
453 mTileCacheWritePalette(renderer->cache, index << 1);
454 }
455}
456
457static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
458 UNUSED(renderer);
459 UNUSED(endX);
460 UNUSED(startX);
461 UNUSED(y);
462 UNUSED(obj);
463 UNUSED(oamMax);
464 // Nothing to do
465}
466
467static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
468 UNUSED(renderer);
469 UNUSED(y);
470 // Nothing to do
471}
472
473static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
474 UNUSED(renderer);
475 // Nothing to do
476}
477
478static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
479 UNUSED(renderer);
480 UNUSED(stride);
481 UNUSED(pixels);
482 // Nothing to do
483}
484
485static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
486 UNUSED(renderer);
487 UNUSED(stride);
488 UNUSED(pixels);
489 // Nothing to do
490}
491
492void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
493 STORE_16LE(video->x, 0, &state->video.x);
494 STORE_16LE(video->ly, 0, &state->video.ly);
495 STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
496 state->video.vramCurrentBank = video->vramCurrentBank;
497
498 GBSerializedVideoFlags flags = 0;
499 flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
500 flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
501 flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
502 flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
503 flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
504 state->video.flags = flags;
505 STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
506 STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
507
508 size_t i;
509 for (i = 0; i < 64; ++i) {
510 STORE_16LE(video->palette[i], i * 2, state->video.palette);
511 }
512
513 STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
514 STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
515
516 memcpy(state->vram, video->vram, GB_SIZE_VRAM);
517 memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
518}
519
520void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
521 LOAD_16LE(video->x, 0, &state->video.x);
522 LOAD_16LE(video->ly, 0, &state->video.ly);
523 LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
524 video->vramCurrentBank = state->video.vramCurrentBank;
525
526 GBSerializedVideoFlags flags = state->video.flags;
527 video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
528 video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
529 video->mode = GBSerializedVideoFlagsGetMode(flags);
530 LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
531 video->bcpIndex &= 0x3F;
532 LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
533 video->ocpIndex &= 0x3F;
534
535 switch (video->mode) {
536 case 0:
537 video->modeEvent.callback = _endMode0;
538 break;
539 case 1:
540 video->modeEvent.callback = _endMode1;
541 break;
542 case 2:
543 video->modeEvent.callback = _endMode2;
544 break;
545 case 3:
546 video->modeEvent.callback = _endMode3;
547 break;
548 }
549
550 uint32_t when;
551 mTimingDeschedule(&video->p->timing, &video->modeEvent);
552 if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
553 LOAD_32LE(when, 0, &state->video.nextMode);
554 mTimingSchedule(&video->p->timing, &video->modeEvent, when);
555 }
556 mTimingDeschedule(&video->p->timing, &video->frameEvent);
557 if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
558 LOAD_32LE(when, 0, &state->video.nextFrame);
559 mTimingSchedule(&video->p->timing, &video->frameEvent, when);
560 }
561
562 size_t i;
563 for (i = 0; i < 64; ++i) {
564 LOAD_16LE(video->palette[i], i * 2, state->video.palette);
565 video->renderer->writePalette(video->renderer, i, video->palette[i]);
566 }
567
568 memcpy(video->vram, state->vram, GB_SIZE_VRAM);
569 memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
570
571 _cleanOAM(video, video->ly);
572 GBVideoSwitchBank(video, video->vramCurrentBank);
573}