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