all repos — mgba @ 62076d885c06b738bc21edb839a452c19777f5ca

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