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