all repos — mgba @ aa0f988daa2319272e3d6816b75ebf66896c6f32

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