all repos — mgba @ 4b4be372c0ff200e2da2e2855bc942a08e544618

mGBA Game Boy Advance Emulator

src/gba/video.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "video.h"
  7
  8#include "core/sync.h"
  9#include "gba/gba.h"
 10#include "gba/io.h"
 11#include "gba/rr/rr.h"
 12#include "gba/serialize.h"
 13
 14#include "util/memory.h"
 15
 16mLOG_DEFINE_CATEGORY(GBA_VIDEO, "GBA Video");
 17
 18static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer);
 19static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer);
 20static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer);
 21static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 22static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
 23static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 24static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
 25static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
 26static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer);
 27static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, const void** pixels);
 28
 29const int GBAVideoObjSizes[16][2] = {
 30	{ 8, 8 },
 31	{ 16, 16 },
 32	{ 32, 32 },
 33	{ 64, 64 },
 34	{ 16, 8 },
 35	{ 32, 8 },
 36	{ 32, 16 },
 37	{ 64, 32 },
 38	{ 8, 16 },
 39	{ 8, 32 },
 40	{ 16, 32 },
 41	{ 32, 64 },
 42	{ 0, 0 },
 43	{ 0, 0 },
 44	{ 0, 0 },
 45	{ 0, 0 },
 46};
 47
 48static struct GBAVideoRenderer dummyRenderer = {
 49	.init = GBAVideoDummyRendererInit,
 50	.reset = GBAVideoDummyRendererReset,
 51	.deinit = GBAVideoDummyRendererDeinit,
 52	.writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
 53	.writeVRAM = GBAVideoDummyRendererWriteVRAM,
 54	.writePalette = GBAVideoDummyRendererWritePalette,
 55	.writeOAM = GBAVideoDummyRendererWriteOAM,
 56	.drawScanline = GBAVideoDummyRendererDrawScanline,
 57	.finishFrame = GBAVideoDummyRendererFinishFrame,
 58	.getPixels = GBAVideoDummyRendererGetPixels
 59};
 60
 61void GBAVideoInit(struct GBAVideo* video) {
 62	video->renderer = &dummyRenderer;
 63	video->vram = 0;
 64	video->frameskip = 0;
 65}
 66
 67void GBAVideoReset(struct GBAVideo* video) {
 68	if (video->p->memory.fullBios) {
 69		video->vcount = 0;
 70	} else {
 71		// TODO: Verify exact scanline hardware
 72		video->vcount = 0x7E;
 73	}
 74	video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
 75
 76	video->nextHblank = VIDEO_HDRAW_LENGTH;
 77	video->nextEvent = video->nextHblank;
 78	video->eventDiff = 0;
 79
 80	video->nextHblankIRQ = 0;
 81	video->nextVblankIRQ = 0;
 82	video->nextVcounterIRQ = 0;
 83
 84	video->frameCounter = 0;
 85	video->frameskipCounter = 0;
 86
 87	if (video->vram) {
 88		mappedMemoryFree(video->vram, SIZE_VRAM);
 89	}
 90	video->vram = anonymousMemoryMap(SIZE_VRAM);
 91	video->renderer->vram = video->vram;
 92
 93	memset(video->palette, 0, sizeof(video->palette));
 94	memset(video->oam.raw, 0, sizeof(video->oam.raw));
 95
 96	video->renderer->deinit(video->renderer);
 97	video->renderer->init(video->renderer);
 98}
 99
