all repos — mgba @ eeee6fe44eb11e77774c7bb3f08c2016682d33cd

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 = anonymousMemoryMap(SIZE_VRAM);
 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	int32_t nextEvent = VIDEO_HDRAW_LENGTH;
 82	if (video->p->memory.fullBios) {
 83		video->vcount = 0;
 84	} else {
 85		// TODO: Verify exact scanline on hardware
 86		video->vcount = 0x7E;
 87		nextEvent = 170;
 88	}
 89	video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
 90
 91	video->event.callback = _startHblank;
 92	mTimingSchedule(&video->p->timing, &video->event, nextEvent);
 93
 94	video->frameCounter = 0;
 95	video->frameskipCounter = 0;
 96	video->renderer->vram = video->vram;
 97
 98	memset(video->palette, 0, sizeof(video->palette));
 99	memset(video->oam.raw, 0, sizeof(video->oam.raw));
100
101	video->renderer->reset(video->renderer);
102}
103
104void GBAVideoDeinit(struct GBAVideo* video) {
105	video->renderer->deinit(video->renderer);
106	mappedMemoryFree(video->vram, SIZE_VRAM);
107}
108
109void GBAVideoAssociateRenderer(struct GBAVideo* video, struct GBAVideoRenderer* renderer) {
110	video->renderer->deinit(video->renderer);
111	renderer->cache = video->renderer->cache;
112	video->renderer = renderer;
113	renderer->palette = video->palette;
114	renderer->vram = video->vram;
115	renderer->oam = &video->oam;
116	video->renderer->init(video->renderer);
117}
118
119void _startHdraw(struct mTiming* timing, void* context, uint32_t cyclesLate) {
120	struct GBAVideo* video = context;
121	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
122	dispstat = GBARegisterDISPSTATClearInHblank(dispstat);
123	video->event.callback = _startHblank;
124	mTimingSchedule(timing, &video->event, VIDEO_HDRAW_LENGTH - cyclesLate);
125
126	++video->vcount;
127	if (video->vcount == VIDEO_VERTICAL_TOTAL_PIXELS) {
128		video->vcount = 0;
129	}
130	video->p->memory.io[REG_VCOUNT >> 1] = video->vcount;
131
132	if (video->vcount == GBARegisterDISPSTATGetVcountSetting(dispstat)) {
133		dispstat = GBARegisterDISPSTATFillVcounter(dispstat);
134		if (GBARegisterDISPSTATIsVcounterIRQ(dispstat)) {
135			GBARaiseIRQ(video->p, IRQ_VCOUNTER, cyclesLate);
136		}
137	} else {
138		dispstat = GBARegisterDISPSTATClearVcounter(dispstat);
139	}
140	video->p->memory.io[REG_DISPSTAT >> 1] = dispstat;
141
142	// Note: state may be recorded during callbacks, so ensure it is consistent!
143	switch (video->vcount) {
144	case 0:
145		GBAFrameStarted(video->p);
146		break;
147	case GBA_VIDEO_VERTICAL_PIXELS:
148		video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATFillInVblank(dispstat);
149		if (video->frameskipCounter <= 0) {
150			video->renderer->finishFrame(video->renderer);
151		}
152		GBADMARunVblank(video->p, -cyclesLate);
153		if (GBARegisterDISPSTATIsVblankIRQ(dispstat)) {
154			GBARaiseIRQ(video->p, IRQ_VBLANK, cyclesLate);
155		}
156		GBAFrameEnded(video->p);
157		mCoreSyncPostFrame(video->p->sync);
158		--video->frameskipCounter;
159		if (video->frameskipCounter < 0) {
160			video->frameskipCounter = video->frameskip;
161		}
162		++video->frameCounter;
163		break;
164	case VIDEO_VERTICAL_TOTAL_PIXELS - 1:
165		video->p->memory.io[REG_DISPSTAT >> 1] = GBARegisterDISPSTATClearInVblank(dispstat);
166		break;
167	}
168}
169
170void _startHblank(struct mTiming* timing, void* context, uint32_t cyclesLate) {
171	struct GBAVideo* video = context;
172	GBARegisterDISPSTAT dispstat = video->p->memory.io[REG_DISPSTAT >> 1];
173	dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
174	video->event.callback = _startHdraw;
175	mTimingSchedule(timing, &video->event, VIDEO_HBLANK_LENGTH - cyclesLate);
176
177	// Begin Hblank
178	dispstat = GBARegisterDISPSTATFillInHblank(dispstat);
179	if (video->vcount < GBA_VIDEO_VERTICAL_PIXELS && video->frameskipCounter <= 0) {
180		video->renderer->drawScanline(video->renderer, video->vcount);
181	}
182
183	if (video->vcount < GBA_VIDEO_VERTICAL_PIXELS) {
184		GBADMARunHblank(video->p, -cyclesLate);
185	}
186	if (video->vcount >= 2 && video->vcount < GBA_VIDEO_VERTICAL_PIXELS + 2) {
187		GBADMARunDisplayStart(video->p, -cyclesLate);
188	}
189	if (GBARegisterDISPSTATIsHblankIRQ(dispstat)) {
190		GBARaiseIRQ(video->p, IRQ_HBLANK, cyclesLate);
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	if (renderer->cache) {
218		GBAVideoCacheWriteVideoRegister(renderer->cache, address, value);
219	}
220	switch (address) {
221	case REG_DISPCNT:
222		value &= 0xFFF7;
223		break;
224	case REG_BG0CNT:
225	case REG_BG1CNT:
226		value &= 0xDFFF;
227		break;
228	case REG_BG2CNT:
229	case REG_BG3CNT:
230		value &= 0xFFFF;
231		break;
232	case REG_BG0HOFS:
233	case REG_BG0VOFS:
234	case REG_BG1HOFS:
235	case REG_BG1VOFS:
236	case REG_BG2HOFS:
237	case REG_BG2VOFS:
238	case REG_BG3HOFS:
239	case REG_BG3VOFS:
240		value &= 0x01FF;
241		break;
242	case REG_BLDCNT:
243		value &= 0x3FFF;
244		break;
245	case REG_BLDALPHA:
246		value &= 0x1F1F;
247		break;
248	case REG_WININ:
249	case REG_WINOUT:
250		value &= 0x3F3F;
251		break;
252	default:
253		break;
254	}
255	return value;
256}
257
258static void GBAVideoDummyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
259	if (renderer->cache) {
260		mCacheSetWriteVRAM(renderer->cache, address);
261	}
262}
263
264static void GBAVideoDummyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
265	if (renderer->cache) {
266		mCacheSetWritePalette(renderer->cache, address >> 1, mColorFrom555(value));
267	}
268}
269
270static void GBAVideoDummyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
271	UNUSED(renderer);
272	UNUSED(oam);
273	// Nothing to do
274}
275
276static void GBAVideoDummyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
277	UNUSED(renderer);
278	UNUSED(y);
279	// Nothing to do
280}
281
282static void GBAVideoDummyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
283	UNUSED(renderer);
284	// Nothing to do
285}
286
287static void GBAVideoDummyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
288	UNUSED(renderer);
289	UNUSED(stride);
290	UNUSED(pixels);
291	// Nothing to do
292}
293
294static void GBAVideoDummyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
295	UNUSED(renderer);
296	UNUSED(stride);
297	UNUSED(pixels);
298	// Nothing to do
299}
300
301void GBAVideoSerialize(const struct GBAVideo* video, struct GBASerializedState* state) {
302	memcpy(state->vram, video->vram, SIZE_VRAM);
303	memcpy(state->oam, video->oam.raw, SIZE_OAM);
304	memcpy(state->pram, video->palette, SIZE_PALETTE_RAM);
305	STORE_32(video->event.when - mTimingCurrentTime(&video->p->timing), 0, &state->video.nextEvent);
306	STORE_32(video->frameCounter, 0, &state->video.frameCounter);
307}
308
309void GBAVideoDeserialize(struct GBAVideo* video, const struct GBASerializedState* state) {
310	memcpy(video->vram, state->vram, SIZE_VRAM);
311	uint16_t value;
312	int i;
313	for (i = 0; i < SIZE_OAM; i += 2) {
314		LOAD_16(value, i, state->oam);
315		GBAStore16(video->p->cpu, BASE_OAM | i, value, 0);
316	}
317	for (i = 0; i < SIZE_PALETTE_RAM; i += 2) {
318		LOAD_16(value, i, state->pram);
319		GBAStore16(video->p->cpu, BASE_PALETTE_RAM | i, value, 0);
320	}
321	LOAD_32(video->frameCounter, 0, &state->video.frameCounter);
322
323	uint32_t when;
324	LOAD_32(when, 0, &state->video.nextEvent);
325	GBARegisterDISPSTAT dispstat = state->io[REG_DISPSTAT >> 1];
326	if (GBARegisterDISPSTATIsInHblank(dispstat)) {
327		video->event.callback = _startHdraw;
328	} else {
329		video->event.callback = _startHblank;
330	}
331	mTimingSchedule(&video->p->timing, &video->event, when);
332
333	LOAD_16(video->vcount, REG_VCOUNT, state->io);
334	video->renderer->reset(video->renderer);
335}