all repos — mgba @ 8615defda763c928eadb4c0919081a74b6bd28fc

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 GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax);
 20static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
 21static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer);
 22static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
 23
 24static void _cleanOAM(struct GBVideo* video, int y);
 25
 26static struct GBVideoRenderer dummyRenderer = {
 27	.init = GBVideoDummyRendererInit,
 28	.reset = GBVideoDummyRendererReset,
 29	.deinit = GBVideoDummyRendererDeinit,
 30	.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
 31	.drawRange = GBVideoDummyRendererDrawRange,
 32	.finishScanline = GBVideoDummyRendererFinishScanline,
 33	.finishFrame = GBVideoDummyRendererFinishFrame,
 34	.getPixels = GBVideoDummyRendererGetPixels
 35};
 36
 37void GBVideoInit(struct GBVideo* video) {
 38	video->renderer = &dummyRenderer;
 39	video->vram = 0;
 40	video->frameskip = 0;
 41}
 42
 43void GBVideoReset(struct GBVideo* video) {
 44	video->ly = 0;
 45	video->mode = 1;
 46	video->stat = 1;
 47
 48	video->nextEvent = INT_MAX;
 49	video->eventDiff = 0;
 50
 51	video->nextMode = INT_MAX;
 52	video->dotCounter = INT_MIN;
 53
 54	video->frameCounter = 0;
 55	video->frameskipCounter = 0;
 56
 57	if (video->vram) {
 58		mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 59	}
 60	video->vram = anonymousMemoryMap(GB_SIZE_VRAM);
 61	video->renderer->vram = video->vram;
 62	memset(&video->oam, 0, sizeof(video->oam));
 63	video->renderer->oam = &video->oam;
 64
 65	video->renderer->deinit(video->renderer);
 66	video->renderer->init(video->renderer);
 67}
 68
 69void GBVideoDeinit(struct GBVideo* video) {
 70	GBVideoAssociateRenderer(video, &dummyRenderer);
 71	mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 72}
 73
 74void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
 75	video->renderer->deinit(video->renderer);
 76	video->renderer = renderer;
 77	renderer->vram = video->vram;
 78	video->renderer->init(video->renderer);
 79}
 80
 81int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
 82	video->eventDiff += cycles;
 83	if (video->nextEvent != INT_MAX) {
 84		video->nextEvent -= cycles;
 85	}
 86	if (video->nextEvent <= 0) {
 87		if (video->nextEvent != INT_MAX) {
 88			video->nextMode -= video->eventDiff;
 89		}
 90		video->nextEvent = INT_MAX;
 91		GBVideoProcessDots(video);
 92		if (video->nextMode <= 0) {
 93			int lyc = video->p->memory.io[REG_LYC];
 94			switch (video->mode) {
 95			case 0:
 96				if (video->frameskipCounter <= 0) {
 97					video->renderer->finishScanline(video->renderer, video->ly);
 98				}
 99				++video->ly;
100				video->p->memory.io[REG_LY] = video->ly;
101				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
102				if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
103					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
104					video->mode = 2;
105					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
106						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
107					}
108				} else {
109					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
110					video->mode = 1;
111					--video->frameskipCounter;
112					if (video->frameskipCounter < 0) {
113						video->renderer->finishFrame(video->renderer);
114						mCoreSyncPostFrame(video->p->sync);
115						video->frameskipCounter = video->frameskip;
116					}
117					++video->frameCounter;
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->dotCounter = 0;
161				video->nextEvent = GB_VIDEO_HORIZONTAL_LENGTH;
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->nextMode < video->nextEvent) {
179			video->nextEvent = video->nextMode;
180		}
181		video->eventDiff = 0;
182	}
183	return video->nextEvent;
184}
185
186static void _cleanOAM(struct GBVideo* video, int y) {
187	// TODO: GBC differences
188	// TODO: Optimize
189	video->objMax = 0;
190	int spriteHeight = 8;
191	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
192		spriteHeight = 16;
193	}
194	int o = 0;
195	int i;
196	for (i = 0; i < 40; ++i) {
197		uint8_t oy = video->oam.obj[i].y;
198		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
199			continue;
200		}
201		// TODO: Sort
202		video->objThisLine[o] = &video->oam.obj[i];
203		++o;
204		if (o == 10) {
205			break;
206		}
207	}
208	video->objMax = o;
209}
210
211void GBVideoProcessDots(struct GBVideo* video) {
212	if (video->mode != 3 || video->dotCounter < 0) {
213		return;
214	}
215	int oldX = video->x;
216	video->x = video->dotCounter + video->eventDiff + video->p->cpu->cycles;
217	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
218		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
219	}
220	if (video->x == GB_VIDEO_HORIZONTAL_PIXELS) {
221		video->dotCounter = INT_MIN;
222	}
223	if (video->frameskipCounter <= 0) {
224		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
225	}
226}
227
228void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
229	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
230		video->mode = 2;
231		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
232		video->nextEvent = video->nextMode;
233		video->eventDiff = -video->p->cpu->cycles;
234		// TODO: Does this read as 0 for 4 T-cycles?
235		video->stat = GBRegisterSTATSetMode(video->stat, 2);
236		video->p->memory.io[REG_STAT] = video->stat;
237		video->ly = 0;
238		video->p->memory.io[REG_LY] = 0;
239
240		if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) {
241			video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent;
242		}
243		return;
244	}
245	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
246		video->mode = 0;
247		video->nextMode = INT_MAX;
248		video->nextEvent = INT_MAX;
249		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
250		video->p->memory.io[REG_STAT] = video->stat;
251		video->ly = 0;
252		video->p->memory.io[REG_LY] = 0;
253	}
254}
255
256void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
257	video->stat = (video->stat & 0x7) | (value & 0x78);
258}
259
260static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer) {
261	UNUSED(renderer);
262	// Nothing to do
263}
264
265static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer) {
266	UNUSED(renderer);
267	// Nothing to do
268}
269
270static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
271	UNUSED(renderer);
272	// Nothing to do
273}
274
275static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
276	UNUSED(renderer);
277	UNUSED(address);
278	return value;
279}
280
281static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax) {
282	UNUSED(renderer);
283	UNUSED(endX);
284	UNUSED(startX);
285	UNUSED(y);
286	UNUSED(obj);
287	UNUSED(oamMax);
288	// Nothing to do
289}
290
291static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
292	UNUSED(renderer);
293	UNUSED(y);
294	// Nothing to do
295}
296
297static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
298	UNUSED(renderer);
299	// Nothing to do
300}
301
302static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
303	UNUSED(renderer);
304	UNUSED(stride);
305	UNUSED(pixels);
306	// Nothing to do
307}