all repos — mgba @ 98922b7a87f6d18bc0d63bc2fd9db092ee4b8318

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, enum GBModel model);
 16static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer);
 17static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 18static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_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	.deinit = GBVideoDummyRendererDeinit,
 29	.writeVideoRegister = GBVideoDummyRendererWriteVideoRegister,
 30	.writePalette = GBVideoDummyRendererWritePalette,
 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	GBVideoSwitchBank(video, 0);
 62	video->renderer->vram = video->vram;
 63	memset(&video->oam, 0, sizeof(video->oam));
 64	video->renderer->oam = &video->oam;
 65	memset(&video->palette, 0, sizeof(video->palette));
 66
 67	video->renderer->deinit(video->renderer);
 68	video->renderer->init(video->renderer, video->p->model);
 69}
 70
 71void GBVideoDeinit(struct GBVideo* video) {
 72	GBVideoAssociateRenderer(video, &dummyRenderer);
 73	mappedMemoryFree(video->vram, GB_SIZE_VRAM);
 74}
 75
 76void GBVideoAssociateRenderer(struct GBVideo* video, struct GBVideoRenderer* renderer) {
 77	video->renderer->deinit(video->renderer);
 78	video->renderer = renderer;
 79	renderer->vram = video->vram;
 80	video->renderer->init(video->renderer, video->p->model);
 81}
 82
 83int32_t GBVideoProcessEvents(struct GBVideo* video, int32_t cycles) {
 84	video->eventDiff += cycles;
 85	if (video->nextEvent != INT_MAX) {
 86		video->nextEvent -= cycles;
 87	}
 88	if (video->nextEvent <= 0) {
 89		if (video->nextEvent != INT_MAX) {
 90			video->nextMode -= video->eventDiff;
 91		}
 92		video->nextEvent = INT_MAX;
 93		GBVideoProcessDots(video);
 94		if (video->nextMode <= 0) {
 95			int lyc = video->p->memory.io[REG_LYC];
 96			switch (video->mode) {
 97			case 0:
 98				if (video->frameskipCounter <= 0) {
 99					video->renderer->finishScanline(video->renderer, video->ly);
100				}
101				++video->ly;
102				video->p->memory.io[REG_LY] = video->ly;
103				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->ly);
104				if (video->ly < GB_VIDEO_VERTICAL_PIXELS) {
105					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
106					video->mode = 2;
107					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
108						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
109					}
110				} else {
111					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
112					video->mode = 1;
113					--video->frameskipCounter;
114					if (video->frameskipCounter < 0) {
115						video->renderer->finishFrame(video->renderer);
116						mCoreSyncPostFrame(video->p->sync);
117						video->frameskipCounter = video->frameskip;
118					}
119					GBFrameEnded(video->p);
120					++video->frameCounter;
121
122					struct mCoreThread* thread = mCoreThreadGet();
123					if (thread && thread->frameCallback) {
124						thread->frameCallback(thread);
125					}
126
127					if (video->p->stream && video->p->stream->postVideoFrame) {
128						const color_t* pixels;
129						unsigned stride;
130						video->renderer->getPixels(video->renderer, &stride, (const void**) &pixels);
131						video->p->stream->postVideoFrame(video->p->stream, pixels, stride);
132					}
133
134					if (GBRegisterSTATIsVblankIRQ(video->stat) || GBRegisterSTATIsOAMIRQ(video->stat)) {
135						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
136					}
137					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_VBLANK);
138				}
139				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->ly) {
140					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
141				}
142				GBUpdateIRQs(video->p);
143				break;
144			case 1:
145				// TODO: One M-cycle delay
146				++video->ly;
147				if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS + 1) {
148					video->ly = 0;
149					video->p->memory.io[REG_LY] = video->ly;
150					video->nextMode = GB_VIDEO_MODE_2_LENGTH;
151					video->mode = 2;
152					if (GBRegisterSTATIsOAMIRQ(video->stat)) {
153						video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
154						GBUpdateIRQs(video->p);
155					}
156					break;
157				} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS) {
158					video->p->memory.io[REG_LY] = 0;
159					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH - 8;
160				} else if (video->ly == GB_VIDEO_VERTICAL_TOTAL_PIXELS - 1) {
161					video->p->memory.io[REG_LY] = video->ly;
162					video->nextMode = 8;
163				} else {
164					video->p->memory.io[REG_LY] = video->ly;
165					video->nextMode = GB_VIDEO_HORIZONTAL_LENGTH;
166				}
167
168				video->stat = GBRegisterSTATSetLYC(video->stat, lyc == video->p->memory.io[REG_LY]);
169				if (GBRegisterSTATIsLYCIRQ(video->stat) && lyc == video->p->memory.io[REG_LY]) {
170					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
171					GBUpdateIRQs(video->p);
172				}
173				if (video->p->memory.mbcType == GB_MBC7 && video->p->memory.rotation && video->p->memory.rotation->sample) {
174					video->p->memory.rotation->sample(video->p->memory.rotation);
175				}
176				break;
177			case 2:
178				_cleanOAM(video, video->ly);
179				video->dotCounter = 0;
180				video->nextEvent = GB_VIDEO_HORIZONTAL_LENGTH;
181				video->x = 0;
182				video->nextMode = GB_VIDEO_MODE_3_LENGTH_BASE + video->objMax * 12;
183				video->mode = 3;
184				break;
185			case 3:
186				video->nextMode = GB_VIDEO_MODE_0_LENGTH_BASE - video->objMax * 12;
187				video->mode = 0;
188				if (GBRegisterSTATIsHblankIRQ(video->stat)) {
189					video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
190					GBUpdateIRQs(video->p);
191				}
192				if (video->ly < GB_VIDEO_VERTICAL_PIXELS && video->p->memory.isHdma && video->p->memory.io[REG_HDMA5] != 0xFF) {
193					video->p->memory.hdmaRemaining = 0x10;
194					video->p->memory.hdmaNext = video->p->cpu->cycles;
195				}
196				break;
197			}
198			video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
199			video->p->memory.io[REG_STAT] = video->stat;
200		}
201		if (video->nextMode < video->nextEvent) {
202			video->nextEvent = video->nextMode;
203		}
204		video->eventDiff = 0;
205	}
206	return video->nextEvent;
207}
208
209static void _cleanOAM(struct GBVideo* video, int y) {
210	// TODO: GBC differences
211	// TODO: Optimize
212	video->objMax = 0;
213	int spriteHeight = 8;
214	if (GBRegisterLCDCIsObjSize(video->p->memory.io[REG_LCDC])) {
215		spriteHeight = 16;
216	}
217	int o = 0;
218	int i;
219	for (i = 0; i < 40; ++i) {
220		uint8_t oy = video->oam.obj[i].y;
221		if (y < oy - 16 || y >= oy - 16 + spriteHeight) {
222			continue;
223		}
224		// TODO: Sort
225		video->objThisLine[o] = &video->oam.obj[i];
226		++o;
227		if (o == 10) {
228			break;
229		}
230	}
231	video->objMax = o;
232}
233
234void GBVideoProcessDots(struct GBVideo* video) {
235	if (video->mode != 3 || video->dotCounter < 0) {
236		return;
237	}
238	int oldX = video->x;
239	video->x = video->dotCounter + video->eventDiff + (video->p->cpu->cycles >> video->p->doubleSpeed);
240	if (video->x > GB_VIDEO_HORIZONTAL_PIXELS) {
241		video->x = GB_VIDEO_HORIZONTAL_PIXELS;
242	}
243	if (video->x == GB_VIDEO_HORIZONTAL_PIXELS) {
244		video->dotCounter = INT_MIN;
245	}
246	if (video->frameskipCounter <= 0) {
247		video->renderer->drawRange(video->renderer, oldX, video->x, video->ly, video->objThisLine, video->objMax);
248	}
249}
250
251void GBVideoWriteLCDC(struct GBVideo* video, GBRegisterLCDC value) {
252	if (!GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && GBRegisterLCDCIsEnable(value)) {
253		video->mode = 2;
254		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
255		video->nextEvent = video->nextMode;
256		video->eventDiff = -video->p->cpu->cycles >> video->p->doubleSpeed;
257		// TODO: Does this read as 0 for 4 T-cycles?
258		video->stat = GBRegisterSTATSetMode(video->stat, 2);
259		video->p->memory.io[REG_STAT] = video->stat;
260		video->ly = 0;
261		video->p->memory.io[REG_LY] = 0;
262
263		if (video->p->cpu->cycles + (video->nextEvent << video->p->doubleSpeed) < video->p->cpu->nextEvent) {
264			video->p->cpu->nextEvent = video->p->cpu->cycles + (video->nextEvent << video->p->doubleSpeed);
265		}
266		return;
267	}
268	if (GBRegisterLCDCIsEnable(video->p->memory.io[REG_LCDC]) && !GBRegisterLCDCIsEnable(value)) {
269		video->mode = 0;
270		video->nextMode = INT_MAX;
271		video->nextEvent = INT_MAX;
272		video->stat = GBRegisterSTATSetMode(video->stat, video->mode);
273		video->p->memory.io[REG_STAT] = video->stat;
274		video->ly = 0;
275		video->p->memory.io[REG_LY] = 0;
276	}
277}
278
279void GBVideoWriteSTAT(struct GBVideo* video, GBRegisterSTAT value) {
280	video->stat = (video->stat & 0x7) | (value & 0x78);
281	if (video->p->model == GB_MODEL_DMG && video->mode == 1) {
282		video->p->memory.io[REG_IF] |= (1 << GB_IRQ_LCDSTAT);
283		GBUpdateIRQs(video->p);
284	}
285}
286
287void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value) {
288	static const uint16_t dmgPalette[4] = { 0x7FFF, 0x56B5, 0x294A, 0x0000};
289	if (video->p->model < GB_MODEL_CGB) {
290		switch (address) {
291		case REG_BGP:
292			video->palette[0] = dmgPalette[value & 3];
293			video->palette[1] = dmgPalette[(value >> 2) & 3];
294			video->palette[2] = dmgPalette[(value >> 4) & 3];
295			video->palette[3] = dmgPalette[(value >> 6) & 3];
296			video->renderer->writePalette(video->renderer, 0, video->palette[0]);
297			video->renderer->writePalette(video->renderer, 1, video->palette[1]);
298			video->renderer->writePalette(video->renderer, 2, video->palette[2]);
299			video->renderer->writePalette(video->renderer, 3, video->palette[3]);
300			break;
301		case REG_OBP0:
302			video->palette[8 * 4 + 0] = dmgPalette[value & 3];
303			video->palette[8 * 4 + 1] = dmgPalette[(value >> 2) & 3];
304			video->palette[8 * 4 + 2] = dmgPalette[(value >> 4) & 3];
305			video->palette[8 * 4 + 3] = dmgPalette[(value >> 6) & 3];
306			video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
307			video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
308			video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
309			video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
310			break;
311		case REG_OBP1:
312			video->palette[9 * 4 + 0] = dmgPalette[value & 3];
313			video->palette[9 * 4 + 1] = dmgPalette[(value >> 2) & 3];
314			video->palette[9 * 4 + 2] = dmgPalette[(value >> 4) & 3];
315			video->palette[9 * 4 + 3] = dmgPalette[(value >> 6) & 3];
316			video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
317			video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
318			video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
319			video->renderer->writePalette(video->renderer, 9 * 4 + 3, video->palette[9 * 4 + 3]);
320			break;
321		}
322	} else {
323		switch (address) {
324		case REG_BCPD:
325			if (video->bcpIndex & 1) {
326				video->palette[video->bcpIndex >> 1] &= 0x00FF;
327				video->palette[video->bcpIndex >> 1] |= value << 8;
328			} else {
329				video->palette[video->bcpIndex >> 1] &= 0xFF00;
330				video->palette[video->bcpIndex >> 1] |= value;
331			}
332			video->renderer->writePalette(video->renderer, video->bcpIndex >> 1, video->palette[video->bcpIndex >> 1]);
333			if (video->bcpIncrement) {
334				++video->bcpIndex;
335				video->bcpIndex &= 0x3F;
336				video->p->memory.io[REG_BCPD] = video->palette[video->bcpIndex >> 1];
337			}
338			break;
339		case REG_OCPD:
340			if (video->ocpIndex & 1) {
341				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0x00FF;
342				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value << 8;
343			} else {
344				video->palette[8 * 4 + (video->ocpIndex >> 1)] &= 0xFF00;
345				video->palette[8 * 4 + (video->ocpIndex >> 1)] |= value;
346			}
347			video->renderer->writePalette(video->renderer, 8 * 4 + (video->ocpIndex >> 1), video->palette[8 * 4 + (video->ocpIndex >> 1)]);
348			if (video->ocpIncrement) {
349				++video->ocpIndex;
350				video->ocpIndex &= 0x3F;
351				video->p->memory.io[REG_OCPD] = video->palette[8 * 4 + (video->ocpIndex >> 1)];
352			}
353			break;
354		}
355	}
356}
357
358void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
359	value &= 1;
360	video->vramBank = &video->vram[value * GB_SIZE_VRAM_BANK0];
361	video->vramCurrentBank = value;
362}
363
364static void GBVideoDummyRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
365	UNUSED(renderer);
366	UNUSED(model);
367	// Nothing to do
368}
369
370static void GBVideoDummyRendererDeinit(struct GBVideoRenderer* renderer) {
371	UNUSED(renderer);
372	// Nothing to do
373}
374
375static uint8_t GBVideoDummyRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
376	UNUSED(renderer);
377	UNUSED(address);
378	return value;
379}
380
381static void GBVideoDummyRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
382	UNUSED(renderer);
383	UNUSED(index);
384	UNUSED(value);
385}
386
387static void GBVideoDummyRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax) {
388	UNUSED(renderer);
389	UNUSED(endX);
390	UNUSED(startX);
391	UNUSED(y);
392	UNUSED(obj);
393	UNUSED(oamMax);
394	// Nothing to do
395}
396
397static void GBVideoDummyRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
398	UNUSED(renderer);
399	UNUSED(y);
400	// Nothing to do
401}
402
403static void GBVideoDummyRendererFinishFrame(struct GBVideoRenderer* renderer) {
404	UNUSED(renderer);
405	// Nothing to do
406}
407
408static void GBVideoDummyRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels) {
409	UNUSED(renderer);
410	UNUSED(stride);
411	UNUSED(pixels);
412	// Nothing to do
413}