all repos — mgba @ 79d4011650ec05c332666bce059cb10cbaf2884e

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-serialize.h"
  6#include "gba-thread.h"
  7#include "memory.h"
  8
  9#include <limits.h>
 10#include <string.h>
 11
 12static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer);
 13static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer);
 14static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 15static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
 16static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer);
 17
 18static struct GBAVideoRenderer dummyRenderer = {
 19	.init = GBAVideoDummyRendererInit,
 20	.deinit = GBAVideoDummyRendererDeinit,
 21	.writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
 22	.drawScanline = GBAVideoDummyRendererDrawScanline,
 23	.finishFrame = GBAVideoDummyRendererFinishFrame
 24};
 25
 26void GBAVideoInit(struct GBAVideo* video) {
 27	video->renderer = &dummyRenderer;
 28
 29	video->inHblank = 0;
 30	video->inVblank = 0;
 31	video->vcounter = 0;
 32	video->vblankIRQ = 0;
 33	video->hblankIRQ = 0;
 34	video->vcounterIRQ = 0;
 35	video->vcountSetting = 0;
 36
 37	video->vcount = -1;
 38
 39	video->lastHblank = 0;
 40	video->nextHblank = VIDEO_HDRAW_LENGTH;
 41	video->nextEvent = video->nextHblank;
 42	video->eventDiff = video->nextEvent;
 43
 44	video->nextHblankIRQ = 0;
 45	video->nextVblankIRQ = 0;
 46	video->nextVcounterIRQ = 0;
 47
 48	video->vram = anonymousMemoryMap(SIZE_VRAM);
 49
 50	int i;
 51	for (i = 0; i < 128; ++i) {
 52		video->oam.obj[i].disable = 1;
 53	}
 54}
 55
 56void GBAVideoDeinit(struct GBAVideo* video) {
 57	GBAVideoAssociateRenderer(video, &dummyRenderer);
 58	mappedMemoryFree(video->vram, SIZE_VRAM);
 59}
 60
 61void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
 62	video->renderer->deinit(video->renderer);
 63	video->renderer = renderer;
 64	renderer->palette = video->palette;
 65	renderer->vram = video->vram;
 66	renderer->oam = &video->oam;
 67	video->renderer->init(video->renderer);
 68}
 69
 70int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
 71	video->nextEvent -= cycles;
 72	video->eventDiff += cycles;
 73	if (video->nextEvent <= 0) {
 74		int32_t lastEvent = video->nextEvent;
 75		video->lastHblank -= video->eventDiff;
 76		video->nextHblank -= video->eventDiff;
 77		video->nextHblankIRQ -= video->eventDiff;
 78		video->nextVcounterIRQ -= video->eventDiff;
 79
 80		if (video->inHblank) {
 81			// End Hblank
 82			video->inHblank = 0;
 83			video->nextEvent = video->nextHblank;
 84
 85			++video->vcount;
 86			video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
 87
 88			switch (video->vcount) {
 89			case VIDEO_VERTICAL_PIXELS:
 90				video->inVblank = 1;
 91				if (GBASyncDrawingFrame(video->p->sync)) {
 92					video->renderer->finishFrame(video->renderer);
 93				}
 94				video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
 95				GBAMemoryRunVblankDMAs(&video->p->memory, lastEvent);
 96				if (video->vblankIRQ) {
 97					GBARaiseIRQ(video->p, IRQ_VBLANK);
 98				}
 99				GBASyncPostFrame(video->p->sync);
100				break;
101			case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
102				video->inVblank = 0;
103				break;
104			case VIDEO_VERTICAL_TOTAL_PIXELS:
105				video->vcount = 0;
106				video->p->memory.io[REG_VCOUNT >> 1] = 0;
107				break;
108			}
109
110			video->vcounter = video->vcount == video->vcountSetting;
111			if (video->vcounter && video->vcounterIRQ) {
112				GBARaiseIRQ(video->p, IRQ_VCOUNTER);
113				video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
114			}
115
116			if (video->vcount < VIDEO_VERTICAL_PIXELS && GBASyncDrawingFrame(video->p->sync)) {
117				video->renderer->drawScanline(video->renderer, video->vcount);
118			}
119		} else {
120			// Begin Hblank
121			video->inHblank = 1;
122			video->lastHblank = video->nextHblank;
123			video->nextEvent = video->lastHblank + VIDEO_HBLANK_LENGTH;
124			video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
125			video->nextHblankIRQ = video->nextHblank;
126
127			if (video->vcount < VIDEO_VERTICAL_PIXELS) {
128				GBAMemoryRunHblankDMAs(&video->p->memory, lastEvent);
129			}
130			if (video->hblankIRQ) {
131				GBARaiseIRQ(video->p, IRQ_HBLANK);
132			}
133		}
134
135		video->eventDiff = 0;
136	}
137	return video->nextEvent;
138}
139
140void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
141	union GBARegisterDISPSTAT dispstat;
142	dispstat.packed = value;
143	video->vblankIRQ = dispstat.vblankIRQ;
144	video->hblankIRQ = dispstat.hblankIRQ;
145	video->vcounterIRQ = dispstat.vcounterIRQ;
146	video->vcountSetting = dispstat.vcountSetting;
147
148	if (video->vcounterIRQ) {
149		// FIXME: this can be too late if we're in the middle of an Hblank
150		video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (video->vcountSetting - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
151		if (video->nextVcounterIRQ < video->nextEvent) {
152			video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
153		}
154	}
155}
156
157uint16_t GBAVideoReadDISPSTAT(struct GBAVideo* video) {
158	return (video->inVblank) | (video->inHblank << 1) | (video->vcounter << 2);
159}
160
161static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
162	(void)(renderer);
163	// Nothing to do
164}
165
166static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
167	(void)(renderer);
168	// Nothing to do
169}
170
171static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
172	(void)(renderer);
173	(void)(address);
174	return value;
175}
176
177static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
178	(void)(renderer);
179	(void)(y);
180	// Nothing to do
181}
182
183static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
184	(void)(renderer);
185	// Nothing to do
186}
187
188void GBAVideoSerialize(struct GBAVideo* video, struct GBASerializedState* state) {
189	memcpy(state->vram, video->renderer->vram, SIZE_VRAM);
190	memcpy(state->oam, video->oam.raw, SIZE_OAM);
191	memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
192	union GBARegisterDISPSTAT dispstat;
193	dispstat.inVblank = video->inVblank;
194	dispstat.inHblank = video->inHblank;
195	dispstat.vcounter = video->vcounter;
196	dispstat.vblankIRQ = video->vblankIRQ;
197	dispstat.hblankIRQ = video->hblankIRQ;
198	dispstat.vcounterIRQ = video->vcounterIRQ;
199	dispstat.vcountSetting = video->vcountSetting;
200	state->io[REG_DISPSTAT >> 1] = dispstat.packed;
201	state->video.nextEvent = video->nextEvent;
202	state->video.eventDiff = video->eventDiff;
203	state->video.lastHblank = video->lastHblank;
204	state->video.nextHblank = video->nextHblank;
205	state->video.nextHblankIRQ = video->nextHblankIRQ;
206	state->video.nextVblankIRQ = video->nextVblankIRQ;
207	state->video.nextVcounterIRQ = video->nextVcounterIRQ;
208}
209
210void GBAVideoDeserialize(struct GBAVideo* video, struct GBASerializedState* state) {
211	memcpy(video->renderer->vram, state->vram, SIZE_VRAM);
212	int i;
213	for (i = 0; i < SIZE_OAM; i += 2) {
214		GBAStore16(&video->p->memory.d, BASE_OAM | i, state->oam[i >> 1], 0);
215	}
216	for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
217		GBAStore16(&video->p->memory.d, BASE_PALETTE_RAM | i, state->pram[i >> 1], 0);
218	}
219	union GBARegisterDISPSTAT dispstat;
220	dispstat.packed = state->io[REG_DISPSTAT >> 1];
221	video->inVblank = dispstat.inVblank;
222	video->inHblank = dispstat.inHblank;
223	video->vcounter = dispstat.vcounter;
224	video->vblankIRQ = dispstat.vblankIRQ;
225	video->hblankIRQ = dispstat.hblankIRQ;
226	video->vcounterIRQ = dispstat.vcounterIRQ;
227	video->vcountSetting = dispstat.vcountSetting;
228	video->nextEvent = state->video.nextEvent;
229	video->eventDiff = state->video.eventDiff;
230	video->lastHblank = state->video.lastHblank;
231	video->nextHblank = state->video.nextHblank;
232	video->nextHblankIRQ = state->video.nextHblankIRQ;
233	video->nextVblankIRQ = state->video.nextVblankIRQ;
234	video->nextVcounterIRQ = state->video.nextVcounterIRQ;
235	video->vcount = state->io[REG_VCOUNT >> 1];
236}