all repos — mgba @ db96be98dcd40796597d96afe4776b7f4ceff928

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