all repos — mgba @ 7551afc952498113047689d16c97447caea627da

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 _midHblank(struct mTiming*, void* context, uint32_t cyclesLate);
 35static void _startHdraw(struct mTiming*, void* context, uint32_t cyclesLate);
 36
 37MGBA_EXPORT const int GBAVideoObjSizes[16][2] = {
 38	{ 8, 8 },
 39	{ 16, 16 },
 40	{ 32, 32 },
 41	{ 64, 64 },
 42	{ 16, 8 },
 43	{ 32, 8 },
 44	{ 32, 16 },
 45	{ 64, 32 },
 46	{ 8, 16 },
 47	{ 8, 32 },
 48	{ 16, 32 },
 49	{ 32, 64 },
 50	{ 0, 0 },
 51	{ 0, 0 },
 52	{ 0, 0 },
 53	{ 0, 0 },
 54};
 55
 56void GBAVideoInit(struct GBAVideo* video) {
 57	video->renderer = NULL;
 58	video->vram = anonymousMemoryMap(SIZE_VRAM);
 59	video->frameskip = 0;
 60	video->event.name = "GBA Video";
 61	video->event.callback = NULL;
 62	video->event.context = video;
 63	video->event.priority = 8;
 64}
 65
 66void GBAVideoReset(struct GBAVideo* video) {
 67	int32_t nextEvent = VIDEO_HDRAW_LENGTH;
 68	if (video->p->memory.fullBios) {
 69		video->vcount = 0;
 70	} else {
 71		// TODO: Verify exact scanline on hardware
 72		video->vcount = 0x7E;
 73		nextEvent = 170;
 74	}
 75	video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
 76
 77	video->event.callback = _startHblank;
 78	mTimingSchedule(&video->p->timing, &video->event, nextEvent);
 79
 80	video->frameCounter = 0;
 81	video->frameskipCounter = 0;
 82	video->shouldStall = 0;
 83
 84	memset(video->palette, 0, sizeof(video->palette));
 85	memset(video->oam.raw, 0, sizeof(video->oam.raw));
 86
 87	if (!video->renderer) {
 88		mLOG(GBA_VIDEO, FATAL, "No renderer associated");
 89		return;
 90	}
 91	video->renderer->vram = video->vram;
 92	video->renderer->reset(video->renderer);
 93}
 94
 95void GBAVideoDeinit(struct GBAVideo* video) {
 96	video->renderer->deinit(video->renderer);
 97	mappedMemoryFree(video->vram, SIZE_VRAM);
 98}
 99
