all repos — mgba @ 0fd6532b3817c375836002716c90a21d1111a197

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/sm83/sm83.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(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 GBVideoSkipBIOS(struct GBVideo* video) {
218	video->mode = 1;
219	video->modeEvent.callback = _endMode1;
220
221	int32_t next;
222	if (video->p->model == GB_MODEL_CGB) {
223		video->ly = GB_VIDEO_VERTICAL_PIXELS;
224		video->p->memory.io[REG_LY] = video->ly;
225		video->stat = GBRegisterSTATClearLYC(video->stat);
226		next = 20;
227	} else {
228		video->ly = GB_VIDEO_VERTICAL_TOTAL_PIXELS;
229		video->p->memory.io[REG_LY] = 0;
230		next = 56;
231	}
232	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
233
234	video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
235	GBUpdateIRQs(video->p);
236	video->p->memory.io[REG_STAT] = video->stat;
237	mTimingDeschedule(&video->p->timing, &video->modeEvent);
238	mTimingSchedule(&video->p->timing, &video->modeEvent, next);
239}
240
241void _endMode0(struct mTiming* timing, void* context, uint32_t cyclesLate) {
242	struct GBVideo* video = context;
243	if (video->frameskipCounter <= 0) {
244		video->renderer->finishScanline(video->renderer, video->ly);
245	}
246	int lyc = video->p->memory.io[REG_LYC];
247	int32_t next;
248	++video->ly;
249	video->p->memory.io[REG_LY] = video->ly;
250	GBRegisterSTAT oldStat = video->stat;
251	if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
252		next = GB_VIDEO_MODE_2_LENGTH;
253		video->mode = 2;
254		video->modeEvent.callback = _endMode2;
255	} else {
256		next = GB_VIDEO_HORIZONTAL_LENGTH;
257		video->mode = 1;
258		video->modeEvent.callback = _endMode1;
259
260		mTimingDeschedule(&video->p->timing, &video->frameEvent);
261		mTimingSchedule(&video->p->timing, &video->frameEvent, -cyclesLate);
262
263		if (!_statIRQAsserted(oldStat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
264			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
265		}
266		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
267	}
268	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
269	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
270		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
271	}
272
273	// LYC stat is delayed 1 T-cycle
274	oldStat = video->stat;
275	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
276	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
277		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
278	}
279
280	GBUpdateIRQs(video->p);
281	video->p->memory.io[REG_STAT] = video->stat;
282	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
283}
284
285void _endMode1(struct mTiming* timing, void* context, uint32_t cyclesLate) {
286	struct GBVideo* video = context;
287	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
288		return;
289	}
290	int lyc = video->p->memory.io[REG_LYC];
291	// TODO: One M-cycle delay
292	++video->ly;
293	int32_t next;
294	if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
295		video->ly = 0;
296		video->p->memory.io[REG_LY] = video->ly;
297		next = GB_VIDEO_MODE_2_LENGTH;
298		video->mode = 2;
299		video->modeEvent.callback = _endMode2;
300	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
301		video->p->memory.io[REG_LY] = 0;
302		next = GB_VIDEO_HORIZONTAL_LENGTH - 8;
303	} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
304		video->p->memory.io[REG_LY] = video->ly;
305		next = 8;
306	} else {
307		video->p->memory.io[REG_LY] = video->ly;
308		next = GB_VIDEO_HORIZONTAL_LENGTH;
309	}
310
311	GBRegisterSTAT oldStat = video->stat;
312	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
313	video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
314	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
315		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
316		GBUpdateIRQs(video->p);
317	}
318	video->p->memory.io[REG_STAT] = video->stat;
319	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
320}
321
322void _endMode2(struct mTiming* timing, void* context, uint32_t cyclesLate) {
323	struct GBVideo* video = context;
324	_cleanOAM(video, video->ly);
325	video->x = -(video->p->memory.io[REG_SCX] & 7);
326	video->dotClock = mTimingCurrentTime(timing) - cyclesLate + 5 - (video->x << video->p->doubleSpeed);
327	int32_t next = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 6 - video->x;
328	video->mode = 3;
329	video->modeEvent.callback = _endMode3;
330	GBRegisterSTAT oldStat = video->stat;
331	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
332	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
333		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
334		GBUpdateIRQs(video->p);
335	}
336	video->p->memory.io[REG_STAT] = video->stat;
337	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
338}
339
340void _endMode3(struct mTiming* timing, void* context, uint32_t cyclesLate) {
341	struct GBVideo* video = context;
342	GBVideoProcessDots(video, cyclesLate);
343	if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
344		video->p->memory.hdmaRemaining = 0x10;
345		video->p->cpuBlocked = true;
346		mTimingDeschedule(timing, &video->p->memory.hdmaEvent);
347		mTimingSchedule(timing, &video->p->memory.hdmaEvent, 0);
348	}
349	video->mode = 0;
350	video->modeEvent.callback = _endMode0;
351	GBRegisterSTAT oldStat = video->stat;
352	video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
353	if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
354		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
355		GBUpdateIRQs(video->p);
356	}
357	video->p->memory.io[REG_STAT] = video->stat;
358	// TODO: Cache SCX & 7 in case it changes
359	int32_t next = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 6 - (video->p->memory.io[REG_SCX] & 7);
360	mTimingSchedule(timing, &video->modeEvent, (next << video->p->doubleSpeed) - cyclesLate);
361}
362
363void _updateFrameCount(struct mTiming* timing, void* context, uint32_t cyclesLate) {
364	UNUSED(cyclesLate);
365	struct GBVideo* video = context;
366	if (video->p->cpu->executionState != SM83_CORE_FETCH) {
367		mTimingSchedule(timing, &video->frameEvent, 4 - ((video->p->cpu->executionState + 1) & 3));
368		return;
369	}
370	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
371		mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
372	}
373
374	--video->frameskipCounter;
375	if (video->frameskipCounter < 0) {
376		video->renderer->finishFrame(video->renderer);
377		video->frameskipCounter = video->frameskip;
378	}
379	GBFrameEnded(video->p);
380	mCoreSyncPostFrame(video->p->sync);
381	++video->frameCounter;
382
383	GBFrameStarted(video->p);
384}
385
386static void _cleanOAM(struct GBVideo* video, int y) {
387	// TODO: GBC differences
388	// TODO: Optimize
389	video->objMax = 0;
390	int spriteHeight = 8;
391	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
392		spriteHeight = 16;
393	}
394	int o = 0;
395	int i;
396	for (i = 0; i < 40; ++i) {
397		uint8_t oy = video->oam.obj[i].y;
398		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
399			continue;
400		}
401		// TODO: Sort
402		video->objThisLine[o] = video->oam.obj[i];
403		++o;
404		if (o == 10) {
405			break;
406		}
407	}
408	video->objMax = o;
409}
410
411void GBVideoProcessDots(struct GBVideo* video, uint32_t cyclesLate) {
412	if (video->mode != 3) {
413		return;
414	}
415	int oldX = video->x;
416	video->x = (int32_t) (mTimingCurrentTime(&video->p->timing) - cyclesLate - video->dotClock) >> video->p->doubleSpeed;
417	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
418		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
419	} else if (video->x < 0) {
420		return;
421	}
422	if (oldX < 0) {
423		oldX = 0;
424	}
425	if (video->frameskipCounter <= 0) {
426		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
427	}
428}
429
430void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
431	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
432		video->mode = 2;
433		video->modeEvent.callback = _endMode2;
434		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
435		mTimingDeschedule(&video->p->timing, &video->modeEvent);
436		mTimingSchedule(&video->p->timing, &video->modeEvent, next << video->p->doubleSpeed);
437
438		video->ly = 0;
439		video->p->memory.io[REG_LY] = 0;
440		GBRegisterSTAT oldStat = video->stat;
441		video->stat = GBRegisterSTATSetMode(video->stat, 0);
442		video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
443		if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
444			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
445			GBUpdateIRQs(video->p);
446		}
447		video->p->memory.io[REG_STAT] = video->stat;
448		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
449
450		mTimingDeschedule(&video->p->timing, &video->frameEvent);
451	}
452	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
453		// TODO: Fix serialization; this gets internal and visible modes out of sync
454		video->mode = 0;
455		video->stat = GBRegisterSTATSetMode(video->stat, 0);
456		video->p->memory.io[REG_STAT] = video->stat;
457		video->ly = 0;
458		video->p->memory.io[REG_LY] = 0;
459		video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
460	
461		mTimingDeschedule(&video->p->timing, &video->modeEvent);
462		mTimingDeschedule(&video->p->timing, &video->frameEvent);
463		mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
464	}
465	video->p->memory.io[REG_STAT] = video->stat;
466}
467
468void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
469	GBRegisterSTAT oldStat = video->stat;
470	video->stat = (video->stat & 0x7) | (value & 0x78);
471	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) || video->p->model >= GB_MODEL_CGB) {
472		return;
473	}
474	if (!_statIRQAsserted(oldStat) && video->mode < 3) {
475		// TODO: variable for the IRQ line value?
476		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
477		GBUpdateIRQs(video->p);
478	}
479}
480
481void GBVideoWriteLYC(struct GBVideo* video, uint8_t value) {
482	GBRegisterSTAT oldStat = video->stat;
483	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
484		video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
485		if (!_statIRQAsserted(oldStat) && _statIRQAsserted(video->stat)) {
486			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
487			GBUpdateIRQs(video->p);
488		}
489	}
490	video->p->memory.io[REG_STAT] = video->stat;
491}
492
493void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
494	if (video->p->model < GB_MODEL_SGB) {
495		switch (address) {
496		case REG_BGP:
497			video->palette[0] = video->dmgPalette[value & 3];
498			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
499			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
500			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
501			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
502			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
503			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
504			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
505			break;
506		case REG_OBP0:
507			video->palette[8 * 4 + 0] = video->dmgPalette[(value & 3) + 4];
508			video->palette[8 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 4];
509			video->palette[8 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 4];
510			video->palette[8 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 4];
511			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
512			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
513			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
514			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
515			break;
516		case REG_OBP1:
517			video->palette[9 * 4 + 0] = video->dmgPalette[(value & 3) + 8];
518			video->palette[9 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 8];
519			video->palette[9 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 8];
520			video->palette[9 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 8];
521			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
522			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
523			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
524			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
525			break;
526		}
527	} else if (video->p->model & GB_MODEL_SGB) {
528		video->renderer->writeVideoRegister(video->renderer, address, value);
529	} else {
530		switch (address) {
531		case REG_BCPD:
532			if (video->mode != 3) {
533				if (video->bcpIndex & 1) {
534					video->palette[video->bcpIndex >> 1] &= 0x00FF;
535					video->palette[video->bcpIndex >> 1] |= value << 8;
536				} else {
537					video->palette[video->bcpIndex >> 1] &= 0xFF00;
538					video->palette[video->bcpIndex >> 1] |= value;
539				}
540				video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
541			}
542			if (video->bcpIncrement) {
543				++video->bcpIndex;
544				video->bcpIndex &= 0x3F;
545				video->p->memory.io[REG_BCPS] &= 0x80;
546				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
547			}
548			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
549			break;
550		case REG_OCPD:
551			if (video->mode != 3) {
552				if (video->ocpIndex & 1) {
553					video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
554					video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
555				} else {
556					video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
557					video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
558				}
559				video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
560			}
561			if (video->ocpIncrement) {
562				++video->ocpIndex;
563				video->ocpIndex &= 0x3F;
564				video->p->memory.io[REG_OCPS] &= 0x80;
565				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
566			}
567			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
568			break;
569		}
570	}
571}
572
573void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
574	value &= 1;
575	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
576	video->vramCurrentBank = value;
577}
578
579void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
580	if (index >= 12) {
581		return;
582	}
583	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
584}
585
586void GBVideoDisableCGB(struct GBVideo* video) {
587	video->dmgPalette[0] = video->palette[0];
588	video->dmgPalette[1] = video->palette[1];
589	video->dmgPalette[2] = video->palette[2];
590	video->dmgPalette[3] = video->palette[3];
591	video->dmgPalette[4] = video->palette[8 * 4 + 0];
592	video->dmgPalette[5] = video->palette[8 * 4 + 1];
593	video->dmgPalette[6] = video->palette[8 * 4 + 2];
594	video->dmgPalette[7] = video->palette[8 * 4 + 3];
595	video->dmgPalette[8] = video->palette[9 * 4 + 0];
596	video->dmgPalette[9] = video->palette[9 * 4 + 1];
597	video->dmgPalette[10] = video->palette[9 * 4 + 2];
598	video->dmgPalette[11] = video->palette[9 * 4 + 3];
599	video->renderer->deinit(video->renderer);
600	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
601}
602
603void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data) {
604	int i;
605	if (!(video->sgbCommandHeader & 7)) {
606		video->sgbBufferIndex = 0;
607		if ((data[0] >> 3) > SGB_OBJ_TRN) {
608			video->sgbCommandHeader = 0;
609			return;
610		}
611		video->sgbCommandHeader = data[0];
612	}
613	--video->sgbCommandHeader;
614	memcpy(&video->sgbPacketBuffer[video->sgbBufferIndex << 4], data, 16);
615	++video->sgbBufferIndex;
616	if (video->sgbCommandHeader & 7) {
617		return;
618	}
619	switch (video->sgbCommandHeader >> 3) {
620	case SGB_PAL01:
621		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
622		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
623		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
624		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
625
626		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
627		video->palette[5] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
628		video->palette[6] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
629		video->palette[7] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
630
631		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
632		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
633
634		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
635		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
636		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
637		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
638		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
639		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
640		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
641		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
642		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
643		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
644		break;
645	case SGB_PAL23:
646		video->palette[9] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
647		video->palette[10] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
648		video->palette[11] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
649
650		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
651		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
652		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
653		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
654		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
655		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
656		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
657		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
658		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
659		break;
660	case SGB_PAL03:
661		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
662		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
663		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
664		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
665
666		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
667		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
668
669		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
670		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
671		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
672		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
673		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
674		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
675		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
676		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
677		video->renderer->writePalette(video->renderer, 4, video->palette[4]);
678		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
679		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
680		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
681		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
682		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
683		break;
684	case SGB_PAL12:
685		video->palette[5] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
686		video->palette[6] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
687		video->palette[7] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
688
689		video->palette[9] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
690		video->palette[10] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
691		video->palette[11] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
692		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
693		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
694		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
695		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
696		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
697		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
698		break;
699	case SGB_PAL_SET:
700		for (i = 0; i < 4; ++i) {
701			uint16_t entry = (video->sgbPacketBuffer[2 + (i * 2)] << 8) | video->sgbPacketBuffer[1 + (i * 2)];
702			if (entry >= 0x200) {
703				mLOG(GB, STUB, "Unimplemented SGB palette overflow: %03X", entry);
704				continue;
705			}
706			LOAD_16LE(video->palette[i * 4 + 0], entry * 8 + 0, video->renderer->sgbPalRam);
707			video->renderer->writePalette(video->renderer, i * 4 + 0, video->palette[i * 4 + 0]);
708			LOAD_16LE(video->palette[i * 4 + 1], entry * 8 + 2, video->renderer->sgbPalRam);
709			video->renderer->writePalette(video->renderer, i * 4 + 1, video->palette[i * 4 + 1]);
710			LOAD_16LE(video->palette[i * 4 + 2], entry * 8 + 4, video->renderer->sgbPalRam);
711			video->renderer->writePalette(video->renderer, i * 4 + 2, video->palette[i * 4 + 2]);
712			LOAD_16LE(video->palette[i * 4 + 3], entry * 8 + 6, video->renderer->sgbPalRam);
713			video->renderer->writePalette(video->renderer, i * 4 + 3, video->palette[i * 4 + 3]);
714		}
715		break;
716	case SGB_ATTR_BLK:
717	case SGB_ATTR_DIV:
718	case SGB_ATTR_CHR:
719	case SGB_ATTR_LIN:
720	case SGB_PAL_TRN:
721	case SGB_ATRC_EN:
722	case SGB_CHR_TRN:
723	case SGB_PCT_TRN:
724	case SGB_ATTR_TRN:
725	case SGB_ATTR_SET:
726		break;
727	case SGB_MLT_REQ:
728		if ((video->sgbPacketBuffer[1] & 0x3) == 2) { // XXX: This unmasked increment appears to be an SGB hardware bug
729			++video->p->sgbCurrentController;
730		}
731		video->p->sgbControllers = video->sgbPacketBuffer[1] & 0x3;
732		video->p->sgbCurrentController &= video->p->sgbControllers;
733		return;
734	case SGB_MASK_EN:
735		video->renderer->sgbRenderMode = video->sgbPacketBuffer[1] & 0x3;
736		break;
737	default:
738		mLOG(GB, STUB, "Unimplemented SGB command: %02X", video->sgbPacketBuffer[0] >> 3);
739		return;
740	}
741	video->renderer->writeSGBPacket(video->renderer, video->sgbPacketBuffer);
742}
743
744static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders) {
745	UNUSED(renderer);
746	UNUSED(model);
747	UNUSED(borders);
748	// Nothing to do
749}
750
751static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
752	UNUSED(renderer);
753	// Nothing to do
754}
755
756static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
757	if (renderer->cache) {
758		GBVideoCacheWriteVideoRegister(renderer->cache, address, value);
759	}
760	return value;
761}
762
763static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
764	UNUSED(renderer);
765	UNUSED(data);
766}
767
768static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
769	if (renderer->cache) {
770		mCacheSetWriteVRAM(renderer->cache, address);
771	}
772}
773
774static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
775	UNUSED(renderer);
776	UNUSED(oam);
777	// Nothing to do
778}
779
780static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
781	if (renderer->cache) {
782		mCacheSetWritePalette(renderer->cache, index, mColorFrom555(value));
783	}
784}
785
786static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
787	UNUSED(renderer);
788	UNUSED(endX);
789	UNUSED(startX);
790	UNUSED(y);
791	UNUSED(obj);
792	UNUSED(oamMax);
793	// Nothing to do
794}
795
796static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
797	UNUSED(renderer);
798	UNUSED(y);
799	// Nothing to do
800}
801
802static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
803	UNUSED(renderer);
804	// Nothing to do
805}
806
807static void GBVideoDummyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
808	UNUSED(renderer);
809	UNUSED(enable);
810	// Nothing to do
811}
812
813static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
814	UNUSED(renderer);
815	UNUSED(stride);
816	UNUSED(pixels);
817	// Nothing to do
818}
819
820static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
821	UNUSED(renderer);
822	UNUSED(stride);
823	UNUSED(pixels);
824	// Nothing to do
825}
826
827void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
828	STORE_16LE(video->x, 0, &state->video.x);
829	STORE_16LE(video->ly, 0, &state->video.ly);
830	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
831	STORE_32LE(video->dotClock, 0, &state->video.dotCounter);
832	state->video.vramCurrentBank = video->vramCurrentBank;
833
834	GBSerializedVideoFlags flags = 0;
835	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
836	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
837	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
838	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
839	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
840	state->video.flags = flags;
841	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
842	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
843
844	size_t i;
845	for (i = 0; i < 64; ++i) {
846		STORE_16LE(video->palette[i], i * 2, state->video.palette);
847	}
848
849	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
850	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
851
852	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
853	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
854}
855
856void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
857	LOAD_16LE(video->x, 0, &state->video.x);
858	LOAD_16LE(video->ly, 0, &state->video.ly);
859	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
860	LOAD_32LE(video->dotClock, 0, &state->video.dotCounter);
861	video->vramCurrentBank = state->video.vramCurrentBank;
862
863	GBSerializedVideoFlags flags = state->video.flags;
864	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
865	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
866	video->mode = GBSerializedVideoFlagsGetMode(flags);
867	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
868	video->bcpIndex &= 0x3F;
869	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
870	video->ocpIndex &= 0x3F;
871
872	switch (video->mode) {
873	case 0:
874		video->modeEvent.callback = _endMode0;
875		break;
876	case 1:
877		video->modeEvent.callback = _endMode1;
878		break;
879	case 2:
880		video->modeEvent.callback = _endMode2;
881		break;
882	case 3:
883		video->modeEvent.callback = _endMode3;
884		break;
885	}
886
887	uint32_t when;
888	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
889		LOAD_32LE(when, 0, &state->video.nextMode);
890		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
891	}
892	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
893		LOAD_32LE(when, 0, &state->video.nextFrame);
894		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
895	}
896
897	video->renderer->deinit(video->renderer);
898	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
899
900	size_t i;
901	for (i = 0; i < 64; ++i) {
902		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
903		video->renderer->writePalette(video->renderer, i, video->palette[i]);
904	}
905
906	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
907	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
908
909	_cleanOAM(video, video->ly);
910	GBVideoSwitchBank(video, video->vramCurrentBank);
911}