all repos — mgba @ 8750f78808736560c24cc662d5a570b81e64b6e0

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