all repos — mgba @ bbe52bf632b75e0ea3e7d3a3027fe985ad5ebc29

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