all repos — mgba @ 53d9e6b43213d9eed2c7bc5b38d16f4fdad37bc3

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/cache-set.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 void _handleEvent(struct mVideoLogger* logger, enum mVideoLoggerEvent event);
 25static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* packet);
 26static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address);
 27
 28void GBAVideoProxyRendererCreate(struct GBAVideoProxyRenderer* renderer, struct GBAVideoRenderer* backend) {
 29	renderer->d.init = GBAVideoProxyRendererInit;
 30	renderer->d.reset = GBAVideoProxyRendererReset;
 31	renderer->d.deinit = GBAVideoProxyRendererDeinit;
 32	renderer->d.writeVideoRegister = GBAVideoProxyRendererWriteVideoRegister;
 33	renderer->d.writeVRAM = GBAVideoProxyRendererWriteVRAM;
 34	renderer->d.writeOAM = GBAVideoProxyRendererWriteOAM;
 35	renderer->d.writePalette = GBAVideoProxyRendererWritePalette;
 36	renderer->d.drawScanline = GBAVideoProxyRendererDrawScanline;
 37	renderer->d.finishFrame = GBAVideoProxyRendererFinishFrame;
 38	renderer->d.getPixels = GBAVideoProxyRendererGetPixels;
 39	renderer->d.putPixels = GBAVideoProxyRendererPutPixels;
 40
 41	renderer->d.disableBG[0] = false;
 42	renderer->d.disableBG[1] = false;
 43	renderer->d.disableBG[2] = false;
 44	renderer->d.disableBG[3] = false;
 45	renderer->d.disableOBJ = false;
 46
 47	renderer->logger->context = renderer;
 48	renderer->logger->parsePacket = _parsePacket;
 49	renderer->logger->handleEvent = _handleEvent;
 50	renderer->logger->vramBlock = _vramBlock;
 51	renderer->logger->paletteSize = SIZE_PALETTE_RAM;
 52	renderer->logger->vramSize = SIZE_VRAM;
 53	renderer->logger->oamSize = SIZE_OAM;
 54
 55	renderer->backend = backend;
 56}
 57
 58static void _init(struct GBAVideoProxyRenderer* proxyRenderer) {
 59	mVideoLoggerRendererInit(proxyRenderer->logger);
 60
 61	if (proxyRenderer->logger->block) {
 62		proxyRenderer->backend->palette = proxyRenderer->logger->palette;
 63		proxyRenderer->backend->vram = proxyRenderer->logger->vram;
 64		proxyRenderer->backend->oam = (union GBAOAM*) proxyRenderer->logger->oam;
 65		proxyRenderer->backend->cache = NULL;
 66	}
 67}
 68
 69static void _reset(struct GBAVideoProxyRenderer* proxyRenderer) {
 70	memcpy(proxyRenderer->logger->oam, &proxyRenderer->d.oam->raw, SIZE_OAM);
 71	memcpy(proxyRenderer->logger->palette, proxyRenderer->d.palette, SIZE_PALETTE_RAM);
 72	memcpy(proxyRenderer->logger->vram, proxyRenderer->d.vram, SIZE_VRAM);
 73
 74	mVideoLoggerRendererReset(proxyRenderer->logger);
 75}
 76
 77void GBAVideoProxyRendererShim(struct GBAVideo* video, struct GBAVideoProxyRenderer* renderer) {
 78	if ((renderer->backend && video->renderer != renderer->backend) || video->renderer == &renderer->d) {
 79		return;
 80	}
 81	renderer->backend = video->renderer;
 82	video->renderer = &renderer->d;
 83	renderer->d.cache = renderer->backend->cache;
 84	renderer->d.palette = video->palette;
 85	renderer->d.vram = video->vram;
 86	renderer->d.oam = &video->oam;
 87	_init(renderer);
 88	_reset(renderer);
 89}
 90
 91void GBAVideoProxyRendererUnshim(struct GBAVideo* video, struct GBAVideoProxyRenderer* renderer) {
 92	if (video->renderer != &renderer->d) {
 93		return;
 94	}
 95	renderer->backend->cache = video->renderer->cache;
 96	video->renderer = renderer->backend;
 97	renderer->backend->palette = video->palette;
 98	renderer->backend->vram = video->vram;
 99	renderer->backend->oam = &video->oam;
100
101	mVideoLoggerRendererDeinit(renderer->logger);
102}
103
104void GBAVideoProxyRendererInit(struct GBAVideoRenderer* renderer) {
105	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
106
107	_init(proxyRenderer);
108	_reset(proxyRenderer);
109
110	if (!proxyRenderer->logger->block) {
111		proxyRenderer->backend->init(proxyRenderer->backend);
112	} else {
113		proxyRenderer->logger->postEvent(proxyRenderer->logger, LOGGER_EVENT_INIT);
114	}
115}
116
117void GBAVideoProxyRendererReset(struct GBAVideoRenderer* renderer) {
118	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
119
120	_reset(proxyRenderer);
121
122	if (!proxyRenderer->logger->block) {
123		proxyRenderer->backend->reset(proxyRenderer->backend);
124	} else {
125		proxyRenderer->logger->postEvent(proxyRenderer->logger, LOGGER_EVENT_RESET);
126	}
127}
128
129void GBAVideoProxyRendererDeinit(struct GBAVideoRenderer* renderer) {
130	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
131
132	if (!proxyRenderer->logger->block) {
133		proxyRenderer->backend->deinit(proxyRenderer->backend);
134	} else {
135		proxyRenderer->logger->postEvent(proxyRenderer->logger, LOGGER_EVENT_DEINIT);
136	}
137
138	mVideoLoggerRendererDeinit(proxyRenderer->logger);
139}
140
141static void _handleEvent(struct mVideoLogger* logger, enum mVideoLoggerEvent event) {
142	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
143	switch (event) {
144	default:
145		break;
146	case LOGGER_EVENT_INIT:
147		proxyRenderer->backend->init(proxyRenderer->backend);
148		break;
149	case LOGGER_EVENT_DEINIT:
150		proxyRenderer->backend->deinit(proxyRenderer->backend);
151		break;
152	case LOGGER_EVENT_RESET:
153		proxyRenderer->backend->reset(proxyRenderer->backend);
154		break;
155	case LOGGER_EVENT_GET_PIXELS:
156		proxyRenderer->backend->getPixels(proxyRenderer->backend, &logger->pixelStride, &logger->pixelBuffer);
157		break;
158	}
159}
160
161static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* item) {
162	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
163	switch (item->type) {
164	case DIRTY_REGISTER:
165		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, item->address, item->value);
166		break;
167	case DIRTY_PALETTE:
168		if (item->address < SIZE_PALETTE_RAM) {
169			logger->palette[item->address >> 1] = item->value;
170			proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
171		}
172		break;
173	case DIRTY_OAM:
174		if (item->address < SIZE_OAM) {
175			logger->oam[item->address] = item->value;
176			proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
177		}
178		break;
179	case DIRTY_VRAM:
180		if (item->address <= SIZE_VRAM - 0x1000) {
181			logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
182			proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
183		} else {
184			logger->readData(logger, NULL, 0x1000, true);
185		}
186		break;
187	case DIRTY_SCANLINE:
188		proxyRenderer->backend->disableBG[0] = proxyRenderer->d.disableBG[0];
189		proxyRenderer->backend->disableBG[1] = proxyRenderer->d.disableBG[1];
190		proxyRenderer->backend->disableBG[2] = proxyRenderer->d.disableBG[2];
191		proxyRenderer->backend->disableBG[3] = proxyRenderer->d.disableBG[3];
192		proxyRenderer->backend->disableOBJ = proxyRenderer->d.disableOBJ;
193		if (item->address < GBA_VIDEO_VERTICAL_PIXELS) {
194			proxyRenderer->backend->drawScanline(proxyRenderer->backend, item->address);
195		}
196		break;
197	case DIRTY_FRAME:
198		proxyRenderer->backend->finishFrame(proxyRenderer->backend);
199		break;
200	case DIRTY_FLUSH:
201		return false;
202	default:
203		return false;
204	}
205	return true;
206}
207
208static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
209	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
210	return &proxyRenderer->d.vram[address >> 1];
211}
212
213uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
214	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
215	switch (address) {
216	case REG_DISPCNT:
217		value &= 0xFFF7;
218		break;
219	case REG_BG0CNT:
220	case REG_BG1CNT:
221		value &= 0xDFFF;
222		break;
223	case REG_BG2CNT:
224	case REG_BG3CNT:
225		value &= 0xFFFF;
226		break;
227	case REG_BG0HOFS:
228	case REG_BG0VOFS:
229	case REG_BG1HOFS:
230	case REG_BG1VOFS:
231	case REG_BG2HOFS:
232	case REG_BG2VOFS:
233	case REG_BG3HOFS:
234	case REG_BG3VOFS:
235		value &= 0x01FF;
236		break;
237	}
238	if (address > REG_BLDY) {
239		return value;
240	}
241
242	mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
243	if (!proxyRenderer->logger->block) {
244		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
245	}
246	return value;
247}
248
249void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
250	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
251	mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
252	if (!proxyRenderer->logger->block) {
253		proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
254	}
255	if (renderer->cache) {
256		mCacheSetWriteVRAM(renderer->cache, address);
257	}
258}
259
260void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
261	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
262	mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
263	if (!proxyRenderer->logger->block) {
264		proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
265	}
266	if (renderer->cache) {
267		mCacheSetWritePalette(renderer->cache, address >> 1, mColorFrom555(value));
268	}
269}
270
271void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
272	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
273	if (!proxyRenderer->logger->block) {
274		proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
275	}
276	mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, proxyRenderer->d.oam->raw[oam]);
277}
278
279void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
280	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
281	if (!proxyRenderer->logger->block) {
282		proxyRenderer->backend->drawScanline(proxyRenderer->backend, y);
283	}
284	mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
285	if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
286		proxyRenderer->logger->wake(proxyRenderer->logger, y);
287	}
288}
289
290void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
291	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
292	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
293		proxyRenderer->logger->lock(proxyRenderer->logger);
294	}
295	if (!proxyRenderer->logger->block) {
296		proxyRenderer->backend->finishFrame(proxyRenderer->backend);
297	}
298	mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
299	mVideoLoggerRendererFlush(proxyRenderer->logger);
300	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
301		proxyRenderer->logger->unlock(proxyRenderer->logger);
302	}
303}
304
305static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
306	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
307	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
308		proxyRenderer->logger->lock(proxyRenderer->logger);
309		// Insert an extra item into the queue to make sure it gets flushed
310		mVideoLoggerRendererFlush(proxyRenderer->logger);
311		proxyRenderer->logger->postEvent(proxyRenderer->logger, LOGGER_EVENT_GET_PIXELS);
312		mVideoLoggerRendererFlush(proxyRenderer->logger);
313		proxyRenderer->logger->unlock(proxyRenderer->logger);
314		*pixels = proxyRenderer->logger->pixelBuffer;
315		*stride = proxyRenderer->logger->pixelStride;
316	} else {
317		proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
318	}
319}
320
321static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
322	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
323	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
324		proxyRenderer->logger->lock(proxyRenderer->logger);
325		// Insert an extra item into the queue to make sure it gets flushed
326		mVideoLoggerRendererFlush(proxyRenderer->logger);
327	}
328	proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
329	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
330		proxyRenderer->logger->unlock(proxyRenderer->logger);
331	}
332}