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