all repos — mgba @ 7ae3640bb54302527bd7e1726a588f020ae455c6

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