all repos — mgba @ a8f29906140feba11bdd4cd88574aee275f6f096

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/tile-cache.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/serialize.h>
 15
 16#include <mgba-util/memory.h>
 17
 18mLOG_DEFINE_CATEGORY(GBA_VIDEO, "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	video->event.priority = 8;
 77}
 78
 79void GBAVideoReset(struct GBAVideo* video) {
 80	if (video->p->memory.fullBios) {
 81		video->vcount = 0;
 82	} else {
 83		// TODO: Verify exact scanline hardware
 84		video->vcount = 0x7E;
 85	}
 86	video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
 87
 88	video->event.callback = _startHblank;
 89	mTimingSchedule(&video->p->timing, &video->event, VIDEO_HDRAW_LENGTH);
 90
 91	video->frameCounter = 0;
 92	video->frameskipCounter = 0;
 93
 94	if (video->vram) {
 95		mappedMemoryFree(video->vram, SIZE_VRAM);
 96	}
 97	video->vram = anonymousMemoryMap(SIZE_VRAM);
 98	video->renderer->vram = video->vram;
 99
100	memset(video->palette, 0, sizeof(video->palette));
101	memset(video->oam.raw, 0, sizeof(video->oam.raw));
102
103	video->renderer->deinit(video->renderer);
104	video->renderer->init(video->renderer);
105}
106
107void GBAVideoDeinit(struct GBAVideo* video) {
108	GBAVideoAssociateRenderer(video, &dummyRenderer);
109	mappedMemoryFree(video->vram, SIZE_VRAM);
110}
111
112void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
113	video->renderer->deinit(video->renderer);
114	renderer->cache = video->renderer->cache;
115	video->renderer = renderer;
116	renderer->palette = video->palette;
117	renderer->vram = video->vram;
118	renderer->oam = &video->oam;
119	video->renderer->init(video->renderer);
120}
121
122void _startHdraw(struct mTiming* timing, void* context, uint32_t cyclesLate) {
123	struct GBAVideo* video = context;
124	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
125	dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
126	video->event.callback = _startHblank;
127	mTimingSchedule(timing, &video->event, VIDEO_HDRAW_LENGTH - cyclesLate);
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		GBADMARunVblank(video->p, -cyclesLate);
156		if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
157			GBARaiseIRQ(video->p, IRQ_VBLANK);
158		}
159		GBAFrameEnded(video->p);
160		mCoreSyncPostFrame(video->p->sync);
161		--video->frameskipCounter;
162		if (video->frameskipCounter < 0) {
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}
172
173void _startHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
174	struct GBAVideo* video = context;
175	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
176	dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
177	video->event.callback = _startHdraw;
178	mTimingSchedule(timing, &video->event, VIDEO_HBLANK_LENGTH - cyclesLate);
179
180	// Begin Hblank
181	dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
182	if (video->vcount < VIDEO_VERTICAL_PIXELS && video->frameskipCounter <= 0) {
183		video->renderer->drawScanline(video->renderer, video->vcount);
184	}
185
186	if (video->vcount < VIDEO_VERTICAL_PIXELS) {
187		GBADMARunHblank(video->p, -cyclesLate);
188	}
189	if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
190		GBARaiseIRQ(video->p, IRQ_HBLANK);
191	}
192	video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
193}
194
195void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
196	video->p->memory.io[REG_DISPSTAT >> 1] &= 0x7;
197	video->p->memory.io[REG_DISPSTAT >> 1] |= value;
198	// TODO: Does a VCounter IRQ trigger on write?
199}
200
201static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
202	UNUSED(renderer);
203	// Nothing to do
204}
205
206static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) {
207	UNUSED(renderer);
208	// Nothing to do
209}
210
211static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
212	UNUSED(renderer);
213	// Nothing to do
214}
215
216static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
217	UNUSED(renderer);
218	switch (address) {
219	case REG_BG0CNT:
220	case REG_BG1CNT:
221		value &= 0xDFFF;
222		break;
223	case REG_BG2CNT:
224	case REG_BG3CNT:
225		value &= 0xFFFF;
226		break;
227	case REG_BG0HOFS:
228	case REG_BG0VOFS:
229	case REG_BG1HOFS:
230	case REG_BG1VOFS:
231	case REG_BG2HOFS:
232	case REG_BG2VOFS:
233	case REG_BG3HOFS:
234	case REG_BG3VOFS:
235		value &= 0x01FF;
236		break;
237	case REG_BLDCNT:
238		value &= 0x3FFF;
239		break;
240	case REG_BLDALPHA:
241		value &= 0x1F1F;
242		break;
243	case REG_WININ:
244	case REG_WINOUT:
245		value &= 0x3F3F;
246		break;
247	default:
248		break;
249	}
250	return value;
251}
252
253static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
254	if (renderer->cache) {
255		mTileCacheWriteVRAM(renderer->cache, address);
256	}
257}
258
259static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
260	UNUSED(value);
261	if (renderer->cache) {
262		mTileCacheWritePalette(renderer->cache, address);
263	}
264}
265
266static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
267	UNUSED(renderer);
268	UNUSED(oam);
269	// Nothing to do
270}
271
272static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
273	UNUSED(renderer);
274	UNUSED(y);
275	// Nothing to do
276}
277
278static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
279	UNUSED(renderer);
280	// Nothing to do
281}
282
283static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
284	UNUSED(renderer);
285	UNUSED(stride);
286	UNUSED(pixels);
287	// Nothing to do
288}
289
290static void GBAVideoDummyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
291	UNUSED(renderer);
292	UNUSED(stride);
293	UNUSED(pixels);
294	// Nothing to do
295}
296
297void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
298	memcpy(state->vram, video->vram, SIZE_VRAM);
299	memcpy(state->oam, video->oam.raw, SIZE_OAM);
300	memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
301	STORE_32(video->event.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextEvent);
302	STORE_32(video->frameCounter, 0, &state->video.frameCounter);
303}
304
305void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
306	memcpy(video->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->frameCounter, 0, &state->video.frameCounter);
318
319	uint32_t when;
320	LOAD_32(when, 0, &state->video.nextEvent);
321	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
322	if (GBARegisterDISPSTATIsInHblank(dispstat)) {
323		video->event.callback = _startHdraw;
324	} else {
325		video->event.callback = _startHblank;
326	}
327	mTimingSchedule(&video->p->timing, &video->event, when);
328
329	LOAD_16(video->vcount, REG_VCOUNT, state->io);
330	video->renderer->reset(video->renderer);
331}