all repos — mgba @ fc69cdce60f9aee4782e1b21d75fd7b6c2dfa19b

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->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	renderer->cache = video->renderer->cache;
108	video->renderer = renderer;
109	renderer->palette = video->palette;
110	renderer->vram = video->vram;
111	renderer->oam = &video->oam;
112	video->renderer->init(video->renderer);
113}
114
115int32_t GBAVideoProcessEvents(struct GBAVideo* video, int32_t cycles) {
116	video->nextEvent -= cycles;
117	video->eventDiff += cycles;
118	if (video->nextEvent <= 0) {
119		int32_t lastEvent = video->nextEvent;
120		video->nextHblank -= video->eventDiff;
121		video->eventDiff = 0;
122		uint16_t dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
123
124		if (GBARegisterDISPSTATIsInHblank(dispstat)) {
125			// End Hblank
126			dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
127			video->nextEvent = video->nextHblank;
128
129			++video->vcount;
130			if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
131				video->vcount = 0;
132			}
133			video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
134
135			if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
136				dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
137				if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
138					GBARaiseIRQ(video->p, IRQ_VCOUNTER);
139				}
140			} else {
141				dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
142			}
143			video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
144
145			// Note: state may be recorded during callbacks, so ensure it is consistent!
146			switch (video->vcount) {
147			case 0:
148				GBAFrameStarted(video->p);
149				break;
150			case VIDEO_VERTICAL_PIXELS:
151				video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
152				if (video->frameskipCounter <= 0) {
153					video->renderer->finishFrame(video->renderer);
154				}
155				GBAMemoryRunVblankDMAs(video->p, lastEvent);
156				if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
157					GBARaiseIRQ(video->p, IRQ_VBLANK);
158				}
159				GBAFrameEnded(video->p);
160				--video->frameskipCounter;
161				if (video->frameskipCounter < 0) {
162					mCoreSyncPostFrame(video->p->sync);
163					video->frameskipCounter = video->frameskip;
164				}
165				++video->frameCounter;
166				break;
167			case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
168				video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATClearInVblank(dispstat);
169				break;
170			}
171		} else {
172			// Begin Hblank
173			dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
174			video->nextEvent = video->nextHblank + VIDEO_HBLANK_LENGTH;
175			video->nextHblank = video->nextEvent + VIDEO_HDRAW_LENGTH;
176
177			if (video->vcount < VIDEO_VERTICAL_PIXELS && video->frameskipCounter <= 0) {
178				video->renderer->drawScanline(video->renderer, video->vcount);
179			}
180
181			if (video->vcount < VIDEO_VERTICAL_PIXELS) {
182				GBAMemoryRunHblankDMAs(video->p, lastEvent);
183			}
184			if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
185				GBARaiseIRQ(video->p, IRQ_HBLANK);
186			}
187			video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
188		}
189	}
190	return video->nextEvent;
191}
192
193void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
194	video->p->memory.io[REG_DISPSTAT >> 1] &= 0x7;
195	video->p->memory.io[REG_DISPSTAT >> 1] |= value;
196	// TODO: Does a VCounter IRQ trigger on write?
197}
198
199static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
200	UNUSED(renderer);
201	// Nothing to do
202}
203
204static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) {
205	UNUSED(renderer);
206	// Nothing to do
207}
208
209static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
210	UNUSED(renderer);
211	// Nothing to do
212}
213
214static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
215	UNUSED(renderer);
216	switch (address) {
217	case REG_BG0CNT:
218	case REG_BG1CNT:
219		value &= 0xDFFF;
220		break;
221	case REG_BG2CNT:
222	case REG_BG3CNT:
223		value &= 0xFFFF;
224		break;
225	case REG_BG0HOFS:
226	case REG_BG0VOFS:
227	case REG_BG1HOFS:
228	case REG_BG1VOFS:
229	case REG_BG2HOFS:
230	case REG_BG2VOFS:
231	case REG_BG3HOFS:
232	case REG_BG3VOFS:
233		value &= 0x01FF;
234		break;
235	case REG_BLDCNT:
236		value &= 0x3FFF;
237		break;
238	case REG_BLDALPHA:
239		value &= 0x1F1F;
240		break;
241	case REG_WININ:
242	case REG_WINOUT:
243		value &= 0x3F3F;
244		break;
245	default:
246		break;
247	}
248	return value;
249}
250
251static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
252	if (renderer->cache) {
253		GBAVideoTileCacheWriteVRAM(renderer->cache, address);
254	}
255}
256
257static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
258	UNUSED(value);
259	if (renderer->cache) {
260		GBAVideoTileCacheWritePalette(renderer->cache, address);
261	}
262}
263
264static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
265	UNUSED(renderer);
266	UNUSED(oam);
267	// Nothing to do
268}
269
270static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
271	UNUSED(renderer);
272	UNUSED(y);
273	// Nothing to do
274}
275
276static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
277	UNUSED(renderer);
278	// Nothing to do
279}
280
281static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
282	UNUSED(renderer);
283	UNUSED(stride);
284	UNUSED(pixels);
285	// Nothing to do
286}
287
288static void GBAVideoDummyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
289	UNUSED(renderer);
290	UNUSED(stride);
291	UNUSED(pixels);
292	// Nothing to do
293}
294
295void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
296	memcpy(state->vram, video->renderer->vram, SIZE_VRAM);
297	memcpy(state->oam, video->oam.raw, SIZE_OAM);
298	memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
299	STORE_32(video->nextEvent, 0, &state->video.nextEvent);
300	STORE_32(video->eventDiff, 0, &state->video.eventDiff);
301	STORE_32(video->nextHblank, 0, &state->video.nextHblank);
302	STORE_32(video->frameCounter, 0, &state->video.frameCounter);
303}
304
305void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
306	memcpy(video->renderer->vram, state->vram, SIZE_VRAM);
307	uint16_t value;
308	int i;
309	for (i = 0; i < SIZE_OAM; i += 2) {
310		LOAD_16(value, i, state->oam);
311		GBAStore16(video->p->cpu, BASE_OAM | i, value, 0);
312	}
313	for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
314		LOAD_16(value, i, state->pram);
315		GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, value, 0);
316	}
317	LOAD_32(video->nextEvent, 0, &state->video.nextEvent);
318	LOAD_32(video->eventDiff, 0, &state->video.eventDiff);
319	LOAD_32(video->nextHblank, 0, &state->video.nextHblank);
320	LOAD_32(video->frameCounter, 0, &state->video.frameCounter);
321	LOAD_16(video->vcount, REG_VCOUNT, state->io);
322	video->renderer->reset(video->renderer);
323}