all repos — mgba @ e27963bd29653427b99a97742c48a79e7d3a887a

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 <mgba/internal/gb/video.h>
  7
  8#include <mgba/core/sync.h>
  9#include <mgba/core/thread.h>
 10#include <mgba/core/cache-set.h>
 11#include <mgba/internal/gb/gb.h>
 12#include <mgba/internal/gb/io.h>
 13#include <mgba/internal/gb/renderers/cache-set.h>
 14#include <mgba/internal/gb/serialize.h>
 15#include <mgba/internal/sm83/sm83.h>
 16
 17#include <mgba-util/memory.h>
 18
 19mLOG_DEFINE_CATEGORY(GB_VIDEO, "GB Video", "gb.video");
 20
 21static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders);
 22static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
 23static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 24static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data);
 25static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
 26static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
 27static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam);
 28static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
 29static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
 30static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
 31static void GBVideoDummyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable);
 32static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
 33static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
 34
 35static void _cleanOAM(struct GBVideo* video, int y);
 36
 37static void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate);
 38static void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate);
 39static void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate);
 40static void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate);
 41static void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate);
 42
 43void GBVideoInit(struct GBVideo* video) {
 44	video->renderer = NULL;
 45	video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
 46	video->frameskip = 0;
 47
 48	video->modeEvent.context = video;
 49	video->modeEvent.name = "GB Video Mode";
 50	video->modeEvent.callback = NULL;
 51	video->modeEvent.priority = 8;
 52	video->frameEvent.context = video;
 53	video->frameEvent.name = "GB Video Frame";
 54	video->frameEvent.callback = _updateFrameCount;
 55	video->frameEvent.priority = 9;
 56
 57	video->dmgPalette[0] = 0x7FFF;
 58	video->dmgPalette[1] = 0x56B5;
 59	video->dmgPalette[2] = 0x294A;
 60	video->dmgPalette[3] = 0x0000;
 61	video->dmgPalette[4] = 0x7FFF;
 62	video->dmgPalette[5] = 0x56B5;
 63	video->dmgPalette[6] = 0x294A;
 64	video->dmgPalette[7] = 0x0000;
 65	video->dmgPalette[8] = 0x7FFF;
 66	video->dmgPalette[9] = 0x56B5;
 67	video->dmgPalette[10] = 0x294A;
 68	video->dmgPalette[11] = 0x0000;
 69
 70	video->sgbBorders = true;
 71}
 72
 73void GBVideoReset(struct GBVideo* video) {
 74	video->ly = 0;
 75	video->x = 0;
 76	video->mode = 1;
 77	video->stat = 1;
 78
 79	video->frameCounter = 0;
 80	video->frameskipCounter = 0;
 81
 82	GBVideoSwitchBank(video, 0);
 83	video->renderer->vram = video->vram;
 84	memset(&video->oam, 0, sizeof(video->oam));
 85	video->renderer->oam = &video->oam;
 86	memset(&video->palette, 0, sizeof(video->palette));
 87
 88	if (video->p->model & GB_MODEL_SGB) {
 89		video->renderer->sgbCharRam = anonymousMemoryMap(SGB_SIZE_CHAR_RAM);
 90		video->renderer->sgbMapRam = anonymousMemoryMap(SGB_SIZE_MAP_RAM);
 91		video->renderer->sgbPalRam = anonymousMemoryMap(SGB_SIZE_PAL_RAM);
 92		video->renderer->sgbAttributeFiles = anonymousMemoryMap(SGB_SIZE_ATF_RAM);
 93		video->renderer->sgbAttributes = malloc(90 * 45);
 94		memset(video->renderer->sgbAttributes, 0, 90 * 45);
 95		video->sgbCommandHeader = 0;
 96		video->sgbBufferIndex = 0;
 97	} else {
 98		video->renderer->sgbCharRam = NULL;
 99		video->renderer->sgbMapRam = NULL;
100		video->renderer->sgbPalRam = NULL;
101		video->renderer->sgbAttributes = NULL;
102		video->renderer->sgbAttributeFiles = NULL;
103	}
104
105	video->palette[0] = video->dmgPalette[0];
106	video->palette[1] = video->dmgPalette[1];
107	video->palette[2] = video->dmgPalette[2];
108	video->palette[3] = video->dmgPalette[3];
109	video->palette[8 * 4 + 0] = video->dmgPalette[4];
110	video->palette[8 * 4 + 1] = video->dmgPalette[5];
111	video->palette[8 * 4 + 2] = video->dmgPalette[6];
112	video->palette[8 * 4 + 3] = video->dmgPalette[7];
113	video->palette[9 * 4 + 0] = video->dmgPalette[8];
114	video->palette[9 * 4 + 1] = video->dmgPalette[9];
115	video->palette[9 * 4 + 2] = video->dmgPalette[10];
116	video->palette[9 * 4 + 3] = video->dmgPalette[11];
117
118	if (!video->renderer) {
119		mLOG(GB_VIDEO, FATAL, "No renderer associated");
120		return;
121	}
122
123	video->renderer->deinit(video->renderer);
124	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
125
126	video->renderer->writePalette(video->renderer, 0, video->palette[0]);
127	video->renderer->writePalette(video->renderer, 1, video->palette[1]);
128	video->renderer->writePalette(video->renderer, 2, video->palette[2]);
129	video->renderer->writePalette(video->renderer, 3, video->palette[3]);
130	video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
131	video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
132	video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
133	video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
134	video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
135	video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
136	video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
137	video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
138}
139
140void GBVideoDeinit(struct GBVideo* video) {
141	video->renderer->deinit(video->renderer);
142	mappedMemoryFree(video->vram, GB_SIZE_VRAM);
143	if (video->renderer->sgbCharRam) {
144		mappedMemoryFree(video->renderer->sgbCharRam, SGB_SIZE_CHAR_RAM);
145		video->renderer->sgbCharRam = NULL;
146	}
147	if (video->renderer->sgbMapRam) {
148		mappedMemoryFree(video->renderer->sgbMapRam, SGB_SIZE_MAP_RAM);
149		video->renderer->sgbMapRam = NULL;
150	}
151	if (video->renderer->sgbPalRam) {
152		mappedMemoryFree(video->renderer->sgbPalRam, SGB_SIZE_PAL_RAM);
153		video->renderer->sgbPalRam = NULL;
154	}
155	if (video->renderer->sgbAttributeFiles) {
156		mappedMemoryFree(video->renderer->sgbAttributeFiles, SGB_SIZE_ATF_RAM);
157		video->renderer->sgbAttributeFiles = NULL;
158	}
159	if (video->renderer->sgbAttributes) {
160		free(video->renderer->sgbAttributes);
161		video->renderer->sgbAttributes = NULL;
162	}
163}
164
165void GBVideoDummyRendererCreate(struct GBVideoRenderer* renderer) {
166	static const struct GBVideoRenderer dummyRenderer = {
167		.init = GBVideoDummyRendererInit,
168		.deinit = GBVideoDummyRendererDeinit,
169		.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
170		.writeSGBPacket = GBVideoDummyRendererWriteSGBPacket,
171		.writeVRAM = GBVideoDummyRendererWriteVRAM,
172		.writeOAM = GBVideoDummyRendererWriteOAM,
173		.writePalette = GBVideoDummyRendererWritePalette,
174		.drawRange = GBVideoDummyRendererDrawRange,
175		.finishScanline = GBVideoDummyRendererFinishScanline,
176		.finishFrame = GBVideoDummyRendererFinishFrame,
177		.enableSGBBorder = GBVideoDummyRendererEnableSGBBorder,
178		.getPixels = GBVideoDummyRendererGetPixels,
179		.putPixels = GBVideoDummyRendererPutPixels,
180	};
181	memcpy(renderer, &dummyRenderer, sizeof(*renderer));
182}
183
184void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
185	if (video->renderer) {
186		video->renderer->deinit(video->renderer);
187		renderer->cache = video->renderer->cache;
188		renderer->sgbRenderMode = video->renderer->sgbRenderMode;
189		renderer->sgbCharRam = video->renderer->sgbCharRam;
190		renderer->sgbMapRam = video->renderer->sgbMapRam;
191		renderer->sgbPalRam = video->renderer->sgbPalRam;
192		renderer->sgbAttributeFiles = video->renderer->sgbAttributeFiles;
193		renderer->sgbAttributes = video->renderer->sgbAttributes;
194	} else {
195		renderer->cache = NULL;
196		renderer->sgbRenderMode = 0;
197		renderer->sgbCharRam = NULL;
198		renderer->sgbMapRam = NULL;
199		renderer->sgbPalRam = NULL;
200		renderer->sgbAttributeFiles = NULL;
201		renderer->sgbAttributes = NULL;
202	}
203	video->renderer = renderer;
204	renderer->vram = video->vram;
205	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
206}
207
208static bool _statIRQAsserted(GBRegisterSTAT stat) {
209	// TODO: variable for the IRQ line value?
210	if (GBRegisterSTATIsLYCIRQ(stat) && GBRegisterSTATIsLYC(stat)) {
211		return true;
212	}
213	switch (GBRegisterSTATGetMode(stat)) {
214	case 0:
215		if (GBRegisterSTATIsHblankIRQ(stat)) {
216			return true;
217		}
218		break;
219	case 1:
220		if (GBRegisterSTATIsVblankIRQ(stat)) {
221			return true;
222		}
223		break;
224	case 2:
225		if (GBRegisterSTATIsOAMIRQ(stat)) {
226			return true;
227		}
228		break;
229	case 3:
230		break;
231	}
232	return false;
233}
234
235void GBVideoSkipBIOS(struct GBVideo* video) {
236	video->mode = 1;
237	video->modeEvent.callback = _endMode1;
238
239	int32_t next;
240	if (video->p->model == GB_MODEL_CGB) {
241		video->ly = GB_VIDEO_VERTICAL_PIXELS;
242		video->p->memory.io[REG_LY] = video->ly;
243		video->stat = GBRegisterSTATClearLYC(video->stat);
244		next = 20;
245	} else {
246		video->ly = GB_VIDEO_VERTICAL_TOTAL_PIXELS;
247		video->p->memory.io[REG_LY] = 0;
248		next = 56;
249	}
250	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
251
252	video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
253	GBUpdateIRQs(video->p);
254	video->p->memory.io[REG_STAT] = video->stat;
255	mTimingDeschedule(&video->p->timing, &video->modeEvent);
256	mTimingSchedule(&video->p->timing, &video->modeEvent, next);
257}
258
259void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate) {
260	struct GBVideo* video = context;
261	if (video->frameskipCounter <= 0) {
262		video->renderer->finishScanline(video->renderer, video->ly);
263	}
264	int lyc = video->p->memory.io[REG_LYC];
265	int32_t next;
266	++video->ly;
267	video->p->memory.io[REG_LY] = video->ly;
268	GBRegisterSTAT oldStat = video->stat;
269	if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
270		next = GB_VIDEO_MODE_2_LENGTH;
271		video->mode = 2;
272		video->modeEvent.callback = _endMode2;
273	} else {
274		next = GB_VIDEO_HORIZONTAL_LENGTH;
275		video->mode = 1;
276		video->modeEvent.callback = _endMode1;
277
278		mTimingDeschedule(&video->p->timing, &video->frameEvent);
279		mTimingSchedule(&video->p->timing, &video->frameEvent, -cyclesLate);
280
281		if (!_statIRQAsserted(oldStat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
282			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
283		}
284		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
285	}
286	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
287	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
288		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
289	}
290
291	// LYC stat is delayed 1 T-cycle
292	oldStat = video->stat;
293	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
294	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
295		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
296	}
297
298	GBUpdateIRQs(video->p);
299	video->p->memory.io[REG_STAT] = video->stat;
300	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
301}
302
303void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
304	struct GBVideo* video = context;
305	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
306		return;
307	}
308	int lyc = video->p->memory.io[REG_LYC];
309	// TODO: One M-cycle delay
310	++video->ly;
311	int32_t next;
312	if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
313		video->ly = 0;
314		video->p->memory.io[REG_LY] = video->ly;
315		next = GB_VIDEO_MODE_2_LENGTH;
316		video->mode = 2;
317		video->modeEvent.callback = _endMode2;
318	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
319		video->p->memory.io[REG_LY] = 0;
320		next = GB_VIDEO_HORIZONTAL_LENGTH - 8;
321	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
322		video->p->memory.io[REG_LY] = video->ly;
323		next = 8;
324	} else {
325		video->p->memory.io[REG_LY] = video->ly;
326		next = GB_VIDEO_HORIZONTAL_LENGTH;
327	}
328
329	GBRegisterSTAT oldStat = video->stat;
330	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
331	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
332	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
333		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
334		GBUpdateIRQs(video->p);
335	}
336	video->p->memory.io[REG_STAT] = video->stat;
337	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
338}
339
340void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
341	struct GBVideo* video = context;
342	_cleanOAM(video, video->ly);
343	video->x = -(video->p->memory.io[REG_SCX] & 7);
344	video->dotClock = mTimingCurrentTime(timing) - cyclesLate + 5 - (video->x << video->p->doubleSpeed);
345	int32_t next = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 6 - video->x;
346	video->mode = 3;
347	video->modeEvent.callback = _endMode3;
348	GBRegisterSTAT oldStat = video->stat;
349	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
350	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
351		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
352		GBUpdateIRQs(video->p);
353	}
354	video->p->memory.io[REG_STAT] = video->stat;
355	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
356}
357
358void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
359	struct GBVideo* video = context;
360	GBVideoProcessDots(video, cyclesLate);
361	if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
362		video->p->memory.hdmaRemaining = 0x10;
363		video->p->cpuBlocked = true;
364		mTimingDeschedule(timing, &video->p->memory.hdmaEvent);
365		mTimingSchedule(timing, &video->p->memory.hdmaEvent, 0);
366	}
367	video->mode = 0;
368	video->modeEvent.callback = _endMode0;
369	GBRegisterSTAT oldStat = video->stat;
370	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
371	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
372		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
373		GBUpdateIRQs(video->p);
374	}
375	video->p->memory.io[REG_STAT] = video->stat;
376	// TODO: Cache SCX & 7 in case it changes
377	int32_t next = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 6 - (video->p->memory.io[REG_SCX] & 7);
378	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
379}
380
381void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate) {
382	UNUSED(cyclesLate);
383	struct GBVideo* video = context;
384	if (video->p->cpu->executionState != SM83_CORE_FETCH) {
385		mTimingSchedule(timing, &video->frameEvent, 4 - ((video->p->cpu->executionState + 1) & 3));
386		return;
387	}
388	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
389		mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
390	}
391
392	--video->frameskipCounter;
393	if (video->frameskipCounter < 0) {
394		video->renderer->finishFrame(video->renderer);
395		video->frameskipCounter = video->frameskip;
396	}
397	GBFrameEnded(video->p);
398	mCoreSyncPostFrame(video->p->sync);
399	++video->frameCounter;
400	video->p->earlyExit = true;
401
402	GBFrameStarted(video->p);
403}
404
405static void _cleanOAM(struct GBVideo* video, int y) {
406	// TODO: GBC differences
407	// TODO: Optimize
408	video->objMax = 0;
409	int spriteHeight = 8;
410	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
411		spriteHeight = 16;
412	}
413	int o = 0;
414	int i;
415	for (i = 0; i < 40; ++i) {
416		uint8_t oy = video->oam.obj[i].y;
417		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
418			continue;
419		}
420		// TODO: Sort
421		video->objThisLine[o] = video->oam.obj[i];
422		++o;
423		if (o == 10) {
424			break;
425		}
426	}
427	video->objMax = o;
428}
429
430void GBVideoProcessDots(struct GBVideo* video, uint32_t cyclesLate) {
431	if (video->mode != 3) {
432		return;
433	}
434	int oldX = video->x;
435	video->x = (int32_t) (mTimingCurrentTime(&video->p->timing) - cyclesLate - video->dotClock) >> video->p->doubleSpeed;
436	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
437		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
438	} else if (video->x < 0) {
439		return;
440	}
441	if (oldX < 0) {
442		oldX = 0;
443	}
444	if (video->frameskipCounter <= 0) {
445		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
446	}
447}
448
449void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
450	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
451		video->mode = 2;
452		video->modeEvent.callback = _endMode2;
453		int32_t next = GB_VIDEO_MODE_2_LENGTH - 5; // TODO: Why is this fudge factor needed? Might be related to T-cycles for load/store differing
454		mTimingDeschedule(&video->p->timing, &video->modeEvent);
455		mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
456
457		video->ly = 0;
458		video->p->memory.io[REG_LY] = 0;
459		GBRegisterSTAT oldStat = video->stat;
460		video->stat = GBRegisterSTATSetMode(video->stat, 0);
461		video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
462		if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
463			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
464			GBUpdateIRQs(video->p);
465		}
466		video->p->memory.io[REG_STAT] = video->stat;
467		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
468
469		mTimingDeschedule(&video->p->timing, &video->frameEvent);
470	}
471	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
472		// TODO: Fix serialization; this gets internal and visible modes out of sync
473		video->mode = 0;
474		video->stat = GBRegisterSTATSetMode(video->stat, 0);
475		video->p->memory.io[REG_STAT] = video->stat;
476		video->ly = 0;
477		video->p->memory.io[REG_LY] = 0;
478		video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
479	
480		mTimingDeschedule(&video->p->timing, &video->modeEvent);
481		mTimingDeschedule(&video->p->timing, &video->frameEvent);
482		mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
483	}
484	video->p->memory.io[REG_STAT] = video->stat;
485}
486
487void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
488	GBRegisterSTAT oldStat = video->stat;
489	video->stat = (video->stat & 0x7) | (value & 0x78);
490	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) || video->p->model >= GB_MODEL_CGB) {
491		return;
492	}
493	if (!_statIRQAsserted(oldStat) && video->mode < 3) {
494		// TODO: variable for the IRQ line value?
495		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
496		GBUpdateIRQs(video->p);
497	}
498}
499
500void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
501	GBRegisterSTAT oldStat = video->stat;
502	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
503		video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
504		if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
505			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
506			GBUpdateIRQs(video->p);
507		}
508	}
509	video->p->memory.io[REG_STAT] = video->stat;
510}
511
512void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
513	if (video->p->model < GB_MODEL_SGB) {
514		switch (address) {
515		case REG_BGP:
516			video->palette[0] = video->dmgPalette[value & 3];
517			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
518			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
519			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
520			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
521			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
522			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
523			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
524			break;
525		case REG_OBP0:
526			video->palette[8 * 4 + 0] = video->dmgPalette[(value & 3) + 4];
527			video->palette[8 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 4];
528			video->palette[8 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 4];
529			video->palette[8 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 4];
530			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
531			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
532			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
533			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
534			break;
535		case REG_OBP1:
536			video->palette[9 * 4 + 0] = video->dmgPalette[(value & 3) + 8];
537			video->palette[9 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 8];
538			video->palette[9 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 8];
539			video->palette[9 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 8];
540			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
541			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
542			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
543			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
544			break;
545		}
546	} else if (video->p->model & GB_MODEL_SGB) {
547		video->renderer->writeVideoRegister(video->renderer, address, value);
548	} else {
549		switch (address) {
550		case REG_BCPD:
551			if (video->mode != 3) {
552				if (video->bcpIndex & 1) {
553					video->palette[video->bcpIndex >> 1] &= 0x00FF;
554					video->palette[video->bcpIndex >> 1] |= value << 8;
555				} else {
556					video->palette[video->bcpIndex >> 1] &= 0xFF00;
557					video->palette[video->bcpIndex >> 1] |= value;
558				}
559				video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
560			}
561			if (video->bcpIncrement) {
562				++video->bcpIndex;
563				video->bcpIndex &= 0x3F;
564				video->p->memory.io[REG_BCPS] &= 0x80;
565				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
566			}
567			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
568			break;
569		case REG_OCPD:
570			if (video->mode != 3) {
571				if (video->ocpIndex & 1) {
572					video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
573					video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
574				} else {
575					video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
576					video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
577				}
578				video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
579			}
580			if (video->ocpIncrement) {
581				++video->ocpIndex;
582				video->ocpIndex &= 0x3F;
583				video->p->memory.io[REG_OCPS] &= 0x80;
584				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
585			}
586			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
587			break;
588		}
589	}
590}
591
592void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
593	value &= 1;
594	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
595	video->vramCurrentBank = value;
596}
597
598void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
599	if (index >= 12) {
600		return;
601	}
602	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
603}
604
605void GBVideoDisableCGB(struct GBVideo* video) {
606	video->dmgPalette[0] = video->palette[0];
607	video->dmgPalette[1] = video->palette[1];
608	video->dmgPalette[2] = video->palette[2];
609	video->dmgPalette[3] = video->palette[3];
610	video->dmgPalette[4] = video->palette[8 * 4 + 0];
611	video->dmgPalette[5] = video->palette[8 * 4 + 1];
612	video->dmgPalette[6] = video->palette[8 * 4 + 2];
613	video->dmgPalette[7] = video->palette[8 * 4 + 3];
614	video->dmgPalette[8] = video->palette[9 * 4 + 0];
615	video->dmgPalette[9] = video->palette[9 * 4 + 1];
616	video->dmgPalette[10] = video->palette[9 * 4 + 2];
617	video->dmgPalette[11] = video->palette[9 * 4 + 3];
618	video->renderer->deinit(video->renderer);
619	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
620}
621
622void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data) {
623	int i;
624	if (!(video->sgbCommandHeader & 7)) {
625		video->sgbBufferIndex = 0;
626		if ((data[0] >> 3) > SGB_OBJ_TRN) {
627			video->sgbCommandHeader = 0;
628			return;
629		}
630		video->sgbCommandHeader = data[0];
631	}
632	--video->sgbCommandHeader;
633	memcpy(&video->sgbPacketBuffer[video->sgbBufferIndex << 4], data, 16);
634	++video->sgbBufferIndex;
635	if (video->sgbCommandHeader & 7) {
636		return;
637	}
638	switch (video->sgbCommandHeader >> 3) {
639	case SGB_PAL01:
640		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
641		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
642		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
643		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
644
645		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
646		video->palette[5] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
647		video->palette[6] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
648		video->palette[7] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
649
650		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
651		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
652
653		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
654		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
655		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
656		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
657		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
658		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
659		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
660		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
661		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
662		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
663		break;
664	case SGB_PAL23:
665		video->palette[9] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
666		video->palette[10] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
667		video->palette[11] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
668
669		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
670		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
671		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
672		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
673		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
674		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
675		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
676		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
677		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
678		break;
679	case SGB_PAL03:
680		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
681		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
682		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
683		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
684
685		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
686		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
687
688		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
689		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
690		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
691		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
692		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
693		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
694		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
695		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
696		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
697		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
698		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
699		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
700		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
701		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
702		break;
703	case SGB_PAL12:
704		video->palette[5] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
705		video->palette[6] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
706		video->palette[7] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
707
708		video->palette[9] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
709		video->palette[10] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
710		video->palette[11] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
711		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
712		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
713		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
714		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
715		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
716		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
717		break;
718	case SGB_PAL_SET:
719		for (i = 0; i < 4; ++i) {
720			uint16_t entry = (video->sgbPacketBuffer[2 + (i * 2)] << 8) | video->sgbPacketBuffer[1 + (i * 2)];
721			if (entry >= 0x200) {
722				mLOG(GB, STUB, "Unimplemented SGB palette overflow: %03X", entry);
723				continue;
724			}
725			LOAD_16LE(video->palette[i * 4 + 0], entry * 8 + 0, video->renderer->sgbPalRam);
726			video->renderer->writePalette(video->renderer, i * 4 + 0, video->palette[i * 4 + 0]);
727			LOAD_16LE(video->palette[i * 4 + 1], entry * 8 + 2, video->renderer->sgbPalRam);
728			video->renderer->writePalette(video->renderer, i * 4 + 1, video->palette[i * 4 + 1]);
729			LOAD_16LE(video->palette[i * 4 + 2], entry * 8 + 4, video->renderer->sgbPalRam);
730			video->renderer->writePalette(video->renderer, i * 4 + 2, video->palette[i * 4 + 2]);
731			LOAD_16LE(video->palette[i * 4 + 3], entry * 8 + 6, video->renderer->sgbPalRam);
732			video->renderer->writePalette(video->renderer, i * 4 + 3, video->palette[i * 4 + 3]);
733		}
734		break;
735	case SGB_ATTR_BLK:
736	case SGB_ATTR_DIV:
737	case SGB_ATTR_CHR:
738	case SGB_ATTR_LIN:
739	case SGB_PAL_TRN:
740	case SGB_ATRC_EN:
741	case SGB_CHR_TRN:
742	case SGB_PCT_TRN:
743	case SGB_ATTR_TRN:
744	case SGB_ATTR_SET:
745		break;
746	case SGB_MLT_REQ:
747		if ((video->sgbPacketBuffer[1] & 0x3) == 2) { // XXX: This unmasked increment appears to be an SGB hardware bug
748			++video->p->sgbCurrentController;
749		}
750		video->p->sgbControllers = video->sgbPacketBuffer[1] & 0x3;
751		video->p->sgbCurrentController &= video->p->sgbControllers;
752		return;
753	case SGB_MASK_EN:
754		video->renderer->sgbRenderMode = video->sgbPacketBuffer[1] & 0x3;
755		break;
756	default:
757		mLOG(GB, STUB, "Unimplemented SGB command: %02X", video->sgbPacketBuffer[0] >> 3);
758		return;
759	}
760	video->renderer->writeSGBPacket(video->renderer, video->sgbPacketBuffer);
761}
762
763static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders) {
764	UNUSED(renderer);
765	UNUSED(model);
766	UNUSED(borders);
767	// Nothing to do
768}
769
770static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
771	UNUSED(renderer);
772	// Nothing to do
773}
774
775static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
776	if (renderer->cache) {
777		GBVideoCacheWriteVideoRegister(renderer->cache, address, value);
778	}
779	return value;
780}
781
782static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
783	UNUSED(renderer);
784	UNUSED(data);
785}
786
787static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
788	if (renderer->cache) {
789		mCacheSetWriteVRAM(renderer->cache, address);
790	}
791}
792
793static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
794	UNUSED(renderer);
795	UNUSED(oam);
796	// Nothing to do
797}
798
799static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
800	if (renderer->cache) {
801		mCacheSetWritePalette(renderer->cache, index, mColorFrom555(value));
802	}
803}
804
805static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
806	UNUSED(renderer);
807	UNUSED(endX);
808	UNUSED(startX);
809	UNUSED(y);
810	UNUSED(obj);
811	UNUSED(oamMax);
812	// Nothing to do
813}
814
815static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
816	UNUSED(renderer);
817	UNUSED(y);
818	// Nothing to do
819}
820
821static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
822	UNUSED(renderer);
823	// Nothing to do
824}
825
826static void GBVideoDummyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
827	UNUSED(renderer);
828	UNUSED(enable);
829	// Nothing to do
830}
831
832static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
833	UNUSED(renderer);
834	UNUSED(stride);
835	UNUSED(pixels);
836	// Nothing to do
837}
838
839static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
840	UNUSED(renderer);
841	UNUSED(stride);
842	UNUSED(pixels);
843	// Nothing to do
844}
845
846void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
847	STORE_16LE(video->x, 0, &state->video.x);
848	STORE_16LE(video->ly, 0, &state->video.ly);
849	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
850	STORE_32LE(video->dotClock, 0, &state->video.dotCounter);
851	state->video.vramCurrentBank = video->vramCurrentBank;
852
853	GBSerializedVideoFlags flags = 0;
854	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
855	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
856	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
857	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
858	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
859	state->video.flags = flags;
860	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
861	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
862
863	size_t i;
864	for (i = 0; i < 64; ++i) {
865		STORE_16LE(video->palette[i], i * 2, state->video.palette);
866	}
867
868	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
869	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
870
871	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
872	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
873}
874
875void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
876	LOAD_16LE(video->x, 0, &state->video.x);
877	LOAD_16LE(video->ly, 0, &state->video.ly);
878	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
879	LOAD_32LE(video->dotClock, 0, &state->video.dotCounter);
880	video->vramCurrentBank = state->video.vramCurrentBank;
881
882	GBSerializedVideoFlags flags = state->video.flags;
883	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
884	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
885	video->mode = GBSerializedVideoFlagsGetMode(flags);
886	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
887	video->bcpIndex &= 0x3F;
888	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
889	video->ocpIndex &= 0x3F;
890
891	switch (video->mode) {
892	case 0:
893		video->modeEvent.callback = _endMode0;
894		break;
895	case 1:
896		video->modeEvent.callback = _endMode1;
897		break;
898	case 2:
899		video->modeEvent.callback = _endMode2;
900		break;
901	case 3:
902		video->modeEvent.callback = _endMode3;
903		break;
904	}
905
906	uint32_t when;
907	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
908		LOAD_32LE(when, 0, &state->video.nextMode);
909		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
910	}
911	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
912		LOAD_32LE(when, 0, &state->video.nextFrame);
913		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
914	}
915
916	video->renderer->deinit(video->renderer);
917	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
918
919	size_t i;
920	for (i = 0; i < 64; ++i) {
921		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
922		video->renderer->writePalette(video->renderer, i, video->palette[i]);
923	}
924
925	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
926	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
927
928	_cleanOAM(video, video->ly);
929	GBVideoSwitchBank(video, video->vramCurrentBank);
930}