100void GBAVideoDeinit(struct GBAVideo* video) {
101	GBAVideoAssociateRenderer(video, &dummyRenderer);
102	mappedMemoryFree(video->vram, SIZE_VRAM);
103}
104
105void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
106	video->renderer->deinit(video->renderer);
107	video->renderer = renderer;
108	renderer->palette = video->palette;
109	renderer->vram = video->vram;
110	renderer->oam = &video->oam;
111	video->renderer->init(video->renderer);
112}
113
114int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
115	video->nextEvent -= cycles;
116	video->eventDiff += cycles;
117	if (video->nextEvent <= 0) {
118		int32_t lastEvent = video->nextEvent;
119		video->nextHblank -= video->eventDiff;
120		video->nextHblankIRQ -= video->eventDiff;
121		video->nextVcounterIRQ -= video->eventDiff;
122		video->eventDiff = 0;
123		uint16_t dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
124
125		if (GBARegisterDISPSTATIsInHblank(dispstat)) {
126			// End Hblank
127			dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
128			video->nextEvent = video->nextHblank;
129
130			++video->vcount;
131			if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
132				video->vcount = 0;
133			}
134			video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
135
136			if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
137				dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
138				if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
139					GBARaiseIRQ(video->p, IRQ_VCOUNTER);
140					video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
141				}
142			} else {
143				dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
144			}
145			video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
146
147			// Note: state may be recorded during callbacks, so ensure it is consistent!
148			switch (video->vcount) {
149			case 0:
150				GBAFrameStarted(video->p);
151				break;
152			case VIDEO_VERTICAL_PIXELS:
153				video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
154				if (video->frameskipCounter <= 0) {
155					video->renderer->finishFrame(video->renderer);
156				}
157				video->nextVblankIRQ = video->nextEvent + VIDEO_TOTAL_LENGTH;
158				GBAMemoryRunVblankDMAs(video->p, lastEvent);
159				if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
160					GBARaiseIRQ(video->p, IRQ_VBLANK);
161				}
162				GBAFrameEnded(video->p);
163				--video->frameskipCounter;
164				if (video->frameskipCounter < 0) {
165					mCoreSyncPostFrame(video->p->sync);
166					video->frameskipCounter = video->frameskip;
167				}
168				++video->frameCounter;
169				break;
170			case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
171				video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATClearInVblank(dispstat);
172				break;
173			}
174		} else {
175			// Begin Hblank
176			dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
177			video->nextEvent = video->nextHblank + VIDEO_HBLANK_LENGTH;
178			video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
179			video->nextHblankIRQ = video->nextHblank;
180
181			if (video->vcount < VIDEO_VERTICAL_PIXELS && video->frameskipCounter <= 0) {
182				video->renderer->drawScanline(video->renderer, video->vcount);
183			}
184
185			if (video->vcount < VIDEO_VERTICAL_PIXELS) {
186				GBAMemoryRunHblankDMAs(video->p, lastEvent);
187			}
188			if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
189				GBARaiseIRQ(video->p, IRQ_HBLANK);
190			}
191			video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
192		}
193	}
194	return video->nextEvent;
195}
196
197void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
198	video->p->memory.io[REG_DISPSTAT >> 1] &= 0x7;
199	video->p->memory.io[REG_DISPSTAT >> 1] |= value;
200
201	uint16_t dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
202
203	if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
204		// FIXME: this can be too late if we're in the middle of an Hblank
205		video->nextVcounterIRQ = video->nextHblank + VIDEO_HBLANK_LENGTH + (GBARegisterDISPSTATGetVcountSetting(dispstat) - video->vcount) * VIDEO_HORIZONTAL_LENGTH;
206		if (video->nextVcounterIRQ < video->nextEvent) {
207			video->nextVcounterIRQ += VIDEO_TOTAL_LENGTH;
208		}
209	}
210}
211
212static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
213	UNUSED(renderer);
214	// Nothing to do
215}
216
217static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) {
218	UNUSED(renderer);
219	// Nothing to do
220}
221
222static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
223	UNUSED(renderer);
224	// Nothing to do
225}
226
227static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
228	UNUSED(renderer);
229	UNUSED(address);
230	return value;
231}
232
233static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
234	UNUSED(renderer);
235	UNUSED(address);
236	// Nothing to do
237}
238
239static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
240	UNUSED(renderer);
241	UNUSED(address);
242	UNUSED(value);
243	// Nothing to do
244}
245
246static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
247	UNUSED(renderer);
248	UNUSED(oam);
249	// Nothing to do
250}
251
252static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
253	UNUSED(renderer);
254	UNUSED(y);
255	// Nothing to do
256}
257
258static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
259	UNUSED(renderer);
260	// Nothing to do
261}
262
263static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, unsigned* stride, const void** pixels) {
264	UNUSED(renderer);
265	UNUSED(stride);
266	UNUSED(pixels);
267	// Nothing to do
268}
269
270void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
271	memcpy(state->vram, video->renderer->vram, SIZE_VRAM);
272	memcpy(state->oam, video->oam.raw, SIZE_OAM);
273	memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
274	STORE_32(video->nextEvent, 0, &state->video.nextEvent);
275	STORE_32(video->eventDiff, 0, &state->video.eventDiff);
276	STORE_32(video->nextHblank, 0, &state->video.nextHblank);
277	STORE_32(video->nextHblankIRQ, 0, &state->video.nextHblankIRQ);
278	STORE_32(video->nextVblankIRQ, 0, &state->video.nextVblankIRQ);
279	STORE_32(video->nextVcounterIRQ, 0, &state->video.nextVcounterIRQ);
280	STORE_32(video->frameCounter, 0, &state->video.frameCounter);
281}
282
283void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
284	memcpy(video->renderer->vram, state->vram, SIZE_VRAM);
285	uint16_t value;
286	int i;
287	for (i = 0; i < SIZE_OAM; i += 2) {
288		LOAD_16(value, i, state->oam);
289		GBAStore16(video->p->cpu, BASE_OAM | i, value, 0);
290	}
291	for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
292		LOAD_16(value, i, state->pram);
293		GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, value, 0);
294	}
295	LOAD_32(video->nextEvent, 0, &state->video.nextEvent);
296	LOAD_32(video->eventDiff, 0, &state->video.eventDiff);
297	LOAD_32(video->nextHblank, 0, &state->video.nextHblank);
298	LOAD_32(video->nextHblankIRQ, 0, &state->video.nextHblankIRQ);
299	LOAD_32(video->nextVblankIRQ, 0, &state->video.nextVblankIRQ);
300	LOAD_32(video->nextVcounterIRQ, 0, &state->video.nextVcounterIRQ);
301	LOAD_32(video->frameCounter, 0, &state->video.frameCounter);
302	LOAD_16(video->vcount, REG_VCOUNT, state->io);
303}