100void GBAVideoDummyRendererCreate(struct GBAVideoRenderer* renderer) {
101	static const struct GBAVideoRenderer dummyRenderer = {
102		.init = GBAVideoDummyRendererInit,
103		.reset = GBAVideoDummyRendererReset,
104		.deinit = GBAVideoDummyRendererDeinit,
105		.writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
106		.writeVRAM = GBAVideoDummyRendererWriteVRAM,
107		.writePalette = GBAVideoDummyRendererWritePalette,
108		.writeOAM = GBAVideoDummyRendererWriteOAM,
109		.drawScanline = GBAVideoDummyRendererDrawScanline,
110		.finishFrame = GBAVideoDummyRendererFinishFrame,
111		.getPixels = GBAVideoDummyRendererGetPixels,
112		.putPixels = GBAVideoDummyRendererPutPixels,
113	};
114	memcpy(renderer, &dummyRenderer, sizeof(*renderer));
115}
116
117void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
118	if (video->renderer) {
119		video->renderer->deinit(video->renderer);
120		renderer->cache = video->renderer->cache;
121	} else {
122		renderer->cache = NULL;
123	}
124	video->renderer = renderer;
125	renderer->palette = video->palette;
126	renderer->vram = video->vram;
127	renderer->oam = &video->oam;
128	video->renderer->init(video->renderer);
129}
130
131void _midHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
132	struct GBAVideo* video = context;
133	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
134	dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
135	video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
136	video->event.callback = _startHdraw;
137	mTimingSchedule(timing, &video->event, VIDEO_HBLANK_FLIP - cyclesLate);
138}
139
140void _startHdraw(struct mTiming* timing, void* context, uint32_t cyclesLate) {
141	struct GBAVideo* video = context;
142	video->event.callback = _startHblank;
143	mTimingSchedule(timing, &video->event, VIDEO_HDRAW_LENGTH - cyclesLate);
144
145	++video->vcount;
146	if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
147		video->vcount = 0;
148	}
149	video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
150
151	if (video->vcount < GBA_VIDEO_VERTICAL_PIXELS) {
152		if (video->frameskipCounter <= 0) {
153			video->renderer->drawScanline(video->renderer, video->vcount);
154		}
155		video->shouldStall = 1;
156	}
157
158	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
159	if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
160		dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
161		if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
162			GBARaiseIRQ(video->p, IRQ_VCOUNTER, cyclesLate);
163		}
164	} else {
165		dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
166	}
167	video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
168
169	// Note: state may be recorded during callbacks, so ensure it is consistent!
170	switch (video->vcount) {
171	case 0:
172		GBAFrameStarted(video->p);
173		break;
174	case GBA_VIDEO_VERTICAL_PIXELS:
175		video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
176		if (video->frameskipCounter <= 0) {
177			video->renderer->finishFrame(video->renderer);
178		}
179		GBADMARunVblank(video->p, -cyclesLate);
180		if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
181			GBARaiseIRQ(video->p, IRQ_VBLANK, cyclesLate);
182		}
183		GBAFrameEnded(video->p);
184		mCoreSyncPostFrame(video->p->sync);
185		--video->frameskipCounter;
186		if (video->frameskipCounter < 0) {
187			video->frameskipCounter = video->frameskip;
188		}
189		++video->frameCounter;
190		video->p->earlyExit = true;
191		break;
192	case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
193		video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATClearInVblank(dispstat);
194		break;
195	}
196}
197
198void _startHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
199	struct GBAVideo* video = context;
200	video->event.callback = _midHblank;
201	mTimingSchedule(timing, &video->event, VIDEO_HBLANK_LENGTH - VIDEO_HBLANK_FLIP - cyclesLate);
202
203	// Begin Hblank
204	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
205	dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
206
207	if (video->vcount < GBA_VIDEO_VERTICAL_PIXELS) {
208		GBADMARunHblank(video->p, -cyclesLate);
209	}
210	if (video->vcount >= 2 && video->vcount < GBA_VIDEO_VERTICAL_PIXELS + 2) {
211		GBADMARunDisplayStart(video->p, -cyclesLate);
212	}
213	if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
214		GBARaiseIRQ(video->p, IRQ_HBLANK, cyclesLate);
215	}
216	video->shouldStall = 0;
217	video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
218}
219
220void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
221	video->p->memory.io[REG_DISPSTAT >> 1] &= 0x7;
222	video->p->memory.io[REG_DISPSTAT >> 1] |= value;
223	// TODO: Does a VCounter IRQ trigger on write?
224}
225
226static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
227	UNUSED(renderer);
228	// Nothing to do
229}
230
231static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) {
232	UNUSED(renderer);
233	// Nothing to do
234}
235
236static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
237	UNUSED(renderer);
238	// Nothing to do
239}
240
241static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
242	if (renderer->cache) {
243		GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
244	}
245	switch (address) {
246	case REG_DISPCNT:
247		value &= 0xFFF7;
248		break;
249	case REG_BG0CNT:
250	case REG_BG1CNT:
251		value &= 0xDFFF;
252		break;
253	case REG_BG2CNT:
254	case REG_BG3CNT:
255		value &= 0xFFFF;
256		break;
257	case REG_BG0HOFS:
258	case REG_BG0VOFS:
259	case REG_BG1HOFS:
260	case REG_BG1VOFS:
261	case REG_BG2HOFS:
262	case REG_BG2VOFS:
263	case REG_BG3HOFS:
264	case REG_BG3VOFS:
265		value &= 0x01FF;
266		break;
267	case REG_BLDCNT:
268		value &= 0x3FFF;
269		break;
270	case REG_BLDALPHA:
271		value &= 0x1F1F;
272		break;
273	case REG_WININ:
274	case REG_WINOUT:
275		value &= 0x3F3F;
276		break;
277	default:
278		break;
279	}
280	return value;
281}
282
283static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
284	if (renderer->cache) {
285		mCacheSetWriteVRAM(renderer->cache, address);
286	}
287}
288
289static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
290	if (renderer->cache) {
291		mCacheSetWritePalette(renderer->cache, address >> 1, mColorFrom555(value));
292	}
293}
294
295static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
296	UNUSED(renderer);
297	UNUSED(oam);
298	// Nothing to do
299}
300
301static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
302	UNUSED(renderer);
303	UNUSED(y);
304	// Nothing to do
305}
306
307static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
308	UNUSED(renderer);
309	// Nothing to do
310}
311
312static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
313	UNUSED(renderer);
314	UNUSED(stride);
315	UNUSED(pixels);
316	// Nothing to do
317}
318
319static void GBAVideoDummyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
320	UNUSED(renderer);
321	UNUSED(stride);
322	UNUSED(pixels);
323	// Nothing to do
324}
325
326void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
327	memcpy(state->vram, video->vram, SIZE_VRAM);
328	memcpy(state->oam, video->oam.raw, SIZE_OAM);
329	memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
330	STORE_32(video->event.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextEvent);
331	int32_t flags = 0;
332	if (video->event.callback == _startHdraw) {
333		flags = GBASerializedVideoFlagsSetMode(flags, 1);
334	} else if (video->event.callback == _startHblank) {
335		flags = GBASerializedVideoFlagsSetMode(flags, 2);
336	} else if (video->event.callback == _midHblank) {
337		flags = GBASerializedVideoFlagsSetMode(flags, 3);
338	}
339	STORE_32(flags, 0, &state->video.flags);
340	STORE_32(video->frameCounter, 0, &state->video.frameCounter);
341}
342
343void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
344	memcpy(video->vram, state->vram, SIZE_VRAM);
345	uint16_t value;
346	int i;
347	for (i = 0; i < SIZE_OAM; i += 2) {
348		LOAD_16(value, i, state->oam);
349		GBAStore16(video->p->cpu, BASE_OAM | i, value, 0);
350	}
351	for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
352		LOAD_16(value, i, state->pram);
353		GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, value, 0);
354	}
355	LOAD_32(video->frameCounter, 0, &state->video.frameCounter);
356
357	video->shouldStall = 0;
358	int32_t flags;
359	LOAD_32(flags, 0, &state->video.flags);
360	GBARegisterDISPSTAT dispstat = state->io[REG_DISPSTAT >> 1];
361	switch (GBASerializedVideoFlagsGetMode(flags)) {
362	case 0:
363		if (GBARegisterDISPSTATIsInHblank(dispstat)) {
364			video->event.callback = _startHdraw;
365		} else {
366			video->event.callback = _startHblank;
367		}
368		break;
369	case 1:
370		video->event.callback = _startHdraw;
371		break;
372	case 2:
373		video->event.callback = _startHblank;
374		video->shouldStall = 1;
375		break;
376	case 3:
377		video->event.callback = _midHblank;
378		break;
379	}
380	uint32_t when;
381	LOAD_32(when, 0, &state->video.nextEvent);
382	mTimingSchedule(&video->p->timing, &video->event, when);
383
384	LOAD_16(video->vcount, REG_VCOUNT, state->io);
385	video->renderer->reset(video->renderer);
386}