all repos — mgba @ c0b965a41ed22373b76cc6bd8d23e0ff3bdf9c99

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 GBVideoDummyRendererDrawScanline(struct GBVideoRenderer* renderer, int y);
 19static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
 20static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
 21
 22static struct GBVideoRenderer dummyRenderer = {
 23	.init = GBVideoDummyRendererInit,
 24	.reset = GBVideoDummyRendererReset,
 25	.deinit = GBVideoDummyRendererDeinit,
 26	.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
 27	.writeVRAM = GBVideoDummyRendererWriteVRAM,
 28	.drawScanline = GBVideoDummyRendererDrawScanline,
 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
 43	video->nextEvent = INT_MAX;
 44	video->eventDiff = 0;
 45
 46	video->nextMode = INT_MAX;
 47
 48	video->frameCounter = 0;
 49	video->frameskipCounter = 0;
 50
 51	if (video->vram) {
 52		mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 53	}
 54	video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
 55	video->renderer->vram = video->vram;
 56
 57	video->renderer->deinit(video->renderer);
 58	video->renderer->init(video->renderer);
 59}
 60
 61void GBVideoDeinit(struct GBVideo* video) {
 62	GBVideoAssociateRenderer(video, &dummyRenderer);
 63	mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 64}
 65
 66void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
 67	video->renderer->deinit(video->renderer);
 68	video->renderer = renderer;
 69	renderer->vram = video->vram;
 70	video->renderer->init(video->renderer);
 71}
 72
 73int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
 74	video->eventDiff += cycles;
 75	if (video->nextEvent != INT_MAX) {
 76		video->nextEvent -= cycles;
 77	}
 78	if (video->nextEvent <= 0) {
 79		if (video->nextEvent != INT_MAX) {
 80			video->nextMode -= video->eventDiff;
 81		}
 82		if (video->nextMode <= 0) {
 83			switch (video->mode) {
 84			case 0:
 85				++video->ly;
 86				video->p->memory.io[REG_LY] = video->ly;
 87				if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
 88					video->renderer->drawScanline(video->renderer, video->ly);
 89					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
 90					video->mode = 2;
 91					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
 92						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
 93						GBUpdateIRQs(video->p);
 94					}
 95				} else {
 96					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
 97					video->mode = 1;
 98					++video->frameCounter;
 99					video->renderer->finishFrame(video->renderer);
100					if (GBRegisterSTATIsVblankIRQ(video->stat)) {
101						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
102					}
103					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
104					GBUpdateIRQs(video->p);
105				}
106				break;
107			case 1:
108				++video->ly;
109				if (video->ly >= GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
110					video->ly = 0;
111					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
112					video->mode = 2;
113					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
114						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
115						GBUpdateIRQs(video->p);
116					}
117				} else {
118					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
119				}
120				video->p->memory.io[REG_LY] = video->ly;
121				break;
122			case 2:
123				video->nextMode = GB_VIDEO_MODE_3_LENGTH;
124				video->mode = 3;
125				break;
126			case 3:
127				video->nextMode = GB_VIDEO_MODE_0_LENGTH;
128				video->mode = 0;
129				if (GBRegisterSTATIsHblankIRQ(video->stat)) {
130					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
131					GBUpdateIRQs(video->p);
132				}
133				break;
134			}
135			video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
136			video->p->memory.io[REG_STAT] = video->stat;
137		}
138
139		video->nextEvent = video->nextMode;
140		video->eventDiff = 0;
141	}
142	return video->nextEvent;
143}
144
145void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
146	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
147		// TODO: Does enabling the LCD start in vblank?
148		video->mode = 2;
149		video->nextMode = GB_VIDEO_MODE_2_LENGTH;
150		video->nextEvent = video->nextMode;
151		video->eventDiff = 0;
152		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
153		video->p->memory.io[REG_STAT] = video->stat;
154		video->eventDiff = 0;
155		if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) {
156			video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent;
157		}
158		return;
159	}
160	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
161		video->mode = 0;
162		video->nextMode = INT_MAX;
163		video->nextEvent = INT_MAX;
164		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
165		video->p->memory.io[REG_STAT] = video->stat;
166		video->ly = 0;
167		video->p->memory.io[REG_LY] = 0;
168	}
169}
170
171void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
172	video->stat = (video->stat & 0x7) | (value & 0x78);
173}
174
175static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer) {
176	UNUSED(renderer);
177	// Nothing to do
178}
179
180static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer) {
181	UNUSED(renderer);
182	// Nothing to do
183}
184
185static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
186	UNUSED(renderer);
187	// Nothing to do
188}
189
190static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
191	UNUSED(renderer);
192	UNUSED(address);
193	return value;
194}
195
196static void GBVideoDummyRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
197	UNUSED(renderer);
198	UNUSED(address);
199	// Nothing to do
200}
201
202static void GBVideoDummyRendererDrawScanline(struct GBVideoRenderer* renderer, int y) {
203	UNUSED(renderer);
204	UNUSED(y);
205	// Nothing to do
206}
207
208static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
209	UNUSED(renderer);
210	// Nothing to do
211}
212
213static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
214	UNUSED(renderer);
215	UNUSED(stride);
216	UNUSED(pixels);
217	// Nothing to do
218}