all repos — mgba @ 575a37fd832f457b3ac6684e37f79928dcf9da9f

mGBA Game Boy Advance Emulator

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 GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
316	static const uint16_t dmgPalette[4] = { 0x7FFF, 0x56B5, 0x294A, 0x0000};
317	if (video->p->model < GB_MODEL_CGB) {
318		switch (address) {
319		case REG_BGP:
320			video->palette[0] = dmgPalette[value & 3];
321			video->palette[1] = dmgPalette[(value >> 2) & 3];
322			video->palette[2] = dmgPalette[(value >> 4) & 3];
323			video->palette[3] = dmgPalette[(value >> 6) & 3];
324			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
325			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
326			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
327			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
328			break;
329		case REG_OBP0:
330			video->palette[8 * 4 + 0] = dmgPalette[value & 3];
331			video->palette[8 * 4 + 1] = dmgPalette[(value >> 2) & 3];
332			video->palette[8 * 4 + 2] = dmgPalette[(value >> 4) & 3];
333			video->palette[8 * 4 + 3] = dmgPalette[(value >> 6) & 3];
334			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
335			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
336			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
337			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
338			break;
339		case REG_OBP1:
340			video->palette[9 * 4 + 0] = dmgPalette[value & 3];
341			video->palette[9 * 4 + 1] = dmgPalette[(value >> 2) & 3];
342			video->palette[9 * 4 + 2] = dmgPalette[(value >> 4) & 3];
343			video->palette[9 * 4 + 3] = dmgPalette[(value >> 6) & 3];
344			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
345			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
346			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
347			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
348			break;
349		}
350	} else {
351		switch (address) {
352		case REG_BCPD:
353			if (video->bcpIndex & 1) {
354				video->palette[video->bcpIndex >> 1] &= 0x00FF;
355				video->palette[video->bcpIndex >> 1] |= value << 8;
356			} else {
357				video->palette[video->bcpIndex >> 1] &= 0xFF00;
358				video->palette[video->bcpIndex >> 1] |= value;
359			}
360			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
361			if (video->bcpIncrement) {
362				++video->bcpIndex;
363				video->bcpIndex &= 0x3F;
364				video->p->memory.io[REG_BCPS] &= 0x80;
365				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
366			}
367			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
368			break;
369		case REG_OCPD:
370			if (video->ocpIndex & 1) {
371				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
372				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
373			} else {
374				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
375				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
376			}
377			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
378			if (video->ocpIncrement) {
379				++video->ocpIndex;
380				video->ocpIndex &= 0x3F;
381				video->p->memory.io[REG_OCPS] &= 0x80;
382				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
383			}
384			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
385			break;
386		}
387	}
388}
389
390void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
391	value &= 1;
392	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
393	video->vramCurrentBank = value;
394}
395
396static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
397	UNUSED(renderer);
398	UNUSED(model);
399	// Nothing to do
400}
401
402static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
403	UNUSED(renderer);
404	// Nothing to do
405}
406
407static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
408	UNUSED(renderer);
409	UNUSED(address);
410	return value;
411}
412
413static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
414	UNUSED(renderer);
415	UNUSED(index);
416	UNUSED(value);
417}
418
419static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
420	UNUSED(renderer);
421	UNUSED(endX);
422	UNUSED(startX);
423	UNUSED(y);
424	UNUSED(obj);
425	UNUSED(oamMax);
426	// Nothing to do
427}
428
429static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
430	UNUSED(renderer);
431	UNUSED(y);
432	// Nothing to do
433}
434
435static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
436	UNUSED(renderer);
437	// Nothing to do
438}
439
440static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
441	UNUSED(renderer);
442	UNUSED(stride);
443	UNUSED(pixels);
444	// Nothing to do
445}
446
447static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
448	UNUSED(renderer);
449	UNUSED(stride);
450	UNUSED(pixels);
451	// Nothing to do
452}
453
454void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
455	STORE_16LE(video->x, 0, &state->video.x);
456	STORE_16LE(video->ly, 0, &state->video.ly);
457	STORE_32LE(video->nextEvent, 0, &state->video.nextEvent);
458	STORE_32LE(video->eventDiff, 0, &state->video.eventDiff);
459	STORE_32LE(video->nextMode, 0, &state->video.nextMode);
460	STORE_32LE(video->dotCounter, 0, &state->video.dotCounter);
461	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
462	state->video.vramCurrentBank = video->vramCurrentBank;
463
464	GBSerializedVideoFlags flags = 0;
465	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
466	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
467	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
468	state->video.flags = flags;
469	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
470	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
471
472	size_t i;
473	for (i = 0; i < 64; ++i) {
474		STORE_16LE(video->palette[i], i * 2, state->video.palette);
475	}
476
477	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
478	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
479}
480
481void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
482	LOAD_16LE(video->x, 0, &state->video.x);
483	LOAD_16LE(video->ly, 0, &state->video.ly);
484	LOAD_32LE(video->nextEvent, 0, &state->video.nextEvent);
485	LOAD_32LE(video->eventDiff, 0, &state->video.eventDiff);
486	LOAD_32LE(video->nextMode, 0, &state->video.nextMode);
487	LOAD_32LE(video->dotCounter, 0, &state->video.dotCounter);
488	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
489	video->vramCurrentBank = state->video.vramCurrentBank;
490
491	GBSerializedVideoFlags flags = state->video.flags;
492	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
493	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
494	video->mode = GBSerializedVideoFlagsGetMode(flags);
495	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
496	video->bcpIndex &= 0x3F;
497	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
498	video->ocpIndex &= 0x3F;
499
500	size_t i;
501	for (i = 0; i < 64; ++i) {
502		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
503		video->renderer->writePalette(video->renderer, i, video->palette[i]);
504	}
505
506	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
507	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
508
509	_cleanOAM(video, video->ly);
510	GBVideoSwitchBank(video, video->vramCurrentBank);
511}