all repos — mgba @ 4a10ea2f699d0d2bade8df17d513ba5f90bef949

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