all repos — mgba @ e6e535e39af599989e44957a7f297ada148a9185

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