all repos — mgba @ a949fdfcedb5792685e485ee0ac68d6cc1a81d76

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