all repos — mgba @ 6f9a0d9758537b80df5291bd05dff26a626113ff

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