all repos — mgba @ b78825738ee6d094e892c3980c730d67db315c13

mGBA Game Boy Advance Emulator

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