all repos — mgba @ 7bc45acd619038b880825dfa8112ddba3a21cf2e

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
 56static struct GBAVideoRenderer dummyRenderer = {
 57	.init = GBAVideoDummyRendererInit,
 58	.reset = GBAVideoDummyRendererReset,
 59	.deinit = GBAVideoDummyRendererDeinit,
 60	.writeVideoRegister = GBAVideoDummyRendererWriteVideoRegister,
 61	.writeVRAM = GBAVideoDummyRendererWriteVRAM,
 62	.writePalette = GBAVideoDummyRendererWritePalette,
 63	.writeOAM = GBAVideoDummyRendererWriteOAM,
 64	.drawScanline = GBAVideoDummyRendererDrawScanline,
 65	.finishFrame = GBAVideoDummyRendererFinishFrame,
 66	.getPixels = GBAVideoDummyRendererGetPixels,
 67	.putPixels = GBAVideoDummyRendererPutPixels,
 68};
 69
 70void GBAVideoInit(struct GBAVideo* video) {
 71	video->renderer = &dummyRenderer;
 72	video->renderer->cache = NULL;
 73	video->vram = anonymousMemoryMap(SIZE_VRAM);
 74	video->frameskip = 0;
 75	video->event.name = "GBA Video";
 76	video->event.callback = NULL;
 77	video->event.context = video;
 78	video->event.priority = 8;
 79}
 80
 81void GBAVideoReset(struct GBAVideo* video) {
 82	int32_t nextEvent = VIDEO_HDRAW_LENGTH;
 83	if (video->p->memory.fullBios) {
 84		video->vcount = 0;
 85	} else {
 86		// TODO: Verify exact scanline on hardware
 87		video->vcount = 0x7E;
 88		nextEvent = 170;
 89	}
 90	video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
 91
 92	video->event.callback = _startHblank;
 93	mTimingSchedule(&video->p->timing, &video->event, nextEvent);
 94
 95	video->frameCounter = 0;
 96	video->frameskipCounter = 0;
 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->reset(video->renderer);
103}
104
105void GBAVideoDeinit(struct GBAVideo* video) {
106	video->renderer->deinit(video->renderer);
107	mappedMemoryFree(video->vram, SIZE_VRAM);
108}
109
110void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
111	video->renderer->deinit(video->renderer);
112	renderer->cache = video->renderer->cache;
113	video->renderer = renderer;
114	renderer->palette = video->palette;
115	renderer->vram = video->vram;
116	renderer->oam = &video->oam;
117	video->renderer->init(video->renderer);
118}
119
120void _midHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
121	struct GBAVideo* video = context;
122	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
123	dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
124	video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
125	video->event.callback = _startHdraw;
126	mTimingSchedule(timing, &video->event, VIDEO_HBLANK_FLIP - cyclesLate);
127}
128
129void _startHdraw(struct mTiming* timing, void* context, uint32_t cyclesLate) {
130	struct GBAVideo* video = context;
131	video->event.callback = _startHblank;
132	mTimingSchedule(timing, &video->event, VIDEO_HDRAW_LENGTH - cyclesLate);
133
134	++video->vcount;
135	if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
136		video->vcount = 0;
137	}
138	video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
139
140	if (video->vcount < GBA_VIDEO_VERTICAL_PIXELS && video->frameskipCounter <= 0) {
141		video->renderer->drawScanline(video->renderer, video->vcount);
142	}
143
144	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
145	if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
146		dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
147		if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
148			GBARaiseIRQ(video->p, IRQ_VCOUNTER, cyclesLate);
149		}
150	} else {
151		dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
152	}
153	video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
154
155	// Note: state may be recorded during callbacks, so ensure it is consistent!
156	switch (video->vcount) {
157	case 0:
158		GBAFrameStarted(video->p);
159		break;
160	case GBA_VIDEO_VERTICAL_PIXELS:
161		video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
162		if (video->frameskipCounter <= 0) {
163			video->renderer->finishFrame(video->renderer);
164		}
165		GBADMARunVblank(video->p, -cyclesLate);
166		if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
167			GBARaiseIRQ(video->p, IRQ_VBLANK, cyclesLate);
168		}
169		GBAFrameEnded(video->p);
170		mCoreSyncPostFrame(video->p->sync);
171		--video->frameskipCounter;
172		if (video->frameskipCounter < 0) {
173			video->frameskipCounter = video->frameskip;
174		}
175		++video->frameCounter;
176		break;
177	case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
178		video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATClearInVblank(dispstat);
179		break;
180	}
181}
182
183void _startHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
184	struct GBAVideo* video = context;
185	video->event.callback = _midHblank;
186	mTimingSchedule(timing, &video->event, VIDEO_HBLANK_LENGTH - VIDEO_HBLANK_FLIP - cyclesLate);
187
188	// Begin Hblank
189	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
190	dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
191
192	if (video->vcount < GBA_VIDEO_VERTICAL_PIXELS) {
193		GBADMARunHblank(video->p, -cyclesLate);
194	}
195	if (video->vcount >= 2 && video->vcount < GBA_VIDEO_VERTICAL_PIXELS + 2) {
196		GBADMARunDisplayStart(video->p, -cyclesLate);
197	}
198	if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
199		GBARaiseIRQ(video->p, IRQ_HBLANK, cyclesLate);
200	}
201	video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
202}
203
204void GBAVideoWriteDISPSTAT(struct GBAVideo* video, uint16_t value) {
205	video->p->memory.io[REG_DISPSTAT >> 1] &= 0x7;
206	video->p->memory.io[REG_DISPSTAT >> 1] |= value;
207	// TODO: Does a VCounter IRQ trigger on write?
208}
209
210static void GBAVideoDummyRendererInit(struct GBAVideoRenderer* renderer) {
211	UNUSED(renderer);
212	// Nothing to do
213}
214
215static void GBAVideoDummyRendererReset(struct GBAVideoRenderer* renderer) {
216	UNUSED(renderer);
217	// Nothing to do
218}
219
220static void GBAVideoDummyRendererDeinit(struct GBAVideoRenderer* renderer) {
221	UNUSED(renderer);
222	// Nothing to do
223}
224
225static uint16_t GBAVideoDummyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
226	if (renderer->cache) {
227		GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
228	}
229	switch (address) {
230	case REG_DISPCNT:
231		value &= 0xFFF7;
232		break;
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		mCacheSetWriteVRAM(renderer->cache, address);
270	}
271}
272
273static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
274	if (renderer->cache) {
275		mCacheSetWritePalette(renderer->cache, address >> 1, mColorFrom555(value));
276	}
277}
278
279static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
280	UNUSED(renderer);
281	UNUSED(oam);
282	// Nothing to do
283}
284
285static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
286	UNUSED(renderer);
287	UNUSED(y);
288	// Nothing to do
289}
290
291static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
292	UNUSED(renderer);
293	// Nothing to do
294}
295
296static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
297	UNUSED(renderer);
298	UNUSED(stride);
299	UNUSED(pixels);
300	// Nothing to do
301}
302
303static void GBAVideoDummyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
304	UNUSED(renderer);
305	UNUSED(stride);
306	UNUSED(pixels);
307	// Nothing to do
308}
309
310void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
311	memcpy(state->vram, video->vram, SIZE_VRAM);
312	memcpy(state->oam, video->oam.raw, SIZE_OAM);
313	memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
314	STORE_32(video->event.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextEvent);
315	int32_t flags = 0;
316	if (video->event.callback == _startHdraw) {
317		flags = GBASerializedVideoFlagsSetMode(flags, 1);
318	} else if (video->event.callback == _startHblank) {
319		flags = GBASerializedVideoFlagsSetMode(flags, 2);
320	} else if (video->event.callback == _midHblank) {
321		flags = GBASerializedVideoFlagsSetMode(flags, 3);
322	}
323	STORE_32(flags, 0, &state->video.flags);
324	STORE_32(video->frameCounter, 0, &state->video.frameCounter);
325}
326
327void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
328	memcpy(video->vram, state->vram, SIZE_VRAM);
329	uint16_t value;
330	int i;
331	for (i = 0; i < SIZE_OAM; i += 2) {
332		LOAD_16(value, i, state->oam);
333		GBAStore16(video->p->cpu, BASE_OAM | i, value, 0);
334	}
335	for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
336		LOAD_16(value, i, state->pram);
337		GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, value, 0);
338	}
339	LOAD_32(video->frameCounter, 0, &state->video.frameCounter);
340
341	int32_t flags;
342	LOAD_32(flags, 0, &state->video.flags);
343	GBARegisterDISPSTAT dispstat = state->io[REG_DISPSTAT >> 1];
344	switch (GBASerializedVideoFlagsGetMode(flags)) {
345	case 0:
346		if (GBARegisterDISPSTATIsInHblank(dispstat)) {
347			video->event.callback = _startHdraw;
348		} else {
349			video->event.callback = _startHblank;
350		}
351		break;
352	case 1:
353		video->event.callback = _startHdraw;
354		break;
355	case 2:
356		video->event.callback = _startHblank;
357		break;
358	case 3:
359		video->event.callback = _midHblank;
360		break;
361	}
362	uint32_t when;
363	LOAD_32(when, 0, &state->video.nextEvent);
364	mTimingSchedule(&video->p->timing, &video->event, when);
365
366	LOAD_16(video->vcount, REG_VCOUNT, state->io);
367	video->renderer->reset(video->renderer);
368}