all repos — mgba @ 9b7521cceaa5130466a32eea6a70aa43352abaef

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/tile-cache.h>
 11#include <mgba/internal/gb/gb.h>
 12#include <mgba/internal/gb/io.h>
 13#include <mgba/internal/gb/serialize.h>
 14#include <mgba/internal/lr35902/lr35902.h>
 15
 16#include <mgba-util/memory.h>
 17
 18static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
 19static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
 20static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 21static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
 22static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
 23static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam);
 24static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
 25static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
 26static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
 27static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
 28static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
 29
 30static void _cleanOAM(struct GBVideo* video, int y);
 31
 32static void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate);
 33static void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate);
 34static void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate);
 35static void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate);
 36static void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate);
 37
 38static struct GBVideoRenderer dummyRenderer = {
 39	.init = GBVideoDummyRendererInit,
 40	.deinit = GBVideoDummyRendererDeinit,
 41	.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
 42	.writeVRAM = GBVideoDummyRendererWriteVRAM,
 43	.writeOAM = GBVideoDummyRendererWriteOAM,
 44	.writePalette = GBVideoDummyRendererWritePalette,
 45	.drawRange = GBVideoDummyRendererDrawRange,
 46	.finishScanline = GBVideoDummyRendererFinishScanline,
 47	.finishFrame = GBVideoDummyRendererFinishFrame,
 48	.getPixels = GBVideoDummyRendererGetPixels,
 49	.putPixels = GBVideoDummyRendererPutPixels,
 50};
 51
 52void GBVideoInit(struct GBVideo* video) {
 53	video->renderer = &dummyRenderer;
 54	video->renderer->cache = NULL;
 55	video->vram = 0;
 56	video->frameskip = 0;
 57
 58	video->modeEvent.context = video;
 59	video->modeEvent.name = "GB Video Mode";
 60	video->modeEvent.callback = NULL;
 61	video->modeEvent.priority = 8;
 62	video->frameEvent.context = video;
 63	video->frameEvent.name = "GB Video Frame";
 64	video->frameEvent.callback = _updateFrameCount;
 65	video->frameEvent.priority = 9;
 66
 67	video->dmgPalette[0] = 0x7FFF;
 68	video->dmgPalette[1] = 0x56B5;
 69	video->dmgPalette[2] = 0x294A;
 70	video->dmgPalette[3] = 0x0000;
 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	if (video->vram) {
 83		mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 84	}
 85	video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
 86	GBVideoSwitchBank(video, 0);
 87	video->renderer->vram = video->vram;
 88	memset(&video->oam, 0, sizeof(video->oam));
 89	video->renderer->oam = &video->oam;
 90	memset(&video->palette, 0, sizeof(video->palette));
 91
 92	video->renderer->deinit(video->renderer);
 93	video->renderer->init(video->renderer, video->p->model);
 94}
 95
 96void GBVideoDeinit(struct GBVideo* video) {
 97	GBVideoAssociateRenderer(video, &dummyRenderer);
 98	mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 99}
