all repos — mgba @ 2eb5696ff7484a0dff996a96712c55e3606559cf

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 "software.h"
  7
  8#include "gb/io.h"
  9#include "util/memory.h"
 10
 11static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer);
 12static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer);
 13static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer);
 14static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 15static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax);
 16static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y);
 17static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer);
 18static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
 19static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, unsigned stride, void* pixels);
 20
 21static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int y, int sx, int sy);
 22static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y);
 23
 24#ifdef COLOR_16_BIT
 25#ifdef COLOR_5_6_5
 26static const color_t GB_PALETTE[4] = { 0xFFFF, 0x528A, 0x2945, 0x0000};
 27#else
 28static const color_t GB_PALETTE[4] = { 0x7FFF, 0x294A, 0x14A5, 0x0000};
 29#endif
 30#else
 31static const color_t GB_PALETTE[4] = { 0xFFFFFF, 0xACACAC, 0x565656, 0x000000};
 32#endif
 33
 34void GBVideoSoftwareRendererCreate(struct GBVideoSoftwareRenderer* renderer) {
 35	renderer->d.init = GBVideoSoftwareRendererInit;
 36	renderer->d.reset = GBVideoSoftwareRendererReset;
 37	renderer->d.deinit = GBVideoSoftwareRendererDeinit;
 38	renderer->d.writeVideoRegister = GBVideoSoftwareRendererWriteVideoRegister;
 39	renderer->d.drawRange = GBVideoSoftwareRendererDrawRange;
 40	renderer->d.finishScanline = GBVideoSoftwareRendererFinishScanline;
 41	renderer->d.finishFrame = GBVideoSoftwareRendererFinishFrame;
 42	renderer->d.getPixels = 0;
 43	renderer->d.putPixels = 0;
 44
 45	renderer->temporaryBuffer = 0;
 46}
 47
 48static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer) {
 49	GBVideoSoftwareRendererReset(renderer);
 50
 51	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 52
 53	int y;
 54	for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
 55		color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 56		int x;
 57		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
 58			row[x] = GB_PALETTE[0];
 59		}
 60	}
 61}
 62
 63static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer) {
 64	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 65	softwareRenderer->scy = 0;
 66	softwareRenderer->scx = 0;
 67	softwareRenderer->wy = 0;
 68	softwareRenderer->currentWy = 0;
 69	softwareRenderer->wx = 0;
 70}
 71
 72static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
 73	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 74	UNUSED(softwareRenderer);
 75}
 76
 77static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
 78	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 79	switch (address) {
 80	case REG_LCDC:
 81		softwareRenderer->lcdc = value;
 82		break;
 83	case REG_BGP:
 84		softwareRenderer->palette[0] = GB_PALETTE[value & 3];
 85		softwareRenderer->palette[1] = GB_PALETTE[(value >> 2) & 3];
 86		softwareRenderer->palette[2] = GB_PALETTE[(value >> 4) & 3];
 87		softwareRenderer->palette[3] = GB_PALETTE[(value >> 6) & 3];
 88		break;
 89	case REG_OBP0:
 90		softwareRenderer->palette[8 * 4 + 0] = GB_PALETTE[value & 3];
 91		softwareRenderer->palette[8 * 4 + 1] = GB_PALETTE[(value >> 2) & 3];
 92		softwareRenderer->palette[8 * 4 + 2] = GB_PALETTE[(value >> 4) & 3];
 93		softwareRenderer->palette[8 * 4 + 3] = GB_PALETTE[(value >> 6) & 3];
 94		break;
 95	case REG_OBP1:
 96		softwareRenderer->palette[9 * 4 + 0] = GB_PALETTE[value & 3];
 97		softwareRenderer->palette[9 * 4 + 1] = GB_PALETTE[(value >> 2) & 3];
 98		softwareRenderer->palette[9 * 4 + 2] = GB_PALETTE[(value >> 4) & 3];
 99		softwareRenderer->palette[9 * 4 + 3] = GB_PALETTE[(value >> 6) & 3];
100		break;
101	case REG_SCY:
102		softwareRenderer->scy = value;
103		break;
104	case REG_SCX:
105		softwareRenderer->scx = value;
106		break;
107	case REG_WY:
108		softwareRenderer->wy = value;
109		softwareRenderer->currentWy = value;
110		break;
111	case REG_WX:
112		softwareRenderer->wx = value;
113		break;
114	}
115	return value;
116}
117
118static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax) {
119	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
120	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
121	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
122		maps += GB_SIZE_MAP;
123	}
124	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc)) {
125		GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, y, softwareRenderer->scx, softwareRenderer->scy);
126
127		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && endX >= softwareRenderer->wx - 7) {
128			maps = &softwareRenderer->d.vram[GB_BASE_MAP];
129			if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
130				maps += GB_SIZE_MAP;
131			}
132			GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, y, 7 - softwareRenderer->wx, (softwareRenderer->currentWy - y) - softwareRenderer->wy);
133		}
134	} else {
135		memset(&softwareRenderer->row[startX], 0, endX - startX);
136	}
137
138	if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc)) {
139		size_t i;
140		for (i = 0; i < oamMax; ++i) {
141			GBVideoSoftwareRendererDrawObj(softwareRenderer, obj[i], startX, endX, y);
142		}
143	}
144	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
145	int x;
146	for (x = startX; x < endX; x += 8) {
147		row[x] = softwareRenderer->palette[softwareRenderer->row[x]];
148		row[x + 1] = softwareRenderer->palette[softwareRenderer->row[x + 1]];
149		row[x + 2] = softwareRenderer->palette[softwareRenderer->row[x + 2]];
150		row[x + 3] = softwareRenderer->palette[softwareRenderer->row[x + 3]];
151		row[x + 4] = softwareRenderer->palette[softwareRenderer->row[x + 4]];
152		row[x + 5] = softwareRenderer->palette[softwareRenderer->row[x + 5]];
153		row[x + 6] = softwareRenderer->palette[softwareRenderer->row[x + 6]];
154		row[x + 7] = softwareRenderer->palette[softwareRenderer->row[x + 7]];
155	}
156}
157
158static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
159	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
160	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) && GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && softwareRenderer->wx - 7 < GB_VIDEO_HORIZONTAL_PIXELS) {
161		++softwareRenderer->currentWy;
162	}
163}
164
165static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
166	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
167
168	if (softwareRenderer->temporaryBuffer) {
169		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
170		softwareRenderer->temporaryBuffer = 0;
171	}
172	softwareRenderer->currentWy = softwareRenderer->wy;
173}
174
175static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int y, int sx, int sy) {
176	uint8_t* data = renderer->d.vram;
177	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
178		data += 0x1000;
179	}
180	int topY = (((y + sy) >> 3) & 0x1F) * 0x20;
181	int bottomY = (y + sy) & 7;
182	if (startX < 0) {
183		startX = 0;
184	}
185	int x;
186	if ((startX + sx) & 7) {
187		int startX2 = startX + 8 - ((startX + sx) & 7);
188		for (x = startX; x < startX2; ++x) {
189			int topX = ((x + sx) >> 3) & 0x1F;
190			int bottomX = 7 - ((x + sx) & 7);
191			int bgTile;
192			if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
193				bgTile = maps[topX + topY];
194			} else {
195				bgTile = ((int8_t*) maps)[topX + topY];
196			}
197			uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
198			uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
199			tileDataUpper >>= bottomX;
200			tileDataLower >>= bottomX;
201			renderer->row[x] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
202		}
203		startX = startX2;
204	}
205	for (x = startX; x < endX; x += 8) {
206		int topX = ((x + sx) >> 3) & 0x1F;
207		int bgTile;
208		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
209			bgTile = maps[topX + topY];
210		} else {
211			bgTile = ((int8_t*) maps)[topX + topY];
212		}
213		uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
214		uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
215		renderer->row[x + 7] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
216		renderer->row[x + 6] = (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
217		renderer->row[x + 5] = ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
218		renderer->row[x + 4] = ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
219		renderer->row[x + 3] = ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
220		renderer->row[x + 2] = ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
221		renderer->row[x + 1] = ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
222		renderer->row[x + 0] = ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
223	}
224}
225
226static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
227	int ix = obj->x - 8;
228	if (endX < ix || startX >= ix + 8) {
229		return;
230	}
231	if (obj->x < endX) {
232		endX = obj->x;
233	}
234	if (obj->x - 8 > startX) {
235		startX = obj->x - 8;
236	}
237	if (startX < 0) {
238		startX = 0;
239	}
240	uint8_t* data = renderer->d.vram;
241	int tileOffset = 0;
242	int bottomY;
243	if (GBObjAttributesIsYFlip(obj->attr)) {
244		bottomY = 7 - ((y - obj->y - 16) & 7);
245		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
246			++tileOffset;
247		}
248	} else {
249		bottomY = (y - obj->y - 16) & 7;
250		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
251			++tileOffset;
252		}
253	}
254	uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x20;
255	int p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
256	int bottomX;
257	int x;
258	for (x = startX; x < endX; ++x) {
259		if (GBObjAttributesIsXFlip(obj->attr)) {
260			bottomX = (x - obj->x) & 7;
261		} else {
262			bottomX = 7 - ((x - obj->x) & 7);
263		}
264		int objTile = obj->tile + tileOffset;
265		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
266		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
267		tileDataUpper >>= bottomX;
268		tileDataLower >>= bottomX;
269		color_t current = renderer->row[x];
270		if (((tileDataUpper | tileDataLower) & 1) && current <= mask) {
271			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
272		}
273	}
274}