all repos — mgba @ 06f47c8d2ca39ac0a02a5878ae09ccf6958b05e6

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, 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.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}
 67
 68static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) {
 69	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 70	UNUSED(softwareRenderer);
 71}
 72
 73static void GBVideoSoftwareRendererWriteVRAM(struct GBVideoRenderer* renderer, uint16_t address) {
 74	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
 75	// TODO
 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->bgPalette[0] = GB_PALETTE[value & 3];
 85		softwareRenderer->bgPalette[1] = GB_PALETTE[(value >> 2) & 3];
 86		softwareRenderer->bgPalette[2] = GB_PALETTE[(value >> 4) & 3];
 87		softwareRenderer->bgPalette[3] = GB_PALETTE[(value >> 6) & 3];
 88		break;
 89	case REG_OBP0:
 90		softwareRenderer->objPalette[0][0] = GB_PALETTE[value & 3];
 91		softwareRenderer->objPalette[0][1] = GB_PALETTE[(value >> 2) & 3];
 92		softwareRenderer->objPalette[0][2] = GB_PALETTE[(value >> 4) & 3];
 93		softwareRenderer->objPalette[0][3] = GB_PALETTE[(value >> 6) & 3];
 94		break;
 95	case REG_OBP1:
 96		softwareRenderer->objPalette[1][0] = GB_PALETTE[value & 3];
 97		softwareRenderer->objPalette[1][1] = GB_PALETTE[(value >> 2) & 3];
 98		softwareRenderer->objPalette[1][2] = GB_PALETTE[(value >> 4) & 3];
 99		softwareRenderer->objPalette[1][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	}
108	return value;
109}
110
111static void GBVideoSoftwareRendererDrawScanline(struct GBVideoRenderer* renderer, int y) {
112	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
113
114	color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y];
115
116	GBVideoSoftwareRendererDrawBackground(softwareRenderer, y);
117
118	size_t x;
119#ifdef COLOR_16_BIT
120#if defined(__ARM_NEON) && !defined(__APPLE__)
121	_to16Bit(row, softwareRenderer->row, GB_VIDEO_HORIZONTAL_PIXELS);
122#else
123	for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
124		row[x] = softwareRenderer->row[x];
125	}
126#endif
127#else
128	memcpy(row, softwareRenderer->row, GB_VIDEO_HORIZONTAL_PIXELS * sizeof(*row));
129#endif
130}
131
132static void GBVideoSoftwareRendererFinishFrame(struct GBVideoRenderer* renderer) {
133	struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
134
135	if (softwareRenderer->temporaryBuffer) {
136		mappedMemoryFree(softwareRenderer->temporaryBuffer, GB_VIDEO_HORIZONTAL_PIXELS * GB_VIDEO_VERTICAL_PIXELS * 4);
137		softwareRenderer->temporaryBuffer = 0;
138	}
139}
140
141static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer* renderer, int y) {
142	uint8_t* maps = &renderer->d.vram[GB_BASE_MAP];
143	if (GBRegisterLCDCIsTileMap(renderer->lcdc)) {
144		maps += GB_SIZE_MAP;
145	}
146	uint8_t* data = renderer->d.vram;
147	if (!GBRegisterLCDCIsTileData(renderer->lcdc)) {
148		data += 0x1000;
149	}
150	int x;
151	int topY = (((y + renderer->scy) >> 3) & 0x1F) * 0x20;
152	int bottomY = (y + renderer->scy) & 7;
153	for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) {
154		int topX = ((x + renderer->scx) >> 3) & 0x1F;
155		int bottomX = 7 - ((x + renderer->scx) & 7);
156		int bgTile;
157		if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
158			bgTile = maps[topX + topY];
159		} else {
160			bgTile = ((int8_t*) maps)[topX + topY];
161		}
162		uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2];
163		uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
164		tileDataUpper >>= bottomX;
165		tileDataLower >>= bottomX;
166		renderer->row[x] = renderer->bgPalette[((tileDataUpper & 1) << 1) | (tileDataLower & 1)];
167	}
168}