100
101void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
102	video->renderer->deinit(video->renderer);
103	renderer->cache = video->renderer->cache;
104	video->renderer = renderer;
105	renderer->vram = video->vram;
106	video->renderer->init(video->renderer, video->p->model);
107}
108
109static bool _statIRQAsserted(struct GBVideo* video, GBRegisterSTAT stat) {
110	// TODO: variable for the IRQ line value?
111	if (GBRegisterSTATIsLYCIRQ(stat) && GBRegisterSTATIsLYC(stat)) {
112		return true;
113	}
114	switch (GBRegisterSTATGetMode(stat)) {
115	case 0:
116		if (GBRegisterSTATIsHblankIRQ(stat)) {
117			return true;
118		}
119		break;
120	case 1:
121		if (GBRegisterSTATIsVblankIRQ(stat)) {
122			return true;
123		}
124		break;
125	case 2:
126		if (GBRegisterSTATIsOAMIRQ(stat)) {
127			return true;
128		}
129		break;
130	case 3:
131		break;
132	}
133	return false;
134}
135
136void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate) {
137	struct GBVideo* video = context;
138	if (video->frameskipCounter <= 0) {
139		video->renderer->finishScanline(video->renderer, video->ly);
140	}
141	int lyc = video->p->memory.io[REG_LYC];
142	int32_t next;
143	++video->ly;
144	video->p->memory.io[REG_LY] = video->ly;
145	GBRegisterSTAT oldStat = video->stat;
146	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
147	if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
148		// TODO: Cache SCX & 7 in case it changes during mode 2
149		next = GB_VIDEO_MODE_2_LENGTH + (video->p->memory.io[REG_SCX] & 7);
150		video->mode = 2;
151		video->modeEvent.callback = _endMode2;
152	} else {
153		next = GB_VIDEO_HORIZONTAL_LENGTH;
154		video->mode = 1;
155		video->modeEvent.callback = _endMode1;
156
157		mTimingSchedule(&video->p->timing, &video->frameEvent, -cyclesLate);
158
159		if (!_statIRQAsserted(video, oldStat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
160			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
161		}
162		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
163	}
164	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
165	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
166		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
167	}
168	GBUpdateIRQs(video->p);
169	video->p->memory.io[REG_STAT] = video->stat;
170	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
171}
172
173void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
174	struct GBVideo* video = context;
175	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
176		return;
177	}
178	int lyc = video->p->memory.io[REG_LYC];
179	// TODO: One M-cycle delay
180	++video->ly;
181	int32_t next;
182	if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
183		video->ly = 0;
184		video->p->memory.io[REG_LY] = video->ly;
185		next = GB_VIDEO_MODE_2_LENGTH + (video->p->memory.io[REG_SCX] & 7);
186		video->mode = 2;
187		video->modeEvent.callback = _endMode2;
188	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
189		video->p->memory.io[REG_LY] = 0;
190		next = GB_VIDEO_HORIZONTAL_LENGTH - 8;
191	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
192		video->p->memory.io[REG_LY] = video->ly;
193		next = 8;
194	} else {
195		video->p->memory.io[REG_LY] = video->ly;
196		next = GB_VIDEO_HORIZONTAL_LENGTH;
197	}
198
199	GBRegisterSTAT oldStat = video->stat;
200	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
201	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
202	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
203		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
204		GBUpdateIRQs(video->p);
205	}
206	video->p->memory.io[REG_STAT] = video->stat;
207	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
208}
209
210void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
211	struct GBVideo* video = context;
212	_cleanOAM(video, video->ly);
213	video->x = 0;
214	video->dotClock = mTimingCurrentTime(timing) - cyclesLate;
215	int32_t next = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 6 - (video->p->memory.io[REG_SCX] & 7);
216	video->mode = 3;
217	video->modeEvent.callback = _endMode3;
218	GBRegisterSTAT oldStat = video->stat;
219	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
220	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
221		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
222		GBUpdateIRQs(video->p);
223	}
224	video->p->memory.io[REG_STAT] = video->stat;
225	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
226}
227
228void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
229	struct GBVideo* video = context;
230	GBVideoProcessDots(video);
231	if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
232		video->p->memory.hdmaRemaining = 0x10;
233		mTimingDeschedule(timing, &video->p->memory.hdmaEvent);
234		mTimingSchedule(timing, &video->p->memory.hdmaEvent, 0);
235	}
236	video->mode = 0;
237	video->modeEvent.callback = _endMode0;
238	GBRegisterSTAT oldStat = video->stat;
239	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
240	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
241		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
242		GBUpdateIRQs(video->p);
243	}
244	video->p->memory.io[REG_STAT] = video->stat;
245	int32_t next = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 6;
246	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
247}
248
249void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate) {
250	UNUSED(cyclesLate);
251	struct GBVideo* video = context;
252	if (video->p->cpu->executionState != LR35902_CORE_FETCH) {
253		mTimingSchedule(timing, &video->frameEvent, 4 - ((video->p->cpu->executionState + 1) & 3));
254		return;
255	}
256
257	size_t c;
258	for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) {
259		struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c);
260		if (callbacks->videoFrameEnded) {
261			callbacks->videoFrameEnded(callbacks->context);
262		}
263	}
264
265	GBFrameEnded(video->p);
266	--video->frameskipCounter;
267	if (video->frameskipCounter < 0) {
268		video->renderer->finishFrame(video->renderer);
269		mCoreSyncPostFrame(video->p->sync);
270		video->frameskipCounter = video->frameskip;
271	}
272	++video->frameCounter;
273
274	// TODO: Move to common code
275	if (video->p->stream && video->p->stream->postVideoFrame) {
276		const color_t* pixels;
277		size_t stride;
278		video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);
279		video->p->stream->postVideoFrame(video->p->stream, pixels, stride);
280	}
281
282	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
283		mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
284	}
285
286	for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) {
287		struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c);
288		if (callbacks->videoFrameStarted) {
289			callbacks->videoFrameStarted(callbacks->context);
290		}
291	}
292}
293
294static void _cleanOAM(struct GBVideo* video, int y) {
295	// TODO: GBC differences
296	// TODO: Optimize
297	video->objMax = 0;
298	int spriteHeight = 8;
299	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
300		spriteHeight = 16;
301	}
302	int o = 0;
303	int i;
304	for (i = 0; i < 40; ++i) {
305		uint8_t oy = video->oam.obj[i].y;
306		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
307			continue;
308		}
309		// TODO: Sort
310		video->objThisLine[o] = video->oam.obj[i];
311		++o;
312		if (o == 10) {
313			break;
314		}
315	}
316	video->objMax = o;
317}
318
319void GBVideoProcessDots(struct GBVideo* video) {
320	if (video->mode != 3) {
321		return;
322	}
323	int oldX = video->x;
324	video->x = (video->p->timing.masterCycles - video->dotClock + video->p->cpu->cycles) >> video->p->doubleSpeed;
325	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
326		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
327	} else if (video->x < 0) {
328		mLOG(GB, FATAL, "Video dot clock went negative!");
329		video->x = oldX;
330	}
331	if (video->frameskipCounter <= 0) {
332		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
333	}
334}
335
336void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
337	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
338		video->mode = 2;
339		video->modeEvent.callback = _endMode2;
340		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
341		mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
342
343		video->ly = 0;
344		video->p->memory.io[REG_LY] = 0;
345		// TODO: Does this read as 0 for 4 T-cycles?
346		GBRegisterSTAT oldStat = video->stat;
347		video->stat = GBRegisterSTATSetMode(video->stat, 2);
348		video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
349		if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
350			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
351			GBUpdateIRQs(video->p);
352		}
353		video->p->memory.io[REG_STAT] = video->stat;
354		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
355
356		mTimingDeschedule(&video->p->timing, &video->frameEvent);
357	}
358	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
359		// TODO: Fix serialization; this gets internal and visible modes out of sync
360		video->stat = GBRegisterSTATSetMode(video->stat, 0);
361		video->p->memory.io[REG_STAT] = video->stat;
362		video->ly = 0;
363		video->p->memory.io[REG_LY] = 0;
364		video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
365	
366		mTimingDeschedule(&video->p->timing, &video->modeEvent);
367		mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
368	}
369	video->p->memory.io[REG_STAT] = video->stat;
370}
371
372void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
373	GBRegisterSTAT oldStat = video->stat;
374	video->stat = (video->stat & 0x7) | (value & 0x78);
375	if (video->p->model == GB_MODEL_DMG && !_statIRQAsserted(video, oldStat) && video->mode < 3) {
376		// TODO: variable for the IRQ line value?
377		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
378		GBUpdateIRQs(video->p);
379	}
380}
381
382void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
383	GBRegisterSTAT oldStat = video->stat;
384	video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
385	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
386		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
387		GBUpdateIRQs(video->p);
388	}
389	video->p->memory.io[REG_STAT] = video->stat;
390}
391
392void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
393	if (video->p->model < GB_MODEL_CGB) {
394		switch (address) {
395		case REG_BGP:
396			video->palette[0] = video->dmgPalette[value & 3];
397			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
398			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
399			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
400			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
401			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
402			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
403			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
404			break;
405		case REG_OBP0:
406			video->palette[8 * 4 + 0] = video->dmgPalette[value & 3];
407			video->palette[8 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
408			video->palette[8 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
409			video->palette[8 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
410			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
411			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
412			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
413			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
414			break;
415		case REG_OBP1:
416			video->palette[9 * 4 + 0] = video->dmgPalette[value & 3];
417			video->palette[9 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
418			video->palette[9 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
419			video->palette[9 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
420			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
421			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
422			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
423			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
424			break;
425		}
426	} else {
427		switch (address) {
428		case REG_BCPD:
429			if (video->bcpIndex & 1) {
430				video->palette[video->bcpIndex >> 1] &= 0x00FF;
431				video->palette[video->bcpIndex >> 1] |= value << 8;
432			} else {
433				video->palette[video->bcpIndex >> 1] &= 0xFF00;
434				video->palette[video->bcpIndex >> 1] |= value;
435			}
436			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
437			if (video->bcpIncrement) {
438				++video->bcpIndex;
439				video->bcpIndex &= 0x3F;
440				video->p->memory.io[REG_BCPS] &= 0x80;
441				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
442			}
443			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
444			break;
445		case REG_OCPD:
446			if (video->ocpIndex & 1) {
447				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
448				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
449			} else {
450				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
451				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
452			}
453			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
454			if (video->ocpIncrement) {
455				++video->ocpIndex;
456				video->ocpIndex &= 0x3F;
457				video->p->memory.io[REG_OCPS] &= 0x80;
458				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
459			}
460			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
461			break;
462		}
463	}
464}
465
466void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
467	value &= 1;
468	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
469	video->vramCurrentBank = value;
470}
471
472void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
473	if (index >= 4) {
474		return;
475	}
476	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
477}
478
479static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
480	UNUSED(renderer);
481	UNUSED(model);
482	// Nothing to do
483}
484
485static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
486	UNUSED(renderer);
487	// Nothing to do
488}
489
490static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
491	UNUSED(renderer);
492	UNUSED(address);
493	return value;
494}
495
496static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
497	if (renderer->cache) {
498		mTileCacheWriteVRAM(renderer->cache, address);
499	}
500}
501
502static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
503	UNUSED(renderer);
504	UNUSED(oam);
505	// Nothing to do
506}
507
508static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
509	UNUSED(value);
510	if (renderer->cache) {
511		mTileCacheWritePalette(renderer->cache, index << 1);
512	}
513}
514
515static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
516	UNUSED(renderer);
517	UNUSED(endX);
518	UNUSED(startX);
519	UNUSED(y);
520	UNUSED(obj);
521	UNUSED(oamMax);
522	// Nothing to do
523}
524
525static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
526	UNUSED(renderer);
527	UNUSED(y);
528	// Nothing to do
529}
530
531static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
532	UNUSED(renderer);
533	// Nothing to do
534}
535
536static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
537	UNUSED(renderer);
538	UNUSED(stride);
539	UNUSED(pixels);
540	// Nothing to do
541}
542
543static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
544	UNUSED(renderer);
545	UNUSED(stride);
546	UNUSED(pixels);
547	// Nothing to do
548}
549
550void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
551	STORE_16LE(video->x, 0, &state->video.x);
552	STORE_16LE(video->ly, 0, &state->video.ly);
553	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
554	state->video.vramCurrentBank = video->vramCurrentBank;
555
556	GBSerializedVideoFlags flags = 0;
557	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
558	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
559	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
560	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
561	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
562	state->video.flags = flags;
563	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
564	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
565
566	size_t i;
567	for (i = 0; i < 64; ++i) {
568		STORE_16LE(video->palette[i], i * 2, state->video.palette);
569	}
570
571	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
572	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
573
574	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
575	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
576}
577
578void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
579	LOAD_16LE(video->x, 0, &state->video.x);
580	LOAD_16LE(video->ly, 0, &state->video.ly);
581	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
582	video->vramCurrentBank = state->video.vramCurrentBank;
583
584	GBSerializedVideoFlags flags = state->video.flags;
585	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
586	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
587	video->mode = GBSerializedVideoFlagsGetMode(flags);
588	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
589	video->bcpIndex &= 0x3F;
590	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
591	video->ocpIndex &= 0x3F;
592
593	switch (video->mode) {
594	case 0:
595		video->modeEvent.callback = _endMode0;
596		break;
597	case 1:
598		video->modeEvent.callback = _endMode1;
599		break;
600	case 2:
601		video->modeEvent.callback = _endMode2;
602		break;
603	case 3:
604		video->modeEvent.callback = _endMode3;
605		break;
606	}
607
608	uint32_t when;
609	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
610		LOAD_32LE(when, 0, &state->video.nextMode);
611		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
612	}
613	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
614		LOAD_32LE(when, 0, &state->video.nextFrame);
615		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
616	}
617
618	size_t i;
619	for (i = 0; i < 64; ++i) {
620		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
621		video->renderer->writePalette(video->renderer, i, video->palette[i]);
622	}
623
624	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
625	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
626
627	_cleanOAM(video, video->ly);
628	GBVideoSwitchBank(video, video->vramCurrentBank);
629
630	video->renderer->deinit(video->renderer);
631	video->renderer->init(video->renderer, video->p->model);
632}