all repos — mgba @ ccf66299e684f6dd1f27afb3e1ec32d2ac26ec7a

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