all repos — mgba @ e4aed77f1e9405c64afefaaff4f929388c7c7e54

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