all repos — mgba @ 35a13753bb414253c5901013b3dcc1b05ff9dc6c

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/tile-cache.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);
 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) {
 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) {
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	int i;
181	for (i = 0; i < 64; ++i) {
182		softwareRenderer->lookup[i] = i;
183		softwareRenderer->lookup[i] = i;
184		softwareRenderer->lookup[i] = i;
185		softwareRenderer->lookup[i] = i;
186	}
187}
188
189static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
190	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
191	UNUSED(softwareRenderer);
192}
193
194static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
195	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
196	switch (address) {
197	case REG_LCDC:
198		softwareRenderer->lcdc = value;
199		break;
200	case REG_SCY:
201		softwareRenderer->scy = value;
202		break;
203	case REG_SCX:
204		softwareRenderer->scx = value;
205		break;
206	case REG_WY:
207		softwareRenderer->wy = value;
208		break;
209	case REG_WX:
210		softwareRenderer->wx = value;
211		break;
212	case REG_BGP:
213		softwareRenderer->lookup[0] = value & 3;
214		softwareRenderer->lookup[1] = (value >> 2) & 3;
215		softwareRenderer->lookup[2] = (value >> 4) & 3;
216		softwareRenderer->lookup[3] = (value >> 6) & 3;
217		break;
218	case REG_OBP0:
219		softwareRenderer->lookup[0x20 + 0] = value & 3;
220		softwareRenderer->lookup[0x20 + 1] = (value >> 2) & 3;
221		softwareRenderer->lookup[0x20 + 2] = (value >> 4) & 3;
222		softwareRenderer->lookup[0x20 + 3] = (value >> 6) & 3;
223		break;
224	case REG_OBP1:
225		softwareRenderer->lookup[0x24 + 0] = value & 3;
226		softwareRenderer->lookup[0x24 + 1] = (value >> 2) & 3;
227		softwareRenderer->lookup[0x24 + 2] = (value >> 4) & 3;
228		softwareRenderer->lookup[0x24 + 3] = (value >> 6) & 3;
229		break;
230	}
231	return value;
232}
233
234static void GBVideoSoftwareRendererWriteSGBPacket(struct GBVideoRenderer* renderer, uint8_t* data) {
235	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
236	memcpy(softwareRenderer->sgbPacket, data, sizeof(softwareRenderer->sgbPacket));
237	int i;
238	if (!(softwareRenderer->sgbCommandHeader & 7)) {
239		softwareRenderer->sgbCommandHeader = data[0];
240		softwareRenderer->sgbPacketId = 0;
241		softwareRenderer->sgbTransfer = 0;
242	}
243	--softwareRenderer->sgbCommandHeader;
244	++softwareRenderer->sgbPacketId;
245	int set;
246	switch (softwareRenderer->sgbCommandHeader >> 3) {
247	case SGB_PAL_SET:
248		softwareRenderer->sgbPacket[1] = data[9];
249		if (!(data[9] & 0x80)) {
250			break;
251		}
252		// Fall through
253	case SGB_ATTR_SET:
254		set = softwareRenderer->sgbPacket[1] & 0x3F;
255		if (set <= 0x2C) {
256			memcpy(renderer->sgbAttributes, &renderer->sgbAttributeFiles[set * 90], 90);
257		}
258		break;
259	case SGB_ATTR_BLK:
260		if (softwareRenderer->sgbPacketId == 1) {
261			softwareRenderer->sgbDataSets = softwareRenderer->sgbPacket[1];
262			i = 2;
263		} else {
264			i = (9 - softwareRenderer->sgbPacketId) % 3 * -2;
265		}
266		for (; i <= 10 && softwareRenderer->sgbDataSets; i += 6, --softwareRenderer->sgbDataSets) {
267			_parseAttrBlock(softwareRenderer, i);
268		}
269		if (i < 16 && softwareRenderer->sgbDataSets) {
270			memcpy(softwareRenderer->sgbPartialDataSet, &softwareRenderer->sgbPacket[i], 16 - i);
271		}
272		break;
273	case SGB_ATRC_EN:
274		_regenerateSGBBorder(softwareRenderer);
275		break;
276	}
277}
278
279static void GBVideoSoftwareRendererWritePalette(struct GBVideoRenderer* renderer, int index, uint16_t value) {
280	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
281	color_t color = mColorFrom555(value);
282	softwareRenderer->palette[index] = color;
283	if (renderer->cache) {
284		mTileCacheWritePalette(renderer->cache, index << 1);
285	}
286}
287
288static void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
289	if (renderer->cache) {
290		mTileCacheWriteVRAM(renderer->cache, address);
291	}
292}
293
294static void GBVideoSoftwareRendererWriteOAM(struct GBVideoRenderer* renderer, uint16_t oam) {
295	UNUSED(renderer);
296	UNUSED(oam);
297	// Nothing to do
298}
299
300static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj* obj, size_t oamMax) {
301	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
302	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
303	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
304		maps += GB_SIZE_MAP;
305	}
306	if (softwareRenderer->d.disableBG) {
307		memset(&softwareRenderer->row[startX], 0, endX - startX);
308	}
309	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) || softwareRenderer->model >= GB_MODEL_CGB) {
310		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && endX >= softwareRenderer->wx - 7) {
311			if (softwareRenderer->wx - 7 > 0 && !softwareRenderer->d.disableBG) {
312				GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, softwareRenderer->wx - 7, softwareRenderer->scx, softwareRenderer->scy + y);
313			}
314
315			maps = &softwareRenderer->d.vram[GB_BASE_MAP];
316			if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
317				maps += GB_SIZE_MAP;
318			}
319			if (!softwareRenderer->d.disableWIN) {
320				GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, 7 - softwareRenderer->wx, softwareRenderer->currentWy);
321			}
322		} else if (!softwareRenderer->d.disableBG) {
323			GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, softwareRenderer->scx, softwareRenderer->scy + y);
324		}
325	} else if (!softwareRenderer->d.disableBG) {
326		memset(&softwareRenderer->row[startX], 0, endX - startX);
327	}
328
329	if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc) && !softwareRenderer->d.disableOBJ) {
330		size_t i;
331		for (i = 0; i < oamMax; ++i) {
332			GBVideoSoftwareRendererDrawObj(softwareRenderer, &obj[i], startX, endX, y);
333		}
334	}
335}
336
337static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
338	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
339	size_t sgbOffset = 0;
340	if (softwareRenderer->model == GB_MODEL_SGB) {
341		sgbOffset = softwareRenderer->outputBufferStride * 40 + 48;
342	}
343	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y + sgbOffset];
344	int x;
345	switch (softwareRenderer->d.sgbRenderMode) {
346	case 0:
347		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; x += 8) {
348			int p = 0;
349			if (softwareRenderer->model == GB_MODEL_SGB) {
350				p = softwareRenderer->d.sgbAttributes[(x >> 5) + 5 * (y >> 3)];
351				p >>= 6 - ((x / 4) & 0x6);
352				p &= 3;
353				p <<= 2;
354			}
355			row[x + 0] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x] & 0x7F]];
356			row[x + 1] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 1] & 0x7F]];
357			row[x + 2] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 2] & 0x7F]];
358			row[x + 3] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 3] & 0x7F]];
359			row[x + 4] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 4] & 0x7F]];
360			row[x + 5] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 5] & 0x7F]];
361			row[x + 6] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 6] & 0x7F]];
362			row[x + 7] = softwareRenderer->palette[p | softwareRenderer->lookup[softwareRenderer->row[x + 7] & 0x7F]];
363		}
364		break;
365	case 1:
366		break;
367	case 2:
368		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; x += 8) {
369			row[x] = 0;
370			row[x + 1] = 0;
371			row[x + 2] = 0;
372			row[x + 3] = 0;
373			row[x + 4] = 0;
374			row[x + 5] = 0;
375			row[x + 6] = 0;
376			row[x + 7] = 0;
377		}
378		break;
379	case 3:
380		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; x += 8) {
381			row[x] = softwareRenderer->palette[0];
382			row[x + 1] = softwareRenderer->palette[0];
383			row[x + 2] = softwareRenderer->palette[0];
384			row[x + 3] = softwareRenderer->palette[0];
385			row[x + 4] = softwareRenderer->palette[0];
386			row[x + 5] = softwareRenderer->palette[0];
387			row[x + 6] = softwareRenderer->palette[0];
388			row[x + 7] = softwareRenderer->palette[0];
389		}
390		break;
391	}
392
393	if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && softwareRenderer->wx - 7 < GB_VIDEO_HORIZONTAL_PIXELS) {
394		++softwareRenderer->currentWy;
395	}
396	if (softwareRenderer->sgbTransfer == 1) {
397		size_t offset = 2 * ((y & 7) + (y >> 3) * GB_VIDEO_HORIZONTAL_PIXELS);
398		if (offset >= 0x1000) {
399			return;
400		}
401		uint8_t* buffer = NULL;
402		switch (softwareRenderer->sgbCommandHeader >> 3) {
403		case SGB_PAL_TRN:
404			buffer = renderer->sgbPalRam;
405			break;
406		case SGB_CHR_TRN:
407			buffer = &renderer->sgbCharRam[SGB_SIZE_CHAR_RAM / 2 * (softwareRenderer->sgbPacket[1] & 1)];
408			break;
409		case SGB_PCT_TRN:
410			buffer = renderer->sgbMapRam;
411			break;
412		case SGB_ATTR_TRN:
413			buffer = renderer->sgbAttributeFiles;
414			break;
415		default:
416			break;
417		}
418		if (buffer) {
419			int i;
420			for (i = 0; i < GB_VIDEO_HORIZONTAL_PIXELS; i += 8) {
421				if (UNLIKELY(offset + (i << 1) + 1 >= 0x1000)) {
422					break;
423				}
424				uint8_t hi = 0;
425				uint8_t lo = 0;
426				hi |= (softwareRenderer->row[i + 0] & 0x2) << 6;
427				lo |= (softwareRenderer->row[i + 0] & 0x1) << 7;
428				hi |= (softwareRenderer->row[i + 1] & 0x2) << 5;
429				lo |= (softwareRenderer->row[i + 1] & 0x1) << 6;
430				hi |= (softwareRenderer->row[i + 2] & 0x2) << 4;
431				lo |= (softwareRenderer->row[i + 2] & 0x1) << 5;
432				hi |= (softwareRenderer->row[i + 3] & 0x2) << 3;
433				lo |= (softwareRenderer->row[i + 3] & 0x1) << 4;
434				hi |= (softwareRenderer->row[i + 4] & 0x2) << 2;
435				lo |= (softwareRenderer->row[i + 4] & 0x1) << 3;
436				hi |= (softwareRenderer->row[i + 5] & 0x2) << 1;
437				lo |= (softwareRenderer->row[i + 5] & 0x1) << 2;
438				hi |= (softwareRenderer->row[i + 6] & 0x2) << 0;
439				lo |= (softwareRenderer->row[i + 6] & 0x1) << 1;
440				hi |= (softwareRenderer->row[i + 7] & 0x2) >> 1;
441				lo |= (softwareRenderer->row[i + 7] & 0x1) >> 0;
442				buffer[offset + (i << 1) + 0] = lo;
443				buffer[offset + (i << 1) + 1] = hi;
444			}
445		}
446	}
447}
448
449static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
450	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
451
452	if (softwareRenderer->temporaryBuffer) {
453		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
454		softwareRenderer->temporaryBuffer = 0;
455	}
456	if (!GBRegisterLCDCIsEnable(softwareRenderer->lcdc)) {
457		_clearScreen(softwareRenderer);
458	}
459	if (softwareRenderer->model == GB_MODEL_SGB) {
460		switch (softwareRenderer->sgbCommandHeader >> 3) {
461		case SGB_PAL_SET:
462		case SGB_ATTR_SET:
463			if (softwareRenderer->sgbPacket[1] & 0x40) {
464				renderer->sgbRenderMode = 0;
465			}
466			break;
467		case SGB_PAL_TRN:
468		case SGB_CHR_TRN:
469		case SGB_PCT_TRN:
470			if (softwareRenderer->sgbTransfer > 0) {
471				// Make sure every buffer sees this if we're multibuffering
472				_regenerateSGBBorder(softwareRenderer);
473			}
474			// Fall through
475		case SGB_ATTR_TRN:
476			++softwareRenderer->sgbTransfer;
477			if (softwareRenderer->sgbTransfer == 5) {
478				softwareRenderer->sgbCommandHeader = 0;
479			}
480			break;
481		default:
482			break;
483		}
484	}
485	softwareRenderer->currentWy = 0;
486}
487
488static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int sx, int sy) {
489	uint8_t* data = renderer->d.vram;
490	uint8_t* attr = &maps[GB_SIZE_VRAM_BANK0];
491	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
492		data += 0x1000;
493	}
494	int topY = ((sy >> 3) & 0x1F) * 0x20;
495	int bottomY = sy & 7;
496	if (startX < 0) {
497		startX = 0;
498	}
499	int x;
500	if ((startX + sx) & 7) {
501		int startX2 = startX + 8 - ((startX + sx) & 7);
502		for (x = startX; x < startX2; ++x) {
503			uint8_t* localData = data;
504			int localY = bottomY;
505			int topX = ((x + sx) >> 3) & 0x1F;
506			int bottomX = 7 - ((x + sx) & 7);
507			int bgTile;
508			if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
509				bgTile = maps[topX + topY];
510			} else {
511				bgTile = ((int8_t*) maps)[topX + topY];
512			}
513			int p = 0;
514			if (renderer->model >= GB_MODEL_CGB) {
515				GBObjAttributes attrs = attr[topX + topY];
516				p = GBObjAttributesGetCGBPalette(attrs) * 4;
517				if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
518					p |= 0x80;
519				}
520				if (GBObjAttributesIsBank(attrs)) {
521					localData += GB_SIZE_VRAM_BANK0;
522				}
523				if (GBObjAttributesIsYFlip(attrs)) {
524					localY = 7 - bottomY;
525				}
526				if (GBObjAttributesIsXFlip(attrs)) {
527					bottomX = 7 - bottomX;
528				}
529			}
530			uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
531			uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
532			tileDataUpper >>= bottomX;
533			tileDataLower >>= bottomX;
534			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
535		}
536		startX = startX2;
537	}
538	for (x = startX; x < endX; x += 8) {
539		uint8_t* localData = data;
540		int localY = bottomY;
541		int topX = ((x + sx) >> 3) & 0x1F;
542		int bgTile;
543		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
544			bgTile = maps[topX + topY];
545		} else {
546			bgTile = ((int8_t*) maps)[topX + topY];
547		}
548		int p = 0;
549		if (renderer->model >= GB_MODEL_CGB) {
550			GBObjAttributes attrs = attr[topX + topY];
551			p = GBObjAttributesGetCGBPalette(attrs) * 4;
552			if (GBObjAttributesIsPriority(attrs) && GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
553				p |= 0x80;
554			}
555			if (GBObjAttributesIsBank(attrs)) {
556				localData += GB_SIZE_VRAM_BANK0;
557			}
558			if (GBObjAttributesIsYFlip(attrs)) {
559				localY = 7 - bottomY;
560			}
561			if (GBObjAttributesIsXFlip(attrs)) {
562				uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
563				uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
564				renderer->row[x + 0] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
565				renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
566				renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
567				renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
568				renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
569				renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
570				renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
571				renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
572				continue;
573			}
574		}
575		uint8_t tileDataLower = localData[(bgTile * 8 + localY) * 2];
576		uint8_t tileDataUpper = localData[(bgTile * 8 + localY) * 2 + 1];
577		renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
578		renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
579		renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
580		renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
581		renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
582		renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
583		renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
584		renderer->row[x + 0] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
585	}
586}
587
588static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
589	int ix = obj->x - 8;
590	if (endX < ix || startX >= ix + 8) {
591		return;
592	}
593	if (obj->x < endX) {
594		endX = obj->x;
595	}
596	if (obj->x - 8 > startX) {
597		startX = obj->x - 8;
598	}
599	if (startX < 0) {
600		startX = 0;
601	}
602	uint8_t* data = renderer->d.vram;
603	int tileOffset = 0;
604	int bottomY;
605	if (GBObjAttributesIsYFlip(obj->attr)) {
606		bottomY = 7 - ((y - obj->y - 16) & 7);
607		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
608			++tileOffset;
609		}
610	} else {
611		bottomY = (y - obj->y - 16) & 7;
612		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
613			++tileOffset;
614		}
615	}
616	if (GBRegisterLCDCIsObjSize(renderer->lcdc) && obj->tile & 1) {
617		--tileOffset;
618	}
619	uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0x63 : 0x60;
620	uint8_t mask2 = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x83;
621	int p;
622	if (renderer->model >= GB_MODEL_CGB) {
623		p = (GBObjAttributesGetCGBPalette(obj->attr) + 8) * 4;
624		if (GBObjAttributesIsBank(obj->attr)) {
625			data += GB_SIZE_VRAM_BANK0;
626		}
627		if (!GBRegisterLCDCIsBgEnable(renderer->lcdc)) {
628			mask = 0x60;
629			mask2 = 0x83;
630		}
631	} else {
632		p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
633	}
634	int bottomX;
635	int x = startX;
636	if ((x - obj->x) & 7) {
637		for (; x < endX; ++x) {
638			if (GBObjAttributesIsXFlip(obj->attr)) {
639				bottomX = (x - obj->x) & 7;
640			} else {
641				bottomX = 7 - ((x - obj->x) & 7);
642			}
643			int objTile = obj->tile + tileOffset;
644			uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
645			uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
646			tileDataUpper >>= bottomX;
647			tileDataLower >>= bottomX;
648			color_t current = renderer->row[x];
649			if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
650				renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
651			}
652		}
653	} else if (GBObjAttributesIsXFlip(obj->attr)) {
654		int objTile = obj->tile + tileOffset;
655		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
656		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
657		color_t current;
658		current = renderer->row[x];
659		if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
660			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
661		}
662		current = renderer->row[x + 1];
663		if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
664			renderer->row[x + 1] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
665		}
666		current = renderer->row[x + 2];
667		if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
668			renderer->row[x + 2] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
669		}
670		current = renderer->row[x + 3];
671		if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
672			renderer->row[x + 3] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
673		}
674		current = renderer->row[x + 4];
675		if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
676			renderer->row[x + 4] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
677		}
678		current = renderer->row[x + 5];
679		if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
680			renderer->row[x + 5] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
681		}
682		current = renderer->row[x + 6];
683		if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
684			renderer->row[x + 6] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
685		}
686		current = renderer->row[x + 7];
687		if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
688			renderer->row[x + 7] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
689		}
690	} else {
691		int objTile = obj->tile + tileOffset;
692		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
693		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
694		color_t current;
695		current = renderer->row[x + 7];
696		if (((tileDataUpper | tileDataLower) & 1) && !(current & mask) && (current & mask2) <= 0x80) {
697			renderer->row[x + 7] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
698		}
699		current = renderer->row[x + 6];
700		if (((tileDataUpper | tileDataLower) & 2) && !(current & mask) && (current & mask2) <= 0x80) {
701			renderer->row[x + 6] = p | (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
702		}
703		current = renderer->row[x + 5];
704		if (((tileDataUpper | tileDataLower) & 4) && !(current & mask) && (current & mask2) <= 0x80) {
705			renderer->row[x + 5] = p | ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
706		}
707		current = renderer->row[x + 4];
708		if (((tileDataUpper | tileDataLower) & 8) && !(current & mask) && (current & mask2) <= 0x80) {
709			renderer->row[x + 4] = p | ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
710		}
711		current = renderer->row[x + 3];
712		if (((tileDataUpper | tileDataLower) & 16) && !(current & mask) && (current & mask2) <= 0x80) {
713			renderer->row[x + 3] = p | ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
714		}
715		current = renderer->row[x + 2];
716		if (((tileDataUpper | tileDataLower) & 32) && !(current & mask) && (current & mask2) <= 0x80) {
717			renderer->row[x + 2] = p | ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
718		}
719		current = renderer->row[x + 1];
720		if (((tileDataUpper | tileDataLower) & 64) && !(current & mask) && (current & mask2) <= 0x80) {
721			renderer->row[x + 1] = p | ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
722		}
723		current = renderer->row[x];
724		if (((tileDataUpper | tileDataLower) & 128) && !(current & mask) && (current & mask2) <= 0x80) {
725			renderer->row[x] = p | ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
726		}
727	}
728}
729
730static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, size_t* stride, const void** pixels) {
731	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
732	*stride = softwareRenderer->outputBufferStride;
733	*pixels = softwareRenderer->outputBuffer;
734}
735
736static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, size_t stride, const void* pixels) {
737	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
738	// TODO: Share with GBAVideoSoftwareRendererGetPixels
739
740	const color_t* colorPixels = pixels;
741	unsigned i;
742	for (i = 0; i < GB_VIDEO_VERTICAL_PIXELS; ++i) {
743		memmove(&softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * i], &colorPixels[stride * i], GB_VIDEO_HORIZONTAL_PIXELS * BYTES_PER_PIXEL);
744	}
745}