all repos — mgba @ df9616c15c58454332c0dfb7f2b37e0098f518b9

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