all repos — mgba @ f6755a6e1b7b0cf2b944cd8ca842746f11d6bf82

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