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