all repos — mgba @ 65665324ef9124280a8a310ab9cea048d519a5a9

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