all repos — mgba @ 0083fad9663157e2745e363896644267c5155105

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 "video.h"
  7
  8#include "core/sync.h"
  9#include "core/thread.h"
 10#include "gb/gb.h"
 11#include "gb/io.h"
 12#include "gb/serialize.h"
 13
 14#include "util/memory.h"
 15
 16static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
 17static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
 18static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 19static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
 20static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
 21static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
 22static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
 23static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
 24
 25static void _cleanOAM(struct GBVideo* video, int y);
 26
 27static struct GBVideoRenderer dummyRenderer = {
 28	.init = GBVideoDummyRendererInit,
 29	.deinit = GBVideoDummyRendererDeinit,
 30	.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
 31	.writePalette = GBVideoDummyRendererWritePalette,
 32	.drawRange = GBVideoDummyRendererDrawRange,
 33	.finishScanline = GBVideoDummyRendererFinishScanline,
 34	.finishFrame = GBVideoDummyRendererFinishFrame,
 35	.getPixels = GBVideoDummyRendererGetPixels
 36};
 37
 38void GBVideoInit(struct GBVideo* video) {
 39	video->renderer = &dummyRenderer;
 40	video->vram = 0;
 41	video->frameskip = 0;
 42}
 43
 44void GBVideoReset(struct GBVideo* video) {
 45	video->ly = 0;
 46	video->x = 0;
 47	video->mode = 1;
 48	video->stat = 1;
 49
 50	video->nextEvent = INT_MAX;
 51	video->eventDiff = 0;
 52
 53	video->nextMode = INT_MAX;
 54	video->dotCounter = INT_MIN;
 55	video->nextFrame = INT_MAX;
 56
 57	video->frameCounter = 0;
 58	video->frameskipCounter = 0;
 59
 60	if (video->vram) {
 61		mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 62	}
 63	video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
 64	GBVideoSwitchBank(video, 0);
 65	video->renderer->vram = video->vram;
 66	memset(&video->oam, 0, sizeof(video->oam));
 67	video->renderer->oam = &video->oam;
 68	memset(&video->palette, 0, sizeof(video->palette));
 69
 70	video->renderer->deinit(video->renderer);
 71	video->renderer->init(video->renderer, video->p->model);
 72}
 73
 74void GBVideoDeinit(struct GBVideo* video) {
 75	GBVideoAssociateRenderer(video, &dummyRenderer);
 76	mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 77}
 78
 79void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
 80	video->renderer->deinit(video->renderer);
 81	video->renderer = renderer;
 82	renderer->vram = video->vram;
 83	video->renderer->init(video->renderer, video->p->model);
 84}
 85
 86int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
 87	video->eventDiff += cycles;
 88	if (video->nextEvent != INT_MAX) {
 89		video->nextEvent -= cycles;
 90	}
 91	if (video->nextEvent <= 0) {
 92		if (video->nextEvent != INT_MAX) {
 93			video->nextMode -= video->eventDiff;
 94			video->nextFrame -= video->eventDiff;
 95		}
 96		video->nextEvent = INT_MAX;
 97		GBVideoProcessDots(video);
 98		if (video->nextMode <= 0) {
 99			int lyc = video->p->memory.io[REG_LYC];
100			switch (video->mode) {
101			case 0:
102				if (video->frameskipCounter <= 0) {
103					video->renderer->finishScanline(video->renderer, video->ly);
104				}
105				++video->ly;
106				video->p->memory.io[REG_LY] = video->ly;
107				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
108				if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
109					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
110					video->mode = 2;
111					if (!GBRegisterSTATIsHblankIRQ(video->stat) && GBRegisterSTATIsOAMIRQ(video->stat)) {
112						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
113					}
114				} else {
115					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
116					video->mode = 1;
117					--video->frameskipCounter;
118					if (video->frameskipCounter < 0) {
119						mCoreSyncPostFrame(video->p->sync);
120						video->frameskipCounter = video->frameskip;
121					}
122					++video->frameCounter;
123
124					if (video->nextFrame != 0) {
125						video->nextFrame = 0;
126					}
127
128					if (video->p->stream && video->p->stream->postVideoFrame) {
129						const color_t* pixels;
130						unsigned stride;
131						video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);
132						video->p->stream->postVideoFrame(video->p->stream, pixels, stride);
133					}
134
135					if (GBRegisterSTATIsVblankIRQ(video->stat) || GBRegisterSTATIsOAMIRQ(video->stat)) {
136						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
137					}
138					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
139				}
140				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
141					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
142				}
143				GBUpdateIRQs(video->p);
144				break;
145			case 1:
146				// TODO: One M-cycle delay
147				++video->ly;
148				if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
149					video->ly = 0;
150					video->p->memory.io[REG_LY] = video->ly;
151					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
152					video->mode = 2;
153					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
154						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
155						GBUpdateIRQs(video->p);
156					}
157					video->renderer->finishFrame(video->renderer);
158					break;
159				} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
160					video->p->memory.io[REG_LY] = 0;
161					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH - 8;
162				} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
163					video->p->memory.io[REG_LY] = video->ly;
164					video->nextMode = 8;
165				} else {
166					video->p->memory.io[REG_LY] = video->ly;
167					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
168				}
169
170				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
171				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->p->memory.io[REG_LY]) {
172					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
173					GBUpdateIRQs(video->p);
174				}
175				if (video->p->memory.mbcType == GB_MBC7 && video->p->memory.rotation && video->p->memory.rotation->sample) {
176					video->p->memory.rotation->sample(video->p->memory.rotation);
177				}
178				break;
179			case 2:
180				_cleanOAM(video, video->ly);
181				video->dotCounter = 0;
182				video->nextEvent = GB_VIDEO_HORIZONTAL_LENGTH;
183				video->x = 0;
184				video->nextMode = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 12;
185				video->mode = 3;
186				break;
187			case 3:
188				video->nextMode = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 12;
189				video->mode = 0;
190				if (GBRegisterSTATIsHblankIRQ(video->stat)) {
191					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
192					GBUpdateIRQs(video->p);
193				}
194				if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
195					video->p->memory.hdmaRemaining = 0x10;
196					video->p->memory.hdmaNext = video->p->cpu->cycles;
197				}
198				break;
199			}
200			video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
201			video->p->memory.io[REG_STAT] = video->stat;
202		}
203		if (video->nextFrame <= 0) {
204			if (video->p->cpu->executionState == LR35902_CORE_FETCH) {
205				GBFrameEnded(video->p);
206				struct mCoreThread* thread = mCoreThreadGet();
207				if (thread && thread->frameCallback) {
208					thread->frameCallback(thread);
209				}
210				video->nextFrame = GB_VIDEO_TOTAL_LENGTH;
211			} else {
212				video->nextFrame = 4 - ((video->p->cpu->executionState + 1) & 3);
213				if (video->nextFrame < video->nextEvent) {
214					video->nextEvent = video->nextFrame;
215				}
216			}
217		}
218		if (video->nextMode < video->nextEvent) {
219			video->nextEvent = video->nextMode;
220		}
221		video->eventDiff = 0;
222	}
223	return video->nextEvent;
224}
225
226static void _cleanOAM(struct GBVideo* video, int y) {
227	// TODO: GBC differences
228	// TODO: Optimize
229	video->objMax = 0;
230	int spriteHeight = 8;
231	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
232		spriteHeight = 16;
233	}
234	int o = 0;
235	int i;
236	for (i = 0; i < 40; ++i) {
237		uint8_t oy = video->oam.obj[i].y;
238		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
239			continue;
240		}
241		// TODO: Sort
242		video->objThisLine[o] = video->oam.obj[i];
243		++o;
244		if (o == 10) {
245			break;
246		}
247	}
248	video->objMax = o;
249}
250
251void GBVideoProcessDots(struct GBVideo* video) {
252	if (video->mode != 3 || video->dotCounter < 0) {
253		return;
254	}
255	int oldX = video->x;
256	video->x = video->dotCounter + video->eventDiff + (video->p->cpu->cycles >> video->p->doubleSpeed);
257	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
258		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
259	}
260	if (video->x == GB_VIDEO_HORIZONTAL_PIXELS) {
261		video->dotCounter = INT_MIN;
262	}
263	if (video->frameskipCounter <= 0) {
264		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
265	}
266}
267
268void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
269	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
270		video->mode = 2;
271		video->nextMode = GB_VIDEO_MODE_2_LENGTH - 5; // TODO: Why is this fudge factor needed? Might be related to T-cycles for load/store differing
272		video->nextEvent = video->nextMode;
273		video->eventDiff = -video->p->cpu->cycles >> video->p->doubleSpeed;
274		video->ly = 0;
275		video->p->memory.io[REG_LY] = 0;
276		// TODO: Does this read as 0 for 4 T-cycles?
277		video->stat = GBRegisterSTATSetMode(video->stat, 2);
278		video->stat = GBRegisterSTATSetLYC(video->stat, video->ly == video->p->memory.io[REG_LYC]);
279		if (GBRegisterSTATIsLYCIRQ(video->stat) && video->ly == video->p->memory.io[REG_LYC]) {
280			video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
281			GBUpdateIRQs(video->p);
282		}
283		video->p->memory.io[REG_STAT] = video->stat;
284
285		if (video->p->cpu->cycles + (video->nextEvent << video->p->doubleSpeed) < video->p->cpu->nextEvent) {
286			video->p->cpu->nextEvent = video->p->cpu->cycles + (video->nextEvent << video->p->doubleSpeed);
287		}
288		return;
289	}
290	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
291		video->mode = 0;
292		video->nextMode = INT_MAX;
293		video->nextEvent = INT_MAX;
294		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
295		video->p->memory.io[REG_STAT] = video->stat;
296		video->ly = 0;
297		video->p->memory.io[REG_LY] = 0;
298	}
299}
300
301void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
302	video->stat = (video->stat & 0x7) | (value & 0x78);
303	if (video->p->model == GB_MODEL_DMG && video->mode == 1) {
304		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
305		GBUpdateIRQs(video->p);
306	}
307}
308
309void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
310	static const uint16_t dmgPalette[4] = { 0x7FFF, 0x56B5, 0x294A, 0x0000};
311	if (video->p->model < GB_MODEL_CGB) {
312		switch (address) {
313		case REG_BGP:
314			video->palette[0] = dmgPalette[value & 3];
315			video->palette[1] = dmgPalette[(value >> 2) & 3];
316			video->palette[2] = dmgPalette[(value >> 4) & 3];
317			video->palette[3] = dmgPalette[(value >> 6) & 3];
318			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
319			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
320			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
321			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
322			break;
323		case REG_OBP0:
324			video->palette[8 * 4 + 0] = dmgPalette[value & 3];
325			video->palette[8 * 4 + 1] = dmgPalette[(value >> 2) & 3];
326			video->palette[8 * 4 + 2] = dmgPalette[(value >> 4) & 3];
327			video->palette[8 * 4 + 3] = dmgPalette[(value >> 6) & 3];
328			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
329			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
330			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
331			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
332			break;
333		case REG_OBP1:
334			video->palette[9 * 4 + 0] = dmgPalette[value & 3];
335			video->palette[9 * 4 + 1] = dmgPalette[(value >> 2) & 3];
336			video->palette[9 * 4 + 2] = dmgPalette[(value >> 4) & 3];
337			video->palette[9 * 4 + 3] = dmgPalette[(value >> 6) & 3];
338			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
339			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
340			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
341			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
342			break;
343		}
344	} else {
345		switch (address) {
346		case REG_BCPD:
347			if (video->bcpIndex & 1) {
348				video->palette[video->bcpIndex >> 1] &= 0x00FF;
349				video->palette[video->bcpIndex >> 1] |= value << 8;
350			} else {
351				video->palette[video->bcpIndex >> 1] &= 0xFF00;
352				video->palette[video->bcpIndex >> 1] |= value;
353			}
354			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
355			if (video->bcpIncrement) {
356				++video->bcpIndex;
357				video->bcpIndex &= 0x3F;
358				video->p->memory.io[REG_BCPS] &= 0x80;
359				video->p->memory.io[REG_BCPS] |= video->bcpIndex;
360			}
361			video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1] >> (8 * (video->bcpIndex & 1));
362			break;
363		case REG_OCPD:
364			if (video->ocpIndex & 1) {
365				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
366				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
367			} else {
368				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
369				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
370			}
371			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
372			if (video->ocpIncrement) {
373				++video->ocpIndex;
374				video->ocpIndex &= 0x3F;
375				video->p->memory.io[REG_OCPS] &= 0x80;
376				video->p->memory.io[REG_OCPS] |= video->ocpIndex;
377			}
378			video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)] >> (8 * (video->ocpIndex & 1));
379			break;
380		}
381	}
382}
383
384void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
385	value &= 1;
386	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
387	video->vramCurrentBank = value;
388}
389
390static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
391	UNUSED(renderer);
392	UNUSED(model);
393	// Nothing to do
394}
395
396static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
397	UNUSED(renderer);
398	// Nothing to do
399}
400
401static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
402	UNUSED(renderer);
403	UNUSED(address);
404	return value;
405}
406
407static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
408	UNUSED(renderer);
409	UNUSED(index);
410	UNUSED(value);
411}
412
413static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
414	UNUSED(renderer);
415	UNUSED(endX);
416	UNUSED(startX);
417	UNUSED(y);
418	UNUSED(obj);
419	UNUSED(oamMax);
420	// Nothing to do
421}
422
423static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
424	UNUSED(renderer);
425	UNUSED(y);
426	// Nothing to do
427}
428
429static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
430	UNUSED(renderer);
431	// Nothing to do
432}
433
434static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
435	UNUSED(renderer);
436	UNUSED(stride);
437	UNUSED(pixels);
438	// Nothing to do
439}
440
441void GBVideoSerialize(const struct GBVideo* video, struct GBSerializedState* state) {
442	STORE_16LE(video->x, 0, &state->video.x);
443	STORE_16LE(video->ly, 0, &state->video.ly);
444	STORE_32LE(video->nextEvent, 0, &state->video.nextEvent);
445	STORE_32LE(video->eventDiff, 0, &state->video.eventDiff);
446	STORE_32LE(video->nextMode, 0, &state->video.nextMode);
447	STORE_32LE(video->dotCounter, 0, &state->video.dotCounter);
448	STORE_32LE(video->frameCounter, 0, &state->video.frameCounter);
449	state->video.vramCurrentBank = video->vramCurrentBank;
450
451	GBSerializedVideoFlags flags = 0;
452	flags = GBSerializedVideoFlagsSetBcpIncrement(flags, video->bcpIncrement);
453	flags = GBSerializedVideoFlagsSetOcpIncrement(flags, video->ocpIncrement);
454	state->video.flags = flags;
455	STORE_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
456	STORE_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
457
458	size_t i;
459	for (i = 0; i < 64; ++i) {
460		STORE_16LE(video->palette[i], i * 2, state->video.palette);
461	}
462
463	memcpy(state->vram, video->vram, GB_SIZE_VRAM);
464	memcpy(state->oam, &video->oam.raw, GB_SIZE_OAM);
465}
466
467void GBVideoDeserialize(struct GBVideo* video, const struct GBSerializedState* state) {
468	LOAD_16LE(video->x, 0, &state->video.x);
469	LOAD_16LE(video->ly, 0, &state->video.ly);
470	LOAD_32LE(video->nextEvent, 0, &state->video.nextEvent);
471	LOAD_32LE(video->eventDiff, 0, &state->video.eventDiff);
472	LOAD_32LE(video->nextMode, 0, &state->video.nextMode);
473	LOAD_32LE(video->dotCounter, 0, &state->video.dotCounter);
474	LOAD_32LE(video->frameCounter, 0, &state->video.frameCounter);
475	video->vramCurrentBank = state->video.vramCurrentBank;
476
477	GBSerializedVideoFlags flags = state->video.flags;
478	video->bcpIncrement = GBSerializedVideoFlagsGetBcpIncrement(flags);
479	video->ocpIncrement = GBSerializedVideoFlagsGetOcpIncrement(flags);
480	LOAD_16LE(video->bcpIndex, 0, &state->video.bcpIndex);
481	LOAD_16LE(video->ocpIndex, 0, &state->video.ocpIndex);
482
483	size_t i;
484	for (i = 0; i < 64; ++i) {
485		LOAD_16LE(video->palette[i], i * 2, state->video.palette);
486		video->renderer->writePalette(video->renderer, i, video->palette[i]);
487	}
488
489	memcpy(video->vram, state->vram, GB_SIZE_VRAM);
490	memcpy(&video->oam.raw, state->oam, GB_SIZE_OAM);
491
492	_cleanOAM(video, video->ly);
493	GBVideoSwitchBank(video, video->vramCurrentBank);
494}