all repos — mgba @ e6e535e39af599989e44957a7f297ada148a9185

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 GBVideoSoftwareRendererDrawDot(struct GBVideoRenderer* renderer, int x, int y, struct GBObj** obj, size_t oamMax);
 16static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer);
 17static void GBVideoSoftwareRendererGetPixels(struct GBVideoRenderer* renderer, unsigned* stride, const void** pixels);
 18static void GBVideoSoftwareRendererPutPixels(struct GBVideoRenderer* renderer, unsigned stride, void* pixels);
 19
 20static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int x, int y, int sx, int sy);
 21static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int x, int y);
 22
 23#ifdef COLOR_16_BIT
 24#ifdef COLOR_5_6_5
 25static const color_t GB_PALETTE[4] = { 0xFFFF, 0x39C7, 0x18C3, 0x0000};
 26#else
 27static const color_t GB_PALETTE[4] = { 0x7FFF, 0x1DE7, 0x0C63, 0x0000};
 28#endif
 29#else
 30static const color_t GB_PALETTE[4] = { 0xFFFFFF, 0x808080, 0x404040, 0x000000};
 31#endif
 32
 33void GBVideoSoftwareRendererCreate(struct GBVideoSoftwareRenderer* renderer) {
 34	renderer->d.init = GBVideoSoftwareRendererInit;
 35	renderer->d.reset = GBVideoSoftwareRendererReset;
 36	renderer->d.deinit = GBVideoSoftwareRendererDeinit;
 37	renderer->d.writeVideoRegister = GBVideoSoftwareRendererWriteVideoRegister;
 38	renderer->d.drawDot = GBVideoSoftwareRendererDrawDot;
 39	renderer->d.finishFrame = GBVideoSoftwareRendererFinishFrame;
 40	renderer->d.getPixels = 0;
 41	renderer->d.putPixels = 0;
 42
 43	renderer->temporaryBuffer = 0;
 44}
 45
 46static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer) {
 47	GBVideoSoftwareRendererReset(renderer);
 48
 49	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 50
 51	int y;
 52	for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
 53		color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 54		int x;
 55		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
 56			row[x] = GB_PALETTE[0];
 57		}
 58	}
 59}
 60
 61static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer) {
 62	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 63	softwareRenderer->scy = 0;
 64	softwareRenderer->scx = 0;
 65	softwareRenderer->wy = 0;
 66	softwareRenderer->currentWy = 0;
 67	softwareRenderer->wx = 0;
 68}
 69
 70static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
 71	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 72	UNUSED(softwareRenderer);
 73}
 74
 75static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
 76	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 77	switch (address) {
 78	case REG_LCDC:
 79		softwareRenderer->lcdc = value;
 80		break;
 81	case REG_BGP:
 82		softwareRenderer->palette[0] = GB_PALETTE[value & 3];
 83		softwareRenderer->palette[1] = GB_PALETTE[(value >> 2) & 3];
 84		softwareRenderer->palette[2] = GB_PALETTE[(value >> 4) & 3];
 85		softwareRenderer->palette[3] = GB_PALETTE[(value >> 6) & 3];
 86		break;
 87	case REG_OBP0:
 88		softwareRenderer->palette[8 * 4 + 0] = GB_PALETTE[value & 3];
 89		softwareRenderer->palette[8 * 4 + 1] = GB_PALETTE[(value >> 2) & 3];
 90		softwareRenderer->palette[8 * 4 + 2] = GB_PALETTE[(value >> 4) & 3];
 91		softwareRenderer->palette[8 * 4 + 3] = GB_PALETTE[(value >> 6) & 3];
 92		break;
 93	case REG_OBP1:
 94		softwareRenderer->palette[9 * 4 + 0] = GB_PALETTE[value & 3];
 95		softwareRenderer->palette[9 * 4 + 1] = GB_PALETTE[(value >> 2) & 3];
 96		softwareRenderer->palette[9 * 4 + 2] = GB_PALETTE[(value >> 4) & 3];
 97		softwareRenderer->palette[9 * 4 + 3] = GB_PALETTE[(value >> 6) & 3];
 98		break;
 99	case REG_SCY:
100		softwareRenderer->scy = value;
101		break;
102	case REG_SCX:
103		softwareRenderer->scx = value;
104		break;
105	case REG_WY:
106		softwareRenderer->wy = value;
107		break;
108	case REG_WX:
109		softwareRenderer->wx = value;
110		break;
111	}
112	return value;
113}
114
115static void GBVideoSoftwareRendererDrawDot(struct GBVideoRenderer* renderer, int x, int y, struct GBObj** obj, size_t oamMax) {
116	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
117	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
118	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
119		maps += GB_SIZE_MAP;
120	}
121	if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc)) {
122		GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, x, y, softwareRenderer->scx, softwareRenderer->scy);
123
124		if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y && x >= softwareRenderer->wx - 7) {
125			maps = &softwareRenderer->d.vram[GB_BASE_MAP];
126			if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
127				maps += GB_SIZE_MAP;
128			}
129			GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, x, y, 7 - softwareRenderer->wx, (softwareRenderer->currentWy - y) - softwareRenderer->wy);
130			if (x == 159) { // TODO: Find a better way to do this
131				++softwareRenderer->currentWy;
132			}
133		}
134	} else {
135		softwareRenderer->row[x] = 0;
136	}
137
138	if (GBRegisterLCDCIsObjEnable(softwareRenderer->lcdc)) {
139		size_t i;
140		for (i = 0; i < oamMax; ++i) {
141			GBVideoSoftwareRendererDrawObj(softwareRenderer, obj[i], x, y);
142		}
143	}
144
145	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
146	row[x] = softwareRenderer->palette[softwareRenderer->row[x]];
147}
148
149static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
150	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
151
152	if (softwareRenderer->temporaryBuffer) {
153		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
154		softwareRenderer->temporaryBuffer = 0;
155	}
156	softwareRenderer->currentWy = 0;
157}
158
159static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int x, int y, int sx, int sy) {
160	uint8_t* data = renderer->d.vram;
161	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
162		data += 0x1000;
163	}
164	int topY = (((y + sy) >> 3) & 0x1F) * 0x20;
165	int bottomY = (y + sy) & 7;
166	int topX = ((x + sx) >> 3) & 0x1F;
167	int bottomX = 7 - ((x + sx) & 7);
168	int bgTile;
169	if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
170		bgTile = maps[topX + topY];
171	} else {
172		bgTile = ((int8_t*) maps)[topX + topY];
173	}
174	uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
175	uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
176	tileDataUpper >>= bottomX;
177	tileDataLower >>= bottomX;
178	renderer->row[x] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
179}
180
181static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* renderer, struct GBObj* obj, int x, int y) {
182	uint8_t* data = renderer->d.vram;
183	int tileOffset = 0;
184	int bottomY;
185	if (GBObjAttributesIsYFlip(obj->attr)) {
186		bottomY = 7 - ((y - obj->y - 16) & 7);
187		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) {
188			++tileOffset;
189		}
190	} else {
191		bottomY = (y - obj->y - 16) & 7;
192		if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) {
193			++tileOffset;
194		}
195	}
196	int end = GB_VIDEO_HORIZONTAL_PIXELS;
197	if (obj->x < end) {
198		end = obj->x;
199	}
200	int ix = obj->x - 8;
201	if (x < ix || x >= ix + 8) {
202		return;
203	}
204	uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x20;
205	int p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4;
206	int bottomX;
207	if (GBObjAttributesIsXFlip(obj->attr)) {
208		bottomX = (x - obj->x) & 7;
209	} else {
210		bottomX = 7 - ((x - obj->x) & 7);
211	}
212	int objTile = obj->tile + tileOffset;
213	uint8_t tileDataLower = data[(objTile * 8 + bottomY) * 2];
214	uint8_t tileDataUpper = data[(objTile * 8 + bottomY) * 2 + 1];
215	tileDataUpper >>= bottomX;
216	tileDataLower >>= bottomX;
217	color_t current = renderer->row[x];
218	if (((tileDataUpper | tileDataLower) & 1) && current <= mask) {
219		renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
220	}
221}