all repos — mgba @ 38e8cc1ee45bc49164e76c979463e3b9d1a7370e

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