all repos — mgba @ c59bfd173d3c197916fe267a734120a40ec0787f

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 uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value);
 16static void GBVideoSoftwareRendererDrawScanline(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 x, int y, int sx, int sy);
 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.writeVRAM = GBVideoSoftwareRendererWriteVRAM;
 39	renderer->d.drawScanline = GBVideoSoftwareRendererDrawScanline;
 40	renderer->d.finishFrame = GBVideoSoftwareRendererFinishFrame;
 41	renderer->d.getPixels = 0;
 42	renderer->d.putPixels = 0;
 43
 44	renderer->temporaryBuffer = 0;
 45}
 46
 47static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer) {
 48	GBVideoSoftwareRendererReset(renderer);
 49
 50	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 51
 52	int y;
 53	for (y = 0; y < GB_VIDEO_VERTICAL_PIXELS; ++y) {
 54		color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
 55		int x;
 56		for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
 57			row[x] = GB_PALETTE[0];
 58		}
 59	}
 60}
 61
 62static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer) {
 63	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 64	softwareRenderer->scy = 0;
 65	softwareRenderer->scx = 0;
 66	softwareRenderer->wy = 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 void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
 76	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 77	// TODO
 78}
 79static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* renderer, uint16_t address, uint8_t value) {
 80	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 81	switch (address) {
 82	case REG_LCDC:
 83		softwareRenderer->lcdc = value;
 84		break;
 85	case REG_BGP:
 86		softwareRenderer->bgPalette[0] = GB_PALETTE[value & 3];
 87		softwareRenderer->bgPalette[1] = GB_PALETTE[(value >> 2) & 3];
 88		softwareRenderer->bgPalette[2] = GB_PALETTE[(value >> 4) & 3];
 89		softwareRenderer->bgPalette[3] = GB_PALETTE[(value >> 6) & 3];
 90		break;
 91	case REG_OBP0:
 92		softwareRenderer->objPalette[0][0] = GB_PALETTE[value & 3];
 93		softwareRenderer->objPalette[0][1] = GB_PALETTE[(value >> 2) & 3];
 94		softwareRenderer->objPalette[0][2] = GB_PALETTE[(value >> 4) & 3];
 95		softwareRenderer->objPalette[0][3] = GB_PALETTE[(value >> 6) & 3];
 96		break;
 97	case REG_OBP1:
 98		softwareRenderer->objPalette[1][0] = GB_PALETTE[value & 3];
 99		softwareRenderer->objPalette[1][1] = GB_PALETTE[(value >> 2) & 3];
100		softwareRenderer->objPalette[1][2] = GB_PALETTE[(value >> 4) & 3];
101		softwareRenderer->objPalette[1][3] = GB_PALETTE[(value >> 6) & 3];
102		break;
103	case REG_SCY:
104		softwareRenderer->scy = value;
105		break;
106	case REG_SCX:
107		softwareRenderer->scx = value;
108		break;
109	case REG_WY:
110		softwareRenderer->wy = value;
111		break;
112	case REG_WX:
113		softwareRenderer->wx = value;
114		break;
115	}
116	return value;
117}
118
119static void GBVideoSoftwareRendererDrawScanline(struct GBVideoRenderer* renderer, int y) {
120	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
121
122	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
123
124	uint8_t* maps = &softwareRenderer->d.vram[GB_BASE_MAP];
125	if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) {
126		maps += GB_SIZE_MAP;
127	}
128	GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, 0, y, softwareRenderer->scx, softwareRenderer->scy);
129
130	if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc)) {
131		maps = &softwareRenderer->d.vram[GB_BASE_MAP];
132		if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) {
133			maps += GB_SIZE_MAP;
134		}
135		GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, -7, y, softwareRenderer->wx - 7, softwareRenderer->wy);
136	}
137
138	size_t x;
139#ifdef COLOR_16_BIT
140#if defined(__ARM_NEON) && !defined(__APPLE__)
141	_to16Bit(row, softwareRenderer->row, GB_VIDEO_HORIZONTAL_PIXELS);
142#else
143	for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
144		row[x] = softwareRenderer->row[x];
145	}
146#endif
147#else
148	memcpy(row, softwareRenderer->row, GB_VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
149#endif
150}
151
152static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
153	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
154
155	if (softwareRenderer->temporaryBuffer) {
156		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
157		softwareRenderer->temporaryBuffer = 0;
158	}
159}
160
161static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, uint8_t* maps, int x, int y, int sx, int sy) {
162	uint8_t* data = renderer->d.vram;
163	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
164		data += 0x1000;
165	}
166	int topY = (((y + sy) >> 3) & 0x1F) * 0x20;
167	int bottomY = (y + sy) & 7;
168	if (x < 0) {
169		x = 0;
170	}
171	for (; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
172		int topX = ((x + sx) >> 3) & 0x1F;
173		int bottomX = 7 - ((x + sx) & 7);
174		int bgTile;
175		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
176			bgTile = maps[topX + topY];
177		} else {
178			bgTile = ((int8_t*) maps)[topX + topY];
179		}
180		uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
181		uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
182		tileDataUpper >>= bottomX;
183		tileDataLower >>= bottomX;
184		renderer->row[x] = renderer->bgPalette[((tileDataUpper & 1) << 1) | (tileDataLower & 1)];
185	}
186}