all repos — mgba @ 23e60e081ea4e5dc35e28feb3af14b3d94bef329

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/cache-set.h>
 11#include <mgba/internal/gb/gb.h>
 12#include <mgba/internal/gb/io.h>
 13#include <mgba/internal/gb/renderers/cache-set.h>
 14#include <mgba/internal/gb/serialize.h>
 15#include <mgba/internal/lr35902/lr35902.h>
 16
 17#include <mgba-util/memory.h>
 18
 19static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders);
 20static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
 21static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 22static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data);
 23static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
 24static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
 25static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam);
 26static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
 27static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
 28static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
 29static void GBVideoDummyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable);
 30static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
 31static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
 32
 33static void _cleanOAM(struct GBVideo* video, int y);
 34
 35static void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate);
 36static void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate);
 37static void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate);
 38static void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate);
 39static void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate);
 40
 41static struct GBVideoRenderer dummyRenderer = {
 42	.init = GBVideoDummyRendererInit,
 43	.deinit = GBVideoDummyRendererDeinit,
 44	.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
 45	.writeSGBPacket = GBVideoDummyRendererWriteSGBPacket,
 46	.writeVRAM = GBVideoDummyRendererWriteVRAM,
 47	.writeOAM = GBVideoDummyRendererWriteOAM,
 48	.writePalette = GBVideoDummyRendererWritePalette,
 49	.drawRange = GBVideoDummyRendererDrawRange,
 50	.finishScanline = GBVideoDummyRendererFinishScanline,
 51	.finishFrame = GBVideoDummyRendererFinishFrame,
 52	.enableSGBBorder = GBVideoDummyRendererEnableSGBBorder,
 53	.getPixels = GBVideoDummyRendererGetPixels,
 54	.putPixels = GBVideoDummyRendererPutPixels,
 55};
 56
 57void GBVideoInit(struct GBVideo* video) {
 58	video->renderer = &dummyRenderer;
 59	video->renderer->cache = NULL;
 60	video->renderer->sgbRenderMode = 0;
 61	video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
 62	video->frameskip = 0;
 63
 64	video->modeEvent.context = video;
 65	video->modeEvent.name = "GB Video Mode";
 66	video->modeEvent.callback = NULL;
 67	video->modeEvent.priority = 8;
 68	video->frameEvent.context = video;
 69	video->frameEvent.name = "GB Video Frame";
 70	video->frameEvent.callback = _updateFrameCount;
 71	video->frameEvent.priority = 9;
 72
 73	video->dmgPalette[0] = 0x7FFF;
 74	video->dmgPalette[1] = 0x56B5;
 75	video->dmgPalette[2] = 0x294A;
 76	video->dmgPalette[3] = 0x0000;
 77	video->dmgPalette[4] = 0x7FFF;
 78	video->dmgPalette[5] = 0x56B5;
 79	video->dmgPalette[6] = 0x294A;
 80	video->dmgPalette[7] = 0x0000;
 81	video->dmgPalette[8] = 0x7FFF;
 82	video->dmgPalette[9] = 0x56B5;
 83	video->dmgPalette[10] = 0x294A;
 84	video->dmgPalette[11] = 0x0000;
 85
 86	video->sgbBorders = true;
 87
 88	video->renderer->sgbCharRam = NULL;
 89	video->renderer->sgbMapRam = NULL;
 90	video->renderer->sgbPalRam = NULL;
 91	video->renderer->sgbAttributes = NULL;
 92	video->renderer->sgbAttributeFiles = NULL;
 93}
 94
 95void GBVideoReset(struct GBVideo* video) {
 96	video->ly = 0;
 97	video->x = 0;
 98	video->mode = 1;
 99	video->stat = 1;
100
101	video->frameCounter = 0;
102	video->frameskipCounter = 0;
103
104	GBVideoSwitchBank(video, 0);
105	video->renderer->vram = video->vram;
106	memset(&video->oam, 0, sizeof(video->oam));
107	video->renderer->oam = &video->oam;
108	memset(&video->palette, 0, sizeof(video->palette));
109
110	if (video->p->model & GB_MODEL_SGB) {
111		video->renderer->sgbCharRam = anonymousMemoryMap(SGB_SIZE_CHAR_RAM);
112		video->renderer->sgbMapRam = anonymousMemoryMap(SGB_SIZE_MAP_RAM);
113		video->renderer->sgbPalRam = anonymousMemoryMap(SGB_SIZE_PAL_RAM);
114		video->renderer->sgbAttributeFiles = anonymousMemoryMap(SGB_SIZE_ATF_RAM);
115		video->renderer->sgbAttributes = malloc(90 * 45);
116		memset(video->renderer->sgbAttributes, 0, 90 * 45);
117		video->sgbCommandHeader = 0;
118		video->sgbBufferIndex = 0;
119	}
120
121	video->palette[0] = video->dmgPalette[0];
122	video->palette[1] = video->dmgPalette[1];
123	video->palette[2] = video->dmgPalette[2];
124	video->palette[3] = video->dmgPalette[3];
125	video->palette[8 * 4 + 0] = video->dmgPalette[4];
126	video->palette[8 * 4 + 1] = video->dmgPalette[5];
127	video->palette[8 * 4 + 2] = video->dmgPalette[6];
128	video->palette[8 * 4 + 3] = video->dmgPalette[7];
129	video->palette[9 * 4 + 0] = video->dmgPalette[8];
130	video->palette[9 * 4 + 1] = video->dmgPalette[9];
131	video->palette[9 * 4 + 2] = video->dmgPalette[10];
132	video->palette[9 * 4 + 3] = video->dmgPalette[11];
133
134	video->renderer->deinit(video->renderer);
135	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
136
137	video->renderer->writePalette(video->renderer, 0, video->palette[0]);
138	video->renderer->writePalette(video->renderer, 1, video->palette[1]);
139	video->renderer->writePalette(video->renderer, 2, video->palette[2]);
140	video->renderer->writePalette(video->renderer, 3, video->palette[3]);
141	video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
142	video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
143	video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
144	video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
145	video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
146	video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
147	video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
148	video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
149}
150
151void GBVideoDeinit(struct GBVideo* video) {
152	video->renderer->deinit(video->renderer);
153	mappedMemoryFree(video->vram, GB_SIZE_VRAM);
154	if (video->renderer->sgbCharRam) {
155		mappedMemoryFree(video->renderer->sgbCharRam, SGB_SIZE_CHAR_RAM);
156		video->renderer->sgbCharRam = NULL;
157	}
158	if (video->renderer->sgbMapRam) {
159		mappedMemoryFree(video->renderer->sgbMapRam, SGB_SIZE_MAP_RAM);
160		video->renderer->sgbMapRam = NULL;
161	}
162	if (video->renderer->sgbPalRam) {
163		mappedMemoryFree(video->renderer->sgbPalRam, SGB_SIZE_PAL_RAM);
164		video->renderer->sgbPalRam = NULL;
165	}
166	if (video->renderer->sgbAttributeFiles) {
167		mappedMemoryFree(video->renderer->sgbAttributeFiles, SGB_SIZE_ATF_RAM);
168		video->renderer->sgbAttributeFiles = NULL;
169	}
170	if (video->renderer->sgbAttributes) {
171		free(video->renderer->sgbAttributes);
172		video->renderer->sgbAttributes = NULL;
173	}
174}
175
176void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
177	video->renderer->deinit(video->renderer);
178	renderer->cache = video->renderer->cache;
179	renderer->sgbRenderMode = video->renderer->sgbRenderMode;
180	renderer->sgbCharRam = video->renderer->sgbCharRam;
181	renderer->sgbMapRam = video->renderer->sgbMapRam;
182	renderer->sgbPalRam = video->renderer->sgbPalRam;
183	renderer->sgbAttributeFiles = video->renderer->sgbAttributeFiles;
184	renderer->sgbAttributes = video->renderer->sgbAttributes;
185	video->renderer = renderer;
186	renderer->vram = video->vram;
187	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
188}
189
190static bool _statIRQAsserted(struct GBVideo* video, GBRegisterSTAT stat) {
191	// TODO: variable for the IRQ line value?
192	if (GBRegisterSTATIsLYCIRQ(stat) && GBRegisterSTATIsLYC(stat)) {
193		return true;
194	}
195	switch (GBRegisterSTATGetMode(stat)) {
196	case 0:
197		if (GBRegisterSTATIsHblankIRQ(stat)) {
198			return true;
199		}
200		break;
201	case 1:
202		if (GBRegisterSTATIsVblankIRQ(stat)) {
203			return true;
204		}
205		break;
206	case 2:
207		if (GBRegisterSTATIsOAMIRQ(stat)) {
208			return true;
209		}
210		break;
211	case 3:
212		break;
213	}
214	return false;
215}
216
217void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate) {
218	struct GBVideo* video = context;
219	if (video->frameskipCounter <= 0) {
220		video->renderer->finishScanline(video->renderer, video->ly);
221	}
222	int lyc = video->p->memory.io[REG_LYC];
223	int32_t next;
224	++video->ly;
225	video->p->memory.io[REG_LY] = video->ly;
226	GBRegisterSTAT oldStat = video->stat;
227	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
228	if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
229		next = GB_VIDEO_MODE_2_LENGTH;
230		video->mode = 2;
231		video->modeEvent.callback = _endMode2;
232	} else {
233		next = GB_VIDEO_HORIZONTAL_LENGTH;
234		video->mode = 1;
235		video->modeEvent.callback = _endMode1;
236
237		mTimingDeschedule(&video->p->timing, &video->frameEvent);
238		mTimingSchedule(&video->p->timing, &video->frameEvent, -cyclesLate);
239
240		if (!_statIRQAsserted(video, oldStat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
241			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
242		}
243		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
244	}
245	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
246	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
247		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
248	}
249	GBUpdateIRQs(video->p);
250	video->p->memory.io[REG_STAT] = video->stat;
251	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
252}
253
254void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
255	struct GBVideo* video = context;
256	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
257		return;
258	}
259	int lyc = video->p->memory.io[REG_LYC];
260	// TODO: One M-cycle delay
261	++video->ly;
262	int32_t next;
263	if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
264		video->ly = 0;
265		video->p->memory.io[REG_LY] = video->ly;
266		next = GB_VIDEO_MODE_2_LENGTH;
267		video->mode = 2;
268		video->modeEvent.callback = _endMode2;
269	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
270		video->p->memory.io[REG_LY] = 0;
271		next = GB_VIDEO_HORIZONTAL_LENGTH - 8;
272	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
273		video->p->memory.io[REG_LY] = video->ly;
274		next = 8;
275	} else {
276		video->p->memory.io[REG_LY] = video->ly;
277		next = GB_VIDEO_HORIZONTAL_LENGTH;
278	}
279
280	GBRegisterSTAT oldStat = video->stat;
281	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
282	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
283	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
284		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
285		GBUpdateIRQs(video->p);
286	}
287	video->p->memory.io[REG_STAT] = video->stat;
288	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
289}
290
291void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
292	struct GBVideo* video = context;
293	_cleanOAM(video, video->ly);
294	video->x = -(video->p->memory.io[REG_SCX] & 7);
295	video->dotClock = mTimingCurrentTime(timing) - cyclesLate + 5 - (video->x << video->p->doubleSpeed);
296	int32_t next = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 6 - video->x;
297	video->mode = 3;
298	video->modeEvent.callback = _endMode3;
299	GBRegisterSTAT oldStat = video->stat;
300	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
301	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
302		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
303		GBUpdateIRQs(video->p);
304	}
305	video->p->memory.io[REG_STAT] = video->stat;
306	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
307}
308
309void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
310	struct GBVideo* video = context;
311	GBVideoProcessDots(video, cyclesLate);
312	if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
313		video->p->memory.hdmaRemaining = 0x10;
314		video->p->cpuBlocked = true;
315		mTimingDeschedule(timing, &video->p->memory.hdmaEvent);
316		mTimingSchedule(timing, &video->p->memory.hdmaEvent, 0);
317	}
318	video->mode = 0;
319	video->modeEvent.callback = _endMode0;
320	GBRegisterSTAT oldStat = video->stat;
321	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
322	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
323		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
324		GBUpdateIRQs(video->p);
325	}
326	video->p->memory.io[REG_STAT] = video->stat;
327	// TODO: Cache SCX & 7 in case it changes
328	int32_t next = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 6 - (video->p->memory.io[REG_SCX] & 7);
329	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
330}
331
332void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate) {
333	UNUSED(cyclesLate);
334	struct GBVideo* video = context;
335	if (video->p->cpu->executionState != LR35902_CORE_FETCH) {
336		mTimingSchedule(timing, &video->frameEvent, 4 - ((video->p->cpu->executionState + 1) & 3));
337		return;
338	}
339
340	GBFrameEnded(video->p);
341	mCoreSyncPostFrame(video->p->sync);
342	--video->frameskipCounter;
343	if (video->frameskipCounter < 0) {
344		video->renderer->finishFrame(video->renderer);
345		video->frameskipCounter = video->frameskip;
346	}
347	++video->frameCounter;
348
349	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
350		mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
351	}
352	GBFrameStarted(video->p);
353}
354
355static void _cleanOAM(struct GBVideo* video, int y) {
356	// TODO: GBC differences
357	// TODO: Optimize
358	video->objMax = 0;
359	int spriteHeight = 8;
360	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
361		spriteHeight = 16;
362	}
363	int o = 0;
364	int i;
365	for (i = 0; i < 40; ++i) {
366		uint8_t oy = video->oam.obj[i].y;
367		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
368			continue;
369		}
370		// TODO: Sort
371		video->objThisLine[o] = video->oam.obj[i];
372		++o;
373		if (o == 10) {
374			break;
375		}
376	}
377	video->objMax = o;
378}
379
380void GBVideoProcessDots(struct GBVideo* video, uint32_t cyclesLate) {
381	if (video->mode != 3) {
382		return;
383	}
384	int oldX = video->x;
385	video->x = (int32_t) (mTimingCurrentTime(&video->p->timing) - cyclesLate - video->dotClock) >> video->p->doubleSpeed;
386	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
387		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
388	} else if (video->x < 0) {
389		return;
390	}
391	if (oldX < 0) {
392		oldX = 0;
393	}
394	if (video->frameskipCounter <= 0) {
395		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
396	}
397}
398
399void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
400	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
401		video->mode = 2;
402		video->modeEvent.callback = _endMode2;
403		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
404		mTimingDeschedule(&video->p->timing, &video->modeEvent);
405		mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
406
407		video->ly = 0;
408		video->p->memory.io[REG_LY] = 0;
409		GBRegisterSTAT oldStat = video->stat;
410		video->stat = GBRegisterSTATSetMode(video->stat, 0);
411		video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
412		if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
413			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
414			GBUpdateIRQs(video->p);
415		}
416		video->p->memory.io[REG_STAT] = video->stat;
417		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
418
419		mTimingDeschedule(&video->p->timing, &video->frameEvent);
420	}
421	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
422		// TODO: Fix serialization; this gets internal and visible modes out of sync
423		video->mode = 0;
424		video->stat = GBRegisterSTATSetMode(video->stat, 0);
425		video->p->memory.io[REG_STAT] = video->stat;
426		video->ly = 0;
427		video->p->memory.io[REG_LY] = 0;
428		video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
429	
430		mTimingDeschedule(&video->p->timing, &video->modeEvent);
431		mTimingDeschedule(&video->p->timing, &video->frameEvent);
432		mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
433	}
434	video->p->memory.io[REG_STAT] = video->stat;
435}
436
437void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
438	GBRegisterSTAT oldStat = video->stat;
439	video->stat = (video->stat & 0x7) | (value & 0x78);
440	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) || video->p->model >= GB_MODEL_CGB) {
441		return;
442	}
443	if (!_statIRQAsserted(video, oldStat) && video->mode < 3) {
444		// TODO: variable for the IRQ line value?
445		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
446		GBUpdateIRQs(video->p);
447	}
448}
449
450void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
451	GBRegisterSTAT oldStat = video->stat;
452	video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
453	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
454		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
455		GBUpdateIRQs(video->p);
456	}
457	video->p->memory.io[REG_STAT] = video->stat;
458}
459
460void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
461	if (video->p->model < GB_MODEL_SGB) {
462		switch (address) {
463		case REG_BGP:
464			video->palette[0] = video->dmgPalette[value & 3];
465			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
466			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
467			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
468			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
469			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
470			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
471			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
472			break;
473		case REG_OBP0:
474			video->palette[8 * 4 + 0] = video->dmgPalette[(value & 3) + 4];
475			video->palette[8 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 4];
476			video->palette[8 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 4];
477			video->palette[8 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 4];
478			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
479			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
480			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
481			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
482			break;
483		case REG_OBP1:
484			video->palette[9 * 4 + 0] = video->dmgPalette[(value & 3) + 8];
485			video->palette[9 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 8];
486			video->palette[9 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 8];
487			video->palette[9 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 8];
488			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
489			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
490			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
491			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
492			break;
493		}
494	} else if (video->p->model & GB_MODEL_SGB) {
495		video->renderer->writeVideoRegister(video->renderer, address, value);
496	} else {
497		switch (address) {
498		case REG_BCPD:
499			if (video->bcpIndex & 1) {
500				video->palette[video->bcpIndex >> 1] &= 0x00FF;
501				video->palette[video->bcpIndex >> 1] |= value << 8;
502			} else {
503				video->palette[video->bcpIndex >> 1] &= 0xFF00;
504				video->palette[video->bcpIndex >> 1] |= value;
505			}
506			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
507			if (video->bcpIncrement) {
508				++video->bcpIndex;
509				video->bcpIndex &= 0x3F;
510				video->p->memory.io[REG_BCPS] &= 0x80;
511				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
512			}
513			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
514			break;
515		case REG_OCPD:
516			if (video->ocpIndex & 1) {
517				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
518				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
519			} else {
520				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
521				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
522			}
523			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
524			if (video->ocpIncrement) {
525				++video->ocpIndex;
526				video->ocpIndex &= 0x3F;
527				video->p->memory.io[REG_OCPS] &= 0x80;
528				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
529			}
530			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
531			break;
532		}
533	}
534}
535
536void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
537	value &= 1;
538	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
539	video->vramCurrentBank = value;
540}
541
542void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
543	if (index >= 12) {
544		return;
545	}
546	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
547}
548
549void GBVideoDisableCGB(struct GBVideo* video) {
550	video->dmgPalette[0] = video->palette[0];
551	video->dmgPalette[1] = video->palette[1];
552	video->dmgPalette[2] = video->palette[2];
553	video->dmgPalette[3] = video->palette[3];
554	video->dmgPalette[4] = video->palette[8 * 4 + 0];
555	video->dmgPalette[5] = video->palette[8 * 4 + 1];
556	video->dmgPalette[6] = video->palette[8 * 4 + 2];
557	video->dmgPalette[7] = video->palette[8 * 4 + 3];
558	video->dmgPalette[8] = video->palette[9 * 4 + 0];
559	video->dmgPalette[9] = video->palette[9 * 4 + 1];
560	video->dmgPalette[10] = video->palette[9 * 4 + 2];
561	video->dmgPalette[11] = video->palette[9 * 4 + 3];
562	video->renderer->deinit(video->renderer);
563	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
564}
565
566void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data) {
567	int i;
568	if (!(video->sgbCommandHeader & 7)) {
569		video->sgbBufferIndex = 0;
570		if ((data[0] >> 3) > SGB_OBJ_TRN) {
571			video->sgbCommandHeader = 0;
572			return;
573		}
574		video->sgbCommandHeader = data[0];
575	}
576	--video->sgbCommandHeader;
577	memcpy(&video->sgbPacketBuffer[video->sgbBufferIndex << 4], data, 16);
578	++video->sgbBufferIndex;
579	if (video->sgbCommandHeader & 7) {
580		return;
581	}
582	switch (video->sgbCommandHeader >> 3) {
583	case SGB_PAL01:
584		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
585		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
586		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
587		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
588
589		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
590		video->palette[5] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
591		video->palette[6] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
592		video->palette[7] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
593
594		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
595		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
596
597		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
598		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
599		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
600		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
601		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
602		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
603		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
604		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
605		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
606		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
607		break;
608	case SGB_PAL23:
609		video->palette[9] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
610		video->palette[10] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
611		video->palette[11] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
612
613		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
614		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
615		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
616		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
617		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
618		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
619		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
620		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
621		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
622		break;
623	case SGB_PAL03:
624		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
625		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
626		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
627		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
628
629		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
630		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
631
632		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
633		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
634		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
635		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
636		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
637		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
638		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
639		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
640		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
641		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
642		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
643		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
644		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
645		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
646		break;
647	case SGB_PAL12:
648		video->palette[5] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
649		video->palette[6] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
650		video->palette[7] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
651
652		video->palette[9] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
653		video->palette[10] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
654		video->palette[11] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
655		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
656		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
657		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
658		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
659		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
660		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
661		break;
662	case SGB_PAL_SET:
663		for (i = 0; i < 4; ++i) {
664			uint16_t entry = (video->sgbPacketBuffer[2 + (i * 2)] << 8) | video->sgbPacketBuffer[1 + (i * 2)];
665			if (entry >= 0x200) {
666				mLOG(GB, STUB, "Unimplemented SGB palette overflow: %03X", entry);
667				continue;
668			}
669			LOAD_16LE(video->palette[i * 4 + 0], entry * 8 + 0, video->renderer->sgbPalRam);
670			video->renderer->writePalette(video->renderer, i * 4 + 0, video->palette[i * 4 + 0]);
671			LOAD_16LE(video->palette[i * 4 + 1], entry * 8 + 2, video->renderer->sgbPalRam);
672			video->renderer->writePalette(video->renderer, i * 4 + 1, video->palette[i * 4 + 1]);
673			LOAD_16LE(video->palette[i * 4 + 2], entry * 8 + 4, video->renderer->sgbPalRam);
674			video->renderer->writePalette(video->renderer, i * 4 + 2, video->palette[i * 4 + 2]);
675			LOAD_16LE(video->palette[i * 4 + 3], entry * 8 + 6, video->renderer->sgbPalRam);
676			video->renderer->writePalette(video->renderer, i * 4 + 3, video->palette[i * 4 + 3]);
677		}
678		break;
679	case SGB_ATTR_BLK:
680	case SGB_ATTR_DIV:
681	case SGB_ATTR_CHR:
682	case SGB_ATTR_LIN:
683	case SGB_PAL_TRN:
684	case SGB_ATRC_EN:
685	case SGB_CHR_TRN:
686	case SGB_PCT_TRN:
687	case SGB_ATTR_TRN:
688	case SGB_ATTR_SET:
689		break;
690	case SGB_MLT_REQ:
691		video->p->sgbControllers = video->sgbPacketBuffer[1] & 0x3;
692		video->p->sgbCurrentController = 0;
693		return;
694	case SGB_MASK_EN:
695		video->renderer->sgbRenderMode = video->sgbPacketBuffer[1] & 0x3;
696		break;
697	default:
698		mLOG(GB, STUB, "Unimplemented SGB command: %02X", video->sgbPacketBuffer[0] >> 3);
699		return;
700	}
701	video->renderer->writeSGBPacket(video->renderer, video->sgbPacketBuffer);
702}
703
704static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders) {
705	UNUSED(renderer);
706	UNUSED(model);
707	UNUSED(borders);
708	// Nothing to do
709}
710
711static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
712	UNUSED(renderer);
713	// Nothing to do
714}
715
716static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
717	if (renderer->cache) {
718		GBVideoCacheWriteVideoRegister(renderer->cache, address, value);
719	}
720	return value;
721}
722
723static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
724	UNUSED(renderer);
725	UNUSED(data);
726}
727
728static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
729	if (renderer->cache) {
730		mCacheSetWriteVRAM(renderer->cache, address);
731	}
732}
733
734static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
735	UNUSED(renderer);
736	UNUSED(oam);
737	// Nothing to do
738}
739
740static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
741	if (renderer->cache) {
742		mCacheSetWritePalette(renderer->cache, index, mColorFrom555(value));
743	}
744}
745
746static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
747	UNUSED(renderer);
748	UNUSED(endX);
749	UNUSED(startX);
750	UNUSED(y);
751	UNUSED(obj);
752	UNUSED(oamMax);
753	// Nothing to do
754}
755
756static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
757	UNUSED(renderer);
758	UNUSED(y);
759	// Nothing to do
760}
761
762static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
763	UNUSED(renderer);
764	// Nothing to do
765}
766
767static void GBVideoDummyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
768	UNUSED(renderer);
769	UNUSED(enable);
770	// Nothing to do
771}
772
773static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
774	UNUSED(renderer);
775	UNUSED(stride);
776	UNUSED(pixels);
777	// Nothing to do
778}
779
780static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
781	UNUSED(renderer);
782	UNUSED(stride);
783	UNUSED(pixels);
784	// Nothing to do
785}
786
787void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
788	STORE_16LE(video->x, 0, &state->video.x);
789	STORE_16LE(video->ly, 0, &state->video.ly);
790	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
791	STORE_32LE(video->dotClock, 0, &state->video.dotCounter);
792	state->video.vramCurrentBank = video->vramCurrentBank;
793
794	GBSerializedVideoFlags flags = 0;
795	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
796	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
797	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
798	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
799	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
800	state->video.flags = flags;
801	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
802	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
803
804	size_t i;
805	for (i = 0; i < 64; ++i) {
806		STORE_16LE(video->palette[i], i * 2, state->video.palette);
807	}
808
809	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
810	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
811
812	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
813	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
814}
815
816void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
817	LOAD_16LE(video->x, 0, &state->video.x);
818	LOAD_16LE(video->ly, 0, &state->video.ly);
819	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
820	LOAD_32LE(video->dotClock, 0, &state->video.dotCounter);
821	video->vramCurrentBank = state->video.vramCurrentBank;
822
823	GBSerializedVideoFlags flags = state->video.flags;
824	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
825	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
826	video->mode = GBSerializedVideoFlagsGetMode(flags);
827	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
828	video->bcpIndex &= 0x3F;
829	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
830	video->ocpIndex &= 0x3F;
831
832	switch (video->mode) {
833	case 0:
834		video->modeEvent.callback = _endMode0;
835		break;
836	case 1:
837		video->modeEvent.callback = _endMode1;
838		break;
839	case 2:
840		video->modeEvent.callback = _endMode2;
841		break;
842	case 3:
843		video->modeEvent.callback = _endMode3;
844		break;
845	}
846
847	uint32_t when;
848	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
849		LOAD_32LE(when, 0, &state->video.nextMode);
850		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
851	}
852	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
853		LOAD_32LE(when, 0, &state->video.nextFrame);
854		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
855	}
856
857	size_t i;
858	for (i = 0; i < 64; ++i) {
859		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
860		video->renderer->writePalette(video->renderer, i, video->palette[i]);
861	}
862
863	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
864	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
865
866	_cleanOAM(video, video->ly);
867	GBVideoSwitchBank(video, video->vramCurrentBank);
868
869	video->renderer->deinit(video->renderer);
870	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
871}