all repos — mgba @ 746f2b8e6a8a360289f40c6b7425abc1c07d5d91

mGBA Game Boy Advance Emulator

src/gb/video.c (view raw)

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