all repos — mgba @ 94ff4f7c4e25c717f3dc0b049e0e54c1818688ce

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, 0x39C7, 0x18C3, 0x0000};
 27#else
 28static const color_t GB_PALETTE[4] = { 0x7FFF, 0x1DE7, 0x0C63, 0x0000};
 29#endif
 30#else
 31static const color_t GB_PALETTE[4] = { 0xFFFFFF, 0x808080, 0x404040, 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 && startX >= 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, startX, endX, y, 7 - softwareRenderer->wx, (softwareRenderer->currentWy - y) - softwareRenderer->wy);
132		}
133	} else {
134		int x;
135		for (x = startX; x < endX; ++x) {
136			softwareRenderer->row[x] = 0;
137		}
138	}
139
140	if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc)) {
141		size_t i;
142		for (i = 0; i < oamMax; ++i) {
143			GBVideoSoftwareRendererDrawObj(softwareRenderer, obj[i], startX, endX, y);
144		}
145	}
146
147	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
148	int x;
149	for (x = startX; x < endX; ++x) {
150		row[x] = softwareRenderer->palette[softwareRenderer->row[x]];
151	}
152}
153
154static void GBVideoSoftwareRendererFinishScanline(struct GBVideoRenderer* renderer, int y) {
155	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
156	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc) && GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && softwareRenderer->wx - 7 < GB_VIDEO_HORIZONTAL_PIXELS) {
157		++softwareRenderer->currentWy;
158	}
159}
160
161static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
162	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
163
164	if (softwareRenderer->temporaryBuffer) {
165		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
166		softwareRenderer->temporaryBuffer = 0;
167	}
168	softwareRenderer->currentWy = 0;
169}
170
171static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int startX, int endX, int y, int sx, int sy) {
172	uint8_t* data = renderer->d.vram;
173	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
174		data += 0x1000;
175	}
176	int topY = (((y + sy) >> 3) & 0x1F) * 0x20;
177	int bottomY = (y + sy) & 7;
178	int x;
179	for (x = startX; x < endX; ++x) {
180		int topX = ((x + sx) >> 3) & 0x1F;
181		int bottomX = 7 - ((x + sx) & 7);
182		int bgTile;
183		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
184			bgTile = maps[topX + topY];
185		} else {
186			bgTile = ((int8_t*) maps)[topX + topY];
187		}
188		uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
189		uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
190		tileDataUpper >>= bottomX;
191		tileDataLower >>= bottomX;
192		renderer->row[x] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
193	}
194}
195
196static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int startX, int endX, int y) {
197	int ix = obj->x - 8;
198	if (endX < ix || startX >= ix + 8) {
199		return;
200	}
201	if (obj->x < endX) {
202		endX = obj->x;
203	}
204	if (obj->x - 8 > startX) {
205		startX = obj->x - 8;
206	}
207	uint8_t* data = renderer->d.vram;
208	int tileOffset = 0;
209	int bottomY;
210	if (GBObjAttributesIsYFlip(obj->attr)) {
211		bottomY = 7 - ((y - obj->y - 16) & 7);
212		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
213			++tileOffset;
214		}
215	} else {
216		bottomY = (y - obj->y - 16) & 7;
217		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
218			++tileOffset;
219		}
220	}
221	uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x20;
222	int p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
223	int bottomX;
224	int x;
225	for (x = startX; x < endX; ++x) {
226		if (GBObjAttributesIsXFlip(obj->attr)) {
227			bottomX = (x - obj->x) & 7;
228		} else {
229			bottomX = 7 - ((x - obj->x) & 7);
230		}
231		int objTile = obj->tile + tileOffset;
232		uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
233		uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
234		tileDataUpper >>= bottomX;
235		tileDataLower >>= bottomX;
236		color_t current = renderer->row[x];
237		if (((tileDataUpper | tileDataLower) & 1) && current <= mask) {
238			renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
239		}
240	}
241}