all repos — mgba @ a19928f030e6881be7c7a63898c1aeff946860c2

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