all repos — mgba @ 84844232ecc9175dc90e2ab2ccff380c6edd502d

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