all repos — mgba @ 837f952230ae3a83bdbfa4f3d459ba0841622daf

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