all repos — mgba @ 6e9507f0822f76bba63f29fba6a78d1f14f34b9f

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