all repos — mgba @ 94ff4f7c4e25c717f3dc0b049e0e54c1818688ce

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				video->renderer->finishScanline(video->renderer, video->ly);
 97				++video->ly;
 98				video->p->memory.io[REG_LY] = video->ly;
 99				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
100				if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
101					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
102					video->mode = 2;
103					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
104						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
105					}
106				} else {
107					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
108					video->mode = 1;
109					++video->frameCounter;
110					video->renderer->finishFrame(video->renderer);
111					mCoreSyncPostFrame(video->p->sync);
112
113					struct mCoreThread* thread = mCoreThreadGet();
114					if (thread && thread->frameCallback) {
115						thread->frameCallback(thread);
116					}
117
118					if (GBRegisterSTATIsVblankIRQ(video->stat) || GBRegisterSTATIsOAMIRQ(video->stat)) {
119						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
120					}
121					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
122				}
123				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
124					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
125				}
126				GBUpdateIRQs(video->p);
127				break;
128			case 1:
129				// TODO: One M-cycle delay
130				++video->ly;
131				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
132				if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
133					video->ly = 0;
134					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
135					video->mode = 2;
136					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
137						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
138					}
139				} else {
140					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
141				}
142				if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
143					video->p->memory.io[REG_LY] = 0;
144				} else {
145					video->p->memory.io[REG_LY] = video->ly;
146				}
147				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
148					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
149				}
150				GBUpdateIRQs(video->p);
151				break;
152			case 2:
153				_cleanOAM(video, video->ly);
154				video->dotCounter = 0;
155				video->nextEvent = GB_VIDEO_HORIZONTAL_LENGTH;
156				video->x = 0;
157				video->nextMode = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 8;
158				video->mode = 3;
159				break;
160			case 3:
161				video->nextMode = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 8;
162				video->mode = 0;
163				if (GBRegisterSTATIsHblankIRQ(video->stat)) {
164					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
165					GBUpdateIRQs(video->p);
166				}
167				break;
168			}
169			video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
170			video->p->memory.io[REG_STAT] = video->stat;
171		}
172		if (video->nextMode < video->nextEvent) {
173			video->nextEvent = video->nextMode;
174		}
175		video->eventDiff = 0;
176	}
177	return video->nextEvent;
178}
179
180static void _cleanOAM(struct GBVideo* video, int y) {
181	// TODO: GBC differences
182	// TODO: Optimize
183	video->objMax = 0;
184	int spriteHeight = 8;
185	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
186		spriteHeight = 16;
187	}
188	int o = 0;
189	int i;
190	for (i = 0; i < 40; ++i) {
191		uint8_t oy = video->oam.obj[i].y;
192		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
193			continue;
194		}
195		// TODO: Sort
196		video->objThisLine[o] = &video->oam.obj[i];
197		++o;
198		if (o == 10) {
199			break;
200		}
201	}
202	video->objMax = o;
203}
204
205void GBVideoProcessDots(struct GBVideo* video) {
206	if (video->mode != 3 || video->dotCounter < 0) {
207		return;
208	}
209	int oldX = video->x;
210	video->x = video->dotCounter + video->eventDiff + video->p->cpu->cycles;
211	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
212		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
213	}
214	if (video->x == GB_VIDEO_HORIZONTAL_PIXELS) {
215		video->dotCounter = INT_MIN;
216	}
217	video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
218}
219
220void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
221	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
222		video->mode = 2;
223		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
224		video->nextEvent = video->nextMode;
225		video->eventDiff = -video->p->cpu->cycles;
226		// TODO: Does this read as 0 for 4 T-cycles?
227		video->stat = GBRegisterSTATSetMode(video->stat, 2);
228		video->p->memory.io[REG_STAT] = video->stat;
229		video->ly = 0;
230		video->p->memory.io[REG_LY] = 0;
231
232		if (video->p->cpu->cycles + video->nextEvent < video->p->cpu->nextEvent) {
233			video->p->cpu->nextEvent = video->p->cpu->cycles + video->nextEvent;
234		}
235		return;
236	}
237	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
238		video->mode = 0;
239		video->nextMode = INT_MAX;
240		video->nextEvent = INT_MAX;
241		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
242		video->p->memory.io[REG_STAT] = video->stat;
243		video->ly = 0;
244		video->p->memory.io[REG_LY] = 0;
245	}
246}
247
248void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
249	video->stat = (video->stat & 0x7) | (value & 0x78);
250}
251
252static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer) {
253	UNUSED(renderer);
254	// Nothing to do
255}
256
257static void GBVideoDummyRendererReset(struct GBVideoRenderer* renderer) {
258	UNUSED(renderer);
259	// Nothing to do
260}
261
262static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
263	UNUSED(renderer);
264	// Nothing to do
265}
266
267static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
268	UNUSED(renderer);
269	UNUSED(address);
270	return value;
271}
272
273static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax) {
274	UNUSED(renderer);
275	UNUSED(endX);
276	UNUSED(startX);
277	UNUSED(y);
278	UNUSED(obj);
279	UNUSED(oamMax);
280	// Nothing to do
281}
282
283static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
284	UNUSED(renderer);
285	UNUSED(y);
286	// Nothing to do
287}
288
289static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
290	UNUSED(renderer);
291	// Nothing to do
292}
293
294static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
295	UNUSED(renderer);
296	UNUSED(stride);
297	UNUSED(pixels);
298	// Nothing to do
299}