all repos — mgba @ d620faba0a106abd916e1a909492d3a28853b70b

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