all repos — mgba @ 3e75dae3dce0fdae8e7ea0edc73ddd20a5e0e7d4

mGBA Game Boy Advance Emulator

src/gb/video.c (view raw)

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