all repos — mgba @ 7b543df00225ce9c356f357e5f4c4c2de76ab61e

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 = timing->masterCycles - 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}
390
391void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
392	if (video->p->model < GB_MODEL_CGB) {
393		switch (address) {
394		case REG_BGP:
395			video->palette[0] = video->dmgPalette[value & 3];
396			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
397			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
398			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
399			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
400			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
401			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
402			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
403			break;
404		case REG_OBP0:
405			video->palette[8 * 4 + 0] = video->dmgPalette[value & 3];
406			video->palette[8 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
407			video->palette[8 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
408			video->palette[8 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
409			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
410			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
411			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
412			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
413			break;
414		case REG_OBP1:
415			video->palette[9 * 4 + 0] = video->dmgPalette[value & 3];
416			video->palette[9 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
417			video->palette[9 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
418			video->palette[9 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
419			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
420			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
421			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
422			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
423			break;
424		}
425	} else {
426		switch (address) {
427		case REG_BCPD:
428			if (video->bcpIndex & 1) {
429				video->palette[video->bcpIndex >> 1] &= 0x00FF;
430				video->palette[video->bcpIndex >> 1] |= value << 8;
431			} else {
432				video->palette[video->bcpIndex >> 1] &= 0xFF00;
433				video->palette[video->bcpIndex >> 1] |= value;
434			}
435			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
436			if (video->bcpIncrement) {
437				++video->bcpIndex;
438				video->bcpIndex &= 0x3F;
439				video->p->memory.io[REG_BCPS] &= 0x80;
440				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
441			}
442			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
443			break;
444		case REG_OCPD:
445			if (video->ocpIndex & 1) {
446				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
447				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
448			} else {
449				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
450				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
451			}
452			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
453			if (video->ocpIncrement) {
454				++video->ocpIndex;
455				video->ocpIndex &= 0x3F;
456				video->p->memory.io[REG_OCPS] &= 0x80;
457				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
458			}
459			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
460			break;
461		}
462	}
463}
464
465void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
466	value &= 1;
467	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
468	video->vramCurrentBank = value;
469}
470
471void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
472	if (index >= 4) {
473		return;
474	}
475	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
476}
477
478static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
479	UNUSED(renderer);
480	UNUSED(model);
481	// Nothing to do
482}
483
484static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
485	UNUSED(renderer);
486	// Nothing to do
487}
488
489static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
490	UNUSED(renderer);
491	UNUSED(address);
492	return value;
493}
494
495static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
496	if (renderer->cache) {
497		mTileCacheWriteVRAM(renderer->cache, address);
498	}
499}
500
501static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
502	UNUSED(renderer);
503	UNUSED(oam);
504	// Nothing to do
505}
506
507static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
508	UNUSED(value);
509	if (renderer->cache) {
510		mTileCacheWritePalette(renderer->cache, index << 1);
511	}
512}
513
514static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
515	UNUSED(renderer);
516	UNUSED(endX);
517	UNUSED(startX);
518	UNUSED(y);
519	UNUSED(obj);
520	UNUSED(oamMax);
521	// Nothing to do
522}
523
524static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
525	UNUSED(renderer);
526	UNUSED(y);
527	// Nothing to do
528}
529
530static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
531	UNUSED(renderer);
532	// Nothing to do
533}
534
535static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
536	UNUSED(renderer);
537	UNUSED(stride);
538	UNUSED(pixels);
539	// Nothing to do
540}
541
542static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
543	UNUSED(renderer);
544	UNUSED(stride);
545	UNUSED(pixels);
546	// Nothing to do
547}
548
549void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
550	STORE_16LE(video->x, 0, &state->video.x);
551	STORE_16LE(video->ly, 0, &state->video.ly);
552	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
553	state->video.vramCurrentBank = video->vramCurrentBank;
554
555	GBSerializedVideoFlags flags = 0;
556	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
557	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
558	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
559	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
560	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
561	state->video.flags = flags;
562	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
563	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
564
565	size_t i;
566	for (i = 0; i < 64; ++i) {
567		STORE_16LE(video->palette[i], i * 2, state->video.palette);
568	}
569
570	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
571	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
572
573	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
574	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
575}
576
577void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
578	LOAD_16LE(video->x, 0, &state->video.x);
579	LOAD_16LE(video->ly, 0, &state->video.ly);
580	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
581	video->vramCurrentBank = state->video.vramCurrentBank;
582
583	GBSerializedVideoFlags flags = state->video.flags;
584	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
585	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
586	video->mode = GBSerializedVideoFlagsGetMode(flags);
587	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
588	video->bcpIndex &= 0x3F;
589	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
590	video->ocpIndex &= 0x3F;
591
592	switch (video->mode) {
593	case 0:
594		video->modeEvent.callback = _endMode0;
595		break;
596	case 1:
597		video->modeEvent.callback = _endMode1;
598		break;
599	case 2:
600		video->modeEvent.callback = _endMode2;
601		break;
602	case 3:
603		video->modeEvent.callback = _endMode3;
604		break;
605	}
606
607	uint32_t when;
608	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
609		LOAD_32LE(when, 0, &state->video.nextMode);
610		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
611	}
612	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
613		LOAD_32LE(when, 0, &state->video.nextFrame);
614		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
615	}
616
617	size_t i;
618	for (i = 0; i < 64; ++i) {
619		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
620		video->renderer->writePalette(video->renderer, i, video->palette[i]);
621	}
622
623	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
624	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
625
626	_cleanOAM(video, video->ly);
627	GBVideoSwitchBank(video, video->vramCurrentBank);
628
629	video->renderer->deinit(video->renderer);
630	video->renderer->init(video->renderer, video->p->model);
631}