all repos — mgba @ ac7ae74822d2740f82b62a644e88fbed789ad2be

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	}
156}
157
158static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* item) {
159	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
160	switch (item->type) {
161	case DIRTY_REGISTER:
162		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, item->address, item->value);
163		break;
164	case DIRTY_PALETTE:
165		if (item->address < SIZE_PALETTE_RAM) {
166			logger->palette[item->address >> 1] = item->value;
167			proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
168		}
169		break;
170	case DIRTY_OAM:
171		if (item->address < SIZE_OAM) {
172			logger->oam[item->address] = item->value;
173			proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
174		}
175		break;
176	case DIRTY_VRAM:
177		if (item->address <= SIZE_VRAM - 0x1000) {
178			logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
179			proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
180		} else {
181			logger->readData(logger, NULL, 0x1000, true);
182		}
183		break;
184	case DIRTY_SCANLINE:
185		proxyRenderer->backend->disableBG[0] = proxyRenderer->d.disableBG[0];
186		proxyRenderer->backend->disableBG[1] = proxyRenderer->d.disableBG[1];
187		proxyRenderer->backend->disableBG[2] = proxyRenderer->d.disableBG[2];
188		proxyRenderer->backend->disableBG[3] = proxyRenderer->d.disableBG[3];
189		proxyRenderer->backend->disableOBJ = proxyRenderer->d.disableOBJ;
190		if (item->address < GBA_VIDEO_VERTICAL_PIXELS) {
191			proxyRenderer->backend->drawScanline(proxyRenderer->backend, item->address);
192		}
193		break;
194	case DIRTY_FRAME:
195		proxyRenderer->backend->finishFrame(proxyRenderer->backend);
196		break;
197	case DIRTY_FLUSH:
198		return false;
199	default:
200		return false;
201	}
202	return true;
203}
204
205static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
206	struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
207	return &proxyRenderer->d.vram[address >> 1];
208}
209
210uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
211	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
212	switch (address) {
213	case REG_DISPCNT:
214		value &= 0xFFF7;
215		break;
216	case REG_BG0CNT:
217	case REG_BG1CNT:
218		value &= 0xDFFF;
219		break;
220	case REG_BG2CNT:
221	case REG_BG3CNT:
222		value &= 0xFFFF;
223		break;
224	case REG_BG0HOFS:
225	case REG_BG0VOFS:
226	case REG_BG1HOFS:
227	case REG_BG1VOFS:
228	case REG_BG2HOFS:
229	case REG_BG2VOFS:
230	case REG_BG3HOFS:
231	case REG_BG3VOFS:
232		value &= 0x01FF;
233		break;
234	}
235	if (address > REG_BLDY) {
236		return value;
237	}
238
239	mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
240	if (!proxyRenderer->logger->block) {
241		proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
242	}
243	return value;
244}
245
246void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
247	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
248	mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
249	if (!proxyRenderer->logger->block) {
250		proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
251	}
252	if (renderer->cache) {
253		mCacheSetWriteVRAM(renderer->cache, address);
254	}
255}
256
257void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
258	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
259	mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
260	if (!proxyRenderer->logger->block) {
261		proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
262	}
263	if (renderer->cache) {
264		mCacheSetWritePalette(renderer->cache, address >> 1, mColorFrom555(value));
265	}
266}
267
268void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
269	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
270	if (!proxyRenderer->logger->block) {
271		proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
272	}
273	mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, proxyRenderer->d.oam->raw[oam]);
274}
275
276void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
277	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
278	if (!proxyRenderer->logger->block) {
279		proxyRenderer->backend->drawScanline(proxyRenderer->backend, y);
280	}
281	mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
282	if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
283		proxyRenderer->logger->wake(proxyRenderer->logger, y);
284	}
285}
286
287void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
288	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
289	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
290		proxyRenderer->logger->lock(proxyRenderer->logger);
291	}
292	if (!proxyRenderer->logger->block) {
293		proxyRenderer->backend->finishFrame(proxyRenderer->backend);
294	}
295	mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
296	mVideoLoggerRendererFlush(proxyRenderer->logger);
297	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
298		proxyRenderer->logger->unlock(proxyRenderer->logger);
299	}
300}
301
302static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
303	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
304	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
305		proxyRenderer->logger->lock(proxyRenderer->logger);
306		// Insert an extra item into the queue to make sure it gets flushed
307		mVideoLoggerRendererFlush(proxyRenderer->logger);
308	}
309	proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
310	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
311		proxyRenderer->logger->unlock(proxyRenderer->logger);
312	}
313}
314
315static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
316	struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
317	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
318		proxyRenderer->logger->lock(proxyRenderer->logger);
319		// Insert an extra item into the queue to make sure it gets flushed
320		mVideoLoggerRendererFlush(proxyRenderer->logger);
321	}
322	proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
323	if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
324		proxyRenderer->logger->unlock(proxyRenderer->logger);
325	}
326}