all repos — mgba @ a1689c80a71813c3506eca2a87a4add5dbf3f53e

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