all repos — mgba @ f127525cac37e48843d77300d81a81e94a9b4fb5

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