all repos — mgba @ aac1add766e8bacaa27af86b710b00cd5f67c358

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