all repos — mgba @ 38bad71e119aa71f9c89d066fdfe589d3cdfd2e6

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	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
453		video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
454		if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
455			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
456			GBUpdateIRQs(video->p);
457		}
458	}
459	video->p->memory.io[REG_STAT] = video->stat;
460}
461
462void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
463	if (video->p->model < GB_MODEL_SGB) {
464		switch (address) {
465		case REG_BGP:
466			video->palette[0] = video->dmgPalette[value & 3];
467			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
468			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
469			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
470			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
471			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
472			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
473			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
474			break;
475		case REG_OBP0:
476			video->palette[8 * 4 + 0] = video->dmgPalette[(value & 3) + 4];
477			video->palette[8 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 4];
478			video->palette[8 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 4];
479			video->palette[8 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 4];
480			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
481			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
482			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
483			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
484			break;
485		case REG_OBP1:
486			video->palette[9 * 4 + 0] = video->dmgPalette[(value & 3) + 8];
487			video->palette[9 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 8];
488			video->palette[9 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 8];
489			video->palette[9 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 8];
490			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
491			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
492			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
493			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
494			break;
495		}
496	} else if (video->p->model & GB_MODEL_SGB) {
497		video->renderer->writeVideoRegister(video->renderer, address, value);
498	} else {
499		switch (address) {
500		case REG_BCPD:
501			if (video->bcpIndex & 1) {
502				video->palette[video->bcpIndex >> 1] &= 0x00FF;
503				video->palette[video->bcpIndex >> 1] |= value << 8;
504			} else {
505				video->palette[video->bcpIndex >> 1] &= 0xFF00;
506				video->palette[video->bcpIndex >> 1] |= value;
507			}
508			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
509			if (video->bcpIncrement) {
510				++video->bcpIndex;
511				video->bcpIndex &= 0x3F;
512				video->p->memory.io[REG_BCPS] &= 0x80;
513				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
514			}
515			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
516			break;
517		case REG_OCPD:
518			if (video->ocpIndex & 1) {
519				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
520				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
521			} else {
522				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
523				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
524			}
525			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
526			if (video->ocpIncrement) {
527				++video->ocpIndex;
528				video->ocpIndex &= 0x3F;
529				video->p->memory.io[REG_OCPS] &= 0x80;
530				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
531			}
532			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
533			break;
534		}
535	}
536}
537
538void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
539	value &= 1;
540	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
541	video->vramCurrentBank = value;
542}
543
544void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
545	if (index >= 12) {
546		return;
547	}
548	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
549}
550
551void GBVideoDisableCGB(struct GBVideo* video) {
552	video->dmgPalette[0] = video->palette[0];
553	video->dmgPalette[1] = video->palette[1];
554	video->dmgPalette[2] = video->palette[2];
555	video->dmgPalette[3] = video->palette[3];
556	video->dmgPalette[4] = video->palette[8 * 4 + 0];
557	video->dmgPalette[5] = video->palette[8 * 4 + 1];
558	video->dmgPalette[6] = video->palette[8 * 4 + 2];
559	video->dmgPalette[7] = video->palette[8 * 4 + 3];
560	video->dmgPalette[8] = video->palette[9 * 4 + 0];
561	video->dmgPalette[9] = video->palette[9 * 4 + 1];
562	video->dmgPalette[10] = video->palette[9 * 4 + 2];
563	video->dmgPalette[11] = video->palette[9 * 4 + 3];
564	video->renderer->deinit(video->renderer);
565	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
566}
567
568void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data) {
569	int i;
570	if (!(video->sgbCommandHeader & 7)) {
571		video->sgbBufferIndex = 0;
572		if ((data[0] >> 3) > SGB_OBJ_TRN) {
573			video->sgbCommandHeader = 0;
574			return;
575		}
576		video->sgbCommandHeader = data[0];
577	}
578	--video->sgbCommandHeader;
579	memcpy(&video->sgbPacketBuffer[video->sgbBufferIndex << 4], data, 16);
580	++video->sgbBufferIndex;
581	if (video->sgbCommandHeader & 7) {
582		return;
583	}
584	switch (video->sgbCommandHeader >> 3) {
585	case SGB_PAL01:
586		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
587		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
588		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
589		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
590
591		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
592		video->palette[5] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
593		video->palette[6] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
594		video->palette[7] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
595
596		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
597		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
598
599		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
600		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
601		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
602		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
603		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
604		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
605		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
606		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
607		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
608		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
609		break;
610	case SGB_PAL23:
611		video->palette[9] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
612		video->palette[10] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
613		video->palette[11] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
614
615		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
616		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
617		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
618		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
619		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
620		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
621		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
622		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
623		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
624		break;
625	case SGB_PAL03:
626		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
627		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
628		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
629		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
630
631		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
632		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
633
634		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
635		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
636		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
637		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
638		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
639		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
640		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
641		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
642		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
643		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
644		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
645		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
646		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
647		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
648		break;
649	case SGB_PAL12:
650		video->palette[5] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
651		video->palette[6] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
652		video->palette[7] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
653
654		video->palette[9] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
655		video->palette[10] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
656		video->palette[11] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
657		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
658		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
659		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
660		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
661		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
662		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
663		break;
664	case SGB_PAL_SET:
665		for (i = 0; i < 4; ++i) {
666			uint16_t entry = (video->sgbPacketBuffer[2 + (i * 2)] << 8) | video->sgbPacketBuffer[1 + (i * 2)];
667			if (entry >= 0x200) {
668				mLOG(GB, STUB, "Unimplemented SGB palette overflow: %03X", entry);
669				continue;
670			}
671			LOAD_16LE(video->palette[i * 4 + 0], entry * 8 + 0, video->renderer->sgbPalRam);
672			video->renderer->writePalette(video->renderer, i * 4 + 0, video->palette[i * 4 + 0]);
673			LOAD_16LE(video->palette[i * 4 + 1], entry * 8 + 2, video->renderer->sgbPalRam);
674			video->renderer->writePalette(video->renderer, i * 4 + 1, video->palette[i * 4 + 1]);
675			LOAD_16LE(video->palette[i * 4 + 2], entry * 8 + 4, video->renderer->sgbPalRam);
676			video->renderer->writePalette(video->renderer, i * 4 + 2, video->palette[i * 4 + 2]);
677			LOAD_16LE(video->palette[i * 4 + 3], entry * 8 + 6, video->renderer->sgbPalRam);
678			video->renderer->writePalette(video->renderer, i * 4 + 3, video->palette[i * 4 + 3]);
679		}
680		break;
681	case SGB_ATTR_BLK:
682	case SGB_ATTR_DIV:
683	case SGB_ATTR_CHR:
684	case SGB_ATTR_LIN:
685	case SGB_PAL_TRN:
686	case SGB_ATRC_EN:
687	case SGB_CHR_TRN:
688	case SGB_PCT_TRN:
689	case SGB_ATTR_TRN:
690	case SGB_ATTR_SET:
691		break;
692	case SGB_MLT_REQ:
693		video->p->sgbControllers = video->sgbPacketBuffer[1] & 0x3;
694		video->p->sgbCurrentController = 0;
695		return;
696	case SGB_MASK_EN:
697		video->renderer->sgbRenderMode = video->sgbPacketBuffer[1] & 0x3;
698		break;
699	default:
700		mLOG(GB, STUB, "Unimplemented SGB command: %02X", video->sgbPacketBuffer[0] >> 3);
701		return;
702	}
703	video->renderer->writeSGBPacket(video->renderer, video->sgbPacketBuffer);
704}
705
706static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders) {
707	UNUSED(renderer);
708	UNUSED(model);
709	UNUSED(borders);
710	// Nothing to do
711}
712
713static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
714	UNUSED(renderer);
715	// Nothing to do
716}
717
718static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
719	if (renderer->cache) {
720		GBVideoCacheWriteVideoRegister(renderer->cache, address, value);
721	}
722	return value;
723}
724
725static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
726	UNUSED(renderer);
727	UNUSED(data);
728}
729
730static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
731	if (renderer->cache) {
732		mCacheSetWriteVRAM(renderer->cache, address);
733	}
734}
735
736static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
737	UNUSED(renderer);
738	UNUSED(oam);
739	// Nothing to do
740}
741
742static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
743	if (renderer->cache) {
744		mCacheSetWritePalette(renderer->cache, index, mColorFrom555(value));
745	}
746}
747
748static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
749	UNUSED(renderer);
750	UNUSED(endX);
751	UNUSED(startX);
752	UNUSED(y);
753	UNUSED(obj);
754	UNUSED(oamMax);
755	// Nothing to do
756}
757
758static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
759	UNUSED(renderer);
760	UNUSED(y);
761	// Nothing to do
762}
763
764static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
765	UNUSED(renderer);
766	// Nothing to do
767}
768
769static void GBVideoDummyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
770	UNUSED(renderer);
771	UNUSED(enable);
772	// Nothing to do
773}
774
775static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
776	UNUSED(renderer);
777	UNUSED(stride);
778	UNUSED(pixels);
779	// Nothing to do
780}
781
782static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
783	UNUSED(renderer);
784	UNUSED(stride);
785	UNUSED(pixels);
786	// Nothing to do
787}
788
789void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
790	STORE_16LE(video->x, 0, &state->video.x);
791	STORE_16LE(video->ly, 0, &state->video.ly);
792	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
793	STORE_32LE(video->dotClock, 0, &state->video.dotCounter);
794	state->video.vramCurrentBank = video->vramCurrentBank;
795
796	GBSerializedVideoFlags flags = 0;
797	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
798	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
799	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
800	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
801	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
802	state->video.flags = flags;
803	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
804	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
805
806	size_t i;
807	for (i = 0; i < 64; ++i) {
808		STORE_16LE(video->palette[i], i * 2, state->video.palette);
809	}
810
811	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
812	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
813
814	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
815	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
816}
817
818void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
819	LOAD_16LE(video->x, 0, &state->video.x);
820	LOAD_16LE(video->ly, 0, &state->video.ly);
821	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
822	LOAD_32LE(video->dotClock, 0, &state->video.dotCounter);
823	video->vramCurrentBank = state->video.vramCurrentBank;
824
825	GBSerializedVideoFlags flags = state->video.flags;
826	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
827	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
828	video->mode = GBSerializedVideoFlagsGetMode(flags);
829	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
830	video->bcpIndex &= 0x3F;
831	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
832	video->ocpIndex &= 0x3F;
833
834	switch (video->mode) {
835	case 0:
836		video->modeEvent.callback = _endMode0;
837		break;
838	case 1:
839		video->modeEvent.callback = _endMode1;
840		break;
841	case 2:
842		video->modeEvent.callback = _endMode2;
843		break;
844	case 3:
845		video->modeEvent.callback = _endMode3;
846		break;
847	}
848
849	uint32_t when;
850	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
851		LOAD_32LE(when, 0, &state->video.nextMode);
852		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
853	}
854	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
855		LOAD_32LE(when, 0, &state->video.nextFrame);
856		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
857	}
858
859	size_t i;
860	for (i = 0; i < 64; ++i) {
861		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
862		video->renderer->writePalette(video->renderer, i, video->palette[i]);
863	}
864
865	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
866	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
867
868	_cleanOAM(video, video->ly);
869	GBVideoSwitchBank(video, video->vramCurrentBank);
870
871	video->renderer->deinit(video->renderer);
872	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
873}