all repos — mgba @ 00e8b9877f76420e1e85c34cc2a156d1a52b8a30

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
347	GBFrameEnded(video->p);
348	mCoreSyncPostFrame(video->p->sync);
349	--video->frameskipCounter;
350	if (video->frameskipCounter < 0) {
351		video->renderer->finishFrame(video->renderer);
352		video->frameskipCounter = video->frameskip;
353	}
354	++video->frameCounter;
355
356	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC])) {
357		mTimingSchedule(timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
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->bcpIndex & 1) {
509				video->palette[video->bcpIndex >> 1] &= 0x00FF;
510				video->palette[video->bcpIndex >> 1] |= value << 8;
511			} else {
512				video->palette[video->bcpIndex >> 1] &= 0xFF00;
513				video->palette[video->bcpIndex >> 1] |= value;
514			}
515			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
516			if (video->bcpIncrement) {
517				++video->bcpIndex;
518				video->bcpIndex &= 0x3F;
519				video->p->memory.io[REG_BCPS] &= 0x80;
520				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
521			}
522			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
523			break;
524		case REG_OCPD:
525			if (video->ocpIndex & 1) {
526				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
527				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
528			} else {
529				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
530				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
531			}
532			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
533			if (video->ocpIncrement) {
534				++video->ocpIndex;
535				video->ocpIndex &= 0x3F;
536				video->p->memory.io[REG_OCPS] &= 0x80;
537				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
538			}
539			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
540			break;
541		}
542	}
543}
544
545void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
546	value &= 1;
547	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
548	video->vramCurrentBank = value;
549}
550
551void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
552	if (index >= 12) {
553		return;
554	}
555	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
556}
557
558void GBVideoDisableCGB(struct GBVideo* video) {
559	video->dmgPalette[0] = video->palette[0];
560	video->dmgPalette[1] = video->palette[1];
561	video->dmgPalette[2] = video->palette[2];
562	video->dmgPalette[3] = video->palette[3];
563	video->dmgPalette[4] = video->palette[8 * 4 + 0];
564	video->dmgPalette[5] = video->palette[8 * 4 + 1];
565	video->dmgPalette[6] = video->palette[8 * 4 + 2];
566	video->dmgPalette[7] = video->palette[8 * 4 + 3];
567	video->dmgPalette[8] = video->palette[9 * 4 + 0];
568	video->dmgPalette[9] = video->palette[9 * 4 + 1];
569	video->dmgPalette[10] = video->palette[9 * 4 + 2];
570	video->dmgPalette[11] = video->palette[9 * 4 + 3];
571	video->renderer->deinit(video->renderer);
572	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
573}
574
575void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data) {
576	int i;
577	if (!(video->sgbCommandHeader & 7)) {
578		video->sgbBufferIndex = 0;
579		if ((data[0] >> 3) > SGB_OBJ_TRN) {
580			video->sgbCommandHeader = 0;
581			return;
582		}
583		video->sgbCommandHeader = data[0];
584	}
585	--video->sgbCommandHeader;
586	memcpy(&video->sgbPacketBuffer[video->sgbBufferIndex << 4], data, 16);
587	++video->sgbBufferIndex;
588	if (video->sgbCommandHeader & 7) {
589		return;
590	}
591	switch (video->sgbCommandHeader >> 3) {
592	case SGB_PAL01:
593		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
594		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
595		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
596		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
597
598		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
599		video->palette[5] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
600		video->palette[6] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
601		video->palette[7] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
602
603		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
604		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[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, 4, video->palette[4]);
611		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
612		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
613		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
614		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
615		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
616		break;
617	case SGB_PAL23:
618		video->palette[9] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
619		video->palette[10] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
620		video->palette[11] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
621
622		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
623		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
624		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
625		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
626		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
627		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
628		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
629		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
630		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
631		break;
632	case SGB_PAL03:
633		video->palette[0] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
634		video->palette[1] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
635		video->palette[2] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
636		video->palette[3] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
637
638		video->palette[4] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
639		video->palette[8] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
640
641		video->palette[12] = video->sgbPacketBuffer[1] | (video->sgbPacketBuffer[2] << 8);
642		video->palette[13] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
643		video->palette[14] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
644		video->palette[15] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[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, 4, video->palette[4]);
650		video->renderer->writePalette(video->renderer, 8, video->palette[8]);
651		video->renderer->writePalette(video->renderer, 12, video->palette[12]);
652		video->renderer->writePalette(video->renderer, 13, video->palette[13]);
653		video->renderer->writePalette(video->renderer, 14, video->palette[14]);
654		video->renderer->writePalette(video->renderer, 15, video->palette[15]);
655		break;
656	case SGB_PAL12:
657		video->palette[5] = video->sgbPacketBuffer[3] | (video->sgbPacketBuffer[4] << 8);
658		video->palette[6] = video->sgbPacketBuffer[5] | (video->sgbPacketBuffer[6] << 8);
659		video->palette[7] = video->sgbPacketBuffer[7] | (video->sgbPacketBuffer[8] << 8);
660
661		video->palette[9] = video->sgbPacketBuffer[9] | (video->sgbPacketBuffer[10] << 8);
662		video->palette[10] = video->sgbPacketBuffer[11] | (video->sgbPacketBuffer[12] << 8);
663		video->palette[11] = video->sgbPacketBuffer[13] | (video->sgbPacketBuffer[14] << 8);
664		video->renderer->writePalette(video->renderer, 5, video->palette[5]);
665		video->renderer->writePalette(video->renderer, 6, video->palette[6]);
666		video->renderer->writePalette(video->renderer, 7, video->palette[7]);
667		video->renderer->writePalette(video->renderer, 9, video->palette[9]);
668		video->renderer->writePalette(video->renderer, 10, video->palette[10]);
669		video->renderer->writePalette(video->renderer, 11, video->palette[11]);
670		break;
671	case SGB_PAL_SET:
672		for (i = 0; i < 4; ++i) {
673			uint16_t entry = (video->sgbPacketBuffer[2 + (i * 2)] << 8) | video->sgbPacketBuffer[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[i * 4 + 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_ATTR_DIV:
690	case SGB_ATTR_CHR:
691	case SGB_ATTR_LIN:
692	case SGB_PAL_TRN:
693	case SGB_ATRC_EN:
694	case SGB_CHR_TRN:
695	case SGB_PCT_TRN:
696	case SGB_ATTR_TRN:
697	case SGB_ATTR_SET:
698		break;
699	case SGB_MLT_REQ:
700		video->p->sgbControllers = video->sgbPacketBuffer[1] & 0x3;
701		video->p->sgbCurrentController = 0;
702		return;
703	case SGB_MASK_EN:
704		video->renderer->sgbRenderMode = video->sgbPacketBuffer[1] & 0x3;
705		break;
706	default:
707		mLOG(GB, STUB, "Unimplemented SGB command: %02X", video->sgbPacketBuffer[0] >> 3);
708		return;
709	}
710	video->renderer->writeSGBPacket(video->renderer, video->sgbPacketBuffer);
711}
712
713static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders) {
714	UNUSED(renderer);
715	UNUSED(model);
716	UNUSED(borders);
717	// Nothing to do
718}
719
720static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
721	UNUSED(renderer);
722	// Nothing to do
723}
724
725static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
726	if (renderer->cache) {
727		GBVideoCacheWriteVideoRegister(renderer->cache, address, value);
728	}
729	return value;
730}
731
732static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
733	UNUSED(renderer);
734	UNUSED(data);
735}
736
737static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
738	if (renderer->cache) {
739		mCacheSetWriteVRAM(renderer->cache, address);
740	}
741}
742
743static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
744	UNUSED(renderer);
745	UNUSED(oam);
746	// Nothing to do
747}
748
749static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
750	if (renderer->cache) {
751		mCacheSetWritePalette(renderer->cache, index, mColorFrom555(value));
752	}
753}
754
755static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
756	UNUSED(renderer);
757	UNUSED(endX);
758	UNUSED(startX);
759	UNUSED(y);
760	UNUSED(obj);
761	UNUSED(oamMax);
762	// Nothing to do
763}
764
765static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
766	UNUSED(renderer);
767	UNUSED(y);
768	// Nothing to do
769}
770
771static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
772	UNUSED(renderer);
773	// Nothing to do
774}
775
776static void GBVideoDummyRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
777	UNUSED(renderer);
778	UNUSED(enable);
779	// Nothing to do
780}
781
782static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
783	UNUSED(renderer);
784	UNUSED(stride);
785	UNUSED(pixels);
786	// Nothing to do
787}
788
789static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
790	UNUSED(renderer);
791	UNUSED(stride);
792	UNUSED(pixels);
793	// Nothing to do
794}
795
796void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
797	STORE_16LE(video->x, 0, &state->video.x);
798	STORE_16LE(video->ly, 0, &state->video.ly);
799	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
800	STORE_32LE(video->dotClock, 0, &state->video.dotCounter);
801	state->video.vramCurrentBank = video->vramCurrentBank;
802
803	GBSerializedVideoFlags flags = 0;
804	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
805	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
806	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
807	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
808	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
809	state->video.flags = flags;
810	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
811	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
812
813	size_t i;
814	for (i = 0; i < 64; ++i) {
815		STORE_16LE(video->palette[i], i * 2, state->video.palette);
816	}
817
818	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
819	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
820
821	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
822	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
823}
824
825void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
826	LOAD_16LE(video->x, 0, &state->video.x);
827	LOAD_16LE(video->ly, 0, &state->video.ly);
828	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
829	LOAD_32LE(video->dotClock, 0, &state->video.dotCounter);
830	video->vramCurrentBank = state->video.vramCurrentBank;
831
832	GBSerializedVideoFlags flags = state->video.flags;
833	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
834	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
835	video->mode = GBSerializedVideoFlagsGetMode(flags);
836	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
837	video->bcpIndex &= 0x3F;
838	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
839	video->ocpIndex &= 0x3F;
840
841	switch (video->mode) {
842	case 0:
843		video->modeEvent.callback = _endMode0;
844		break;
845	case 1:
846		video->modeEvent.callback = _endMode1;
847		break;
848	case 2:
849		video->modeEvent.callback = _endMode2;
850		break;
851	case 3:
852		video->modeEvent.callback = _endMode3;
853		break;
854	}
855
856	uint32_t when;
857	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
858		LOAD_32LE(when, 0, &state->video.nextMode);
859		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
860	}
861	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
862		LOAD_32LE(when, 0, &state->video.nextFrame);
863		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
864	}
865
866	size_t i;
867	for (i = 0; i < 64; ++i) {
868		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
869		video->renderer->writePalette(video->renderer, i, video->palette[i]);
870	}
871
872	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
873	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
874
875	_cleanOAM(video, video->ly);
876	GBVideoSwitchBank(video, video->vramCurrentBank);
877
878	video->renderer->deinit(video->renderer);
879	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
880}