all repos — mgba @ 23e60e081ea4e5dc35e28feb3af14b3d94bef329

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 < 104) {
 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			if (SGBBgAttributesIsXFlip(mapData)) {
 83				for (i = 0; i < 8; ++i) {
 84					colorSelector = (tileData[0] >> i & 0x1) << 0 | (tileData[1] >> i & 0x1) << 1 | (tileData[2] >> i & 0x1) << 2 | (tileData[3] >> i & 0x1) << 3;
 85					renderer->outputBuffer[base + i] = renderer->palette[paletteBase | colorSelector];
 86				}
 87			} else {
 88				for (i = 7; i >= 0; --i) {
 89					colorSelector = (tileData[0] >> i & 0x1) << 0 | (tileData[1] >> i & 0x1) << 1 | (tileData[2] >> i & 0x1) << 2 | (tileData[3] >> i & 0x1) << 3;
 90					renderer->outputBuffer[base + 7 - i] = renderer->palette[paletteBase | colorSelector];
 91				}
 92			}
 93		}
 94	}
 95}
 96
 97static inline void _setAttribute(uint8_t* sgbAttributes, unsigned x, unsigned y, int palette) {
 98	int p = sgbAttributes[(x >> 2) + 5 * y];
 99	p &= ~(3 << (2 * (3 - (x & 3))));
100	p |= palette << (2 * (3 - (x & 3)));
101	sgbAttributes[(x >> 2) + 5 * y] = p;
102}
103
104static void _parseAttrBlock(struct GBVideoSoftwareRenderer* renderer, int start) {
105	uint8_t block[6];
106	memcpy(block, &renderer->sgbPacket[start], 6);
107	unsigned x0 = block[2];
108	unsigned x1 = block[4];
109	unsigned y0 = block[3];
110	unsigned y1 = block[5];
111	unsigned x, y;
112	int pIn = block[1] & 3;
113	int pPerim = (block[1] >> 2) & 3;
114	int pOut = (block[1] >> 4) & 3;
115
116	for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS / 8; ++y) {
117		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS / 8; ++x) {
118			if (y > y0 && y < y1 && x > x0 && x < x1) {
119				if (block[0] & 1) {
120					_setAttribute(renderer->d.sgbAttributes, x, y, pIn);
121				}
122			} else if (y < y0 || y > y1 || x < x0 || x > x1) {
123				if (block[0] & 4) {
124					_setAttribute(renderer->d.sgbAttributes, x, y, pOut);
125				}
126			} else {
127				if (block[0] & 2) {
128					_setAttribute(renderer->d.sgbAttributes, x, y, pPerim);
129				} else if (block[0] & 1) {
130					_setAttribute(renderer->d.sgbAttributes, x, y, pIn);
131				} else if (block[0] & 4) {
132					_setAttribute(renderer->d.sgbAttributes, x, y, pOut);
133				}
134			}
135		}
136	}
137}
138
139static void _parseAttrLine(struct GBVideoSoftwareRenderer* renderer, int start) {
140	uint8_t byte = renderer->sgbPacket[start];
141	unsigned line = byte & 0x1F;
142	int pal = (byte >> 5) & 3;
143
144	if (byte & 0x80) {
145		if (line > GB_VIDEO_VERTICAL_PIXELS / 8) {
146			return;
147		}
148		int x;
149		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS / 8; ++x) {
150			_setAttribute(renderer->d.sgbAttributes, x, line, pal);
151		}
152	} else {
153		if (line > GB_VIDEO_HORIZONTAL_PIXELS / 8) {
154			return;
155		}
156		int y;
157		for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS / 8; ++y) {
158			_setAttribute(renderer->d.sgbAttributes, line, y, pal);
159		}
160	}
161}
162
163static bool _inWindow(struct GBVideoSoftwareRenderer* renderer) {
164	return GBRegisterLCDCIsWindow(renderer->lcdc) && GB_VIDEO_HORIZONTAL_PIXELS + 7 > renderer->wx;
165}
166
167void GBVideoSoftwareRendererCreate(struct GBVideoSoftwareRenderer* renderer) {
168	renderer->d.init = GBVideoSoftwareRendererInit;
169	renderer->d.deinit = GBVideoSoftwareRendererDeinit;
170	renderer->d.writeVideoRegister = GBVideoSoftwareRendererWriteVideoRegister;
171	renderer->d.writeSGBPacket = GBVideoSoftwareRendererWriteSGBPacket;
172	renderer->d.writePalette = GBVideoSoftwareRendererWritePalette;
173	renderer->d.writeVRAM = GBVideoSoftwareRendererWriteVRAM;
174	renderer->d.writeOAM = GBVideoSoftwareRendererWriteOAM;
175	renderer->d.drawRange = GBVideoSoftwareRendererDrawRange;
176	renderer->d.finishScanline = GBVideoSoftwareRendererFinishScanline;
177	renderer->d.finishFrame = GBVideoSoftwareRendererFinishFrame;
178	renderer->d.enableSGBBorder = GBVideoSoftwareRendererEnableSGBBorder;
179	renderer->d.getPixels = GBVideoSoftwareRendererGetPixels;
180	renderer->d.putPixels = GBVideoSoftwareRendererPutPixels;
181
182	renderer->d.disableBG = false;
183	renderer->d.disableOBJ = false;
184	renderer->d.disableWIN = false;
185
186	renderer->temporaryBuffer = 0;
187}
188
189static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer, enum GBModel model, bool sgbBorders) {
190	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
191	softwareRenderer->lcdc = 0;
192	softwareRenderer->scy = 0;
193	softwareRenderer->scx = 0;
194	softwareRenderer->wy = 0;
195	softwareRenderer->currentWy = 0;
196	softwareRenderer->lastY = GB_VIDEO_VERTICAL_PIXELS;
197	softwareRenderer->hasWindow = false;
198	softwareRenderer->wx = 0;
199	softwareRenderer->model = model & ~GB_MODEL_MGB;
200	softwareRenderer->sgbTransfer = 0;
201	softwareRenderer->sgbCommandHeader = 0;
202	softwareRenderer->sgbBorders = sgbBorders;
203	softwareRenderer->objOffsetX = 0;
204	softwareRenderer->objOffsetY = 0;
205	softwareRenderer->offsetScx = 0;
206	softwareRenderer->offsetScy = 0;
207	softwareRenderer->offsetWx = 0;
208	softwareRenderer->offsetWy = 0;
209
210	int i;
211	for (i = 0; i < 64; ++i) {
212		softwareRenderer->lookup[i] = i;
213		softwareRenderer->lookup[i] = i;
214		softwareRenderer->lookup[i] = i;
215		softwareRenderer->lookup[i] = i;
216	}
217}
218
219static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
220	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
221	UNUSED(softwareRenderer);
222}
223
224static void GBVideoSoftwareRendererUpdateWindow(struct GBVideoSoftwareRenderer* renderer, bool before, bool after) {
225	if (renderer->lastY >= GB_VIDEO_VERTICAL_PIXELS || after == before) {
226		return;
227	}
228	if (renderer->lastY >= renderer->wy) {
229		if (!after) {
230			renderer->currentWy -= renderer->lastY;
231			renderer->hasWindow = true;
232		} else {
233			if (!renderer->hasWindow) {
234				if (renderer->lastY > renderer->wy) {
235					renderer->currentWy = GB_VIDEO_VERTICAL_PIXELS;
236				} else {
237					renderer->currentWy = renderer->lastY - renderer->wy;
238				}
239			} else {
240				renderer->currentWy += renderer->lastY;
241			}
242		}
243	}
244}
245
246static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
247	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
248	if (renderer->cache) {
249		GBVideoCacheWriteVideoRegister(renderer->cache, address, value);
250	}
251	bool wasWindow = _inWindow(softwareRenderer);
252	switch (address) {
253	case REG_LCDC:
254		softwareRenderer->lcdc = value;
255		GBVideoSoftwareRendererUpdateWindow(softwareRenderer, wasWindow, _inWindow(softwareRenderer));
256		break;
257	case REG_SCY:
258		softwareRenderer->scy = value;
259		break;
260	case REG_SCX:
261		softwareRenderer->scx = value;
262		break;
263	case REG_WY:
264		softwareRenderer->wy = value;
265		GBVideoSoftwareRendererUpdateWindow(softwareRenderer, wasWindow, _inWindow(softwareRenderer));
266		break;
267	case REG_WX:
268		softwareRenderer->wx = value;
269		GBVideoSoftwareRendererUpdateWindow(softwareRenderer, wasWindow, _inWindow(softwareRenderer));
270		break;
271	case REG_BGP:
272		softwareRenderer->lookup[0] = value & 3;
273		softwareRenderer->lookup[1] = (value >> 2) & 3;
274		softwareRenderer->lookup[2] = (value >> 4) & 3;
275		softwareRenderer->lookup[3] = (value >> 6) & 3;
276		break;
277	case REG_OBP0:
278		softwareRenderer->lookup[0x20 + 0] = value & 3;
279		softwareRenderer->lookup[0x20 + 1] = (value >> 2) & 3;
280		softwareRenderer->lookup[0x20 + 2] = (value >> 4) & 3;
281		softwareRenderer->lookup[0x20 + 3] = (value >> 6) & 3;
282		break;
283	case REG_OBP1:
284		softwareRenderer->lookup[0x24 + 0] = value & 3;
285		softwareRenderer->lookup[0x24 + 1] = (value >> 2) & 3;
286		softwareRenderer->lookup[0x24 + 2] = (value >> 4) & 3;
287		softwareRenderer->lookup[0x24 + 3] = (value >> 6) & 3;
288		break;
289	}
290	return value;
291}
292
293static void GBVideoSoftwareRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
294	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
295	memcpy(softwareRenderer->sgbPacket, data, sizeof(softwareRenderer->sgbPacket));
296	int i;
297	softwareRenderer->sgbCommandHeader = data[0];
298	softwareRenderer->sgbTransfer = 0;
299	int set;
300	int sets;
301	int attrX;
302	int attrY;
303	int attrDirection;
304	int pBefore;
305	int pAfter;
306	int pDiv;
307	switch (softwareRenderer->sgbCommandHeader >> 3) {
308	case SGB_PAL_SET:
309		softwareRenderer->sgbPacket[1] = data[9];
310		if (!(data[9] & 0x80)) {
311			break;
312		}
313		// Fall through
314	case SGB_ATTR_SET:
315		set = softwareRenderer->sgbPacket[1] & 0x3F;
316		if (set <= 0x2C) {
317			memcpy(renderer->sgbAttributes, &renderer->sgbAttributeFiles[set * 90], 90);
318		}
319		break;
320	case SGB_ATTR_BLK:
321		sets = softwareRenderer->sgbPacket[1];
322		i = 2;
323		for (; i < (softwareRenderer->sgbCommandHeader & 7) << 4 && sets; i += 6, --sets) {
324			_parseAttrBlock(softwareRenderer, i);
325		}
326		break;
327	case SGB_ATTR_LIN:
328		sets = softwareRenderer->sgbPacket[1];
329		i = 2;
330		for (; i < (softwareRenderer->sgbCommandHeader & 7) << 4 && sets; ++i, --sets) {
331			_parseAttrLine(softwareRenderer, i);
332		}
333		break;
334	case SGB_ATTR_DIV:
335		pAfter = softwareRenderer->sgbPacket[1] & 3;
336		pBefore = (softwareRenderer->sgbPacket[1] >> 2) & 3;
337		pDiv = (softwareRenderer->sgbPacket[1] >> 4) & 3;
338		attrX = softwareRenderer->sgbPacket[2];
339		if (softwareRenderer->sgbPacket[1] & 0x40) {
340			if (attrX > GB_VIDEO_VERTICAL_PIXELS / 8) {
341				attrX = GB_VIDEO_VERTICAL_PIXELS / 8;
342			}
343			int j;
344			for (j = 0; j < attrX; ++j) {
345				for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS / 8; ++i) {
346					_setAttribute(renderer->sgbAttributes, i, j, pBefore);
347				}
348			}
349			if (attrX < GB_VIDEO_VERTICAL_PIXELS / 8) {
350				for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS / 8; ++i) {
351					_setAttribute(renderer->sgbAttributes, i, attrX, pDiv);
352				}
353
354			}
355			for (; j < GB_VIDEO_VERTICAL_PIXELS / 8; ++j) {
356				for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS / 8; ++i) {
357					_setAttribute(renderer->sgbAttributes, i, j, pAfter);
358				}
359			}
360		} else {
361			if (attrX > GB_VIDEO_HORIZONTAL_PIXELS / 8) {
362				attrX = GB_VIDEO_HORIZONTAL_PIXELS / 8;
363			}
364			int j;
365			for (j = 0; j < attrX; ++j) {
366				for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS / 8; ++i) {
367					_setAttribute(renderer->sgbAttributes, j, i, pBefore);
368				}
369			}
370			if (attrX < GB_VIDEO_HORIZONTAL_PIXELS / 8) {
371				for (i = 0; i < GB_VIDEO_VERTICAL_PIXELS / 8; ++i) {
372					_setAttribute(renderer->sgbAttributes, attrX, i, pDiv);
373				}
374
375			}
376			for (; j < GB_VIDEO_HORIZONTAL_PIXELS / 8; ++j) {
377				for (i = 0; i < GB_VIDEO_VERTICAL_PIXELS / 8; ++i) {
378					_setAttribute(renderer->sgbAttributes, j, i, pAfter);
379				}
380			}
381		}
382		break;
383	case SGB_ATTR_CHR:
384		attrX = softwareRenderer->sgbPacket[1];
385		attrY = softwareRenderer->sgbPacket[2];
386		if (attrX >= GB_VIDEO_HORIZONTAL_PIXELS / 8) {
387			attrX = 0;
388		}
389		if (attrY >= GB_VIDEO_VERTICAL_PIXELS / 8) {
390			attrY = 0;
391		}
392		sets = softwareRenderer->sgbPacket[3];
393		sets |= softwareRenderer->sgbPacket[4] << 8;
394		attrDirection = softwareRenderer->sgbPacket[5];
395		i = 6;
396		for (; i < (softwareRenderer->sgbCommandHeader & 7) << 4 && sets; ++i) {
397			int j;
398			for (j = 0; j < 4 && sets; ++j, --sets) {
399				uint8_t p = softwareRenderer->sgbPacket[i] >> (6 - j * 2);
400				_setAttribute(renderer->sgbAttributes, attrX, attrY, p & 3);
401				if (attrDirection) {
402					++attrY;
403					if (attrY >= GB_VIDEO_VERTICAL_PIXELS / 8) {
404						attrY = 0;
405						++attrX;
406					}
407					if (attrX >= GB_VIDEO_HORIZONTAL_PIXELS / 8) {
408						attrX = 0;
409					}
410				} else {
411					++attrX;
412					if (attrX >= GB_VIDEO_HORIZONTAL_PIXELS / 8) {
413						attrX = 0;
414						++attrY;
415					}
416					if (attrY >= GB_VIDEO_VERTICAL_PIXELS / 8) {
417						attrY = 0;
418					}
419				}
420			}
421		}
422
423		break;
424	case SGB_ATRC_EN:
425	case SGB_MASK_EN:
426		if (softwareRenderer->sgbBorders && !renderer->sgbRenderMode) {
427			_regenerateSGBBorder(softwareRenderer);
428		}
429	}
430}
431
432static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
433	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
434	color_t color = mColorFrom555(value);
435	if (softwareRenderer->model == GB_MODEL_SGB) {
436		if (index < 0x10 && index && !(index & 3)) {
437			color = softwareRenderer->palette[0];
438		} else if (index >= 0x40 && !(index & 0xF)) {
439			color = softwareRenderer->palette[0];
440		}
441	}
442	if (renderer->cache) {
443		mCacheSetWritePalette(renderer->cache, index, color);
444	}
445	if (softwareRenderer->model == GB_MODEL_AGB) {
446		unsigned r = M_R5(value);
447		unsigned g = M_G5(value);
448		unsigned b = M_B5(value);
449		r = r * r;
450		g = g * g;
451		b = b * b;
452#ifdef COLOR_16_BIT
453		r /= 31;
454		g /= 31;
455		b /= 31;
456		color = mColorFrom555(r | (g << 5) | (b << 10));
457#else
458		r >>= 2;
459		g >>= 2;
460		b >>= 2;
461		color = r | (g << 8) | (b << 16);
462#endif
463	}
464	softwareRenderer->palette[index] = color;
465
466	if (softwareRenderer->model == GB_MODEL_SGB && !index && GBRegisterLCDCIsEnable(softwareRenderer->lcdc)) {
467		renderer->writePalette(renderer, 0x04, value);
468		renderer->writePalette(renderer, 0x08, value);
469		renderer->writePalette(renderer, 0x0C, value);
470		renderer->writePalette(renderer, 0x40, value);
471		renderer->writePalette(renderer, 0x50, value);
472		renderer->writePalette(renderer, 0x60, value);
473		renderer->writePalette(renderer, 0x70, value);
474		if (softwareRenderer->sgbBorders && !renderer->sgbRenderMode) {
475			_regenerateSGBBorder(softwareRenderer);
476		}
477	}
478}
479
480static void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
481	if (renderer->cache) {
482		mCacheSetWriteVRAM(renderer->cache, address);
483	}
484}
485
486static void GBVideoSoftwareRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
487	UNUSED(renderer);
488	UNUSED(oam);
489	// Nothing to do
490}
491
492static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
493	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
494	softwareRenderer->lastY = y;
495	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
496	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
497		maps += GB_SIZE_MAP;
498	}
499	if (softwareRenderer->d.disableBG) {
500		memset(&softwareRenderer->row[startX], 0, endX - startX);
501	}
502	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) || softwareRenderer->model >= GB_MODEL_CGB) {
503		int wy = softwareRenderer->wy + softwareRenderer->currentWy;
504		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && wy <= y && endX >= softwareRenderer->wx - 7) {
505			if (softwareRenderer->wx - 7 > 0 && !softwareRenderer->d.disableBG) {
506				GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, softwareRenderer->wx - 7, softwareRenderer->scx - softwareRenderer->offsetScx, softwareRenderer->scy + y - softwareRenderer->offsetScy);
507			}
508
509			maps = &softwareRenderer->d.vram[GB_BASE_MAP];
510			if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
511				maps += GB_SIZE_MAP;
512			}
513			if (!softwareRenderer->d.disableWIN) {
514				GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, 7 - softwareRenderer->wx - softwareRenderer->offsetWx, y - wy - softwareRenderer->offsetWy);
515			}
516		} else if (!softwareRenderer->d.disableBG) {
517			GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, softwareRenderer->scx - softwareRenderer->offsetScx, softwareRenderer->scy + y - softwareRenderer->offsetScy);
518		}
519	} else if (!softwareRenderer->d.disableBG) {
520		memset(&softwareRenderer->row[startX], 0, endX - startX);
521	}
522
523	if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc) && !softwareRenderer->d.disableOBJ) {
524		size_t i;
525		for (i = 0; i < oamMax; ++i) {
526			GBVideoSoftwareRendererDrawObj(softwareRenderer, &obj[i], startX, endX, y);
527		}
528	}
529
530	size_t sgbOffset = 0;
531	if (softwareRenderer->model == GB_MODEL_SGB && softwareRenderer->sgbBorders) {
532		sgbOffset = softwareRenderer->outputBufferStride * 40 + 48;
533	}
534	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + sgbOffset];
535	int x = startX;
536	int p = 0;
537	switch (softwareRenderer->d.sgbRenderMode) {
538	case 0:
539		if (softwareRenderer->model == GB_MODEL_SGB) {
540			p = softwareRenderer->d.sgbAttributes[(startX >> 5) + 5 * (y >> 3)];
541			p >>= 6 - ((x / 4) & 0x6);
542			p &= 3;
543			p <<= 2;
544		}
545		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
546			row[x] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
547		}
548		for (; x + 7 < (endX & ~7); x += 8) {
549			if (softwareRenderer->model == GB_MODEL_SGB) {
550				p = softwareRenderer->d.sgbAttributes[(x >> 5) + 5 * (y >> 3)];
551				p >>= 6 - ((x / 4) & 0x6);
552				p &= 3;
553				p <<= 2;
554			}
555			row[x + 0] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
556			row[x + 1] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 1] & 0x7F]];
557			row[x + 2] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 2] & 0x7F]];
558			row[x + 3] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 3] & 0x7F]];
559			row[x + 4] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 4] & 0x7F]];
560			row[x + 5] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 5] & 0x7F]];
561			row[x + 6] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 6] & 0x7F]];
562			row[x + 7] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 7] & 0x7F]];
563		}
564		if (softwareRenderer->model == GB_MODEL_SGB) {
565			p = softwareRenderer->d.sgbAttributes[(x >> 5) + 5 * (y >> 3)];
566			p >>= 6 - ((x / 4) & 0x6);
567			p &= 3;
568			p <<= 2;
569		}
570		for (; x < endX; ++x) {
571			row[x] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
572		}
573		break;
574	case 1:
575		break;
576	case 2:
577		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
578			row[x] = 0;
579		}
580		for (; x + 7 < (endX & ~7); x += 8) {
581			row[x] = 0;
582			row[x + 1] = 0;
583			row[x + 2] = 0;
584			row[x + 3] = 0;
585			row[x + 4] = 0;
586			row[x + 5] = 0;
587			row[x + 6] = 0;
588			row[x + 7] = 0;
589		}
590		for (; x < endX; ++x) {
591			row[x] = 0;
592		}
593		break;
594	case 3:
595		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
596			row[x] = softwareRenderer->palette[0];
597		}
598		for (; x + 7 < (endX & ~7); x += 8) {
599			row[x] = softwareRenderer->palette[0];
600			row[x + 1] = softwareRenderer->palette[0];
601			row[x + 2] = softwareRenderer->palette[0];
602			row[x + 3] = softwareRenderer->palette[0];
603			row[x + 4] = softwareRenderer->palette[0];
604			row[x + 5] = softwareRenderer->palette[0];
605			row[x + 6] = softwareRenderer->palette[0];
606			row[x + 7] = softwareRenderer->palette[0];
607		}
608		for (; x < endX; ++x) {
609			row[x] = softwareRenderer->palette[0];
610		}
611		break;
612	}
613}
614
615static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
616	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
617
618	if (softwareRenderer->sgbTransfer == 1) {
619		size_t offset = 2 * ((y & 7) + (y >> 3) * GB_VIDEO_HORIZONTAL_PIXELS);
620		if (offset >= 0x1000) {
621			return;
622		}
623		uint8_t* buffer = NULL;
624		switch (softwareRenderer->sgbCommandHeader >> 3) {
625		case SGB_PAL_TRN:
626			buffer = renderer->sgbPalRam;
627			break;
628		case SGB_CHR_TRN:
629			buffer = &renderer->sgbCharRam[SGB_SIZE_CHAR_RAM / 2 * (softwareRenderer->sgbPacket[1] & 1)];
630			break;
631		case SGB_PCT_TRN:
632			buffer = renderer->sgbMapRam;
633			break;
634		case SGB_ATTR_TRN:
635			buffer = renderer->sgbAttributeFiles;
636			break;
637		default:
638			break;
639		}
640		if (buffer) {
641			int i;
642			for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS; i += 8) {
643				if (UNLIKELY(offset + (i << 1) + 1 >= 0x1000)) {
644					break;
645				}
646				uint8_t hi = 0;
647				uint8_t lo = 0;
648				hi |= (softwareRenderer->row[i + 0] & 0x2) << 6;
649				lo |= (softwareRenderer->row[i + 0] & 0x1) << 7;
650				hi |= (softwareRenderer->row[i + 1] & 0x2) << 5;
651				lo |= (softwareRenderer->row[i + 1] & 0x1) << 6;
652				hi |= (softwareRenderer->row[i + 2] & 0x2) << 4;
653				lo |= (softwareRenderer->row[i + 2] & 0x1) << 5;
654				hi |= (softwareRenderer->row[i + 3] & 0x2) << 3;
655				lo |= (softwareRenderer->row[i + 3] & 0x1) << 4;
656				hi |= (softwareRenderer->row[i + 4] & 0x2) << 2;
657				lo |= (softwareRenderer->row[i + 4] & 0x1) << 3;
658				hi |= (softwareRenderer->row[i + 5] & 0x2) << 1;
659				lo |= (softwareRenderer->row[i + 5] & 0x1) << 2;
660				hi |= (softwareRenderer->row[i + 6] & 0x2) << 0;
661				lo |= (softwareRenderer->row[i + 6] & 0x1) << 1;
662				hi |= (softwareRenderer->row[i + 7] & 0x2) >> 1;
663				lo |= (softwareRenderer->row[i + 7] & 0x1) >> 0;
664				buffer[offset + (i << 1) + 0] = lo;
665				buffer[offset + (i << 1) + 1] = hi;
666			}
667		}
668	}
669}
670
671static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
672	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
673
674	if (softwareRenderer->temporaryBuffer) {
675		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
676		softwareRenderer->temporaryBuffer = 0;
677	}
678	if (!GBRegisterLCDCIsEnable(softwareRenderer->lcdc)) {
679		_clearScreen(softwareRenderer);
680	}
681	if (softwareRenderer->model == GB_MODEL_SGB) {
682		switch (softwareRenderer->sgbCommandHeader >> 3) {
683		case SGB_PAL_SET:
684		case SGB_ATTR_SET:
685			if (softwareRenderer->sgbPacket[1] & 0x40) {
686				renderer->sgbRenderMode = 0;
687			}
688			break;
689		case SGB_PAL_TRN:
690		case SGB_CHR_TRN:
691		case SGB_PCT_TRN:
692			if (softwareRenderer->sgbTransfer > 0 && softwareRenderer->sgbBorders && !renderer->sgbRenderMode) {
693				// Make sure every buffer sees this if we're multibuffering
694				_regenerateSGBBorder(softwareRenderer);
695			}
696			// Fall through
697		case SGB_ATTR_TRN:
698			++softwareRenderer->sgbTransfer;
699			if (softwareRenderer->sgbTransfer == 5) {
700				softwareRenderer->sgbCommandHeader = 0;
701			}
702			break;
703		default:
704			break;
705		}
706	}
707	softwareRenderer->lastY = GB_VIDEO_VERTICAL_PIXELS;
708	softwareRenderer->currentWy = 0;
709	softwareRenderer->hasWindow = false;
710}
711
712static void GBVideoSoftwareRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
713	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
714	if (softwareRenderer->model == GB_MODEL_SGB) {
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}