all repos — mgba @ 65207f5c0f8173ac31c5f382e1f6f274ab6e701f

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 p = SGBBgAttributesGetPalette(mapData) * 0x10;
 79			if (SGBBgAttributesIsXFlip(mapData)) {
 80				renderer->outputBuffer[base + 0] = renderer->palette[p | ((tileData[0] >> 0) & 0x1) | ((tileData[1] << 1) & 0x2) | ((tileData[2] << 2) & 0x4) | ((tileData[3] << 3) & 0x8)];
 81				renderer->outputBuffer[base + 1] = renderer->palette[p | ((tileData[0] >> 1) & 0x1) | ((tileData[1] >> 0) & 0x2) | ((tileData[2] << 1) & 0x4) | ((tileData[3] << 2) & 0x8)];
 82				renderer->outputBuffer[base + 2] = renderer->palette[p | ((tileData[0] >> 2) & 0x1) | ((tileData[1] >> 1) & 0x2) | ((tileData[2] >> 0) & 0x4) | ((tileData[3] << 1) & 0x8)];
 83				renderer->outputBuffer[base + 3] = renderer->palette[p | ((tileData[0] >> 3) & 0x1) | ((tileData[1] >> 2) & 0x2) | ((tileData[2] >> 1) & 0x4) | ((tileData[3] >> 0) & 0x8)];
 84				renderer->outputBuffer[base + 4] = renderer->palette[p | ((tileData[0] >> 4) & 0x1) | ((tileData[1] >> 3) & 0x2) | ((tileData[2] >> 2) & 0x4) | ((tileData[3] >> 1) & 0x8)];
 85				renderer->outputBuffer[base + 5] = renderer->palette[p | ((tileData[0] >> 5) & 0x1) | ((tileData[1] >> 4) & 0x2) | ((tileData[2] >> 3) & 0x4) | ((tileData[3] >> 2) & 0x8)];
 86				renderer->outputBuffer[base + 6] = renderer->palette[p | ((tileData[0] >> 6) & 0x1) | ((tileData[1] >> 5) & 0x2) | ((tileData[2] >> 4) & 0x4) | ((tileData[3] >> 3) & 0x8)];
 87				renderer->outputBuffer[base + 7] = renderer->palette[p | ((tileData[0] >> 7) & 0x1) | ((tileData[1] >> 6) & 0x2) | ((tileData[2] >> 5) & 0x4) | ((tileData[3] >> 4) & 0x8)];
 88			} else {
 89				renderer->outputBuffer[base + 0] = renderer->palette[p | ((tileData[0] >> 7) & 0x1) | ((tileData[1] >> 6) & 0x2) | ((tileData[2] >> 5) & 0x4) | ((tileData[3] >> 4) & 0x8)];
 90				renderer->outputBuffer[base + 1] = renderer->palette[p | ((tileData[0] >> 6) & 0x1) | ((tileData[1] >> 5) & 0x2) | ((tileData[2] >> 4) & 0x4) | ((tileData[3] >> 3) & 0x8)];
 91				renderer->outputBuffer[base + 2] = renderer->palette[p | ((tileData[0] >> 5) & 0x1) | ((tileData[1] >> 4) & 0x2) | ((tileData[2] >> 3) & 0x4) | ((tileData[3] >> 2) & 0x8)];
 92				renderer->outputBuffer[base + 3] = renderer->palette[p | ((tileData[0] >> 4) & 0x1) | ((tileData[1] >> 3) & 0x2) | ((tileData[2] >> 2) & 0x4) | ((tileData[3] >> 1) & 0x8)];
 93				renderer->outputBuffer[base + 4] = renderer->palette[p | ((tileData[0] >> 3) & 0x1) | ((tileData[1] >> 2) & 0x2) | ((tileData[2] >> 1) & 0x4) | ((tileData[3] >> 0) & 0x8)];
 94				renderer->outputBuffer[base + 5] = renderer->palette[p | ((tileData[0] >> 2) & 0x1) | ((tileData[1] >> 1) & 0x2) | ((tileData[2] >> 0) & 0x4) | ((tileData[3] << 1) & 0x8)];
 95				renderer->outputBuffer[base + 6] = renderer->palette[p | ((tileData[0] >> 1) & 0x1) | ((tileData[1] >> 0) & 0x2) | ((tileData[2] << 1) & 0x4) | ((tileData[3] << 2) & 0x8)];
 96				renderer->outputBuffer[base + 7] = renderer->palette[p | ((tileData[0] >> 0) & 0x1) | ((tileData[1] << 1) & 0x2) | ((tileData[2] << 2) & 0x4) | ((tileData[3] << 3) & 0x8)];
 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 + 1 - 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_ATRC_EN:
307		if (softwareRenderer->sgbBorders) {
308			_regenerateSGBBorder(softwareRenderer);
309		}
310		break;
311	}
312}
313
314static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
315	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
316	color_t color = mColorFrom555(value);
317	softwareRenderer->palette[index] = color;
318	if (renderer->cache) {
319		mCacheSetWritePalette(renderer->cache, index, color);
320	}
321}
322
323static void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
324	if (renderer->cache) {
325		mCacheSetWriteVRAM(renderer->cache, address);
326	}
327}
328
329static void GBVideoSoftwareRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
330	UNUSED(renderer);
331	UNUSED(oam);
332	// Nothing to do
333}
334
335static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
336	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
337	softwareRenderer->lastY = y;
338	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
339	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
340		maps += GB_SIZE_MAP;
341	}
342	if (softwareRenderer->d.disableBG) {
343		memset(&softwareRenderer->row[startX], 0, endX - startX);
344	}
345	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) || softwareRenderer->model >= GB_MODEL_CGB) {
346		int wy = softwareRenderer->wy + softwareRenderer->currentWy;
347		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && wy <= y && endX >= softwareRenderer->wx - 7) {
348			if (softwareRenderer->wx - 7 > 0 && !softwareRenderer->d.disableBG) {
349				GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, softwareRenderer->wx - 7, softwareRenderer->scx, softwareRenderer->scy + y);
350			}
351
352			maps = &softwareRenderer->d.vram[GB_BASE_MAP];
353			if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
354				maps += GB_SIZE_MAP;
355			}
356			if (!softwareRenderer->d.disableWIN) {
357				GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, 7 - softwareRenderer->wx, y - wy);
358			}
359		} else if (!softwareRenderer->d.disableBG) {
360			GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, softwareRenderer->scx, softwareRenderer->scy + y);
361		}
362	} else if (!softwareRenderer->d.disableBG) {
363		memset(&softwareRenderer->row[startX], 0, endX - startX);
364	}
365
366	if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc) && !softwareRenderer->d.disableOBJ) {
367		size_t i;
368		for (i = 0; i < oamMax; ++i) {
369			GBVideoSoftwareRendererDrawObj(softwareRenderer, &obj[i], startX, endX, y);
370		}
371	}
372
373	size_t sgbOffset = 0;
374	if (softwareRenderer->model == GB_MODEL_SGB && softwareRenderer->sgbBorders) {
375		sgbOffset = softwareRenderer->outputBufferStride * 40 + 48;
376	}
377	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + sgbOffset];
378	int x = startX;
379	int p = 0;
380	switch (softwareRenderer->d.sgbRenderMode) {
381	case 0:
382		if (softwareRenderer->model == GB_MODEL_SGB) {
383			p = softwareRenderer->d.sgbAttributes[(startX >> 5) + 5 * (y >> 3)];
384			p >>= 6 - ((x / 4) & 0x6);
385			p &= 3;
386			p <<= 2;
387		}
388		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
389			row[x] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
390		}
391		for (; x + 7 < (endX & ~7); x += 8) {
392			if (softwareRenderer->model == GB_MODEL_SGB) {
393				p = softwareRenderer->d.sgbAttributes[(x >> 5) + 5 * (y >> 3)];
394				p >>= 6 - ((x / 4) & 0x6);
395				p &= 3;
396				p <<= 2;
397			}
398			row[x + 0] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
399			row[x + 1] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 1] & 0x7F]];
400			row[x + 2] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 2] & 0x7F]];
401			row[x + 3] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 3] & 0x7F]];
402			row[x + 4] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 4] & 0x7F]];
403			row[x + 5] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 5] & 0x7F]];
404			row[x + 6] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 6] & 0x7F]];
405			row[x + 7] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 7] & 0x7F]];
406		}
407		if (softwareRenderer->model == GB_MODEL_SGB) {
408			p = softwareRenderer->d.sgbAttributes[(x >> 5) + 5 * (y >> 3)];
409			p >>= 6 - ((x / 4) & 0x6);
410			p &= 3;
411			p <<= 2;
412		}
413		for (; x < endX; ++x) {
414			row[x] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
415		}
416		break;
417	case 1:
418		break;
419	case 2:
420		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
421			row[x] = 0;
422		}
423		for (; x + 7 < (endX & ~7); x += 8) {
424			row[x] = 0;
425			row[x + 1] = 0;
426			row[x + 2] = 0;
427			row[x + 3] = 0;
428			row[x + 4] = 0;
429			row[x + 5] = 0;
430			row[x + 6] = 0;
431			row[x + 7] = 0;
432		}
433		for (; x < endX; ++x) {
434			row[x] = 0;
435		}
436		break;
437	case 3:
438		for (; x < ((startX + 7) & ~7) && x < endX; ++x) {
439			row[x] = softwareRenderer->palette[0];
440		}
441		for (; x + 7 < (endX & ~7); x += 8) {
442			row[x] = softwareRenderer->palette[0];
443			row[x + 1] = softwareRenderer->palette[0];
444			row[x + 2] = softwareRenderer->palette[0];
445			row[x + 3] = softwareRenderer->palette[0];
446			row[x + 4] = softwareRenderer->palette[0];
447			row[x + 5] = softwareRenderer->palette[0];
448			row[x + 6] = softwareRenderer->palette[0];
449			row[x + 7] = softwareRenderer->palette[0];
450		}
451		for (; x < endX; ++x) {
452			row[x] = softwareRenderer->palette[0];
453		}
454		break;
455	}
456}
457
458static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
459	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
460
461	if (softwareRenderer->sgbTransfer == 1) {
462		size_t offset = 2 * ((y & 7) + (y >> 3) * GB_VIDEO_HORIZONTAL_PIXELS);
463		if (offset >= 0x1000) {
464			return;
465		}
466		uint8_t* buffer = NULL;
467		switch (softwareRenderer->sgbCommandHeader >> 3) {
468		case SGB_PAL_TRN:
469			buffer = renderer->sgbPalRam;
470			break;
471		case SGB_CHR_TRN:
472			buffer = &renderer->sgbCharRam[SGB_SIZE_CHAR_RAM / 2 * (softwareRenderer->sgbPacket[1] & 1)];
473			break;
474		case SGB_PCT_TRN:
475			buffer = renderer->sgbMapRam;
476			break;
477		case SGB_ATTR_TRN:
478			buffer = renderer->sgbAttributeFiles;
479			break;
480		default:
481			break;
482		}
483		if (buffer) {
484			int i;
485			for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS; i += 8) {
486				if (UNLIKELY(offset + (i << 1) + 1 >= 0x1000)) {
487					break;
488				}
489				uint8_t hi = 0;
490				uint8_t lo = 0;
491				hi |= (softwareRenderer->row[i + 0] & 0x2) << 6;
492				lo |= (softwareRenderer->row[i + 0] & 0x1) << 7;
493				hi |= (softwareRenderer->row[i + 1] & 0x2) << 5;
494				lo |= (softwareRenderer->row[i + 1] & 0x1) << 6;
495				hi |= (softwareRenderer->row[i + 2] & 0x2) << 4;
496				lo |= (softwareRenderer->row[i + 2] & 0x1) << 5;
497				hi |= (softwareRenderer->row[i + 3] & 0x2) << 3;
498				lo |= (softwareRenderer->row[i + 3] & 0x1) << 4;
499				hi |= (softwareRenderer->row[i + 4] & 0x2) << 2;
500				lo |= (softwareRenderer->row[i + 4] & 0x1) << 3;
501				hi |= (softwareRenderer->row[i + 5] & 0x2) << 1;
502				lo |= (softwareRenderer->row[i + 5] & 0x1) << 2;
503				hi |= (softwareRenderer->row[i + 6] & 0x2) << 0;
504				lo |= (softwareRenderer->row[i + 6] & 0x1) << 1;
505				hi |= (softwareRenderer->row[i + 7] & 0x2) >> 1;
506				lo |= (softwareRenderer->row[i + 7] & 0x1) >> 0;
507				buffer[offset + (i << 1) + 0] = lo;
508				buffer[offset + (i << 1) + 1] = hi;
509			}
510		}
511	}
512}
513
514static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
515	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
516
517	if (softwareRenderer->temporaryBuffer) {
518		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
519		softwareRenderer->temporaryBuffer = 0;
520	}
521	if (!GBRegisterLCDCIsEnable(softwareRenderer->lcdc)) {
522		_clearScreen(softwareRenderer);
523	}
524	if (softwareRenderer->model == GB_MODEL_SGB) {
525		switch (softwareRenderer->sgbCommandHeader >> 3) {
526		case SGB_PAL_SET:
527		case SGB_ATTR_SET:
528			if (softwareRenderer->sgbPacket[1] & 0x40) {
529				renderer->sgbRenderMode = 0;
530			}
531			break;
532		case SGB_PAL_TRN:
533		case SGB_CHR_TRN:
534		case SGB_PCT_TRN:
535			if (softwareRenderer->sgbTransfer > 0 && softwareRenderer->sgbBorders) {
536				// Make sure every buffer sees this if we're multibuffering
537				_regenerateSGBBorder(softwareRenderer);
538			}
539			// Fall through
540		case SGB_ATTR_TRN:
541			++softwareRenderer->sgbTransfer;
542			if (softwareRenderer->sgbTransfer == 5) {
543				softwareRenderer->sgbCommandHeader = 0;
544			}
545			break;
546		default:
547			break;
548		}
549	}
550	softwareRenderer->lastY = GB_VIDEO_VERTICAL_PIXELS;
551	softwareRenderer->currentWy = 0;
552	softwareRenderer->hasWindow = false;
553}
554
555static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int sx, int sy) {
556	uint8_t* data = renderer->d.vram;
557	uint8_t* attr = &maps[GB_SIZE_VRAM_BANK0];
558	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
559		data += 0x1000;
560	}
561	int topY = ((sy >> 3) & 0x1F) * 0x20;
562	int bottomY = sy & 7;
563	if (startX < 0) {
564		startX = 0;
565	}
566	int x;
567	if ((startX + sx) & 7) {
568		int startX2 = startX + 8 - ((startX + sx) & 7);
569		for (x = startX; x < startX2; ++x) {
570			uint8_t* localData = data;
571			int localY = bottomY;
572			int topX = ((x + sx) >> 3) & 0x1F;
573			int bottomX = 7 - ((x + sx) & 7);
574			int bgTile;
575			if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
576				bgTile = maps[topX + topY];
577			} else {
578				bgTile = ((int8_t*) maps)[topX + topY];
579			}
580			int p = 0;
581			if (renderer->model >= GB_MODEL_CGB) {
582				GBObjAttributes attrs = attr[topX + topY];
583				p = GBObjAttributesGetCGBPalette(attrs) * 4;
584				if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
585					p |= 0x80;
586				}
587				if (GBObjAttributesIsBank(attrs)) {
588					localData += GB_SIZE_VRAM_BANK0;
589				}
590				if (GBObjAttributesIsYFlip(attrs)) {
591					localY = 7 - bottomY;
592				}
593				if (GBObjAttributesIsXFlip(attrs)) {
594					bottomX = 7 - bottomX;
595				}
596			}
597			uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
598			uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
599			tileDataUpper >>= bottomX;
600			tileDataLower >>= bottomX;
601			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
602		}
603		startX = startX2;
604	}
605	for (x = startX; x < endX; x += 8) {
606		uint8_t* localData = data;
607		int localY = bottomY;
608		int topX = ((x + sx) >> 3) & 0x1F;
609		int bgTile;
610		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
611			bgTile = maps[topX + topY];
612		} else {
613			bgTile = ((int8_t*) maps)[topX + topY];
614		}
615		int p = 0;
616		if (renderer->model >= GB_MODEL_CGB) {
617			GBObjAttributes attrs = attr[topX + topY];
618			p = GBObjAttributesGetCGBPalette(attrs) * 4;
619			if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
620				p |= 0x80;
621			}
622			if (GBObjAttributesIsBank(attrs)) {
623				localData += GB_SIZE_VRAM_BANK0;
624			}
625			if (GBObjAttributesIsYFlip(attrs)) {
626				localY = 7 - bottomY;
627			}
628			if (GBObjAttributesIsXFlip(attrs)) {
629				uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
630				uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
631				renderer->row[x + 0] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
632				renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
633				renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
634				renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
635				renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
636				renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
637				renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
638				renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
639				continue;
640			}
641		}
642		uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
643		uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
644		renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
645		renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
646		renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
647		renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
648		renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
649		renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
650		renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
651		renderer->row[x + 0] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
652	}
653}
654
655static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
656	int ix = obj->x - 8;
657	if (endX < ix || startX >= ix + 8) {
658		return;
659	}
660	if (obj->x < endX) {
661		endX = obj->x;
662	}
663	if (obj->x - 8 > startX) {
664		startX = obj->x - 8;
665	}
666	if (startX < 0) {
667		startX = 0;
668	}
669	uint8_t* data = renderer->d.vram;
670	int tileOffset = 0;
671	int bottomY;
672	if (GBObjAttributesIsYFlip(obj->attr)) {
673		bottomY = 7 - ((y - obj->y - 16) & 7);
674		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
675			++tileOffset;
676		}
677	} else {
678		bottomY = (y - obj->y - 16) & 7;
679		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
680			++tileOffset;
681		}
682	}
683	if (GBRegisterLCDCIsObjSize(renderer->lcdc) && obj->tile & 1) {
684		--tileOffset;
685	}
686	uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0x63 : 0x60;
687	uint8_t mask2 = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x83;
688	int p;
689	if (renderer->model >= GB_MODEL_CGB) {
690		p = (GBObjAttributesGetCGBPalette(obj->attr) + 8) * 4;
691		if (GBObjAttributesIsBank(obj->attr)) {
692			data += GB_SIZE_VRAM_BANK0;
693		}
694		if (!GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
695			mask = 0x60;
696			mask2 = 0x83;
697		}
698	} else {
699		p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
700	}
701	int bottomX;
702	int x = startX;
703	if ((x - obj->x) & 7) {
704		for (; x < endX; ++x) {
705			if (GBObjAttributesIsXFlip(obj->attr)) {
706				bottomX = (x - obj->x) & 7;
707			} else {
708				bottomX = 7 - ((x - obj->x) & 7);
709			}
710			int objTile = obj->tile + tileOffset;
711			uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
712			uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
713			tileDataUpper >>= bottomX;
714			tileDataLower >>= bottomX;
715			color_t current = renderer->row[x];
716			if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
717				renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
718			}
719		}
720	} else if (GBObjAttributesIsXFlip(obj->attr)) {
721		int objTile = obj->tile + tileOffset;
722		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
723		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
724		color_t current;
725		current = renderer->row[x];
726		if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
727			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
728		}
729		current = renderer->row[x + 1];
730		if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
731			renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
732		}
733		current = renderer->row[x + 2];
734		if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
735			renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
736		}
737		current = renderer->row[x + 3];
738		if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
739			renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
740		}
741		current = renderer->row[x + 4];
742		if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
743			renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
744		}
745		current = renderer->row[x + 5];
746		if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
747			renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
748		}
749		current = renderer->row[x + 6];
750		if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
751			renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
752		}
753		current = renderer->row[x + 7];
754		if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
755			renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
756		}
757	} else {
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		color_t current;
762		current = renderer->row[x + 7];
763		if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
764			renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
765		}
766		current = renderer->row[x + 6];
767		if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
768			renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
769		}
770		current = renderer->row[x + 5];
771		if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
772			renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
773		}
774		current = renderer->row[x + 4];
775		if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
776			renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
777		}
778		current = renderer->row[x + 3];
779		if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
780			renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
781		}
782		current = renderer->row[x + 2];
783		if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
784			renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
785		}
786		current = renderer->row[x + 1];
787		if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
788			renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
789		}
790		current = renderer->row[x];
791		if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
792			renderer->row[x] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
793		}
794	}
795}
796
797static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
798	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
799	*stride = softwareRenderer->outputBufferStride;
800	*pixels = softwareRenderer->outputBuffer;
801}
802
803static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
804	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
805	// TODO: Share with GBAVideoSoftwareRendererGetPixels
806
807	const color_t* colorPixels = pixels;
808	unsigned i;
809	for (i = 0; i < GB_VIDEO_VERTICAL_PIXELS; ++i) {
810		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], GB_VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
811	}
812}