all repos — mgba @ 431ee76c1a362188012ec1ddbf6213422f82dfb4

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		GBRegisterSTAT oldStat = video->stat;
422		video->stat = GBRegisterSTATSetMode(video->stat, 0);
423		video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
424		if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
425			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
426			GBUpdateIRQs(video->p);
427		}
428		video->p->memory.io[REG_STAT] = video->stat;
429		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
430
431		mTimingDeschedule(&video->p->timing, &video->frameEvent);
432	}
433	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
434		// TODO: Fix serialization; this gets internal and visible modes out of sync
435		video->stat = GBRegisterSTATSetMode(video->stat, 0);
436		video->p->memory.io[REG_STAT] = video->stat;
437		video->ly = 0;
438		video->p->memory.io[REG_LY] = 0;
439		video->renderer->writePalette(video->renderer, 0, video->dmgPalette[0]);
440	
441		mTimingDeschedule(&video->p->timing, &video->modeEvent);
442		mTimingSchedule(&video->p->timing, &video->frameEvent, GB_VIDEO_TOTAL_LENGTH);
443	}
444	video->p->memory.io[REG_STAT] = video->stat;
445}
446
447void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
448	GBRegisterSTAT oldStat = video->stat;
449	video->stat = (video->stat & 0x7) | (value & 0x78);
450	if (video->p->model < GB_MODEL_CGB && !_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	video->stat = GBRegisterSTATSetLYC(video->stat, value == video->ly);
460	if (!_statIRQAsserted(video, oldStat) && _statIRQAsserted(video, video->stat)) {
461		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
462		GBUpdateIRQs(video->p);
463	}
464	video->p->memory.io[REG_STAT] = video->stat;
465}
466
467void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
468	if (video->p->model < GB_MODEL_SGB) {
469		switch (address) {
470		case REG_BGP:
471			video->palette[0] = video->dmgPalette[value & 3];
472			video->palette[1] = video->dmgPalette[(value >> 2) & 3];
473			video->palette[2] = video->dmgPalette[(value >> 4) & 3];
474			video->palette[3] = video->dmgPalette[(value >> 6) & 3];
475			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
476			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
477			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
478			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
479			break;
480		case REG_OBP0:
481			video->palette[8 * 4 + 0] = video->dmgPalette[value & 3];
482			video->palette[8 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
483			video->palette[8 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
484			video->palette[8 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
485			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
486			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
487			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
488			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
489			break;
490		case REG_OBP1:
491			video->palette[9 * 4 + 0] = video->dmgPalette[value & 3];
492			video->palette[9 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
493			video->palette[9 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
494			video->palette[9 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
495			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
496			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
497			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
498			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
499			break;
500		}
501	} else if (video->p->model == GB_MODEL_SGB) {
502		video->renderer->writeVideoRegister(video->renderer, address, value);
503	} else {
504		switch (address) {
505		case REG_BCPD:
506			if (video->bcpIndex & 1) {
507				video->palette[video->bcpIndex >> 1] &= 0x00FF;
508				video->palette[video->bcpIndex >> 1] |= value << 8;
509			} else {
510				video->palette[video->bcpIndex >> 1] &= 0xFF00;
511				video->palette[video->bcpIndex >> 1] |= value;
512			}
513			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
514			if (video->bcpIncrement) {
515				++video->bcpIndex;
516				video->bcpIndex &= 0x3F;
517				video->p->memory.io[REG_BCPS] &= 0x80;
518				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
519			}
520			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
521			break;
522		case REG_OCPD:
523			if (video->ocpIndex & 1) {
524				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
525				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
526			} else {
527				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
528				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
529			}
530			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
531			if (video->ocpIncrement) {
532				++video->ocpIndex;
533				video->ocpIndex &= 0x3F;
534				video->p->memory.io[REG_OCPS] &= 0x80;
535				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
536			}
537			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
538			break;
539		}
540	}
541}
542
543void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
544	value &= 1;
545	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
546	video->vramCurrentBank = value;
547}
548
549void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
550	if (index >= 4) {
551		return;
552	}
553	video->dmgPalette[index] = M_RGB8_TO_RGB5(color);
554}
555
556void GBVideoWriteSGBPacket(struct GBVideo* video, uint8_t* data) {
557	int i;
558	if (!(video->sgbCommandHeader & 7)) {
559		if ((data[0] >> 3) > SGB_OBJ_TRN) {
560			video->sgbCommandHeader = 0;
561			return;
562		}
563		video->sgbCommandHeader = data[0];
564	}
565	--video->sgbCommandHeader;
566	switch (video->sgbCommandHeader >> 3) {
567	case SGB_PAL01:
568		video->palette[0] = data[1] | (data[2] << 8);
569		video->palette[1] = data[3] | (data[4] << 8);
570		video->palette[2] = data[5] | (data[6] << 8);
571		video->palette[3] = data[7] | (data[8] << 8);
572
573		video->palette[16] = data[1] | (data[2] << 8);
574		video->palette[17] = data[9] | (data[10] << 8);
575		video->palette[18] = data[11] | (data[12] << 8);
576		video->palette[19] = data[13] | (data[14] << 8);
577
578		video->palette[32] = data[1] | (data[2] << 8);
579		video->palette[48] = data[1] | (data[2] << 8);
580
581		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
582		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
583		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
584		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
585		video->renderer->writePalette(video->renderer, 16, video->palette[0]);
586		video->renderer->writePalette(video->renderer, 17, video->palette[17]);
587		video->renderer->writePalette(video->renderer, 18, video->palette[18]);
588		video->renderer->writePalette(video->renderer, 19, video->palette[19]);
589		video->renderer->writePalette(video->renderer, 32, video->palette[0]);
590		video->renderer->writePalette(video->renderer, 48, video->palette[0]);
591		break;
592	case SGB_PAL23:
593		video->palette[33] = data[3] | (data[4] << 8);
594		video->palette[34] = data[5] | (data[6] << 8);
595		video->palette[35] = data[7] | (data[8] << 8);
596
597		video->palette[49] = data[9] | (data[10] << 8);
598		video->palette[50] = data[11] | (data[12] << 8);
599		video->palette[51] = data[13] | (data[14] << 8);
600		video->renderer->writePalette(video->renderer, 33, video->palette[33]);
601		video->renderer->writePalette(video->renderer, 34, video->palette[34]);
602		video->renderer->writePalette(video->renderer, 35, video->palette[35]);
603		video->renderer->writePalette(video->renderer, 49, video->palette[49]);
604		video->renderer->writePalette(video->renderer, 50, video->palette[50]);
605		video->renderer->writePalette(video->renderer, 51, video->palette[51]);
606		break;
607	case SGB_PAL03:
608		video->palette[0] = data[1] | (data[2] << 8);
609		video->palette[1] = data[3] | (data[4] << 8);
610		video->palette[2] = data[5] | (data[6] << 8);
611		video->palette[3] = data[7] | (data[8] << 8);
612
613		video->palette[16] = data[1] | (data[2] << 8);
614		video->palette[32] = data[1] | (data[2] << 8);
615
616		video->palette[48] = data[1] | (data[2] << 8);
617		video->palette[49] = data[9] | (data[10] << 8);
618		video->palette[50] = data[11] | (data[12] << 8);
619		video->palette[51] = data[13] | (data[14] << 8);
620		video->renderer->writePalette(video->renderer, 0, video->palette[0]);
621		video->renderer->writePalette(video->renderer, 1, video->palette[1]);
622		video->renderer->writePalette(video->renderer, 2, video->palette[2]);
623		video->renderer->writePalette(video->renderer, 3, video->palette[3]);
624		video->renderer->writePalette(video->renderer, 16, video->palette[0]);
625		video->renderer->writePalette(video->renderer, 32, video->palette[0]);
626		video->renderer->writePalette(video->renderer, 48, video->palette[0]);
627		video->renderer->writePalette(video->renderer, 49, video->palette[49]);
628		video->renderer->writePalette(video->renderer, 50, video->palette[50]);
629		video->renderer->writePalette(video->renderer, 51, video->palette[51]);
630		break;
631	case SGB_PAL12:
632		video->palette[17] = data[3] | (data[4] << 8);
633		video->palette[18] = data[5] | (data[6] << 8);
634		video->palette[19] = data[7] | (data[8] << 8);
635
636		video->palette[33] = data[9] | (data[10] << 8);
637		video->palette[34] = data[11] | (data[12] << 8);
638		video->palette[35] = data[13] | (data[14] << 8);
639		video->renderer->writePalette(video->renderer, 17, video->palette[17]);
640		video->renderer->writePalette(video->renderer, 18, video->palette[18]);
641		video->renderer->writePalette(video->renderer, 19, video->palette[19]);
642		video->renderer->writePalette(video->renderer, 33, video->palette[33]);
643		video->renderer->writePalette(video->renderer, 34, video->palette[34]);
644		video->renderer->writePalette(video->renderer, 35, video->palette[35]);
645		break;
646	case SGB_PAL_SET:
647		for (i = 0; i < 4; ++i) {
648			uint16_t entry = (data[2 + (i * 2)] << 8) | data[1 + (i * 2)];
649			if (entry >= 0x200) {
650				mLOG(GB, STUB, "Unimplemented SGB palette overflow: %03X", entry);
651				continue;
652			}
653			LOAD_16LE(video->palette[i * 4 + 0], entry * 8 + 0, video->renderer->sgbPalRam);
654			video->renderer->writePalette(video->renderer, i * 4 + 0, video->palette[0]);
655			LOAD_16LE(video->palette[i * 4 + 1], entry * 8 + 2, video->renderer->sgbPalRam);
656			video->renderer->writePalette(video->renderer, i * 4 + 1, video->palette[i * 4 + 1]);
657			LOAD_16LE(video->palette[i * 4 + 2], entry * 8 + 4, video->renderer->sgbPalRam);
658			video->renderer->writePalette(video->renderer, i * 4 + 2, video->palette[i * 4 + 2]);
659			LOAD_16LE(video->palette[i * 4 + 3], entry * 8 + 6, video->renderer->sgbPalRam);
660			video->renderer->writePalette(video->renderer, i * 4 + 3, video->palette[i * 4 + 3]);
661		}
662		break;
663	case SGB_ATTR_BLK:
664	case SGB_PAL_TRN:
665	case SGB_ATRC_EN:
666	case SGB_CHR_TRN:
667	case SGB_PCT_TRN:
668	case SGB_ATTR_TRN:
669	case SGB_ATTR_SET:
670		break;
671	case SGB_MLT_REG:
672		return;
673	case SGB_MASK_EN:
674		video->renderer->sgbRenderMode = data[1] & 0x3;
675		break;
676	default:
677		mLOG(GB, STUB, "Unimplemented SGB command: %02X", data[0] >> 3);
678		return;
679	}
680	video->renderer->writeSGBPacket(video->renderer, data);
681}
682
683static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool borders) {
684	UNUSED(renderer);
685	UNUSED(model);
686	UNUSED(borders);
687	// Nothing to do
688}
689
690static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
691	UNUSED(renderer);
692	// Nothing to do
693}
694
695static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
696	if (renderer->cache) {
697		GBVideoCacheWriteVideoRegister(renderer->cache, address, value);
698	}
699	return value;
700}
701
702static void GBVideoDummyRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
703	UNUSED(renderer);
704	UNUSED(data);
705}
706
707static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
708	if (renderer->cache) {
709		mCacheSetWriteVRAM(renderer->cache, address);
710	}
711}
712
713static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
714	UNUSED(renderer);
715	UNUSED(oam);
716	// Nothing to do
717}
718
719static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
720	if (renderer->cache) {
721		mCacheSetWritePalette(renderer->cache, index, mColorFrom555(value));
722	}
723}
724
725static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
726	UNUSED(renderer);
727	UNUSED(endX);
728	UNUSED(startX);
729	UNUSED(y);
730	UNUSED(obj);
731	UNUSED(oamMax);
732	// Nothing to do
733}
734
735static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
736	UNUSED(renderer);
737	UNUSED(y);
738	// Nothing to do
739}
740
741static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
742	UNUSED(renderer);
743	// Nothing to do
744}
745
746static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
747	UNUSED(renderer);
748	UNUSED(stride);
749	UNUSED(pixels);
750	// Nothing to do
751}
752
753static void GBVideoDummyRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
754	UNUSED(renderer);
755	UNUSED(stride);
756	UNUSED(pixels);
757	// Nothing to do
758}
759
760void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
761	STORE_16LE(video->x, 0, &state->video.x);
762	STORE_16LE(video->ly, 0, &state->video.ly);
763	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
764	state->video.vramCurrentBank = video->vramCurrentBank;
765
766	GBSerializedVideoFlags flags = 0;
767	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
768	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
769	flags = GBSerializedVideoFlagsSetMode(flags, video->mode);
770	flags = GBSerializedVideoFlagsSetNotModeEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->modeEvent));
771	flags = GBSerializedVideoFlagsSetNotFrameEventScheduled(flags, !mTimingIsScheduled(&video->p->timing, &video->frameEvent));
772	state->video.flags = flags;
773	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
774	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
775
776	size_t i;
777	for (i = 0; i < 64; ++i) {
778		STORE_16LE(video->palette[i], i * 2, state->video.palette);
779	}
780
781	STORE_32LE(video->modeEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextMode);
782	STORE_32LE(video->frameEvent.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextFrame);
783
784	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
785	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
786}
787
788void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
789	LOAD_16LE(video->x, 0, &state->video.x);
790	LOAD_16LE(video->ly, 0, &state->video.ly);
791	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
792	video->vramCurrentBank = state->video.vramCurrentBank;
793
794	GBSerializedVideoFlags flags = state->video.flags;
795	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
796	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
797	video->mode = GBSerializedVideoFlagsGetMode(flags);
798	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
799	video->bcpIndex &= 0x3F;
800	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
801	video->ocpIndex &= 0x3F;
802
803	switch (video->mode) {
804	case 0:
805		video->modeEvent.callback = _endMode0;
806		break;
807	case 1:
808		video->modeEvent.callback = _endMode1;
809		break;
810	case 2:
811		video->modeEvent.callback = _endMode2;
812		break;
813	case 3:
814		video->modeEvent.callback = _endMode3;
815		break;
816	}
817
818	uint32_t when;
819	if (!GBSerializedVideoFlagsIsNotModeEventScheduled(flags)) {
820		LOAD_32LE(when, 0, &state->video.nextMode);
821		mTimingSchedule(&video->p->timing, &video->modeEvent, when);
822	}
823	if (!GBSerializedVideoFlagsIsNotFrameEventScheduled(flags)) {
824		LOAD_32LE(when, 0, &state->video.nextFrame);
825		mTimingSchedule(&video->p->timing, &video->frameEvent, when);
826	}
827
828	size_t i;
829	for (i = 0; i < 64; ++i) {
830		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
831		video->renderer->writePalette(video->renderer, i, video->palette[i]);
832	}
833
834	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
835	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
836
837	_cleanOAM(video, video->ly);
838	GBVideoSwitchBank(video, video->vramCurrentBank);
839
840	video->renderer->deinit(video->renderer);
841	video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
842}