all repos — mgba @ 565ee43bb26639078c70e194b859a9c98ed819c8

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	video->eventDiff += cycles;
 67	if (video->nextEvent <= 0) {
 68		video->lastHblank -= video->eventDiff;
 69		video->nextHblank -= video->eventDiff;
 70		video->nextHblankIRQ -= video->eventDiff;
 71		video->nextVcounterIRQ -= video->eventDiff;
 72
 73		if (video->inHblank) {
 74			// End Hblank
 75			video->inHblank = 0;
 76			video->nextEvent = video->nextHblank;
 77
 78			++video->vcount;
 79			video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
 80
 81			switch (video->vcount) {
 82			case VIDEO_VERTICAL_PIXELS:
 83				video->inVblank = 1;
 84				if (GBASyncDrawingFrame(video->p->sync)) {
 85					video->renderer->finishFrame(video->renderer);
 86				}
 87				video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
 88				GBAMemoryRunVblankDMAs(&video->p->memory);
 89				if (video->vblankIRQ) {
 90					GBARaiseIRQ(video->p, IRQ_VBLANK);
 91				}
 92				GBASyncPostFrame(video->p->sync);
 93				break;
 94			case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
 95				video->inVblank = 0;
 96				break;
 97			case VIDEO_VERTICAL_TOTAL_PIXELS:
 98				video->vcount = 0;
 99				video->p->memory.io[REG_VCOUNT >> 1] = 0;
100				break;
101			}
102
103			video->vcounter = video->vcount == video->vcountSetting;
104			if (video->vcounter && video->vcounterIRQ) {
105				GBARaiseIRQ(video->p, IRQ_VCOUNTER);
106				video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
107			}
108
109			if (video->vcount < VIDEO_VERTICAL_PIXELS && GBASyncDrawingFrame(video->p->sync)) {
110				video->renderer->drawScanline(video->renderer, video->vcount);
111			}
112		} else {
113			// Begin Hblank
114			video->inHblank = 1;
115			video->lastHblank = video->nextHblank;
116			video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH;
117			video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
118			video->nextHblankIRQ = video->nextHblank;
119
120			if (video->vcount < VIDEO_VERTICAL_PIXELS) {
121				GBAMemoryRunHblankDMAs(&video->p->memory);
122			}
123			if (video->hblankIRQ) {
124				GBARaiseIRQ(video->p, IRQ_HBLANK);
125			}
126		}
127
128		video->eventDiff = 0;
129	}
130	return video->nextEvent;
131}
132
133void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
134	video->vblankIRQ = value & 0x0008;
135	video->hblankIRQ = value & 0x0010;
136	video->vcounterIRQ = value & 0x0020;
137	video->vcountSetting = (value & 0xFF00) >> 8;
138
139	if (video->vcounterIRQ) {
140		// FIXME: this can be too late if we're in the middle of an Hblank
141		video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (video->vcountSetting - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
142		if (video->nextVcounterIRQ < video->nextEvent) {
143			video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
144		}
145	}
146}
147
148uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video) {
149	return (video->inVblank) | (video->inHblank << 1) | (video->vcounter << 2);
150}
151
152static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
153	(void)(renderer);
154	// Nothing to do
155}
156
157static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
158	(void)(renderer);
159	// Nothing to do
160}
161
162static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
163	(void)(renderer);
164	(void)(address);
165	return value;
166}
167
168static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
169	(void)(renderer);
170	(void)(y);
171	// Nothing to do
172}
173
174static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
175	(void)(renderer);
176	// Nothing to do
177}