all repos — mgba @ 3021996a49cf72be55d15a1be4b24a13a8b751b9

mGBA Game Boy Advance Emulator

src/gba/extra/proxy.c (view raw)

  1/* Copyright (c) 2013-2017 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/renderers/proxy.h>
  7
  8#include <mgba/core/tile-cache.h>
  9#include <mgba/internal/gba/gba.h>
 10#include <mgba/internal/gba/io.h>
 11
 12static void GBAVideoProxyRendererInit(struct GBAVideoRenderer* renderer);
 13static void GBAVideoProxyRendererReset(struct GBAVideoRenderer* renderer);
 14static void GBAVideoProxyRendererDeinit(struct GBAVideoRenderer* renderer);
 15static uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 16static void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address);
 17static void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value);
 18static void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam);
 19static void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y);
 20static void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer);
 21static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels);
 22static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels);
 23
 24static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* packet);
 25static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address);
 26
 27void GBAVideoProxyRendererCreate(struct GBAVideoProxyRenderer* renderer, struct GBAVideoRenderer* backend) {
 28	renderer->d.init = GBAVideoProxyRendererInit;
 29	renderer->d.reset = GBAVideoProxyRendererReset;
 30	renderer->d.deinit = GBAVideoProxyRendererDeinit;
 31	renderer->d.writeVideoRegister = GBAVideoProxyRendererWriteVideoRegister;
 32	renderer->d.writeVRAM = GBAVideoProxyRendererWriteVRAM;
 33	renderer->d.writeOAM = GBAVideoProxyRendererWriteOAM;
 34	renderer->d.writePalette = GBAVideoProxyRendererWritePalette;
 35	renderer->d.drawScanline = GBAVideoProxyRendererDrawScanline;
 36	renderer->d.finishFrame = GBAVideoProxyRendererFinishFrame;
 37	renderer->d.getPixels = GBAVideoProxyRendererGetPixels;
 38	renderer->d.putPixels = GBAVideoProxyRendererPutPixels;
 39
 40	renderer->d.disableBG[0] = false;
 41	renderer->d.disableBG[1] = false;
 42	renderer->d.disableBG[2] = false;
 43	renderer->d.disableBG[3] = false;
 44	renderer->d.disableOBJ = false;
 45
 46	renderer->logger->context = renderer;
 47	renderer->logger->parsePacket = _parsePacket;
 48	renderer->logger->vramBlock = _vramBlock;
 49	renderer->logger->paletteSize = SIZE_PALETTE_RAM;
 50	renderer->logger->vramSize = SIZE_VRAM;
 51	renderer->logger->oamSize = SIZE_OAM;
 52
 53	renderer->backend = backend;
 54}
 55
 56static void _init(struct GBAVideoProxyRenderer* proxyRenderer) {
 57	mVideoLoggerRendererInit(proxyRenderer->logger);
 58
 59	if (proxyRenderer->logger->block) {
 60		proxyRenderer->backend->palette = proxyRenderer->logger->palette;
 61		proxyRenderer->backend->vram = proxyRenderer->logger->vram;
 62		proxyRenderer->backend->oam = (union GBAOAM*) proxyRenderer->logger->oam;
 63		proxyRenderer->backend->cache = NULL;
 64	}
 65}
 66
 67static void _reset(struct GBAVideoProxyRenderer* proxyRenderer) {
 68	memcpy(proxyRenderer->logger->oam, &proxyRenderer->d.oam->raw, SIZE_OAM);
 69	memcpy(proxyRenderer->logger->palette, proxyRenderer->d.palette, SIZE_PALETTE_RAM);
 70	memcpy(proxyRenderer->logger->vram, proxyRenderer->d.vram, SIZE_VRAM);
 71
 72	mVideoLoggerRendererReset(proxyRenderer->logger);
 73}
 74
 75void GBAVideoProxyRendererShim(struct GBAVideo* video, struct GBAVideoProxyRenderer* renderer) {
 76	if ((renderer->backend && video->renderer != renderer->backend) || video->renderer == &renderer->d) {
 77		return;
 78	}
 79	renderer->backend = video->renderer;
 80	video->renderer = &renderer->d;
 81	renderer->d.cache = renderer->backend->cache;
 82	renderer->d.palette = video->palette;
 83	renderer->d.vram = video->vram;
 84	renderer->d.oam = &video->oam;
 85	_init(renderer);
 86	_reset(renderer);
 87}
 88
 89void GBAVideoProxyRendererUnshim(struct GBAVideo* video, struct GBAVideoProxyRenderer* renderer) {
 90	if (video->renderer != &renderer->d) {
 91		return;
 92	}
 93	renderer->backend->cache = video->renderer->cache;
 94	video->renderer = renderer->backend;
 95	renderer->backend->palette = video->palette;
 96	renderer->backend->vram = video->vram;
 97	renderer->backend->oam = &video->oam;
 98
 99	mVideoLoggerRendererDeinit(renderer->logger);
100}
101
102void GBAVideoProxyRendererInit(struct GBAVideoRenderer* renderer) {
103	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
104
105	_init(proxyRenderer);
106
107	proxyRenderer->backend->init(proxyRenderer->backend);
108}
109
110void GBAVideoProxyRendererReset(struct GBAVideoRenderer* renderer) {
111	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
112
113	_reset(proxyRenderer);
114
115	proxyRenderer->backend->reset(proxyRenderer->backend);
116}
117
118void GBAVideoProxyRendererDeinit(struct GBAVideoRenderer* renderer) {
119	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
120
121	proxyRenderer->backend->deinit(proxyRenderer->backend);
122
123	mVideoLoggerRendererDeinit(proxyRenderer->logger);
124}
125
126static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* item) {
127	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
128	switch (item->type) {
129	case DIRTY_REGISTER:
130		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, item->address, item->value);
131		break;
132	case DIRTY_PALETTE:
133		if (item->address < SIZE_PALETTE_RAM) {
134			logger->palette[item->address >> 1] = item->value;
135			proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
136		}
137		break;
138	case DIRTY_OAM:
139		if (item->address < SIZE_PALETTE_RAM) {
140			logger->oam[item->address] = item->value;
141			proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
142		}
143		break;
144	case DIRTY_VRAM:
145		if (item->address <= SIZE_VRAM - 0x1000) {
146			logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
147			proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
148		}
149		break;
150	case DIRTY_SCANLINE:
151		if (item->address < VIDEO_VERTICAL_PIXELS) {
152			proxyRenderer->backend->drawScanline(proxyRenderer->backend, item->address);
153		}
154		break;
155	case DIRTY_FRAME:
156		proxyRenderer->backend->finishFrame(proxyRenderer->backend);
157		break;
158	case DIRTY_FLUSH:
159		return false;
160	default:
161		return false;
162	}
163	return true;
164}
165
166static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
167	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
168	return &proxyRenderer->d.vram[address >> 1];
169}
170
171uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
172	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
173	switch (address) {
174	case REG_BG0CNT:
175	case REG_BG1CNT:
176	case REG_BG2CNT:
177	case REG_BG3CNT:
178		value &= 0xFFCF;
179		break;
180	case REG_BG0HOFS:
181	case REG_BG0VOFS:
182	case REG_BG1HOFS:
183	case REG_BG1VOFS:
184	case REG_BG2HOFS:
185	case REG_BG2VOFS:
186	case REG_BG3HOFS:
187	case REG_BG3VOFS:
188		value &= 0x01FF;
189		break;
190	}
191	if (address > REG_BLDY) {
192		return value;
193	}
194
195	mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
196	if (!proxyRenderer->logger->block) {
197		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
198	}
199	return value;
200}
201
202void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
203	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
204	mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
205	if (!proxyRenderer->logger->block) {
206		proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
207	}
208	if (renderer->cache) {
209		mTileCacheWriteVRAM(renderer->cache, address);
210	}
211}
212
213void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
214	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
215	mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
216	if (!proxyRenderer->logger->block) {
217		proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
218	}
219	if (renderer->cache) {
220		mTileCacheWritePalette(renderer->cache, address);
221	}
222}
223
224void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
225	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
226	if (!proxyRenderer->logger->block) {
227		proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
228	}
229	mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, proxyRenderer->d.oam->raw[oam]);
230}
231
232void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
233	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
234	if (!proxyRenderer->logger->block) {
235		proxyRenderer->backend->drawScanline(proxyRenderer->backend, y);
236	}
237	mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
238	if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
239		proxyRenderer->logger->wake(proxyRenderer->logger, y);
240	}
241}
242
243void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
244	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
245	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
246		proxyRenderer->logger->lock(proxyRenderer->logger);
247		proxyRenderer->logger->wait(proxyRenderer->logger);
248	}
249	proxyRenderer->backend->finishFrame(proxyRenderer->backend);
250	mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
251	mVideoLoggerRendererFlush(proxyRenderer->logger);
252	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
253		proxyRenderer->logger->unlock(proxyRenderer->logger);
254	}
255}
256
257static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
258	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
259	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
260		proxyRenderer->logger->lock(proxyRenderer->logger);
261		// Insert an extra item into the queue to make sure it gets flushed
262		mVideoLoggerRendererFlush(proxyRenderer->logger);
263		proxyRenderer->logger->wait(proxyRenderer->logger);
264	}
265	proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
266	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
267		proxyRenderer->logger->unlock(proxyRenderer->logger);
268	}
269}
270
271static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
272	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
273	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
274		proxyRenderer->logger->lock(proxyRenderer->logger);
275		// Insert an extra item into the queue to make sure it gets flushed
276		mVideoLoggerRendererFlush(proxyRenderer->logger);
277		proxyRenderer->logger->wait(proxyRenderer->logger);
278	}
279	proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
280	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
281		proxyRenderer->logger->unlock(proxyRenderer->logger);
282	}
283}