all repos — mgba @ 37d65d181ccdfd7d18a20fb8031f873fb95c0d12

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		mTimingDeschedule(&video->p->timing, &video->frameEvent);
158		mTimingSchedule(&video->p->timing, &video->frameEvent, -cyclesLate);
159
160		if (!_statIRQAsserted(video, oldStat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
161			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
162		}
163		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
164	}
165	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
166	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
167		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
168	}
169	GBUpdateIRQs(video->p);
170	video->p->memory.io[REG_STAT] = video->stat;
171	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
172}
173
174void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
175	struct GBVideo* video = context;
176	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
177		return;
178	}
179	int lyc = video->p->memory.io[REG_LYC];
180	// TODO: One M-cycle delay
181	++video->ly;
182	int32_t next;
183	if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
184		video->ly = 0;
185		video->p->memory.io[REG_LY] = video->ly;
186		next = GB_VIDEO_MODE_2_LENGTH + (video->p->memory.io[REG_SCX] & 7);
187		video->mode = 2;
188		video->modeEvent.callback = _endMode2;
189	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
190		video->p->memory.io[REG_LY] = 0;
191		next = GB_VIDEO_HORIZONTAL_LENGTH - 8;
192	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
193		video->p->memory.io[REG_LY] = video->ly;
194		next = 8;
195	} else {
196		video->p->memory.io[REG_LY] = video->ly;
197		next = GB_VIDEO_HORIZONTAL_LENGTH;
198	}
199
200	GBRegisterSTAT oldStat = video->stat;
201	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
202	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
203	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
204		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
205		GBUpdateIRQs(video->p);
206	}
207	video->p->memory.io[REG_STAT] = video->stat;
208	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
209}
210
211void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
212	struct GBVideo* video = context;
213	_cleanOAM(video, video->ly);
214	video->x = 0;
215	video->dotClock = timing->masterCycles - cyclesLate;
216	int32_t next = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 6 - (video->p->memory.io[REG_SCX] & 7);
217	video->mode = 3;
218	video->modeEvent.callback = _endMode3;
219	GBRegisterSTAT oldStat = video->stat;
220	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
221	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
222		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
223		GBUpdateIRQs(video->p);
224	}
225	video->p->memory.io[REG_STAT] = video->stat;
226	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
227}
228
229void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
230	struct GBVideo* video = context;
231	GBVideoProcessDots(video);
232	if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
233		video->p->memory.hdmaRemaining = 0x10;
234		video->p->cpuBlocked = true;
235		mTimingDeschedule(timing, &video->p->memory.hdmaEvent);
236		mTimingSchedule(timing, &video->p->memory.hdmaEvent, 0);
237	}
238	video->mode = 0;
239	video->modeEvent.callback = _endMode0;
240	GBRegisterSTAT oldStat = video->stat;
241	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
242	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
243		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
244		GBUpdateIRQs(video->p);
245	}
246	video->p->memory.io[REG_STAT] = video->stat;
247	int32_t next = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 6;
248	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
249}
250
251void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate) {
252	UNUSED(cyclesLate);
253	struct GBVideo* video = context;
254	if (video->p->cpu->executionState != LR35902_CORE_FETCH) {
255		mTimingSchedule(timing, &video->frameEvent, 4 - ((video->p->cpu->executionState + 1) & 3));
256		return;
257	}
258
259	size_t c;
260	for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) {
261		struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c);
262		if (callbacks->videoFrameEnded) {
263			callbacks->videoFrameEnded(callbacks->context);
264		}
265	}
266
267	GBFrameEnded(video->p);
268	mCoreSyncPostFrame(video->p->sync);
269	--video->frameskipCounter;
270	if (video->frameskipCounter < 0) {
271		video->renderer->finishFrame(video->renderer);
272		video->frameskipCounter = video->frameskip;
273	}
274	++video->frameCounter;
275
276	// TODO: Move to common code
277	if (video->p->stream && video->p->stream->postVideoFrame) {
278		const color_t* pixels;
279		size_t stride;
280		video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);
281		video->p->stream->postVideoFrame(video->p->stream, pixels, stride);
282	}
283
284	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
285		mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
286	}
287
288	for (c = 0; c < mCoreCallbacksListSize(&video->p->coreCallbacks); ++c) {
289		struct mCoreCallbacks* callbacks = mCoreCallbacksListGetPointer(&video->p->coreCallbacks, c);
290		if (callbacks->videoFrameStarted) {
291			callbacks->videoFrameStarted(callbacks->context);
292		}
293	}
294}
295
296static void _cleanOAM(struct GBVideo* video, int y) {
297	// TODO: GBC differences
298	// TODO: Optimize
299	video->objMax = 0;
300	int spriteHeight = 8;
301	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
302		spriteHeight = 16;
303	}
304	int o = 0;
305	int i;
306	for (i = 0; i < 40; ++i) {
307		uint8_t oy = video->oam.obj[i].y;
308		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
309			continue;
310		}
311		// TODO: Sort
312		video->objThisLine[o] = video->oam.obj[i];
313		++o;
314		if (o == 10) {
315			break;
316		}
317	}
318	video->objMax = o;
319}
320
321void GBVideoProcessDots(struct GBVideo* video) {
322	if (video->mode != 3) {
323		return;
324	}
325	int oldX = video->x;
326	video->x = (video->p->timing.masterCycles - video->dotClock + video->p->cpu->cycles) >> video->p->doubleSpeed;
327	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
328		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
329	} else if (video->x < 0) {
330		mLOG(GB, FATAL, "Video dot clock went negative!");
331		video->x = oldX;
332	}
333	if (video->frameskipCounter <= 0) {
334		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
335	}
336}
337
338void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
339	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
340		video->mode = 2;
341		video->modeEvent.callback = _endMode2;
342		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
343		mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
344
345		video->ly = 0;
346		video->p->memory.io[REG_LY] = 0;
347		GBRegisterSTAT oldStat = video->stat;
348		video->stat = GBRegisterSTATSetMode(video->stat, 0);
349		video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
350		if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, 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		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
356
357		mTimingDeschedule(&video->p->timing, &video->frameEvent);
358	}
359	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
360		// TODO: Fix serialization; this gets internal and visible modes out of sync
361		video->stat = GBRegisterSTATSetMode(video->stat, 0);
362		video->p->memory.io[REG_STAT] = video->stat;
363		video->ly = 0;
364		video->p->memory.io[REG_LY] = 0;
365		video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
366	
367		mTimingDeschedule(&video->p->timing, &video->modeEvent);
368		mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
369	}
370	video->p->memory.io[REG_STAT] = video->stat;
371}
372
373void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
374	GBRegisterSTAT oldStat = video->stat;
375	video->stat = (video->stat & 0x7) | (value & 0x78);
376	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) || video->p->model >= GB_MODEL_CGB) {
377		return;
378	}
379	if (!_statIRQAsserted(video, oldStat) && video->mode < 3) {
380		// TODO: variable for the IRQ line value?
381		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
382		GBUpdateIRQs(video->p);
383	}
384}
385
386void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
387	GBRegisterSTAT oldStat = video->stat;
388	video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
389	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
390		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
391		GBUpdateIRQs(video->p);
392	}
393	video->p->memory.io[REG_STAT] = video->stat;
394}
395
396void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
397	if (video->p->model < GB_MODEL_CGB) {
398		switch (address) {
399		case REG_BGP:
400			video->palette[0] = video->dmgPalette[value & 3];
401			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
402			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
403			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
404			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
405			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
406			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
407			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
408			break;
409		case REG_OBP0:
410			video->palette[8 * 4 + 0] = video->dmgPalette[value & 3];
411			video->palette[8 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
412			video->palette[8 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
413			video->palette[8 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
414			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
415			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
416			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
417			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
418			break;
419		case REG_OBP1:
420			video->palette[9 * 4 + 0] = video->dmgPalette[value & 3];
421			video->palette[9 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
422			video->palette[9 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
423			video->palette[9 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
424			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
425			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
426			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
427			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
428			break;
429		}
430	} else {
431		switch (address) {
432		case REG_BCPD:
433			if (video->bcpIndex & 1) {
434				video->palette[video->bcpIndex >> 1] &= 0x00FF;
435				video->palette[video->bcpIndex >> 1] |= value << 8;
436			} else {
437				video->palette[video->bcpIndex >> 1] &= 0xFF00;
438				video->palette[video->bcpIndex >> 1] |= value;
439			}
440			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
441			if (video->bcpIncrement) {
442				++video->bcpIndex;
443				video->bcpIndex &= 0x3F;
444				video->p->memory.io[REG_BCPS] &= 0x80;
445				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
446			}
447			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
448			break;
449		case REG_OCPD:
450			if (video->ocpIndex & 1) {
451				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
452				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
453			} else {
454				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
455				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
456			}
457			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
458			if (video->ocpIncrement) {
459				++video->ocpIndex;
460				video->ocpIndex &= 0x3F;
461				video->p->memory.io[REG_OCPS] &= 0x80;
462				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
463			}
464			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
465			break;
466		}
467	}
468}
469
470void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
471	value &= 1;
472	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
473	video->vramCurrentBank = value;
474}
475
476void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
477	if (index >= 4) {
478		return;
479	}
480	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
481}
482
483static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
484	UNUSED(renderer);
485	UNUSED(model);
486	// Nothing to do
487}
488
489static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
490	UNUSED(renderer);
491	// Nothing to do
492}
493
494static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
495	UNUSED(renderer);
496	UNUSED(address);
497	return value;
498}
499
500static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
501	if (renderer->cache) {
502		mTileCacheWriteVRAM(renderer->cache, address);
503	}
504}
505
506static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
507	UNUSED(renderer);
508	UNUSED(oam);
509	// Nothing to do
510}
511
512static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
513	UNUSED(value);
514	if (renderer->cache) {
515		mTileCacheWritePalette(renderer->cache, index << 1);
516	}
517}
518
519static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
520	UNUSED(renderer);
521	UNUSED(endX);
522	UNUSED(startX);
523	UNUSED(y);
524	UNUSED(obj);
525	UNUSED(oamMax);
526	// Nothing to do
527}
528
529static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
530	UNUSED(renderer);
531	UNUSED(y);
532	// Nothing to do
533}
534
535static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
536	UNUSED(renderer);
537	// Nothing to do
538}
539
540static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
541	UNUSED(renderer);
542	UNUSED(stride);
543	UNUSED(pixels);
544	// Nothing to do
545}
546
547static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
548	UNUSED(renderer);
549	UNUSED(stride);
550	UNUSED(pixels);
551	// Nothing to do
552}
553
554void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
555	STORE_16LE(video->x, 0, &state->video.x);
556	STORE_16LE(video->ly, 0, &state->video.ly);
557	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
558	state->video.vramCurrentBank = video->vramCurrentBank;
559
560	GBSerializedVideoFlags flags = 0;
561	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
562	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
563	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
564	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
565	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
566	state->video.flags = flags;
567	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
568	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
569
570	size_t i;
571	for (i = 0; i < 64; ++i) {
572		STORE_16LE(video->palette[i], i * 2, state->video.palette);
573	}
574
575	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
576	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
577
578	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
579	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
580}
581
582void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
583	LOAD_16LE(video->x, 0, &state->video.x);
584	LOAD_16LE(video->ly, 0, &state->video.ly);
585	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
586	video->vramCurrentBank = state->video.vramCurrentBank;
587
588	GBSerializedVideoFlags flags = state->video.flags;
589	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
590	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
591	video->mode = GBSerializedVideoFlagsGetMode(flags);
592	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
593	video->bcpIndex &= 0x3F;
594	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
595	video->ocpIndex &= 0x3F;
596
597	switch (video->mode) {
598	case 0:
599		video->modeEvent.callback = _endMode0;
600		break;
601	case 1:
602		video->modeEvent.callback = _endMode1;
603		break;
604	case 2:
605		video->modeEvent.callback = _endMode2;
606		break;
607	case 3:
608		video->modeEvent.callback = _endMode3;
609		break;
610	}
611
612	uint32_t when;
613	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
614		LOAD_32LE(when, 0, &state->video.nextMode);
615		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
616	}
617	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
618		LOAD_32LE(when, 0, &state->video.nextFrame);
619		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
620	}
621
622	size_t i;
623	for (i = 0; i < 64; ++i) {
624		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
625		video->renderer->writePalette(video->renderer, i, video->palette[i]);
626	}
627
628	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
629	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
630
631	_cleanOAM(video, video->ly);
632	GBVideoSwitchBank(video, video->vramCurrentBank);
633
634	video->renderer->deinit(video->renderer);
635	video->renderer->init(video->renderer, video->p->model);
636}