all repos — mgba @ e36f3c8211d6b10dbbd3e6c123773bc3b387cee8

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