all repos — mgba @ 65207f5c0f8173ac31c5f382e1f6f274ab6e701f

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