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