src/gb/renderers/software.c (view raw)
1/* Copyright (c) 2013-2016 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/gb/renderers/software.h>
7
8#include <mgba/core/tile-cache.h>
9#include <mgba/internal/gb/io.h>
10#include <mgba-util/math.h>
11#include <mgba-util/memory.h>
12
13static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer, enum GBModel model);
14static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer);
15static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
16static void GBVideoSoftwareRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data);
17static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value);
18static void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address);
19static void GBVideoSoftwareRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam);
20static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax);
21static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
22static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer);
23static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels);
24static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels);
25
26static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int sx, int sy);
27static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y);
28
29static void _clearScreen(struct GBVideoSoftwareRenderer* renderer) {
30 size_t sgbOffset = 0;
31 if (renderer->model == GB_MODEL_SGB) {
32 sgbOffset = renderer->outputBufferStride * 40 + 48;
33 }
34 int y;
35 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
36 color_t* row = &renderer->outputBuffer[renderer->outputBufferStride * y + sgbOffset];
37 int x;
38 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; x += 4) {
39 row[x + 0] = renderer->palette[0];
40 row[x + 1] = renderer->palette[0];
41 row[x + 2] = renderer->palette[0];
42 row[x + 3] = renderer->palette[0];
43 }
44 }
45}
46
47static void _regenerateSGBBorder(struct GBVideoSoftwareRenderer* renderer) {
48 int i;
49 for (i = 0; i < 0x40; ++i) {
50 uint16_t color;
51 LOAD_16LE(color, 0x800 + i * 2, renderer->d.sgbMapRam);
52 renderer->d.writePalette(&renderer->d, i + 0x40, color);
53 }
54 int x, y;
55 for (y = 0; y < 224; ++y) {
56 for (x = 0; x < 256; x += 8) {
57 if (x >= 48 && x < 208 && y >= 40 && y < 104) {
58 continue;
59 }
60 uint16_t mapData;
61 LOAD_16LE(mapData, (x >> 2) + (y & ~7) * 8, renderer->d.sgbMapRam);
62 if (UNLIKELY(SGBBgAttributesGetTile(mapData) >= 0x100)) {
63 continue;
64 }
65
66 int localY = y & 0x7;
67 if (SGBBgAttributesIsYFlip(mapData)) {
68 localY = 7 - localY;
69 }
70 uint8_t tileData[4];
71 tileData[0] = renderer->d.sgbCharRam[(SGBBgAttributesGetTile(mapData) * 16 + localY) * 2 + 0x00];
72 tileData[1] = renderer->d.sgbCharRam[(SGBBgAttributesGetTile(mapData) * 16 + localY) * 2 + 0x01];
73 tileData[2] = renderer->d.sgbCharRam[(SGBBgAttributesGetTile(mapData) * 16 + localY) * 2 + 0x10];
74 tileData[3] = renderer->d.sgbCharRam[(SGBBgAttributesGetTile(mapData) * 16 + localY) * 2 + 0x11];
75
76 size_t base = y * renderer->outputBufferStride + x;
77 int p = SGBBgAttributesGetPalette(mapData) * 0x10;
78 if (SGBBgAttributesIsXFlip(mapData)) {
79 renderer->outputBuffer[base + 0] = renderer->palette[p | ((tileData[0] >> 0) & 0x1) | ((tileData[1] << 1) & 0x2) | ((tileData[2] << 2) & 0x4) | ((tileData[3] << 3) & 0x8)];
80 renderer->outputBuffer[base + 1] = renderer->palette[p | ((tileData[0] >> 1) & 0x1) | ((tileData[1] >> 0) & 0x2) | ((tileData[2] << 1) & 0x4) | ((tileData[3] << 2) & 0x8)];
81 renderer->outputBuffer[base + 2] = renderer->palette[p | ((tileData[0] >> 2) & 0x1) | ((tileData[1] >> 1) & 0x2) | ((tileData[2] >> 0) & 0x4) | ((tileData[3] << 1) & 0x8)];
82 renderer->outputBuffer[base + 3] = renderer->palette[p | ((tileData[0] >> 3) & 0x1) | ((tileData[1] >> 2) & 0x2) | ((tileData[2] >> 1) & 0x4) | ((tileData[3] >> 0) & 0x8)];
83 renderer->outputBuffer[base + 4] = renderer->palette[p | ((tileData[0] >> 4) & 0x1) | ((tileData[1] >> 3) & 0x2) | ((tileData[2] >> 2) & 0x4) | ((tileData[3] >> 1) & 0x8)];
84 renderer->outputBuffer[base + 5] = renderer->palette[p | ((tileData[0] >> 5) & 0x1) | ((tileData[1] >> 4) & 0x2) | ((tileData[2] >> 3) & 0x4) | ((tileData[3] >> 2) & 0x8)];
85 renderer->outputBuffer[base + 6] = renderer->palette[p | ((tileData[0] >> 6) & 0x1) | ((tileData[1] >> 5) & 0x2) | ((tileData[2] >> 4) & 0x4) | ((tileData[3] >> 3) & 0x8)];
86 renderer->outputBuffer[base + 7] = renderer->palette[p | ((tileData[0] >> 7) & 0x1) | ((tileData[1] >> 6) & 0x2) | ((tileData[2] >> 5) & 0x4) | ((tileData[3] >> 4) & 0x8)];
87 } else {
88 renderer->outputBuffer[base + 0] = renderer->palette[p | ((tileData[0] >> 7) & 0x1) | ((tileData[1] >> 6) & 0x2) | ((tileData[2] >> 5) & 0x4) | ((tileData[3] >> 4) & 0x8)];
89 renderer->outputBuffer[base + 1] = renderer->palette[p | ((tileData[0] >> 6) & 0x1) | ((tileData[1] >> 5) & 0x2) | ((tileData[2] >> 4) & 0x4) | ((tileData[3] >> 3) & 0x8)];
90 renderer->outputBuffer[base + 2] = renderer->palette[p | ((tileData[0] >> 5) & 0x1) | ((tileData[1] >> 4) & 0x2) | ((tileData[2] >> 3) & 0x4) | ((tileData[3] >> 2) & 0x8)];
91 renderer->outputBuffer[base + 3] = renderer->palette[p | ((tileData[0] >> 4) & 0x1) | ((tileData[1] >> 3) & 0x2) | ((tileData[2] >> 2) & 0x4) | ((tileData[3] >> 1) & 0x8)];
92 renderer->outputBuffer[base + 4] = renderer->palette[p | ((tileData[0] >> 3) & 0x1) | ((tileData[1] >> 2) & 0x2) | ((tileData[2] >> 1) & 0x4) | ((tileData[3] >> 0) & 0x8)];
93 renderer->outputBuffer[base + 5] = renderer->palette[p | ((tileData[0] >> 2) & 0x1) | ((tileData[1] >> 1) & 0x2) | ((tileData[2] >> 0) & 0x4) | ((tileData[3] << 1) & 0x8)];
94 renderer->outputBuffer[base + 6] = renderer->palette[p | ((tileData[0] >> 1) & 0x1) | ((tileData[1] >> 0) & 0x2) | ((tileData[2] << 1) & 0x4) | ((tileData[3] << 2) & 0x8)];
95 renderer->outputBuffer[base + 7] = renderer->palette[p | ((tileData[0] >> 0) & 0x1) | ((tileData[1] << 1) & 0x2) | ((tileData[2] << 2) & 0x4) | ((tileData[3] << 3) & 0x8)];
96 }
97 }
98 }
99}
100
101static inline void _setAttribute(uint8_t* sgbAttributes, unsigned x, unsigned y, int palette) {
102 int p = sgbAttributes[(x >> 2) + 5 * y];
103 p &= ~(3 << (2 * (3 - (x & 3))));
104 p |= palette << (2 * (3 - (x & 3)));
105 sgbAttributes[(x >> 2) + 5 * y] = p;
106}
107
108static void _parseAttrBlock(struct GBVideoSoftwareRenderer* renderer, int start) {
109 uint8_t block[6];
110 if (start < 0) {
111 memcpy(block, renderer->sgbPartialDataSet, -start);
112 memcpy(&block[-start], renderer->sgbPacket, 6 + start);
113 } else {
114 memcpy(block, &renderer->sgbPacket[start], 6);
115 }
116 unsigned x0 = block[2];
117 unsigned x1 = block[4];
118 unsigned y0 = block[3];
119 unsigned y1 = block[5];
120 unsigned x, y;
121 int pIn = block[1] & 3;
122 int pPerim = (block[1] >> 2) & 3;
123 int pOut = (block[1] >> 4) & 3;
124
125 for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS / 8; ++y) {
126 for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS / 8; ++x) {
127 if (y > y0 && y < y1 && x > x0 && x < x1) {
128 if (block[0] & 1) {
129 _setAttribute(renderer->d.sgbAttributes, x, y, pIn);
130 }
131 } else if (y < y0 || y > y1 || x < x0 || x > x1) {
132 if (block[0] & 4) {
133 _setAttribute(renderer->d.sgbAttributes, x, y, pOut);
134 }
135 } else {
136 if (block[0] & 2) {
137 _setAttribute(renderer->d.sgbAttributes, x, y, pPerim);
138 } else if (block[0] & 1) {
139 _setAttribute(renderer->d.sgbAttributes, x, y, pIn);
140 } else if (block[0] & 4) {
141 _setAttribute(renderer->d.sgbAttributes, x, y, pOut);
142 }
143 }
144 }
145 }
146}
147
148void GBVideoSoftwareRendererCreate(struct GBVideoSoftwareRenderer* renderer) {
149 renderer->d.init = GBVideoSoftwareRendererInit;
150 renderer->d.deinit = GBVideoSoftwareRendererDeinit;
151 renderer->d.writeVideoRegister = GBVideoSoftwareRendererWriteVideoRegister;
152 renderer->d.writeSGBPacket = GBVideoSoftwareRendererWriteSGBPacket;
153 renderer->d.writePalette = GBVideoSoftwareRendererWritePalette;
154 renderer->d.writeVRAM = GBVideoSoftwareRendererWriteVRAM;
155 renderer->d.writeOAM = GBVideoSoftwareRendererWriteOAM;
156 renderer->d.drawRange = GBVideoSoftwareRendererDrawRange;
157 renderer->d.finishScanline = GBVideoSoftwareRendererFinishScanline;
158 renderer->d.finishFrame = GBVideoSoftwareRendererFinishFrame;
159 renderer->d.getPixels = GBVideoSoftwareRendererGetPixels;
160 renderer->d.putPixels = GBVideoSoftwareRendererPutPixels;
161
162 renderer->d.disableBG = false;
163 renderer->d.disableOBJ = false;
164 renderer->d.disableWIN = false;
165
166 renderer->temporaryBuffer = 0;
167}
168
169static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer, enum GBModel model) {
170 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
171 softwareRenderer->lcdc = 0;
172 softwareRenderer->scy = 0;
173 softwareRenderer->scx = 0;
174 softwareRenderer->wy = 0;
175 softwareRenderer->currentWy = 0;
176 softwareRenderer->wx = 0;
177 softwareRenderer->model = model;
178 softwareRenderer->sgbTransfer = 0;
179 softwareRenderer->sgbCommandHeader = 0;
180 int i;
181 for (i = 0; i < 64; ++i) {
182 softwareRenderer->lookup[i] = i;
183 softwareRenderer->lookup[i] = i;
184 softwareRenderer->lookup[i] = i;
185 softwareRenderer->lookup[i] = i;
186 }
187}
188
189static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
190 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
191 UNUSED(softwareRenderer);
192}
193
194static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
195 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
196 switch (address) {
197 case REG_LCDC:
198 softwareRenderer->lcdc = value;
199 break;
200 case REG_SCY:
201 softwareRenderer->scy = value;
202 break;
203 case REG_SCX:
204 softwareRenderer->scx = value;
205 break;
206 case REG_WY:
207 softwareRenderer->wy = value;
208 break;
209 case REG_WX:
210 softwareRenderer->wx = value;
211 break;
212 case REG_BGP:
213 softwareRenderer->lookup[0] = value & 3;
214 softwareRenderer->lookup[1] = (value >> 2) & 3;
215 softwareRenderer->lookup[2] = (value >> 4) & 3;
216 softwareRenderer->lookup[3] = (value >> 6) & 3;
217 break;
218 case REG_OBP0:
219 softwareRenderer->lookup[0x20 + 0] = value & 3;
220 softwareRenderer->lookup[0x20 + 1] = (value >> 2) & 3;
221 softwareRenderer->lookup[0x20 + 2] = (value >> 4) & 3;
222 softwareRenderer->lookup[0x20 + 3] = (value >> 6) & 3;
223 break;
224 case REG_OBP1:
225 softwareRenderer->lookup[0x24 + 0] = value & 3;
226 softwareRenderer->lookup[0x24 + 1] = (value >> 2) & 3;
227 softwareRenderer->lookup[0x24 + 2] = (value >> 4) & 3;
228 softwareRenderer->lookup[0x24 + 3] = (value >> 6) & 3;
229 break;
230 }
231 return value;
232}
233
234static void GBVideoSoftwareRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
235 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
236 memcpy(softwareRenderer->sgbPacket, data, sizeof(softwareRenderer->sgbPacket));
237 int i;
238 if (!(softwareRenderer->sgbCommandHeader & 7)) {
239 softwareRenderer->sgbCommandHeader = data[0];
240 softwareRenderer->sgbPacketId = 0;
241 softwareRenderer->sgbTransfer = 0;
242 }
243 --softwareRenderer->sgbCommandHeader;
244 ++softwareRenderer->sgbPacketId;
245 int set;
246 switch (softwareRenderer->sgbCommandHeader >> 3) {
247 case SGB_PAL_SET:
248 softwareRenderer->sgbPacket[1] = data[9];
249 if (!(data[9] & 0x80)) {
250 break;
251 }
252 // Fall through
253 case SGB_ATTR_SET:
254 set = softwareRenderer->sgbPacket[1] & 0x3F;
255 if (set <= 0x2C) {
256 memcpy(renderer->sgbAttributes, &renderer->sgbAttributeFiles[set * 90], 90);
257 }
258 break;
259 case SGB_ATTR_BLK:
260 if (softwareRenderer->sgbPacketId == 1) {
261 softwareRenderer->sgbDataSets = softwareRenderer->sgbPacket[1];
262 i = 2;
263 } else {
264 i = (9 - softwareRenderer->sgbPacketId) % 3 * -2;
265 }
266 for (; i <= 10 && softwareRenderer->sgbDataSets; i += 6, --softwareRenderer->sgbDataSets) {
267 _parseAttrBlock(softwareRenderer, i);
268 }
269 if (i < 16 && softwareRenderer->sgbDataSets) {
270 memcpy(softwareRenderer->sgbPartialDataSet, &softwareRenderer->sgbPacket[i], 16 - i);
271 }
272 break;
273 case SGB_ATRC_EN:
274 _regenerateSGBBorder(softwareRenderer);
275 break;
276 }
277}
278
279static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
280 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
281 color_t color = mColorFrom555(value);
282 softwareRenderer->palette[index] = color;
283 if (renderer->cache) {
284 mTileCacheWritePalette(renderer->cache, index << 1);
285 }
286}
287
288static void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
289 if (renderer->cache) {
290 mTileCacheWriteVRAM(renderer->cache, address);
291 }
292}
293
294static void GBVideoSoftwareRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
295 UNUSED(renderer);
296 UNUSED(oam);
297 // Nothing to do
298}
299
300static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
301 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
302 uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
303 if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
304 maps += GB_SIZE_MAP;
305 }
306 if (softwareRenderer->d.disableBG) {
307 memset(&softwareRenderer->row[startX], 0, endX - startX);
308 }
309 if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) || softwareRenderer->model >= GB_MODEL_CGB) {
310 if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && endX >= softwareRenderer->wx - 7) {
311 if (softwareRenderer->wx - 7 > 0 && !softwareRenderer->d.disableBG) {
312 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, softwareRenderer->wx - 7, softwareRenderer->scx, softwareRenderer->scy + y);
313 }
314
315 maps = &softwareRenderer->d.vram[GB_BASE_MAP];
316 if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
317 maps += GB_SIZE_MAP;
318 }
319 if (!softwareRenderer->d.disableWIN) {
320 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, 7 - softwareRenderer->wx, softwareRenderer->currentWy);
321 }
322 } else if (!softwareRenderer->d.disableBG) {
323 GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, softwareRenderer->scx, softwareRenderer->scy + y);
324 }
325 } else if (!softwareRenderer->d.disableBG) {
326 memset(&softwareRenderer->row[startX], 0, endX - startX);
327 }
328
329 if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc) && !softwareRenderer->d.disableOBJ) {
330 size_t i;
331 for (i = 0; i < oamMax; ++i) {
332 GBVideoSoftwareRendererDrawObj(softwareRenderer, &obj[i], startX, endX, y);
333 }
334 }
335
336 size_t sgbOffset = 0;
337 if (softwareRenderer->model == GB_MODEL_SGB) {
338 sgbOffset = softwareRenderer->outputBufferStride * 40 + 48;
339 }
340 color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + sgbOffset];
341 int x;
342 int p = 0;
343 switch (softwareRenderer->d.sgbRenderMode) {
344 case 0:
345 if (softwareRenderer->model == GB_MODEL_SGB) {
346 p = softwareRenderer->d.sgbAttributes[(startX >> 5) + 5 * (y >> 3)];
347 p >>= 6 - ((x / 4) & 0x6);
348 p &= 3;
349 p <<= 2;
350 }
351 for (x = startX; x < ((startX + 7) & ~7) && x < endX; ++x) {
352 row[x] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
353 }
354 for (; x + 7 < (endX & ~7); x += 8) {
355 if (softwareRenderer->model == GB_MODEL_SGB) {
356 p = softwareRenderer->d.sgbAttributes[(x >> 5) + 5 * (y >> 3)];
357 p >>= 6 - ((x / 4) & 0x6);
358 p &= 3;
359 p <<= 2;
360 }
361 row[x + 0] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
362 row[x + 1] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 1] & 0x7F]];
363 row[x + 2] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 2] & 0x7F]];
364 row[x + 3] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 3] & 0x7F]];
365 row[x + 4] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 4] & 0x7F]];
366 row[x + 5] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 5] & 0x7F]];
367 row[x + 6] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 6] & 0x7F]];
368 row[x + 7] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 7] & 0x7F]];
369 }
370 if (softwareRenderer->model == GB_MODEL_SGB) {
371 p = softwareRenderer->d.sgbAttributes[(x >> 5) + 5 * (y >> 3)];
372 p >>= 6 - ((x / 4) & 0x6);
373 p &= 3;
374 p <<= 2;
375 }
376 for (; x < endX; ++x) {
377 row[x] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
378 }
379 break;
380 case 1:
381 break;
382 case 2:
383 for (x = startX; x < ((startX + 7) & ~7) && x < endX; ++x) {
384 row[x] = 0;
385 }
386 for (; x + 7 < (endX & ~7); x += 8) {
387 row[x] = 0;
388 row[x + 1] = 0;
389 row[x + 2] = 0;
390 row[x + 3] = 0;
391 row[x + 4] = 0;
392 row[x + 5] = 0;
393 row[x + 6] = 0;
394 row[x + 7] = 0;
395 }
396 for (; x < endX; ++x) {
397 row[x] = 0;
398 }
399 break;
400 case 3:
401 for (x = startX; x < ((startX + 7) & ~7) && x < endX; ++x) {
402 row[x] = softwareRenderer->palette[0];
403 }
404 for (; x + 7 < (endX & ~7); x += 8) {
405 row[x] = softwareRenderer->palette[0];
406 row[x + 1] = softwareRenderer->palette[0];
407 row[x + 2] = softwareRenderer->palette[0];
408 row[x + 3] = softwareRenderer->palette[0];
409 row[x + 4] = softwareRenderer->palette[0];
410 row[x + 5] = softwareRenderer->palette[0];
411 row[x + 6] = softwareRenderer->palette[0];
412 row[x + 7] = softwareRenderer->palette[0];
413 }
414 for (; x < endX; ++x) {
415 row[x] = softwareRenderer->palette[0];
416 }
417 break;
418 }
419}
420
421static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
422 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
423
424 if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && softwareRenderer->wx - 7 < GB_VIDEO_HORIZONTAL_PIXELS) {
425 ++softwareRenderer->currentWy;
426 }
427 if (softwareRenderer->sgbTransfer == 1) {
428 size_t offset = 2 * ((y & 7) + (y >> 3) * GB_VIDEO_HORIZONTAL_PIXELS);
429 if (offset >= 0x1000) {
430 return;
431 }
432 uint8_t* buffer = NULL;
433 switch (softwareRenderer->sgbCommandHeader >> 3) {
434 case SGB_PAL_TRN:
435 buffer = renderer->sgbPalRam;
436 break;
437 case SGB_CHR_TRN:
438 buffer = &renderer->sgbCharRam[SGB_SIZE_CHAR_RAM / 2 * (softwareRenderer->sgbPacket[1] & 1)];
439 break;
440 case SGB_PCT_TRN:
441 buffer = renderer->sgbMapRam;
442 break;
443 case SGB_ATTR_TRN:
444 buffer = renderer->sgbAttributeFiles;
445 break;
446 default:
447 break;
448 }
449 if (buffer) {
450 int i;
451 for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS; i += 8) {
452 if (UNLIKELY(offset + (i << 1) + 1 >= 0x1000)) {
453 break;
454 }
455 uint8_t hi = 0;
456 uint8_t lo = 0;
457 hi |= (softwareRenderer->row[i + 0] & 0x2) << 6;
458 lo |= (softwareRenderer->row[i + 0] & 0x1) << 7;
459 hi |= (softwareRenderer->row[i + 1] & 0x2) << 5;
460 lo |= (softwareRenderer->row[i + 1] & 0x1) << 6;
461 hi |= (softwareRenderer->row[i + 2] & 0x2) << 4;
462 lo |= (softwareRenderer->row[i + 2] & 0x1) << 5;
463 hi |= (softwareRenderer->row[i + 3] & 0x2) << 3;
464 lo |= (softwareRenderer->row[i + 3] & 0x1) << 4;
465 hi |= (softwareRenderer->row[i + 4] & 0x2) << 2;
466 lo |= (softwareRenderer->row[i + 4] & 0x1) << 3;
467 hi |= (softwareRenderer->row[i + 5] & 0x2) << 1;
468 lo |= (softwareRenderer->row[i + 5] & 0x1) << 2;
469 hi |= (softwareRenderer->row[i + 6] & 0x2) << 0;
470 lo |= (softwareRenderer->row[i + 6] & 0x1) << 1;
471 hi |= (softwareRenderer->row[i + 7] & 0x2) >> 1;
472 lo |= (softwareRenderer->row[i + 7] & 0x1) >> 0;
473 buffer[offset + (i << 1) + 0] = lo;
474 buffer[offset + (i << 1) + 1] = hi;
475 }
476 }
477 }
478}
479
480static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
481 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
482
483 if (softwareRenderer->temporaryBuffer) {
484 mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
485 softwareRenderer->temporaryBuffer = 0;
486 }
487 if (!GBRegisterLCDCIsEnable(softwareRenderer->lcdc)) {
488 _clearScreen(softwareRenderer);
489 }
490 if (softwareRenderer->model == GB_MODEL_SGB) {
491 switch (softwareRenderer->sgbCommandHeader >> 3) {
492 case SGB_PAL_SET:
493 case SGB_ATTR_SET:
494 if (softwareRenderer->sgbPacket[1] & 0x40) {
495 renderer->sgbRenderMode = 0;
496 }
497 break;
498 case SGB_PAL_TRN:
499 case SGB_CHR_TRN:
500 case SGB_PCT_TRN:
501 if (softwareRenderer->sgbTransfer > 0) {
502 // Make sure every buffer sees this if we're multibuffering
503 _regenerateSGBBorder(softwareRenderer);
504 }
505 // Fall through
506 case SGB_ATTR_TRN:
507 ++softwareRenderer->sgbTransfer;
508 if (softwareRenderer->sgbTransfer == 5) {
509 softwareRenderer->sgbCommandHeader = 0;
510 }
511 break;
512 default:
513 break;
514 }
515 }
516 softwareRenderer->currentWy = 0;
517}
518
519static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int sx, int sy) {
520 uint8_t* data = renderer->d.vram;
521 uint8_t* attr = &maps[GB_SIZE_VRAM_BANK0];
522 if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
523 data += 0x1000;
524 }
525 int topY = ((sy >> 3) & 0x1F) * 0x20;
526 int bottomY = sy & 7;
527 if (startX < 0) {
528 startX = 0;
529 }
530 int x;
531 if ((startX + sx) & 7) {
532 int startX2 = startX + 8 - ((startX + sx) & 7);
533 for (x = startX; x < startX2; ++x) {
534 uint8_t* localData = data;
535 int localY = bottomY;
536 int topX = ((x + sx) >> 3) & 0x1F;
537 int bottomX = 7 - ((x + sx) & 7);
538 int bgTile;
539 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
540 bgTile = maps[topX + topY];
541 } else {
542 bgTile = ((int8_t*) maps)[topX + topY];
543 }
544 int p = 0;
545 if (renderer->model >= GB_MODEL_CGB) {
546 GBObjAttributes attrs = attr[topX + topY];
547 p = GBObjAttributesGetCGBPalette(attrs) * 4;
548 if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
549 p |= 0x80;
550 }
551 if (GBObjAttributesIsBank(attrs)) {
552 localData += GB_SIZE_VRAM_BANK0;
553 }
554 if (GBObjAttributesIsYFlip(attrs)) {
555 localY = 7 - bottomY;
556 }
557 if (GBObjAttributesIsXFlip(attrs)) {
558 bottomX = 7 - bottomX;
559 }
560 }
561 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
562 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
563 tileDataUpper >>= bottomX;
564 tileDataLower >>= bottomX;
565 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
566 }
567 startX = startX2;
568 }
569 for (x = startX; x < endX; x += 8) {
570 uint8_t* localData = data;
571 int localY = bottomY;
572 int topX = ((x + sx) >> 3) & 0x1F;
573 int bgTile;
574 if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
575 bgTile = maps[topX + topY];
576 } else {
577 bgTile = ((int8_t*) maps)[topX + topY];
578 }
579 int p = 0;
580 if (renderer->model >= GB_MODEL_CGB) {
581 GBObjAttributes attrs = attr[topX + topY];
582 p = GBObjAttributesGetCGBPalette(attrs) * 4;
583 if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
584 p |= 0x80;
585 }
586 if (GBObjAttributesIsBank(attrs)) {
587 localData += GB_SIZE_VRAM_BANK0;
588 }
589 if (GBObjAttributesIsYFlip(attrs)) {
590 localY = 7 - bottomY;
591 }
592 if (GBObjAttributesIsXFlip(attrs)) {
593 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
594 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
595 renderer->row[x + 0] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
596 renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
597 renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
598 renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
599 renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
600 renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
601 renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
602 renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
603 continue;
604 }
605 }
606 uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
607 uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
608 renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
609 renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
610 renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
611 renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
612 renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
613 renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
614 renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
615 renderer->row[x + 0] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
616 }
617}
618
619static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
620 int ix = obj->x - 8;
621 if (endX < ix || startX >= ix + 8) {
622 return;
623 }
624 if (obj->x < endX) {
625 endX = obj->x;
626 }
627 if (obj->x - 8 > startX) {
628 startX = obj->x - 8;
629 }
630 if (startX < 0) {
631 startX = 0;
632 }
633 uint8_t* data = renderer->d.vram;
634 int tileOffset = 0;
635 int bottomY;
636 if (GBObjAttributesIsYFlip(obj->attr)) {
637 bottomY = 7 - ((y - obj->y - 16) & 7);
638 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
639 ++tileOffset;
640 }
641 } else {
642 bottomY = (y - obj->y - 16) & 7;
643 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
644 ++tileOffset;
645 }
646 }
647 if (GBRegisterLCDCIsObjSize(renderer->lcdc) && obj->tile & 1) {
648 --tileOffset;
649 }
650 uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0x63 : 0x60;
651 uint8_t mask2 = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x83;
652 int p;
653 if (renderer->model >= GB_MODEL_CGB) {
654 p = (GBObjAttributesGetCGBPalette(obj->attr) + 8) * 4;
655 if (GBObjAttributesIsBank(obj->attr)) {
656 data += GB_SIZE_VRAM_BANK0;
657 }
658 if (!GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
659 mask = 0x60;
660 mask2 = 0x83;
661 }
662 } else {
663 p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
664 }
665 int bottomX;
666 int x = startX;
667 if ((x - obj->x) & 7) {
668 for (; x < endX; ++x) {
669 if (GBObjAttributesIsXFlip(obj->attr)) {
670 bottomX = (x - obj->x) & 7;
671 } else {
672 bottomX = 7 - ((x - obj->x) & 7);
673 }
674 int objTile = obj->tile + tileOffset;
675 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
676 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
677 tileDataUpper >>= bottomX;
678 tileDataLower >>= bottomX;
679 color_t current = renderer->row[x];
680 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
681 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
682 }
683 }
684 } else if (GBObjAttributesIsXFlip(obj->attr)) {
685 int objTile = obj->tile + tileOffset;
686 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
687 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
688 color_t current;
689 current = renderer->row[x];
690 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
691 renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
692 }
693 current = renderer->row[x + 1];
694 if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
695 renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
696 }
697 current = renderer->row[x + 2];
698 if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
699 renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
700 }
701 current = renderer->row[x + 3];
702 if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
703 renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
704 }
705 current = renderer->row[x + 4];
706 if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
707 renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
708 }
709 current = renderer->row[x + 5];
710 if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
711 renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
712 }
713 current = renderer->row[x + 6];
714 if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
715 renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
716 }
717 current = renderer->row[x + 7];
718 if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
719 renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
720 }
721 } else {
722 int objTile = obj->tile + tileOffset;
723 uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
724 uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
725 color_t current;
726 current = renderer->row[x + 7];
727 if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
728 renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
729 }
730 current = renderer->row[x + 6];
731 if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
732 renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
733 }
734 current = renderer->row[x + 5];
735 if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
736 renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
737 }
738 current = renderer->row[x + 4];
739 if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
740 renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
741 }
742 current = renderer->row[x + 3];
743 if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
744 renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
745 }
746 current = renderer->row[x + 2];
747 if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
748 renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
749 }
750 current = renderer->row[x + 1];
751 if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
752 renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
753 }
754 current = renderer->row[x];
755 if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
756 renderer->row[x] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
757 }
758 }
759}
760
761static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
762 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
763 *stride = softwareRenderer->outputBufferStride;
764 *pixels = softwareRenderer->outputBuffer;
765}
766
767static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
768 struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
769 // TODO: Share with GBAVideoSoftwareRendererGetPixels
770
771 const color_t* colorPixels = pixels;
772 unsigned i;
773 for (i = 0; i < GB_VIDEO_VERTICAL_PIXELS; ++i) {
774 memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], GB_VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
775 }
776}