all repos — mgba @ c0eb7c81f70d94b21b931b6a1ed44e7b9c9f5dd3

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