all repos — mgba @ 9e5070acf8034cf55541adf7d58c3ca51820c6ad

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