all repos — mgba @ 2290dd67810f7ce7a88fadd179a84f987a10384d

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 "gb/gb.h"
  9#include "gb/io.h"
 10
 11#include "util/memory.h"
 12
 13static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer);
 14static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer);
 15static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
 16static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 17static void GBVideoDummyRendererDrawDot(struct GBVideoRenderer* renderer, int x, int y, struct GBObj** obj, size_t oamMax);
 18static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
 19static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
 20
 21static void _cleanOAM(struct GBVideo* video, int y);
 22
 23static struct GBVideoRenderer dummyRenderer = {
 24	.init = GBVideoDummyRendererInit,
 25	.reset = GBVideoDummyRendererReset,
 26	.deinit = GBVideoDummyRendererDeinit,
 27	.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
 28	.drawDot = GBVideoDummyRendererDrawDot,
 29	.finishFrame = GBVideoDummyRendererFinishFrame,
 30	.getPixels = GBVideoDummyRendererGetPixels
 31};
 32
 33void GBVideoInit(struct GBVideo* video) {
 34	video->renderer = &dummyRenderer;
 35	video->vram = 0;
 36	video->frameskip = 0;
 37}
 38
 39void GBVideoReset(struct GBVideo* video) {
 40	video->ly = 0;
 41	video->mode = 1;
 42	video->stat = 1;
 43
 44	video->nextEvent = INT_MAX;
 45	video->eventDiff = 0;
 46
 47	video->nextMode = INT_MAX;
 48	video->nextDot = INT_MAX;
 49
 50	video->frameCounter = 0;
 51	video->frameskipCounter = 0;
 52
 53	if (video->vram) {
 54		mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 55	}
 56	video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
 57	video->renderer->vram = video->vram;
 58	memset(&video->oam, 0, sizeof(video->oam));
 59	video->renderer->oam = &video->oam;
 60
 61	video->renderer->deinit(video->renderer);
 62	video->renderer->init(video->renderer);
 63}
 64
 65void GBVideoDeinit(struct GBVideo* video) {
 66	GBVideoAssociateRenderer(video, &dummyRenderer);
 67	mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 68}
 69
 70void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
 71	video->renderer->deinit(video->renderer);
 72	video->renderer = renderer;
 73	renderer->vram = video->vram;
 74	video->renderer->init(video->renderer);
 75}
 76
 77int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
 78	video->eventDiff += cycles;
 79	if (video->nextEvent != INT_MAX) {
 80		video->nextEvent -= cycles;
 81	}
 82	if (video->nextEvent <= 0) {
 83		if (video->nextEvent != INT_MAX) {
 84			video->nextMode -= video->eventDiff;
 85			video->nextDot -= video->eventDiff;
 86		}
 87		video->nextEvent = INT_MAX;
 88		if (video->nextDot <= 0) {
 89			video->renderer->drawDot(video->renderer, video->x, video->ly, video->objThisLine, video->objMax);
 90			++video->x;
 91			if (video->x < GB_VIDEO_HORIZONTAL_PIXELS) {
 92				video->nextDot = 1;
 93			} else {
 94				video->nextDot = INT_MAX;
 95			}
 96		}
 97		if (video->nextMode <= 0) {
 98			int lyc = video->p->memory.io[REG_LYC];
 99			switch (video->mode) {
100			case 0:
101				++video->ly;
102				video->p->memory.io[REG_LY] = video->ly;
103				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
104				if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
105					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
106					video->mode = 2;
107					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
108						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
109					}
110				} else {
111					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
112					video->mode = 1;
113					++video->frameCounter;
114					video->renderer->finishFrame(video->renderer);
115					if (GBRegisterSTATIsVblankIRQ(video->stat) || GBRegisterSTATIsOAMIRQ(video->stat)) {
116						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
117					}
118					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
119				}
120				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
121					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
122				}
123				GBUpdateIRQs(video->p);
124				break;
125			case 1:
126				++video->ly;
127				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
128				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
129					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
130				}
131				if (video->ly >= GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
132					video->ly = 0;
133					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
134					video->mode = 2;
135					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
136						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
137						GBUpdateIRQs(video->p);
138					}
139				} else {
140					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
141				}
142				video->p->memory.io[REG_LY] = video->ly;
143				break;
144			case 2:
145				_cleanOAM(video, video->ly);
146				video->nextDot = 1;
147				video->nextEvent = video->nextDot;
148				video->x = 0;
149				video->nextMode = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 8;
150				video->mode = 3;
151				break;
152			case 3:
153				video->nextMode = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 8;
154				video->mode = 0;
155				if (GBRegisterSTATIsHblankIRQ(video->stat)) {
156					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
157					GBUpdateIRQs(video->p);
158				}
159				break;
160			}
161			video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
162			video->p->memory.io[REG_STAT] = video->stat;
163		}
164		if (video->nextDot < video->nextEvent) {
165			video->nextEvent = video->nextDot;
166		}
167		if (video->nextMode < video->nextEvent) {
168			video->nextEvent = video->nextMode;
169		}
170		video->eventDiff = 0;
171	}
172	return video->nextEvent;
173}
174
175static void _cleanOAM(struct GBVideo* video, int y) {
176	// TODO: GBC differences
177	// TODO: Optimize
178	video->objMax = 0;
179	int spriteHeight = 8;
180	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
181		spriteHeight = 16;
182	}
183	int o = 0;
184	int i;
185	for (i = 0; i < 40; ++i) {
186		uint8_t oy = video->oam.obj[i].y;
187		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
188			continue;
189		}
190		// TODO: Sort
191		video->objThisLine[o] = &video->oam.obj[i];
192		++o;
193		if (o == 10) {
194			break;
195		}
196	}
197	video->objMax = o;
198}
199
200void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
201	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
202		// TODO: Does enabling the LCD start in vblank?
203		video->mode = 2;
204		video->nextMode = GB_VIDEO_MODE_2_LENGTH;
205		video->nextEvent = video->nextMode;
206		video->eventDiff = 0;
207		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
208		video->p->memory.io[REG_STAT] = video->stat;
209		video->eventDiff = 0;
210		if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) {
211			video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent;
212		}
213		return;
214	}
215	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
216		video->mode = 0;
217		video->nextMode = INT_MAX;
218		video->nextEvent = INT_MAX;
219		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
220		video->p->memory.io[REG_STAT] = video->stat;
221		video->ly = 0;
222		video->p->memory.io[REG_LY] = 0;
223	}
224}
225
226void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
227	video->stat = (video->stat & 0x7) | (value & 0x78);
228}
229
230static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer) {
231	UNUSED(renderer);
232	// Nothing to do
233}
234
235static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer) {
236	UNUSED(renderer);
237	// Nothing to do
238}
239
240static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
241	UNUSED(renderer);
242	// Nothing to do
243}
244
245static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
246	UNUSED(renderer);
247	UNUSED(address);
248	return value;
249}
250
251static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
252	UNUSED(renderer);
253	UNUSED(address);
254	// Nothing to do
255}
256
257static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint8_t oam) {
258	UNUSED(renderer);
259	UNUSED(oam);
260	// Nothing to do
261}
262
263static void GBVideoDummyRendererDrawDot(struct GBVideoRenderer* renderer, int x, int y, struct GBObj** obj, size_t oamMax) {
264	UNUSED(renderer);
265	UNUSED(x);
266	UNUSED(y);
267	UNUSED(obj);
268	UNUSED(oamMax);
269	// Nothing to do
270}
271
272static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
273	UNUSED(renderer);
274	// Nothing to do
275}
276
277static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
278	UNUSED(renderer);
279	UNUSED(stride);
280	UNUSED(pixels);
281	// Nothing to do
282}