all repos — mgba @ 47364e47691f6aa04ad51a11652518e4685391c1

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 GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
 18static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint8_t oam);
 19static void GBVideoDummyRendererDrawScanline(struct GBVideoRenderer* renderer, int y);
 20static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
 21static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
 22
 23static struct GBVideoRenderer dummyRenderer = {
 24	.init = GBVideoDummyRendererInit,
 25	.reset = GBVideoDummyRendererReset,
 26	.deinit = GBVideoDummyRendererDeinit,
 27	.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
 28	.writeVRAM = GBVideoDummyRendererWriteVRAM,
 29	.writeOAM = GBVideoDummyRendererWriteOAM,
 30	.drawScanline = GBVideoDummyRendererDrawScanline,
 31	.finishFrame = GBVideoDummyRendererFinishFrame,
 32	.getPixels = GBVideoDummyRendererGetPixels
 33};
 34
 35void GBVideoInit(struct GBVideo* video) {
 36	video->renderer = &dummyRenderer;
 37	video->vram = 0;
 38	video->frameskip = 0;
 39}
 40
 41void GBVideoReset(struct GBVideo* video) {
 42	video->ly = 0;
 43	video->mode = 1;
 44
 45	video->nextEvent = INT_MAX;
 46	video->eventDiff = 0;
 47
 48	video->nextMode = 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		}
 86		if (video->nextMode <= 0) {
 87			switch (video->mode) {
 88			case 0:
 89				++video->ly;
 90				video->p->memory.io[REG_LY] = video->ly;
 91				int lyc = video->p->memory.io[REG_LYC];
 92				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
 93				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
 94					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
 95				}
 96				if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
 97					video->renderer->drawScanline(video->renderer, video->ly);
 98					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
 99					video->mode = 2;
100					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
101						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
102					}
103				} else {
104					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
105					video->mode = 1;
106					++video->frameCounter;
107					video->renderer->finishFrame(video->renderer);
108					if (GBRegisterSTATIsVblankIRQ(video->stat)) {
109						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
110					}
111					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
112				}
113				GBUpdateIRQs(video->p);
114				break;
115			case 1:
116				++video->ly;
117				if (video->ly >= GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
118					video->ly = 0;
119					video->renderer->drawScanline(video->renderer, video->ly);
120					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
121					video->mode = 2;
122					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
123						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
124						GBUpdateIRQs(video->p);
125					}
126				} else {
127					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
128				}
129				video->p->memory.io[REG_LY] = video->ly;
130				break;
131			case 2:
132				video->nextMode = GB_VIDEO_MODE_3_LENGTH;
133				video->mode = 3;
134				break;
135			case 3:
136				video->nextMode = GB_VIDEO_MODE_0_LENGTH;
137				video->mode = 0;
138				if (GBRegisterSTATIsHblankIRQ(video->stat)) {
139					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
140					GBUpdateIRQs(video->p);
141				}
142				break;
143			}
144			video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
145			video->p->memory.io[REG_STAT] = video->stat;
146		}
147
148		video->nextEvent = video->nextMode;
149		video->eventDiff = 0;
150	}
151	return video->nextEvent;
152}
153
154void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
155	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
156		// TODO: Does enabling the LCD start in vblank?
157		video->mode = 2;
158		video->nextMode = GB_VIDEO_MODE_2_LENGTH;
159		video->nextEvent = video->nextMode;
160		video->eventDiff = 0;
161		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
162		video->p->memory.io[REG_STAT] = video->stat;
163		video->eventDiff = 0;
164		if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) {
165			video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent;
166		}
167		return;
168	}
169	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
170		video->mode = 0;
171		video->nextMode = INT_MAX;
172		video->nextEvent = INT_MAX;
173		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
174		video->p->memory.io[REG_STAT] = video->stat;
175		video->ly = 0;
176		video->p->memory.io[REG_LY] = 0;
177	}
178}
179
180void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
181	video->stat = (video->stat & 0x7) | (value & 0x78);
182}
183
184static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer) {
185	UNUSED(renderer);
186	// Nothing to do
187}
188
189static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer) {
190	UNUSED(renderer);
191	// Nothing to do
192}
193
194static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
195	UNUSED(renderer);
196	// Nothing to do
197}
198
199static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
200	UNUSED(renderer);
201	UNUSED(address);
202	return value;
203}
204
205static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
206	UNUSED(renderer);
207	UNUSED(address);
208	// Nothing to do
209}
210
211static void GBVideoDummyRendererWriteOAM(struct GBVideoRenderer* renderer, uint8_t oam) {
212	UNUSED(renderer);
213	UNUSED(oam);
214	// Nothing to do
215}
216
217static void GBVideoDummyRendererDrawScanline(struct GBVideoRenderer* renderer, int y) {
218	UNUSED(renderer);
219	UNUSED(y);
220	// Nothing to do
221}
222
223static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
224	UNUSED(renderer);
225	// Nothing to do
226}
227
228static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
229	UNUSED(renderer);
230	UNUSED(stride);
231	UNUSED(pixels);
232	// Nothing to do
233}