all repos — mgba @ ef742cb1282b13ae4a1df471c38e1445424e60e8

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