all repos — mgba @ d3a204086d670377e2aa6b685218ac09024b15ba

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->palette[0] = GB_PALETTE[value & 3];
101		softwareRenderer->palette[1] = GB_PALETTE[(value >> 2) & 3];
102		softwareRenderer->palette[2] = GB_PALETTE[(value >> 4) & 3];
103		softwareRenderer->palette[3] = GB_PALETTE[(value >> 6) & 3];
104		break;
105	case REG_OBP0:
106		softwareRenderer->palette[8 * 4 + 0] = GB_PALETTE[value & 3];
107		softwareRenderer->palette[8 * 4 + 1] = GB_PALETTE[(value >> 2) & 3];
108		softwareRenderer->palette[8 * 4 + 2] = GB_PALETTE[(value >> 4) & 3];
109		softwareRenderer->palette[8 * 4 + 3] = GB_PALETTE[(value >> 6) & 3];
110		break;
111	case REG_OBP1:
112		softwareRenderer->palette[9 * 4 + 0] = GB_PALETTE[value & 3];
113		softwareRenderer->palette[9 * 4 + 1] = GB_PALETTE[(value >> 2) & 3];
114		softwareRenderer->palette[9 * 4 + 2] = GB_PALETTE[(value >> 4) & 3];
115		softwareRenderer->palette[9 * 4 + 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	if (softwareRenderer->oamDirty) {
137		_cleanOAM(softwareRenderer);
138	}
139
140	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
141	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
142		maps += GB_SIZE_MAP;
143	}
144	size_t x;
145	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc)) {
146		GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, 0, y, softwareRenderer->scx, softwareRenderer->scy);
147
148		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y) {
149			maps = &softwareRenderer->d.vram[GB_BASE_MAP];
150			if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
151				maps += GB_SIZE_MAP;
152			}
153			GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, 0, y, 7 - softwareRenderer->wx, -softwareRenderer->wy);
154		}
155	} else {
156		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
157			softwareRenderer->row[x] = 0;
158		}
159	}
160
161	int spriteHeight = 8;
162	if (GBRegisterLCDCIsObjSize(softwareRenderer->lcdc)) {
163		spriteHeight = 16;
164	}
165	int i;
166	for (i = 0; i < softwareRenderer->oamMax; ++i) {
167		// TODO: Sprite sizes
168		if (y >= softwareRenderer->obj[i]->y - 16 && y < softwareRenderer->obj[i]->y - 16 + spriteHeight) {
169			GBVideoSoftwareRendererDrawObj(softwareRenderer, softwareRenderer->obj[i], y);
170		}
171	}
172
173	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
174	for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
175		row[x] = softwareRenderer->palette[softwareRenderer->row[x]];
176	}
177}
178
179static void _cleanOAM(struct GBVideoSoftwareRenderer* renderer) {
180	// TODO: GBC differences
181	renderer->oamMax = 0;
182	int o = 0;
183	int i;
184	for (i = 0; i < 40; ++i) {
185		uint8_t y = renderer->d.oam->obj[i].y;
186		if (y == 0 || y >= GB_VIDEO_VERTICAL_PIXELS + 16) {
187			continue;
188		}
189		// TODO: Sort
190		renderer->obj[o] = &renderer->d.oam->obj[i];
191		++o;
192	}
193	renderer->oamMax = o;
194	renderer->oamDirty = false;
195}
196
197static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
198	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
199
200	if (softwareRenderer->temporaryBuffer) {
201		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
202		softwareRenderer->temporaryBuffer = 0;
203	}
204}
205
206static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int x, int y, int sx, int sy) {
207	uint8_t* data = renderer->d.vram;
208	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
209		data += 0x1000;
210	}
211	int topY = (((y + sy) >> 3) & 0x1F) * 0x20;
212	int bottomY = (y + sy) & 7;
213	for (; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
214		int topX = ((x + sx) >> 3) & 0x1F;
215		int bottomX = 7 - ((x + sx) & 7);
216		int bgTile;
217		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
218			bgTile = maps[topX + topY];
219		} else {
220			bgTile = ((int8_t*) maps)[topX + topY];
221		}
222		uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
223		uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
224		tileDataUpper >>= bottomX;
225		tileDataLower >>= bottomX;
226		renderer->row[x] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
227	}
228}
229
230static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int y) {
231	uint8_t* data = renderer->d.vram;
232	int tileOffset = 0;
233	int bottomY;
234	if (GBObjAttributesIsYFlip(obj->attr)) {
235		bottomY = 7 - ((y - obj->y - 16) & 7);
236		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
237			++tileOffset;
238		}
239	} else {
240		bottomY = (y - obj->y - 16) & 7;
241		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
242			++tileOffset;
243		}
244	}
245	int end = GB_VIDEO_HORIZONTAL_PIXELS;
246	if (obj->x < end) {
247		end = obj->x;
248	}
249	int x = obj->x - 8;
250	if (x < 0) {
251		x = 0;
252	}
253	uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x20;
254	int p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
255	for (; x < end; ++x) {
256		int bottomX;
257		if (GBObjAttributesIsXFlip(obj->attr)) {
258			bottomX = (x - obj->x) & 7;
259		} else {
260			bottomX = 7 - ((x - obj->x) & 7);
261		}
262		int objTile = obj->tile + tileOffset;
263		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
264		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
265		tileDataUpper >>= bottomX;
266		tileDataLower >>= bottomX;
267		color_t current = renderer->row[x];
268		if (((tileDataUpper | tileDataLower) & 1) && current <= mask) {
269			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
270		}
271	}
272}