all repos — mgba @ 093c86c94355f296a3dfb201b30f008b1893d967

mGBA Game Boy Advance Emulator

src/gba/gba-video.c (view raw)

  1#include "gba-video.h"
  2
  3#include "gba.h"
  4#include "gba-io.h"
  5#include "gba-thread.h"
  6
  7#include <limits.h>
  8#include <string.h>
  9#include <sys/mman.h>
 10
 11static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer);
 12static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer);
 13static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 14static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
 15static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer);
 16
 17static struct GBAVideoRenderer dummyRenderer = {
 18	.init = GBAVideoDummyRendererInit,
 19	.deinit = GBAVideoDummyRendererDeinit,
 20	.writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
 21	.drawScanline = GBAVideoDummyRendererDrawScanline,
 22	.finishFrame = GBAVideoDummyRendererFinishFrame
 23};
 24
 25void GBAVideoInit(struct GBAVideo* video) {
 26	video->renderer = &dummyRenderer;
 27
 28	video->inHblank = 0;
 29	video->inVblank = 0;
 30	video->vcounter = 0;
 31	video->vblankIRQ = 0;
 32	video->hblankIRQ = 0;
 33	video->vcounterIRQ = 0;
 34	video->vcountSetting = 0;
 35
 36	video->vcount = -1;
 37
 38	video->lastHblank = 0;
 39	video->nextHblank = VIDEO_HDRAW_LENGTH;
 40	video->nextEvent = video->nextHblank;
 41	video->eventDiff = video->nextEvent;
 42
 43	video->nextHblankIRQ = 0;
 44	video->nextVblankIRQ = 0;
 45	video->nextVcounterIRQ = 0;
 46
 47	video->vram = mmap(0, SIZE_VRAM, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
 48}
 49
 50void GBAVideoDeinit(struct GBAVideo* video) {
 51	GBAVideoAssociateRenderer(video, &dummyRenderer);
 52	munmap(video->vram, SIZE_VRAM);
 53}
 54
 55void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
 56	video->renderer->deinit(video->renderer);
 57	video->renderer = renderer;
 58	renderer->palette = video->palette;
 59	renderer->vram = video->vram;
 60	renderer->oam = &video->oam;
 61	video->renderer->init(video->renderer);
 62}
 63
 64int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
 65	video->nextEvent -= cycles;
 66	if (video->nextEvent <= 0) {
 67		video->lastHblank -= video->eventDiff;
 68		video->nextHblank -= video->eventDiff;
 69		video->nextHblankIRQ -= video->eventDiff;
 70		video->nextVcounterIRQ -= video->eventDiff;
 71
 72		if (video->inHblank) {
 73			// End Hblank
 74			video->inHblank = 0;
 75			video->nextEvent = video->nextHblank;
 76
 77			++video->vcount;
 78			video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
 79
 80			switch (video->vcount) {
 81			case VIDEO_VERTICAL_PIXELS:
 82				video->inVblank = 1;
 83				if (GBASyncDrawingFrame(video->p->sync)) {
 84					video->renderer->finishFrame(video->renderer);
 85				}
 86				video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
 87				GBAMemoryRunVblankDMAs(&video->p->memory);
 88				if (video->vblankIRQ) {
 89					GBARaiseIRQ(video->p, IRQ_VBLANK);
 90				}
 91				GBASyncPostFrame(video->p->sync);
 92				break;
 93			case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
 94				video->inVblank = 0;
 95				break;
 96			case VIDEO_VERTICAL_TOTAL_PIXELS:
 97				video->vcount = 0;
 98				video->p->memory.io[REG_VCOUNT >> 1] = 0;
 99				break;
100			}
101
102			video->vcounter = video->vcount == video->vcountSetting;
103			if (video->vcounter && video->vcounterIRQ) {
104				GBARaiseIRQ(video->p, IRQ_VCOUNTER);
105				video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
106			}
107
108			if (video->vcount < VIDEO_VERTICAL_PIXELS && GBASyncDrawingFrame(video->p->sync)) {
109				video->renderer->drawScanline(video->renderer, video->vcount);
110			}
111		} else {
112			// Begin Hblank
113			video->inHblank = 1;
114			video->lastHblank = video->nextHblank;
115			video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH;
116			video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
117			video->nextHblankIRQ = video->nextHblank;
118
119			if (video->vcount < VIDEO_VERTICAL_PIXELS) {
120				GBAMemoryRunHblankDMAs(&video->p->memory);
121			}
122			if (video->hblankIRQ) {
123				GBARaiseIRQ(video->p, IRQ_HBLANK);
124			}
125		}
126
127		video->eventDiff = video->nextEvent;
128	}
129	return video->nextEvent;
130}
131
132void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
133	video->vblankIRQ = value & 0x0008;
134	video->hblankIRQ = value & 0x0010;
135	video->vcounterIRQ = value & 0x0020;
136	video->vcountSetting = (value & 0xFF00) >> 8;
137
138	if (video->vcounterIRQ) {
139		// FIXME: this can be too late if we're in the middle of an Hblank
140		video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (video->vcountSetting - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
141		if (video->nextVcounterIRQ < video->nextEvent) {
142			video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
143		}
144	}
145}
146
147uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video) {
148	return (video->inVblank) | (video->inHblank << 1) | (video->vcounter << 2);
149}
150
151static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
152	(void)(renderer);
153	// Nothing to do
154}
155
156static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
157	(void)(renderer);
158	// Nothing to do
159}
160
161static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
162	(void)(renderer);
163	(void)(address);
164	return value;
165}
166
167static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
168	(void)(renderer);
169	(void)(y);
170	// Nothing to do
171}
172
173static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
174	(void)(renderer);
175	// Nothing to do
176}