all repos — mgba @ 7d360d6cb893f916ecdcece999303cd6a35e952c

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		memset(proxyRenderer->backend->vramBG, 0, sizeof(proxyRenderer->backend->vramBG));
 62		proxyRenderer->backend->vramBG[0] = &proxyRenderer->logger->vram[0x0000];
 63		proxyRenderer->backend->vramBG[1] = &proxyRenderer->logger->vram[0x2000];
 64		proxyRenderer->backend->vramBG[2] = &proxyRenderer->logger->vram[0x4000];
 65		proxyRenderer->backend->vramBG[3] = &proxyRenderer->logger->vram[0x6000];
 66		memset(proxyRenderer->backend->vramOBJ, 0, sizeof(proxyRenderer->backend->vramOBJ));
 67		proxyRenderer->backend->vramOBJ[0] = &proxyRenderer->logger->vram[0x8000];
 68		proxyRenderer->backend->vramOBJ[1] = &proxyRenderer->logger->vram[0xA000];
 69		proxyRenderer->backend->oam = (union GBAOAM*) proxyRenderer->logger->oam;
 70		proxyRenderer->backend->cache = NULL;
 71	}
 72}
 73
 74static void _reset(struct GBAVideoProxyRenderer* proxyRenderer) {
 75	memcpy(proxyRenderer->logger->oam, &proxyRenderer->d.oam->raw, SIZE_OAM);
 76	memcpy(proxyRenderer->logger->palette, proxyRenderer->d.palette, SIZE_PALETTE_RAM);
 77	memcpy(&proxyRenderer->logger->vram[0x0000], proxyRenderer->d.vramBG[0], 0x4000);
 78	memcpy(&proxyRenderer->logger->vram[0x2000], proxyRenderer->d.vramBG[1], 0x4000);
 79	memcpy(&proxyRenderer->logger->vram[0x4000], proxyRenderer->d.vramBG[2], 0x4000);
 80	memcpy(&proxyRenderer->logger->vram[0x6000], proxyRenderer->d.vramBG[3], 0x4000);
 81	memcpy(&proxyRenderer->logger->vram[0x8000], proxyRenderer->d.vramOBJ[0], 0x4000);
 82	memcpy(&proxyRenderer->logger->vram[0xA000], proxyRenderer->d.vramOBJ[1], 0x4000);
 83
 84	mVideoLoggerRendererReset(proxyRenderer->logger);
 85}
 86
 87void GBAVideoProxyRendererShim(struct GBAVideo* video, struct GBAVideoProxyRenderer* renderer) {
 88	if ((renderer->backend && video->renderer != renderer->backend) || video->renderer == &renderer->d) {
 89		return;
 90	}
 91	renderer->backend = video->renderer;
 92	video->renderer = &renderer->d;
 93	renderer->d.cache = renderer->backend->cache;
 94	renderer->d.palette = video->palette;
 95	memcpy(renderer->d.vramBG, renderer->backend->vramBG, sizeof(renderer->backend->vramBG));
 96	memcpy(renderer->d.vramOBJ, renderer->backend->vramOBJ, sizeof(renderer->backend->vramOBJ));
 97	renderer->d.oam = &video->oam;
 98	_init(renderer);
 99	_reset(renderer);
100}
101
102void GBAVideoProxyRendererUnshim(struct GBAVideo* video, struct GBAVideoProxyRenderer* renderer) {
103	if (video->renderer != &renderer->d) {
104		return;
105	}
106	renderer->backend->cache = video->renderer->cache;
107	video->renderer = renderer->backend;
108	renderer->backend->palette = video->palette;
109	memcpy(renderer->backend->vramBG, renderer->d.vramBG, sizeof(renderer->backend->vramBG));
110	memcpy(renderer->backend->vramOBJ, renderer->d.vramOBJ, sizeof(renderer->backend->vramOBJ));
111	renderer->backend->oam = &video->oam;
112
113	mVideoLoggerRendererDeinit(renderer->logger);
114}
115
116void GBAVideoProxyRendererInit(struct GBAVideoRenderer* renderer) {
117	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
118
119	_init(proxyRenderer);
120
121	proxyRenderer->backend->init(proxyRenderer->backend);
122}
123
124void GBAVideoProxyRendererReset(struct GBAVideoRenderer* renderer) {
125	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
126
127	_reset(proxyRenderer);
128
129	proxyRenderer->backend->reset(proxyRenderer->backend);
130}
131
132void GBAVideoProxyRendererDeinit(struct GBAVideoRenderer* renderer) {
133	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
134
135	proxyRenderer->backend->deinit(proxyRenderer->backend);
136
137	mVideoLoggerRendererDeinit(proxyRenderer->logger);
138}
139
140static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* item) {
141	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
142	switch (item->type) {
143	case DIRTY_REGISTER:
144		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, item->address, item->value);
145		break;
146	case DIRTY_PALETTE:
147		if (item->address < SIZE_PALETTE_RAM) {
148			logger->palette[item->address >> 1] = item->value;
149			proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
150		}
151		break;
152	case DIRTY_OAM:
153		if (item->address < SIZE_PALETTE_RAM) {
154			logger->oam[item->address] = item->value;
155			proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
156		}
157		break;
158	case DIRTY_VRAM:
159		if (item->address <= SIZE_VRAM - 0x1000) {
160			logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
161			proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
162		}
163		break;
164	case DIRTY_SCANLINE:
165		if (item->address < VIDEO_VERTICAL_PIXELS) {
166			proxyRenderer->backend->drawScanline(proxyRenderer->backend, item->address);
167		}
168		break;
169	case DIRTY_FRAME:
170		proxyRenderer->backend->finishFrame(proxyRenderer->backend);
171		break;
172	case DIRTY_FLUSH:
173		return false;
174	default:
175		return false;
176	}
177	return true;
178}
179
180static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
181	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
182	if (address < 0x10000) {
183		return &proxyRenderer->d.vramBG[address >> VRAM_BLOCK_OFFSET][(address & VRAM_BLOCK_MASK) >> 1];
184	} else {
185		return &proxyRenderer->d.vramOBJ[(address & 0x7FFF) >> VRAM_BLOCK_OFFSET][(address & VRAM_BLOCK_MASK) >> 1];
186	}
187}
188
189uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
190	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
191	switch (address) {
192	case REG_BG0CNT:
193	case REG_BG1CNT:
194	case REG_BG2CNT:
195	case REG_BG3CNT:
196		value &= 0xFFCF;
197		break;
198	case REG_BG0HOFS:
199	case REG_BG0VOFS:
200	case REG_BG1HOFS:
201	case REG_BG1VOFS:
202	case REG_BG2HOFS:
203	case REG_BG2VOFS:
204	case REG_BG3HOFS:
205	case REG_BG3VOFS:
206		value &= 0x01FF;
207		break;
208	}
209	if (address > REG_BLDY) {
210		return value;
211	}
212
213	mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
214	if (!proxyRenderer->logger->block) {
215		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
216	}
217	return value;
218}
219
220void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
221	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
222	mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
223	if (!proxyRenderer->logger->block) {
224		proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
225	}
226	if (renderer->cache) {
227		mTileCacheWriteVRAM(renderer->cache, address);
228	}
229}
230
231void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
232	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
233	mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
234	if (!proxyRenderer->logger->block) {
235		proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
236	}
237	if (renderer->cache) {
238		mTileCacheWritePalette(renderer->cache, address);
239	}
240}
241
242void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
243	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
244	if (!proxyRenderer->logger->block) {
245		proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
246	}
247	mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, proxyRenderer->d.oam->raw[oam]);
248}
249
250void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
251	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
252	if (!proxyRenderer->logger->block) {
253		proxyRenderer->backend->drawScanline(proxyRenderer->backend, y);
254	}
255	mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
256	if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
257		proxyRenderer->logger->wake(proxyRenderer->logger, y);
258	}
259}
260
261void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
262	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
263	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
264		proxyRenderer->logger->lock(proxyRenderer->logger);
265		proxyRenderer->logger->wait(proxyRenderer->logger);
266	}
267	proxyRenderer->backend->finishFrame(proxyRenderer->backend);
268	mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
269	mVideoLoggerRendererFlush(proxyRenderer->logger);
270	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
271		proxyRenderer->logger->unlock(proxyRenderer->logger);
272	}
273}
274
275static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
276	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
277	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
278		proxyRenderer->logger->lock(proxyRenderer->logger);
279		// Insert an extra item into the queue to make sure it gets flushed
280		mVideoLoggerRendererFlush(proxyRenderer->logger);
281		proxyRenderer->logger->wait(proxyRenderer->logger);
282	}
283	proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
284	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
285		proxyRenderer->logger->unlock(proxyRenderer->logger);
286	}
287}
288
289static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
290	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
291	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
292		proxyRenderer->logger->lock(proxyRenderer->logger);
293		// Insert an extra item into the queue to make sure it gets flushed
294		mVideoLoggerRendererFlush(proxyRenderer->logger);
295		proxyRenderer->logger->wait(proxyRenderer->logger);
296	}
297	proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
298	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
299		proxyRenderer->logger->unlock(proxyRenderer->logger);
300	}
301}