all repos — mgba @ 9630583646b920e5e7ad2eab29b65ebd01d56b30

mGBA Game Boy Advance Emulator

src/gb/renderers/software.c (view raw)

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