all repos — mgba @ ecdcef283b14a458521e80b714cb6d43da9965f6

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		break;
110	case REG_WX:
111		softwareRenderer->wx = value;
112		break;
113	}
114	return value;
115}
116
117static void GBVideoSoftwareRendererDrawRange(struct GBVideoRenderer* renderer, int startX, int endX, int y, struct GBObj** obj, size_t oamMax) {
118	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
119	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
120	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
121		maps += GB_SIZE_MAP;
122	}
123	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc)) {
124		GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, startX, endX, y, softwareRenderer->scx, softwareRenderer->scy);
125
126		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && endX >= softwareRenderer->wx - 7) {
127			maps = &softwareRenderer->d.vram[GB_BASE_MAP];
128			if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
129				maps += GB_SIZE_MAP;
130			}
131			GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, softwareRenderer->wx - 7, endX, y, 7 - softwareRenderer->wx, (softwareRenderer->currentWy - y) - softwareRenderer->wy);
132		}
133	} else {
134		memset(&softwareRenderer->row[startX], 0, endX - startX);
135	}
136
137	if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc)) {
138		size_t i;
139		for (i = 0; i < oamMax; ++i) {
140			GBVideoSoftwareRendererDrawObj(softwareRenderer, obj[i], startX, endX, y);
141		}
142	}
143	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
144	int x;
145	for (x = startX; x < endX; x += 8) {
146		row[x] = softwareRenderer->palette[softwareRenderer->row[x]];
147		row[x + 1] = softwareRenderer->palette[softwareRenderer->row[x + 1]];
148		row[x + 2] = softwareRenderer->palette[softwareRenderer->row[x + 2]];
149		row[x + 3] = softwareRenderer->palette[softwareRenderer->row[x + 3]];
150		row[x + 4] = softwareRenderer->palette[softwareRenderer->row[x + 4]];
151		row[x + 5] = softwareRenderer->palette[softwareRenderer->row[x + 5]];
152		row[x + 6] = softwareRenderer->palette[softwareRenderer->row[x + 6]];
153		row[x + 7] = softwareRenderer->palette[softwareRenderer->row[x + 7]];
154	}
155}
156
157static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
158	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
159	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) && GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && softwareRenderer->wx - 7 < GB_VIDEO_HORIZONTAL_PIXELS) {
160		++softwareRenderer->currentWy;
161	}
162}
163
164static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
165	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
166
167	if (softwareRenderer->temporaryBuffer) {
168		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
169		softwareRenderer->temporaryBuffer = 0;
170	}
171	softwareRenderer->currentWy = softwareRenderer->wy;
172}
173
174static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int y, int sx, int sy) {
175	uint8_t* data = renderer->d.vram;
176	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
177		data += 0x1000;
178	}
179	int topY = (((y + sy) >> 3) & 0x1F) * 0x20;
180	int bottomY = (y + sy) & 7;
181	if (startX < 0) {
182		startX = 0;
183	}
184	int x;
185	if ((startX + sx) & 7) {
186		int startX2 = startX + 8 - ((startX + sx) & 7);
187		for (x = startX; x < startX2; ++x) {
188			int topX = ((x + sx) >> 3) & 0x1F;
189			int bottomX = 7 - ((x + sx) & 7);
190			int bgTile;
191			if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
192				bgTile = maps[topX + topY];
193			} else {
194				bgTile = ((int8_t*) maps)[topX + topY];
195			}
196			uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
197			uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
198			tileDataUpper >>= bottomX;
199			tileDataLower >>= bottomX;
200			renderer->row[x] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
201		}
202		startX = startX2;
203	}
204	for (x = startX; x < endX; x += 8) {
205		int topX = ((x + sx) >> 3) & 0x1F;
206		int bgTile;
207		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
208			bgTile = maps[topX + topY];
209		} else {
210			bgTile = ((int8_t*) maps)[topX + topY];
211		}
212		uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
213		uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
214		renderer->row[x + 7] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
215		renderer->row[x + 6] = (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
216		renderer->row[x + 5] = ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
217		renderer->row[x + 4] = ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
218		renderer->row[x + 3] = ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
219		renderer->row[x + 2] = ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
220		renderer->row[x + 1] = ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
221		renderer->row[x + 0] = ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
222	}
223}
224
225static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
226	int ix = obj->x - 8;
227	if (endX < ix || startX >= ix + 8) {
228		return;
229	}
230	if (obj->x < endX) {
231		endX = obj->x;
232	}
233	if (obj->x - 8 > startX) {
234		startX = obj->x - 8;
235	}
236	if (startX < 0) {
237		startX = 0;
238	}
239	uint8_t* data = renderer->d.vram;
240	int tileOffset = 0;
241	int bottomY;
242	if (GBObjAttributesIsYFlip(obj->attr)) {
243		bottomY = 7 - ((y - obj->y - 16) & 7);
244		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
245			++tileOffset;
246		}
247	} else {
248		bottomY = (y - obj->y - 16) & 7;
249		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
250			++tileOffset;
251		}
252	}
253	uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x20;
254	int p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
255	int bottomX;
256	int x;
257	for (x = startX; x < endX; ++x) {
258		if (GBObjAttributesIsXFlip(obj->attr)) {
259			bottomX = (x - obj->x) & 7;
260		} else {
261			bottomX = 7 - ((x - obj->x) & 7);
262		}
263		int objTile = obj->tile + tileOffset;
264		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
265		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
266		tileDataUpper >>= bottomX;
267		tileDataLower >>= bottomX;
268		color_t current = renderer->row[x];
269		if (((tileDataUpper | tileDataLower) & 1) && current <= mask) {
270			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
271		}
272	}
273}