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