all repos — mgba @ 129af691054cb1acffd9a2be9eb9f1dbf47f51df

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