all repos — mgba @ ce97e265a3e26f7b1b25de872700fe3ef16fb450

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);
 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[GB_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[GB_REG_LY] = 0;
248		next = 56;
249	}
250	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
251
252	video->p->memory.io[GB_REG_IF] |= (1 << GB_IRQ_VBLANK);
253	GBUpdateIRQs(video->p);
254	video->p->memory.io[GB_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[GB_REG_LYC];
265	int32_t next;
266	++video->ly;
267	video->p->memory.io[GB_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[GB_REG_IF] |= (1 << GB_IRQ_LCDSTAT);
283		}
284		video->p->memory.io[GB_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[GB_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[GB_REG_IF] |= (1 << GB_IRQ_LCDSTAT);
296	}
297
298	GBUpdateIRQs(video->p);
299	video->p->memory.io[GB_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[GB_REG_LCDC])) {
306		return;
307	}
308	int lyc = video->p->memory.io[GB_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[GB_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[GB_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[GB_REG_LY] = video->ly;
323		next = 8;
324	} else {
325		video->p->memory.io[GB_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[GB_REG_LY]);
332	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
333		video->p->memory.io[GB_REG_IF] |= (1 << GB_IRQ_LCDSTAT);
334		GBUpdateIRQs(video->p);
335	}
336	video->p->memory.io[GB_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[GB_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[GB_REG_IF] |= (1 << GB_IRQ_LCDSTAT);
352		GBUpdateIRQs(video->p);
353	}
354	video->p->memory.io[GB_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[GB_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[GB_REG_IF] |= (1 << GB_IRQ_LCDSTAT);
373		GBUpdateIRQs(video->p);
374	}
375	video->p->memory.io[GB_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[GB_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[GB_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	int spriteHeight = 8;
407	if (GBRegisterLCDCIsObjSize(video->p->memory.io[GB_REG_LCDC])) {
408		spriteHeight = 16;
409	}
410	int o = 0;
411	int i;
412	for (i = 0; i < GB_VIDEO_MAX_OBJ && o < GB_VIDEO_MAX_LINE_OBJ; ++i) {
413		uint8_t oy = video->oam.obj[i].y;
414		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
415			continue;
416		}
417		++o;
418	}
419	video->objMax = o;
420}
421
422void GBVideoProcessDots(struct GBVideo* video, uint32_t cyclesLate) {
423	if (video->mode != 3) {
424		return;
425	}
426	int oldX = video->x;
427	video->x = (int32_t) (mTimingCurrentTime(&video->p->timing) - cyclesLate - video->dotClock) >> video->p->doubleSpeed;
428	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
429		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
430	} else if (video->x < 0) {
431		return;
432	}
433	if (oldX < 0) {
434		oldX = 0;
435	}
436	if (video->frameskipCounter <= 0) {
437		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly);
438	}
439}
440
441void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
442	if (!GBRegisterLCDCIsEnable(video->p->memory.io[GB_REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
443		video->mode = 2;
444		video->modeEvent.callback = _endMode2;
445		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
446		mTimingDeschedule(&video->p->timing, &video->modeEvent);
447		mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
448
449		video->ly = 0;
450		video->p->memory.io[GB_REG_LY] = 0;
451		GBRegisterSTAT oldStat = video->stat;
452		video->stat = GBRegisterSTATSetMode(video->stat, 0);
453		video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[GB_REG_LYC]);
454		if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
455			video->p->memory.io[GB_REG_IF] |= (1 << GB_IRQ_LCDSTAT);
456			GBUpdateIRQs(video->p);
457		}
458		video->p->memory.io[GB_REG_STAT] = video->stat;
459		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
460
461		mTimingDeschedule(&video->p->timing, &video->frameEvent);
462	}
463	if (GBRegisterLCDCIsEnable(video->p->memory.io[GB_REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
464		// TODO: Fix serialization; this gets internal and visible modes out of sync
465		video->mode = 0;
466		video->stat = GBRegisterSTATSetMode(video->stat, 0);
467		video->p->memory.io[GB_REG_STAT] = video->stat;
468		video->ly = 0;
469		video->p->memory.io[GB_REG_LY] = 0;
470		video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
471	
472		mTimingDeschedule(&video->p->timing, &video->modeEvent);
473		mTimingDeschedule(&video->p->timing, &video->frameEvent);
474		mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
475	}
476	video->p->memory.io[GB_REG_STAT] = video->stat;
477}
478
479void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
480	GBRegisterSTAT oldStat = video->stat;
481	video->stat = (video->stat & 0x7) | (value & 0x78);
482	if (!GBRegisterLCDCIsEnable(video->p->memory.io[GB_REG_LCDC]) || video->p->model >= GB_MODEL_CGB) {
483		return;
484	}
485	if (!_statIRQAsserted(oldStat) && video->mode < 3) {
486		// TODO: variable for the IRQ line value?
487		video->p->memory.io[GB_REG_IF] |= (1 << GB_IRQ_LCDSTAT);
488		GBUpdateIRQs(video->p);
489	}
490}
491
492void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
493	GBRegisterSTAT oldStat = video->stat;
494	if (GBRegisterLCDCIsEnable(video->p->memory.io[GB_REG_LCDC])) {
495		video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
496		if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
497			video->p->memory.io[GB_REG_IF] |= (1 << GB_IRQ_LCDSTAT);
498			GBUpdateIRQs(video->p);
499		}
500	}
501	video->p->memory.io[GB_REG_STAT] = video->stat;
502}
503
504void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
505	if (video->p->model < GB_MODEL_SGB) {
506		switch (address) {
507		case GB_REG_BGP:
508			video->palette[0] = video->dmgPalette[value & 3];
509			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
510			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
511			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
512			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
513			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
514			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
515			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
516			break;
517		case GB_REG_OBP0:
518			video->palette[8 * 4 + 0] = video->dmgPalette[(value & 3) + 4];
519			video->palette[8 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 4];
520			video->palette[8 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 4];
521			video->palette[8 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 4];
522			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
523			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
524			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
525			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
526			break;
527		case GB_REG_OBP1:
528			video->palette[9 * 4 + 0] = video->dmgPalette[(value & 3) + 8];
529			video->palette[9 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 8];
530			video->palette[9 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 8];
531			video->palette[9 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 8];
532			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
533			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
534			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
535			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
536			break;
537		}
538	} else if (video->p->model & GB_MODEL_SGB) {
539		video->renderer->writeVideoRegister(video->renderer, address, value);
540	} else {
541		switch (address) {
542		case GB_REG_BCPD:
543			if (video->mode != 3) {
544				if (video->bcpIndex & 1) {
545					video->palette[video->bcpIndex >> 1] &= 0x00FF;
546					video->palette[video->bcpIndex >> 1] |= value << 8;
547				} else {
548					video->palette[video->bcpIndex >> 1] &= 0xFF00;
549					video->palette[video->bcpIndex >> 1] |= value;
550				}
551				video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
552			}
553			if (video->bcpIncrement) {
554				++video->bcpIndex;
555				video->bcpIndex &= 0x3F;
556				video->p->memory.io[GB_REG_BCPS] &= 0x80;
557				video->p->memory.io[GB_REG_BCPS] |= video->bcpIndex;
558			}
559			video->p->memory.io[GB_REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
560			break;
561		case GB_REG_OCPD:
562			if (video->mode != 3) {
563				if (video->ocpIndex & 1) {
564					video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
565					video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
566				} else {
567					video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
568					video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
569				}
570				video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
571			}
572			if (video->ocpIncrement) {
573				++video->ocpIndex;
574				video->ocpIndex &= 0x3F;
575				video->p->memory.io[GB_REG_OCPS] &= 0x80;
576				video->p->memory.io[GB_REG_OCPS] |= video->ocpIndex;
577			}
578			video->p->memory.io[GB_REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
579			break;
580		}
581	}
582}
583
584void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
585	value &= 1;
586	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
587	video->vramCurrentBank = value;
588}
589
590void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
591	if (index >= 12) {
592		return;
593	}
594	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
595}
596
597void GBVideoDisableCGB(struct GBVideo* video) {
598	video->dmgPalette[0] = video->palette[0];
599	video->dmgPalette[1] = video->palette[1];
600	video->dmgPalette[2] = video->palette[2];
601	video->dmgPalette[3] = video->palette[3];
602	video->dmgPalette[4] = video->palette[8 * 4 + 0];
603	video->dmgPalette[5] = video->palette[8 * 4 + 1];
604	video->dmgPalette[6] = video->palette[8 * 4 + 2];
605	video->dmgPalette[7] = video->palette[8 * 4 + 3];
606	video->dmgPalette[8] = video->palette[9 * 4 + 0];
607	video->dmgPalette[9] = video->palette[9 * 4 + 1];
608	video->dmgPalette[10] = video->palette[9 * 4 + 2];
609	video->dmgPalette[11] = video->palette[9 * 4 + 3];
610	video->renderer->deinit(video->renderer);
611	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
612}
613
614void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data) {
615	int i;
616	if (!(video->sgbCommandHeader & 7)) {
617		video->sgbBufferIndex = 0;
618		if ((data[0] >> 3) > SGB_OBJ_TRN) {
619			video->sgbCommandHeader = 0;
620			return;
621		}
622		video->sgbCommandHeader = data[0];
623	}
624	--video->sgbCommandHeader;
625	memcpy(&video->sgbPacketBuffer[video->sgbBufferIndex << 4], data, 16);
626	++video->sgbBufferIndex;
627	if (video->sgbCommandHeader & 7) {
628		return;
629	}
630	switch (video->sgbCommandHeader >> 3) {
631	case SGB_PAL01:
632		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
633		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
634		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
635		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
636
637		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
638		video->palette[5] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
639		video->palette[6] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
640		video->palette[7] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
641
642		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
643		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
644
645		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
646		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
647		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
648		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
649		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
650		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
651		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
652		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
653		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
654		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
655		break;
656	case SGB_PAL23:
657		video->palette[9] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
658		video->palette[10] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
659		video->palette[11] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
660
661		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
662		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
663		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
664		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
665		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
666		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
667		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
668		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
669		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
670		break;
671	case SGB_PAL03:
672		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
673		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
674		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
675		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
676
677		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
678		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
679
680		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
681		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
682		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
683		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
684		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
685		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
686		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
687		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
688		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
689		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
690		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
691		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
692		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
693		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
694		break;
695	case SGB_PAL12:
696		video->palette[5] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
697		video->palette[6] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
698		video->palette[7] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
699
700		video->palette[9] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
701		video->palette[10] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
702		video->palette[11] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
703		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
704		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
705		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
706		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
707		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
708		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
709		break;
710	case SGB_PAL_SET:
711		for (i = 0; i < 4; ++i) {
712			uint16_t entry = (video->sgbPacketBuffer[2 + (i * 2)] << 8) | video->sgbPacketBuffer[1 + (i * 2)];
713			if (entry >= 0x200) {
714				mLOG(GB, STUB, "Unimplemented SGB palette overflow: %03X", entry);
715				continue;
716			}
717			LOAD_16LE(video->palette[i * 4 + 0], entry * 8 + 0, video->renderer->sgbPalRam);
718			video->renderer->writePalette(video->renderer, i * 4 + 0, video->palette[i * 4 + 0]);
719			LOAD_16LE(video->palette[i * 4 + 1], entry * 8 + 2, video->renderer->sgbPalRam);
720			video->renderer->writePalette(video->renderer, i * 4 + 1, video->palette[i * 4 + 1]);
721			LOAD_16LE(video->palette[i * 4 + 2], entry * 8 + 4, video->renderer->sgbPalRam);
722			video->renderer->writePalette(video->renderer, i * 4 + 2, video->palette[i * 4 + 2]);
723			LOAD_16LE(video->palette[i * 4 + 3], entry * 8 + 6, video->renderer->sgbPalRam);
724			video->renderer->writePalette(video->renderer, i * 4 + 3, video->palette[i * 4 + 3]);
725		}
726		break;
727	case SGB_ATTR_BLK:
728	case SGB_ATTR_DIV:
729	case SGB_ATTR_CHR:
730	case SGB_ATTR_LIN:
731	case SGB_PAL_TRN:
732	case SGB_ATRC_EN:
733	case SGB_CHR_TRN:
734	case SGB_PCT_TRN:
735	case SGB_ATTR_TRN:
736	case SGB_ATTR_SET:
737		break;
738	case SGB_MLT_REQ:
739		if ((video->sgbPacketBuffer[1] & 0x3) == 2) { // XXX: This unmasked increment appears to be an SGB hardware bug
740			++video->p->sgbCurrentController;
741		}
742		video->p->sgbControllers = video->sgbPacketBuffer[1] & 0x3;
743		video->p->sgbCurrentController &= video->p->sgbControllers;
744		return;
745	case SGB_MASK_EN:
746		video->renderer->sgbRenderMode = video->sgbPacketBuffer[1] & 0x3;
747		break;
748	default:
749		mLOG(GB, STUB, "Unimplemented SGB command: %02X", video->sgbPacketBuffer[0] >> 3);
750		return;
751	}
752	video->renderer->writeSGBPacket(video->renderer, video->sgbPacketBuffer);
753}
754
755static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders) {
756	UNUSED(renderer);
757	UNUSED(model);
758	UNUSED(borders);
759	// Nothing to do
760}
761
762static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
763	UNUSED(renderer);
764	// Nothing to do
765}
766
767static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
768	if (renderer->cache) {
769		GBVideoCacheWriteVideoRegister(renderer->cache, address, value);
770	}
771	return value;
772}
773
774static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
775	UNUSED(renderer);
776	UNUSED(data);
777}
778
779static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
780	if (renderer->cache) {
781		mCacheSetWriteVRAM(renderer->cache, address);
782	}
783}
784
785static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
786	UNUSED(renderer);
787	UNUSED(oam);
788	// Nothing to do
789}
790
791static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
792	if (renderer->cache) {
793		mCacheSetWritePalette(renderer->cache, index, mColorFrom555(value));
794	}
795}
796
797static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y) {
798	UNUSED(renderer);
799	UNUSED(endX);
800	UNUSED(startX);
801	UNUSED(y);
802	// Nothing to do
803}
804
805static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
806	UNUSED(renderer);
807	UNUSED(y);
808	// Nothing to do
809}
810
811static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
812	UNUSED(renderer);
813	// Nothing to do
814}
815
816static void GBVideoDummyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
817	UNUSED(renderer);
818	UNUSED(enable);
819	// Nothing to do
820}
821
822static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
823	UNUSED(renderer);
824	UNUSED(stride);
825	UNUSED(pixels);
826	// Nothing to do
827}
828
829static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
830	UNUSED(renderer);
831	UNUSED(stride);
832	UNUSED(pixels);
833	// Nothing to do
834}
835
836void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
837	STORE_16LE(video->x, 0, &state->video.x);
838	STORE_16LE(video->ly, 0, &state->video.ly);
839	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
840	STORE_32LE(video->dotClock, 0, &state->video.dotCounter);
841	state->video.vramCurrentBank = video->vramCurrentBank;
842
843	GBSerializedVideoFlags flags = 0;
844	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
845	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
846	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
847	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
848	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
849	state->video.flags = flags;
850	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
851	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
852
853	size_t i;
854	for (i = 0; i < 64; ++i) {
855		STORE_16LE(video->palette[i], i * 2, state->video.palette);
856	}
857
858	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
859	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
860
861	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
862	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
863}
864
865void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
866	LOAD_16LE(video->x, 0, &state->video.x);
867	LOAD_16LE(video->ly, 0, &state->video.ly);
868	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
869	LOAD_32LE(video->dotClock, 0, &state->video.dotCounter);
870	video->vramCurrentBank = state->video.vramCurrentBank;
871
872	GBSerializedVideoFlags flags = state->video.flags;
873	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
874	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
875	video->mode = GBSerializedVideoFlagsGetMode(flags);
876	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
877	video->bcpIndex &= 0x3F;
878	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
879	video->ocpIndex &= 0x3F;
880
881	switch (video->mode) {
882	case 0:
883		video->modeEvent.callback = _endMode0;
884		break;
885	case 1:
886		video->modeEvent.callback = _endMode1;
887		break;
888	case 2:
889		video->modeEvent.callback = _endMode2;
890		break;
891	case 3:
892		video->modeEvent.callback = _endMode3;
893		break;
894	}
895
896	uint32_t when;
897	LOAD_32LE(when, 0, &state->video.nextMode);
898	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
899		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
900	} else {
901		video->modeEvent.when = when + mTimingCurrentTime(&video->p->timing);
902	}
903	LOAD_32LE(when, 0, &state->video.nextFrame);
904	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
905		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
906	} else {
907		video->frameEvent.when = when + mTimingCurrentTime(&video->p->timing);
908	}
909
910	video->renderer->deinit(video->renderer);
911	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
912
913	size_t i;
914	for (i = 0; i < 64; ++i) {
915		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
916		video->renderer->writePalette(video->renderer, i, video->palette[i]);
917	}
918
919	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
920	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
921
922	_cleanOAM(video, video->ly);
923	GBVideoSwitchBank(video, video->vramCurrentBank);
924}