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