all repos — mgba @ 1b6531e32044481feeb0c631ef01bc61d5ccecc6

mGBA Game Boy Advance Emulator

src/gba/renderers/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		logger->palette[item->address >> 1] = item->value;
134		proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
135		break;
136	case DIRTY_OAM:
137		logger->oam[item->address] = item->value;
138		proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
139		break;
140	case DIRTY_VRAM:
141		logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
142		proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
143		break;
144	case DIRTY_SCANLINE:
145		proxyRenderer->backend->drawScanline(proxyRenderer->backend, item->address);
146		break;
147	case DIRTY_FRAME:
148		proxyRenderer->backend->finishFrame(proxyRenderer->backend);
149		break;
150	case DIRTY_FLUSH:
151		return false;
152	default:
153		return false;
154	}
155	return true;
156}
157
158static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
159	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
160	return &proxyRenderer->d.vram[address >> 1];
161}
162
163uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
164	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
165	switch (address) {
166	case REG_BG0CNT:
167	case REG_BG1CNT:
168	case REG_BG2CNT:
169	case REG_BG3CNT:
170		value &= 0xFFCF;
171		break;
172	case REG_BG0HOFS:
173	case REG_BG0VOFS:
174	case REG_BG1HOFS:
175	case REG_BG1VOFS:
176	case REG_BG2HOFS:
177	case REG_BG2VOFS:
178	case REG_BG3HOFS:
179	case REG_BG3VOFS:
180		value &= 0x01FF;
181		break;
182	}
183	if (address > REG_BLDY) {
184		return value;
185	}
186
187	mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
188	if (!proxyRenderer->logger->block) {
189		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
190	}
191	return value;
192}
193
194void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
195	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
196	mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
197	if (!proxyRenderer->logger->block) {
198		proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
199	}
200	if (renderer->cache) {
201		mTileCacheWriteVRAM(renderer->cache, address);
202	}
203}
204
205void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
206	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
207	mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
208	if (!proxyRenderer->logger->block) {
209		proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
210	}
211	if (renderer->cache) {
212		mTileCacheWritePalette(renderer->cache, address);
213	}
214}
215
216void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
217	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
218	if (!proxyRenderer->logger->block) {
219		proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
220	}
221	mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, proxyRenderer->d.oam->raw[oam]);
222}
223
224void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
225	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
226	if (!proxyRenderer->logger->block) {
227		proxyRenderer->backend->drawScanline(proxyRenderer->backend, y);
228	}
229	mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
230	if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
231		proxyRenderer->logger->wake(proxyRenderer->logger, y);
232	}
233}
234
235void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
236	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
237	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
238		proxyRenderer->logger->lock(proxyRenderer->logger);
239		proxyRenderer->logger->wait(proxyRenderer->logger);
240	}
241	mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
242	mVideoLoggerRendererFlush(proxyRenderer->logger);
243	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
244		proxyRenderer->logger->unlock(proxyRenderer->logger);
245	}
246}
247
248static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
249	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
250	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
251		proxyRenderer->logger->lock(proxyRenderer->logger);
252		// Insert an extra item into the queue to make sure it gets flushed
253		mVideoLoggerRendererFlush(proxyRenderer->logger);
254		proxyRenderer->logger->wait(proxyRenderer->logger);
255	}
256	proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
257	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
258		proxyRenderer->logger->unlock(proxyRenderer->logger);
259	}
260}
261
262static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
263	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
264	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
265		proxyRenderer->logger->lock(proxyRenderer->logger);
266		// Insert an extra item into the queue to make sure it gets flushed
267		mVideoLoggerRendererFlush(proxyRenderer->logger);
268		proxyRenderer->logger->wait(proxyRenderer->logger);
269	}
270	proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
271	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
272		proxyRenderer->logger->unlock(proxyRenderer->logger);
273	}
274}