all repos — mgba @ db4a873d9caab6fbb4d368690b382f7082dd1b12

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