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 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 _reset(proxyRenderer);
107
108 proxyRenderer->backend->init(proxyRenderer->backend);
109}
110
111void GBAVideoProxyRendererReset(struct GBAVideoRenderer* renderer) {
112 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
113
114 _reset(proxyRenderer);
115
116 proxyRenderer->backend->reset(proxyRenderer->backend);
117}
118
119void GBAVideoProxyRendererDeinit(struct GBAVideoRenderer* renderer) {
120 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
121
122 proxyRenderer->backend->deinit(proxyRenderer->backend);
123
124 mVideoLoggerRendererDeinit(proxyRenderer->logger);
125}
126
127static bool _parsePacket(struct mVideoLogger* logger, const struct mVideoLoggerDirtyInfo* item) {
128 struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
129 switch (item->type) {
130 case DIRTY_REGISTER:
131 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, item->address, item->value);
132 break;
133 case DIRTY_PALETTE:
134 if (item->address < SIZE_PALETTE_RAM) {
135 logger->palette[item->address >> 1] = item->value;
136 proxyRenderer->backend->writePalette(proxyRenderer->backend, item->address, item->value);
137 }
138 break;
139 case DIRTY_OAM:
140 if (item->address < SIZE_OAM) {
141 logger->oam[item->address] = item->value;
142 proxyRenderer->backend->writeOAM(proxyRenderer->backend, item->address);
143 }
144 break;
145 case DIRTY_VRAM:
146 if (item->address <= SIZE_VRAM - 0x1000) {
147 logger->readData(logger, &logger->vram[item->address >> 1], 0x1000, true);
148 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, item->address);
149 } else {
150 logger->readData(logger, NULL, 0x1000, true);
151 }
152 break;
153 case DIRTY_SCANLINE:
154 if (item->address < VIDEO_VERTICAL_PIXELS) {
155 proxyRenderer->backend->drawScanline(proxyRenderer->backend, item->address);
156 }
157 break;
158 case DIRTY_FRAME:
159 proxyRenderer->backend->finishFrame(proxyRenderer->backend);
160 break;
161 case DIRTY_FLUSH:
162 return false;
163 default:
164 return false;
165 }
166 return true;
167}
168
169static uint16_t* _vramBlock(struct mVideoLogger* logger, uint32_t address) {
170 struct GBAVideoProxyRenderer* proxyRenderer = logger->context;
171 return &proxyRenderer->d.vram[address >> 1];
172}
173
174uint16_t GBAVideoProxyRendererWriteVideoRegister(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
175 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
176 switch (address) {
177 case REG_DISPCNT:
178 value &= 0xFFF7;
179 break;
180 case REG_BG0CNT:
181 case REG_BG1CNT:
182 value &= 0xDFFF;
183 break;
184 case REG_BG2CNT:
185 case REG_BG3CNT:
186 value &= 0xFFFF;
187 break;
188 case REG_BG0HOFS:
189 case REG_BG0VOFS:
190 case REG_BG1HOFS:
191 case REG_BG1VOFS:
192 case REG_BG2HOFS:
193 case REG_BG2VOFS:
194 case REG_BG3HOFS:
195 case REG_BG3VOFS:
196 value &= 0x01FF;
197 break;
198 }
199 if (address > REG_BLDY) {
200 return value;
201 }
202
203 mVideoLoggerRendererWriteVideoRegister(proxyRenderer->logger, address, value);
204 if (!proxyRenderer->logger->block) {
205 proxyRenderer->backend->writeVideoRegister(proxyRenderer->backend, address, value);
206 }
207 return value;
208}
209
210void GBAVideoProxyRendererWriteVRAM(struct GBAVideoRenderer* renderer, uint32_t address) {
211 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
212 mVideoLoggerRendererWriteVRAM(proxyRenderer->logger, address);
213 if (!proxyRenderer->logger->block) {
214 proxyRenderer->backend->writeVRAM(proxyRenderer->backend, address);
215 }
216 if (renderer->cache) {
217 mCacheSetWriteVRAM(renderer->cache, address);
218 }
219}
220
221void GBAVideoProxyRendererWritePalette(struct GBAVideoRenderer* renderer, uint32_t address, uint16_t value) {
222 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
223 mVideoLoggerRendererWritePalette(proxyRenderer->logger, address, value);
224 if (!proxyRenderer->logger->block) {
225 proxyRenderer->backend->writePalette(proxyRenderer->backend, address, value);
226 }
227 if (renderer->cache) {
228 mCacheSetWritePalette(renderer->cache, address >> 1, mColorFrom555(value));
229 }
230}
231
232void GBAVideoProxyRendererWriteOAM(struct GBAVideoRenderer* renderer, uint32_t oam) {
233 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
234 if (!proxyRenderer->logger->block) {
235 proxyRenderer->backend->writeOAM(proxyRenderer->backend, oam);
236 }
237 mVideoLoggerRendererWriteOAM(proxyRenderer->logger, oam, proxyRenderer->d.oam->raw[oam]);
238}
239
240void GBAVideoProxyRendererDrawScanline(struct GBAVideoRenderer* renderer, int y) {
241 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
242 if (!proxyRenderer->logger->block) {
243 proxyRenderer->backend->drawScanline(proxyRenderer->backend, y);
244 }
245 mVideoLoggerRendererDrawScanline(proxyRenderer->logger, y);
246 if (proxyRenderer->logger->block && proxyRenderer->logger->wake) {
247 proxyRenderer->logger->wake(proxyRenderer->logger, y);
248 }
249}
250
251void GBAVideoProxyRendererFinishFrame(struct GBAVideoRenderer* renderer) {
252 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
253 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
254 proxyRenderer->logger->lock(proxyRenderer->logger);
255 }
256 if (!proxyRenderer->logger->block) {
257 proxyRenderer->backend->finishFrame(proxyRenderer->backend);
258 }
259 mVideoLoggerRendererFinishFrame(proxyRenderer->logger);
260 mVideoLoggerRendererFlush(proxyRenderer->logger);
261 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
262 proxyRenderer->logger->unlock(proxyRenderer->logger);
263 }
264}
265
266static void GBAVideoProxyRendererGetPixels(struct GBAVideoRenderer* renderer, size_t* stride, const void** pixels) {
267 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
268 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
269 proxyRenderer->logger->lock(proxyRenderer->logger);
270 // Insert an extra item into the queue to make sure it gets flushed
271 mVideoLoggerRendererFlush(proxyRenderer->logger);
272 }
273 proxyRenderer->backend->getPixels(proxyRenderer->backend, stride, pixels);
274 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
275 proxyRenderer->logger->unlock(proxyRenderer->logger);
276 }
277}
278
279static void GBAVideoProxyRendererPutPixels(struct GBAVideoRenderer* renderer, size_t stride, const void* pixels) {
280 struct GBAVideoProxyRenderer* proxyRenderer = (struct GBAVideoProxyRenderer*) renderer;
281 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
282 proxyRenderer->logger->lock(proxyRenderer->logger);
283 // Insert an extra item into the queue to make sure it gets flushed
284 mVideoLoggerRendererFlush(proxyRenderer->logger);
285 }
286 proxyRenderer->backend->putPixels(proxyRenderer->backend, stride, pixels);
287 if (proxyRenderer->logger->block && proxyRenderer->logger->wait) {
288 proxyRenderer->logger->unlock(proxyRenderer->logger);
289 }
290}