all repos — mgba @ 7a9e1e4600f0bba80a7734c6d0c3504b4495485d

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->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 REG_LCDC:
 261		softwareRenderer->lcdc = value;
 262		GBVideoSoftwareRendererUpdateWindow(softwareRenderer, wasWindow, _inWindow(softwareRenderer), wy);
 263		break;
 264	case REG_SCY:
 265		softwareRenderer->scy = value;
 266		break;
 267	case REG_SCX:
 268		softwareRenderer->scx = value;
 269		break;
 270	case REG_WY:
 271		softwareRenderer->wy = value;
 272		GBVideoSoftwareRendererUpdateWindow(softwareRenderer, wasWindow, _inWindow(softwareRenderer), wy);
 273		break;
 274	case REG_WX:
 275		softwareRenderer->wx = value;
 276		GBVideoSoftwareRendererUpdateWindow(softwareRenderer, wasWindow, _inWindow(softwareRenderer), wy);
 277		break;
 278	case 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 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 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 GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
 503	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 504	softwareRenderer->lastY = y;
 505	softwareRenderer->lastX = endX;
 506	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
 507	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
 508		maps += GB_SIZE_MAP;
 509	}
 510	if (softwareRenderer->d.disableBG) {
 511		memset(&softwareRenderer->row[startX], 0, endX - startX);
 512	}
 513	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) || softwareRenderer->model >= GB_MODEL_CGB) {
 514		int wy = softwareRenderer->wy + softwareRenderer->currentWy;
 515		int wx = softwareRenderer->wx + softwareRenderer->currentWx - 7;
 516		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && wy == y && wx <= endX) {
 517			softwareRenderer->hasWindow = true;
 518		}
 519		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->hasWindow && wx <= endX) {
 520			if (wx > 0 && !softwareRenderer->d.disableBG) {
 521				GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, wx, softwareRenderer->scx - softwareRenderer->offsetScx, softwareRenderer->scy + y - softwareRenderer->offsetScy);
 522			}
 523
 524			maps = &softwareRenderer->d.vram[GB_BASE_MAP];
 525			if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
 526				maps += GB_SIZE_MAP;
 527			}
 528			if (!softwareRenderer->d.disableWIN) {
 529				GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, wx, endX, -wx - softwareRenderer->offsetWx, y - wy - softwareRenderer->offsetWy);
 530			}
 531		} else if (!softwareRenderer->d.disableBG) {
 532			GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, softwareRenderer->scx - softwareRenderer->offsetScx, softwareRenderer->scy + y - softwareRenderer->offsetScy);
 533		}
 534	} else if (!softwareRenderer->d.disableBG) {
 535		memset(&softwareRenderer->row[startX], 0, endX - startX);
 536	}
 537
 538	if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc) && !softwareRenderer->d.disableOBJ) {
 539		size_t i;
 540		for (i = 0; i < oamMax; ++i) {
 541			GBVideoSoftwareRendererDrawObj(softwareRenderer, &obj[i], startX, endX, y);
 542		}
 543	}
 544
 545	size_t sgbOffset = 0;
 546	if (softwareRenderer->model & GB_MODEL_SGB && softwareRenderer->sgbBorders) {
 547		sgbOffset = softwareRenderer->outputBufferStride * 40 + 48;
 548	}
 549	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + sgbOffset];
 550	int x = startX;
 551	int p = 0;
 552	switch (softwareRenderer->d.sgbRenderMode) {
 553	case 0:
 554		if (softwareRenderer->model & GB_MODEL_SGB) {
 555			p = softwareRenderer->d.sgbAttributes[(startX >> 5) + 5 * (y >> 3)];
 556			p >>= 6 - ((x / 4) & 0x6);
 557			p &= 3;
 558			p <<= 2;
 559		}
 560		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
 561			row[x] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
 562		}
 563		for (; x + 7 < (endX & ~7); x += 8) {
 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			row[x + 0] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
 571			row[x + 1] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 1] & 0x7F]];
 572			row[x + 2] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 2] & 0x7F]];
 573			row[x + 3] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 3] & 0x7F]];
 574			row[x + 4] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 4] & 0x7F]];
 575			row[x + 5] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 5] & 0x7F]];
 576			row[x + 6] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 6] & 0x7F]];
 577			row[x + 7] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 7] & 0x7F]];
 578		}
 579		if (softwareRenderer->model & GB_MODEL_SGB) {
 580			p = softwareRenderer->d.sgbAttributes[(x >> 5) + 5 * (y >> 3)];
 581			p >>= 6 - ((x / 4) & 0x6);
 582			p &= 3;
 583			p <<= 2;
 584		}
 585		for (; x < endX; ++x) {
 586			row[x] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
 587		}
 588		break;
 589	case 1:
 590		break;
 591	case 2:
 592		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
 593			row[x] = 0;
 594		}
 595		for (; x + 7 < (endX & ~7); x += 8) {
 596			row[x] = 0;
 597			row[x + 1] = 0;
 598			row[x + 2] = 0;
 599			row[x + 3] = 0;
 600			row[x + 4] = 0;
 601			row[x + 5] = 0;
 602			row[x + 6] = 0;
 603			row[x + 7] = 0;
 604		}
 605		for (; x < endX; ++x) {
 606			row[x] = 0;
 607		}
 608		break;
 609	case 3:
 610		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
 611			row[x] = softwareRenderer->palette[0];
 612		}
 613		for (; x + 7 < (endX & ~7); x += 8) {
 614			row[x] = softwareRenderer->palette[0];
 615			row[x + 1] = softwareRenderer->palette[0];
 616			row[x + 2] = softwareRenderer->palette[0];
 617			row[x + 3] = softwareRenderer->palette[0];
 618			row[x + 4] = softwareRenderer->palette[0];
 619			row[x + 5] = softwareRenderer->palette[0];
 620			row[x + 6] = softwareRenderer->palette[0];
 621			row[x + 7] = softwareRenderer->palette[0];
 622		}
 623		for (; x < endX; ++x) {
 624			row[x] = softwareRenderer->palette[0];
 625		}
 626		break;
 627	}
 628}
 629
 630static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
 631	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 632
 633	softwareRenderer->lastX = 0;
 634	softwareRenderer->currentWx = 0;
 635
 636	if (softwareRenderer->sgbTransfer == 1) {
 637		size_t offset = 2 * ((y & 7) + (y >> 3) * GB_VIDEO_HORIZONTAL_PIXELS);
 638		if (offset >= 0x1000) {
 639			return;
 640		}
 641		uint8_t* buffer = NULL;
 642		switch (softwareRenderer->sgbCommandHeader >> 3) {
 643		case SGB_PAL_TRN:
 644			buffer = renderer->sgbPalRam;
 645			break;
 646		case SGB_CHR_TRN:
 647			buffer = &renderer->sgbCharRam[SGB_SIZE_CHAR_RAM / 2 * (softwareRenderer->sgbPacket[1] & 1)];
 648			break;
 649		case SGB_PCT_TRN:
 650			buffer = renderer->sgbMapRam;
 651			break;
 652		case SGB_ATTR_TRN:
 653			buffer = renderer->sgbAttributeFiles;
 654			break;
 655		default:
 656			break;
 657		}
 658		if (buffer) {
 659			int i;
 660			for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS; i += 8) {
 661				if (UNLIKELY(offset + (i << 1) + 1 >= 0x1000)) {
 662					break;
 663				}
 664				uint8_t hi = 0;
 665				uint8_t lo = 0;
 666				hi |= (softwareRenderer->row[i + 0] & 0x2) << 6;
 667				lo |= (softwareRenderer->row[i + 0] & 0x1) << 7;
 668				hi |= (softwareRenderer->row[i + 1] & 0x2) << 5;
 669				lo |= (softwareRenderer->row[i + 1] & 0x1) << 6;
 670				hi |= (softwareRenderer->row[i + 2] & 0x2) << 4;
 671				lo |= (softwareRenderer->row[i + 2] & 0x1) << 5;
 672				hi |= (softwareRenderer->row[i + 3] & 0x2) << 3;
 673				lo |= (softwareRenderer->row[i + 3] & 0x1) << 4;
 674				hi |= (softwareRenderer->row[i + 4] & 0x2) << 2;
 675				lo |= (softwareRenderer->row[i + 4] & 0x1) << 3;
 676				hi |= (softwareRenderer->row[i + 5] & 0x2) << 1;
 677				lo |= (softwareRenderer->row[i + 5] & 0x1) << 2;
 678				hi |= (softwareRenderer->row[i + 6] & 0x2) << 0;
 679				lo |= (softwareRenderer->row[i + 6] & 0x1) << 1;
 680				hi |= (softwareRenderer->row[i + 7] & 0x2) >> 1;
 681				lo |= (softwareRenderer->row[i + 7] & 0x1) >> 0;
 682				buffer[offset + (i << 1) + 0] = lo;
 683				buffer[offset + (i << 1) + 1] = hi;
 684			}
 685		}
 686	}
 687}
 688
 689static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
 690	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 691
 692	if (softwareRenderer->temporaryBuffer) {
 693		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
 694		softwareRenderer->temporaryBuffer = 0;
 695	}
 696	if (!GBRegisterLCDCIsEnable(softwareRenderer->lcdc)) {
 697		_clearScreen(softwareRenderer);
 698	}
 699	if (softwareRenderer->model & GB_MODEL_SGB) {
 700		switch (softwareRenderer->sgbCommandHeader >> 3) {
 701		case SGB_PAL_SET:
 702		case SGB_ATTR_SET:
 703			if (softwareRenderer->sgbPacket[1] & 0x40) {
 704				renderer->sgbRenderMode = 0;
 705			}
 706			break;
 707		case SGB_PAL_TRN:
 708		case SGB_CHR_TRN:
 709		case SGB_PCT_TRN:
 710		case SGB_ATRC_EN:
 711		case SGB_MASK_EN:
 712			if (softwareRenderer->sgbBorders && !renderer->sgbRenderMode) {
 713				// Make sure every buffer sees this if we're multibuffering
 714				_regenerateSGBBorder(softwareRenderer);
 715			}
 716			// Fall through
 717		case SGB_ATTR_TRN:
 718			++softwareRenderer->sgbTransfer;
 719			if (softwareRenderer->sgbTransfer == 5) {
 720				softwareRenderer->sgbCommandHeader = 0;
 721			}
 722			break;
 723		default:
 724			break;
 725		}
 726	}
 727	softwareRenderer->lastY = GB_VIDEO_VERTICAL_PIXELS;
 728	softwareRenderer->lastX = 0;
 729	softwareRenderer->currentWy = 0;
 730	softwareRenderer->currentWx = 0;
 731	softwareRenderer->hasWindow = false;
 732}
 733
 734static void GBVideoSoftwareRendererEnableSGBBorder(struct GBVideoRenderer* renderer, bool enable) {
 735	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 736	if (softwareRenderer->model & GB_MODEL_SGB) {
 737		if (enable == softwareRenderer->sgbBorders) {
 738			return;
 739		}
 740		softwareRenderer->sgbBorders = enable;
 741		if (softwareRenderer->sgbBorders && !renderer->sgbRenderMode) {
 742			_regenerateSGBBorder(softwareRenderer);
 743		}
 744	}
 745}
 746
 747static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int sx, int sy) {
 748	uint8_t* data = renderer->d.vram;
 749	uint8_t* attr = &maps[GB_SIZE_VRAM_BANK0];
 750	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
 751		data += 0x1000;
 752	}
 753	int topY = ((sy >> 3) & 0x1F) * 0x20;
 754	int bottomY = sy & 7;
 755	if (startX < 0) {
 756		startX = 0;
 757	}
 758	int x;
 759	if ((startX + sx) & 7) {
 760		int startX2 = startX + 8 - ((startX + sx) & 7);
 761		for (x = startX; x < startX2; ++x) {
 762			uint8_t* localData = data;
 763			int localY = bottomY;
 764			int topX = ((x + sx) >> 3) & 0x1F;
 765			int bottomX = 7 - ((x + sx) & 7);
 766			int bgTile;
 767			if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
 768				bgTile = maps[topX + topY];
 769			} else {
 770				bgTile = ((int8_t*) maps)[topX + topY];
 771			}
 772			int p = 0;
 773			if (renderer->model >= GB_MODEL_CGB) {
 774				GBObjAttributes attrs = attr[topX + topY];
 775				p = GBObjAttributesGetCGBPalette(attrs) * 4;
 776				if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
 777					p |= 0x80;
 778				}
 779				if (GBObjAttributesIsBank(attrs)) {
 780					localData += GB_SIZE_VRAM_BANK0;
 781				}
 782				if (GBObjAttributesIsYFlip(attrs)) {
 783					localY = 7 - bottomY;
 784				}
 785				if (GBObjAttributesIsXFlip(attrs)) {
 786					bottomX = 7 - bottomX;
 787				}
 788			}
 789			uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
 790			uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
 791			tileDataUpper >>= bottomX;
 792			tileDataLower >>= bottomX;
 793			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
 794		}
 795		startX = startX2;
 796	}
 797	for (x = startX; x < endX; x += 8) {
 798		uint8_t* localData = data;
 799		int localY = bottomY;
 800		int topX = ((x + sx) >> 3) & 0x1F;
 801		int bgTile;
 802		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
 803			bgTile = maps[topX + topY];
 804		} else {
 805			bgTile = ((int8_t*) maps)[topX + topY];
 806		}
 807		int p = 0;
 808		if (renderer->model >= GB_MODEL_CGB) {
 809			GBObjAttributes attrs = attr[topX + topY];
 810			p = GBObjAttributesGetCGBPalette(attrs) * 4;
 811			if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
 812				p |= 0x80;
 813			}
 814			if (GBObjAttributesIsBank(attrs)) {
 815				localData += GB_SIZE_VRAM_BANK0;
 816			}
 817			if (GBObjAttributesIsYFlip(attrs)) {
 818				localY = 7 - bottomY;
 819			}
 820			if (GBObjAttributesIsXFlip(attrs)) {
 821				uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
 822				uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
 823				renderer->row[x + 0] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
 824				renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
 825				renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
 826				renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
 827				renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
 828				renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
 829				renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
 830				renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
 831				continue;
 832			}
 833		}
 834		uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
 835		uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
 836		renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
 837		renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
 838		renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
 839		renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
 840		renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
 841		renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
 842		renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
 843		renderer->row[x + 0] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
 844	}
 845}
 846
 847static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
 848	int objX = obj->x + renderer->objOffsetX;
 849	int ix = objX - 8;
 850	if (endX < ix || startX >= ix + 8) {
 851		return;
 852	}
 853	if (objX < endX) {
 854		endX = objX;
 855	}
 856	if (objX - 8 > startX) {
 857		startX = objX - 8;
 858	}
 859	if (startX < 0) {
 860		startX = 0;
 861	}
 862	uint8_t* data = renderer->d.vram;
 863	int tileOffset = 0;
 864	int bottomY;
 865	int objY = obj->y + renderer->objOffsetY;
 866	if (GBObjAttributesIsYFlip(obj->attr)) {
 867		bottomY = 7 - ((y - objY - 16) & 7);
 868		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - objY < -8) {
 869			++tileOffset;
 870		}
 871	} else {
 872		bottomY = (y - objY - 16) & 7;
 873		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - objY >= -8) {
 874			++tileOffset;
 875		}
 876	}
 877	if (GBRegisterLCDCIsObjSize(renderer->lcdc) && obj->tile & 1) {
 878		--tileOffset;
 879	}
 880	uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0x63 : 0x60;
 881	uint8_t mask2 = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x83;
 882	int p;
 883	if (renderer->model >= GB_MODEL_CGB) {
 884		p = (GBObjAttributesGetCGBPalette(obj->attr) + 8) * 4;
 885		if (GBObjAttributesIsBank(obj->attr)) {
 886			data += GB_SIZE_VRAM_BANK0;
 887		}
 888		if (!GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
 889			mask = 0x60;
 890			mask2 = 0x83;
 891		}
 892	} else {
 893		p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
 894	}
 895	int bottomX;
 896	int x = startX;
 897	if ((x - objX) & 7) {
 898		for (; x < endX; ++x) {
 899			if (GBObjAttributesIsXFlip(obj->attr)) {
 900				bottomX = (x - objX) & 7;
 901			} else {
 902				bottomX = 7 - ((x - objX) & 7);
 903			}
 904			int objTile = obj->tile + tileOffset;
 905			uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
 906			uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
 907			tileDataUpper >>= bottomX;
 908			tileDataLower >>= bottomX;
 909			color_t current = renderer->row[x];
 910			if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
 911				renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
 912			}
 913		}
 914	} else if (GBObjAttributesIsXFlip(obj->attr)) {
 915		int objTile = obj->tile + tileOffset;
 916		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
 917		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
 918		color_t current;
 919		current = renderer->row[x];
 920		if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
 921			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
 922		}
 923		current = renderer->row[x + 1];
 924		if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
 925			renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
 926		}
 927		current = renderer->row[x + 2];
 928		if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
 929			renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
 930		}
 931		current = renderer->row[x + 3];
 932		if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
 933			renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
 934		}
 935		current = renderer->row[x + 4];
 936		if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
 937			renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
 938		}
 939		current = renderer->row[x + 5];
 940		if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
 941			renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
 942		}
 943		current = renderer->row[x + 6];
 944		if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
 945			renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
 946		}
 947		current = renderer->row[x + 7];
 948		if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
 949			renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
 950		}
 951	} else {
 952		int objTile = obj->tile + tileOffset;
 953		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
 954		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
 955		color_t current;
 956		current = renderer->row[x + 7];
 957		if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
 958			renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
 959		}
 960		current = renderer->row[x + 6];
 961		if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
 962			renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
 963		}
 964		current = renderer->row[x + 5];
 965		if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
 966			renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
 967		}
 968		current = renderer->row[x + 4];
 969		if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
 970			renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
 971		}
 972		current = renderer->row[x + 3];
 973		if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
 974			renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
 975		}
 976		current = renderer->row[x + 2];
 977		if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
 978			renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
 979		}
 980		current = renderer->row[x + 1];
 981		if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
 982			renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
 983		}
 984		current = renderer->row[x];
 985		if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
 986			renderer->row[x] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
 987		}
 988	}
 989}
 990
 991static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
 992	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 993	*stride = softwareRenderer->outputBufferStride;
 994	*pixels = softwareRenderer->outputBuffer;
 995}
 996
 997static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
 998	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 999	// TODO: Share with GBAVideoSoftwareRendererGetPixels
1000
1001	const color_t* colorPixels = pixels;
1002	unsigned i;
1003	for (i = 0; i < GB_VIDEO_VERTICAL_PIXELS; ++i) {
1004		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], GB_VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
1005	}
1006}