all repos — mgba @ 8ffc716249fc579f009fab120c013f44dfcabcfc

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