all repos — mgba @ 2f0501d3c476e234859d2c6063636bb03a92bea3

mGBA Game Boy Advance Emulator

src/core/tile-cache.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 "tile-cache.h"
  7
  8#include "util/memory.h"
  9
 10void mTileCacheInit(struct mTileCache* cache) {
 11	// TODO: Reconfigurable cache for space savings
 12	cache->cache = NULL;
 13	cache->config = mTileCacheConfigurationFillShouldStore(0);
 14	cache->status = NULL;
 15	cache->activePalette = 0;
 16	memset(cache->globalPaletteVersion, 0, sizeof(cache->globalPaletteVersion));
 17}
 18
 19static void _freeCache(struct mTileCache* cache) {
 20	unsigned count0;
 21	count0 = 1 << mTileCacheSystemInfoGetPalette0Count(cache->sysConfig);
 22	unsigned count1;
 23	count1 = 1 << mTileCacheSystemInfoGetPalette1Count(cache->sysConfig);
 24	unsigned tiles = mTileCacheSystemInfoGetMaxTiles(cache->sysConfig);
 25	unsigned size = count0 > count1 ? count0 : count1;
 26	if (cache->cache) {
 27		mappedMemoryFree(cache->cache, 8 * 8 * 2 * tiles * size);
 28		cache->cache = NULL;
 29	}
 30	if (cache->status) {
 31		mappedMemoryFree(cache->status, tiles * size * sizeof(*cache->status));
 32		cache->status = NULL;
 33	}
 34	free(cache->globalPaletteVersion[0]);
 35	free(cache->globalPaletteVersion[1]);
 36	memset(cache->globalPaletteVersion, 0, sizeof(cache->globalPaletteVersion));
 37}
 38
 39static void _redoCacheSize(struct mTileCache* cache) {
 40	if (!mTileCacheConfigurationIsShouldStore(cache->config)) {
 41		return;
 42	}
 43	unsigned count0 = mTileCacheSystemInfoGetPalette0Count(cache->sysConfig);
 44	unsigned bpp0 = mTileCacheSystemInfoGetPalette0BPP(cache->sysConfig);
 45	bpp0 = 1 << (1 << bpp0);
 46	if (count0) {
 47		count0 = 1 << count0;
 48	}
 49	unsigned count1 = mTileCacheSystemInfoGetPalette1Count(cache->sysConfig);
 50	unsigned bpp1 = mTileCacheSystemInfoGetPalette1BPP(cache->sysConfig);
 51	bpp1 = 1 << (1 << bpp1);
 52	if (count1) {
 53		count1 = 1 << count1;
 54	}
 55	unsigned size = count0 > count1 ? count0 : count1;
 56	if (!size) {
 57		return;
 58	}
 59	cache->entriesPerTile = size;
 60	unsigned tiles = mTileCacheSystemInfoGetMaxTiles(cache->sysConfig);
 61	cache->cache = anonymousMemoryMap(8 * 8 * 2 * tiles * size);
 62	cache->status = anonymousMemoryMap(tiles * size * sizeof(*cache->status));
 63	if (count0) {
 64		cache->globalPaletteVersion[0] = malloc(count0 * bpp0 * sizeof(*cache->globalPaletteVersion[0]));
 65	}
 66	if (count1) {
 67		cache->globalPaletteVersion[1] = malloc(count1 * bpp1 * sizeof(*cache->globalPaletteVersion[1]));
 68	}
 69}
 70
 71void mTileCacheConfigure(struct mTileCache* cache, mTileCacheConfiguration config) {
 72	_freeCache(cache);
 73	cache->config = config;
 74	_redoCacheSize(cache);
 75}
 76
 77void mTileCacheConfigureSystem(struct mTileCache* cache, mTileCacheSystemInfo config) {
 78	_freeCache(cache);
 79	cache->sysConfig = config;
 80	_redoCacheSize(cache);
 81}
 82
 83void mTileCacheDeinit(struct mTileCache* cache) {
 84	_freeCache(cache);
 85}
 86
 87void mTileCacheWriteVRAM(struct mTileCache* cache, uint32_t address) {
 88	unsigned bpp = cache->bpp + 3;
 89	unsigned count = cache->entriesPerTile;
 90	size_t i;
 91	for (i = 0; i < count; ++i) {
 92		cache->status[(address >> bpp) * count + i].vramClean = 0;
 93	}
 94}
 95
 96void mTileCacheWritePalette(struct mTileCache* cache, uint32_t address) {
 97	if (cache->globalPaletteVersion[0]) {
 98		++cache->globalPaletteVersion[0][address >> 1];
 99	}
100	if (cache->globalPaletteVersion[1]) {
101		++cache->globalPaletteVersion[1][address >> 1];
102	}
103}
104
105void mTileCacheSetPalette(struct mTileCache* cache, int palette) {
106	cache->activePalette = palette;
107	if (palette == 0) {
108		cache->bpp = mTileCacheSystemInfoGetPalette0BPP(cache->sysConfig);
109		cache->count = 1 << mTileCacheSystemInfoGetPalette0Count(cache->sysConfig);
110	} else {
111		cache->bpp = mTileCacheSystemInfoGetPalette1BPP(cache->sysConfig);
112		cache->count = 1 << mTileCacheSystemInfoGetPalette1Count(cache->sysConfig);
113	}
114	cache->entries = 1 << (1 << cache->bpp);
115}
116
117static void _regenerateTile4(struct mTileCache* cache, uint16_t* tile, unsigned tileId, unsigned paletteId) {
118	uint8_t* start = (uint8_t*) &cache->vram[tileId << 3];
119	paletteId <<= 2;
120	uint16_t* palette = &cache->palette[paletteId];
121	int i;
122	for (i = 0; i < 8; ++i) {
123		uint8_t tileDataLower = start[0];
124		uint8_t tileDataUpper = start[1];
125		start += 2;
126		int pixel;
127		pixel = ((tileDataUpper & 128) >> 6) | ((tileDataLower & 128) >> 7);
128		tile[0] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
129		pixel = ((tileDataUpper & 64) >> 5) | ((tileDataLower & 64) >> 6);
130		tile[1] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
131		pixel = ((tileDataUpper & 32) >> 4) | ((tileDataLower & 32) >> 5);
132		tile[2] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
133		pixel = ((tileDataUpper & 16) >> 3) | ((tileDataLower & 16) >> 4);
134		tile[3] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
135		pixel = ((tileDataUpper & 8) >> 2) | ((tileDataLower & 8) >> 3);
136		tile[4] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
137		pixel = ((tileDataUpper & 4) >> 1) | ((tileDataLower & 4) >> 2);
138		tile[5] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
139		pixel = (tileDataUpper & 2) | ((tileDataLower & 2) >> 1);
140		tile[6] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
141		pixel = ((tileDataUpper & 1) << 1) | (tileDataLower & 1);
142		tile[7] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
143		tile += 8;
144	}
145}
146
147static void _regenerateTile16(struct mTileCache* cache, uint16_t* tile, unsigned tileId, unsigned paletteId) {
148	uint32_t* start = (uint32_t*) &cache->vram[tileId << 4];
149	paletteId <<= 4;
150	uint16_t* palette = &cache->palette[paletteId];
151	int i;
152	for (i = 0; i < 8; ++i) {
153		uint32_t line = *start;
154		++start;
155		int pixel;
156		pixel = line & 0xF;
157		tile[0] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
158		pixel = (line >> 4) & 0xF;
159		tile[1] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
160		pixel = (line >> 8) & 0xF;
161		tile[2] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
162		pixel = (line >> 12) & 0xF;
163		tile[3] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
164		pixel = (line >> 16) & 0xF;
165		tile[4] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
166		pixel = (line >> 20) & 0xF;
167		tile[5] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
168		pixel = (line >> 24) & 0xF;
169		tile[6] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
170		pixel = (line >> 28) & 0xF;
171		tile[7] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
172		tile += 8;
173	}
174}
175
176static void _regenerateTile256(struct mTileCache* cache, uint16_t* tile, unsigned tileId, unsigned paletteId) {
177	uint32_t* start = (uint32_t*) &cache->vram[tileId << 5];
178	paletteId <<= 8;
179	uint16_t* palette = &cache->palette[paletteId];
180	int i;
181	for (i = 0; i < 8; ++i) {
182		uint32_t line = *start;
183		++start;
184		int pixel;
185		pixel = line & 0xFF;
186		tile[0] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
187		pixel = (line >> 8) & 0xFF;
188		tile[1] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
189		pixel = (line >> 16) & 0xFF;
190		tile[2] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
191		pixel = (line >> 24) & 0xFF;
192		tile[3] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
193
194		line = *start;
195		++start;
196		pixel = line & 0xFF;
197		tile[4] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
198		pixel = (line >> 8) & 0xFF;
199		tile[5] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
200		pixel = (line >> 16) & 0xFF;
201		tile[6] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
202		pixel = (line >> 24) & 0xFF;
203		tile[7] = pixel ? palette[pixel] | 0x8000 : palette[pixel] & 0x7FFF;
204		tile += 8;
205	}
206}
207
208static inline uint16_t* _tileLookup(struct mTileCache* cache, unsigned tileId, unsigned paletteId) {
209	if (mTileCacheConfigurationIsShouldStore(cache->config)) {
210		unsigned tiles = mTileCacheSystemInfoGetMaxTiles(cache->sysConfig);
211		return &cache->cache[(tileId + paletteId * tiles) << 6];
212	} else {
213		return cache->temporaryTile;
214	}
215}
216
217const uint16_t* mTileCacheGetTile(struct mTileCache* cache, unsigned tileId, unsigned paletteId) {
218	unsigned cPaletteId = cache->activePalette;
219	unsigned count = cache->entriesPerTile;
220	unsigned bpp = cache->bpp;
221	struct mTileCacheEntry* status = &cache->status[tileId * count + paletteId];
222	struct mTileCacheEntry desiredStatus = {
223		.paletteVersion = cache->globalPaletteVersion[cPaletteId][paletteId],
224		.vramClean = 1,
225		.paletteId = paletteId,
226		.activePalette = cPaletteId
227	};
228	uint16_t* tile = _tileLookup(cache, tileId, paletteId);
229	if (!mTileCacheConfigurationIsShouldStore(cache->config) || memcmp(status, &desiredStatus, sizeof(*status))) {
230		switch (bpp) {
231		case 0:
232			return NULL;
233		case 1:
234			_regenerateTile4(cache, tile, tileId, paletteId);
235			break;
236		case 2:
237			_regenerateTile16(cache, tile, tileId, paletteId);
238			break;
239		case 3:
240			_regenerateTile256(cache, tile, tileId, paletteId);
241			break;
242		}
243		*status = desiredStatus;
244	}
245	return tile;
246}
247
248const uint16_t* mTileCacheGetTileIfDirty(struct mTileCache* cache, struct mTileCacheEntry* entry, unsigned tileId, unsigned paletteId) {
249	unsigned cPaletteId = cache->activePalette;
250	unsigned count = cache->entriesPerTile;
251	unsigned bpp = cache->bpp;
252	struct mTileCacheEntry* status = &cache->status[tileId * count + paletteId];
253	struct mTileCacheEntry desiredStatus = {
254		.paletteVersion = cache->globalPaletteVersion[cPaletteId][paletteId],
255		.vramClean = 1,
256		.paletteId = paletteId,
257		.activePalette = cPaletteId
258	};
259	uint16_t* tile = NULL;
260	if (memcmp(status, &desiredStatus, sizeof(*status))) {
261		tile = _tileLookup(cache, tileId, paletteId);
262		switch (bpp) {
263		case 0:
264			return NULL;
265		case 1:
266			_regenerateTile4(cache, tile, tileId, paletteId);
267			break;
268		case 2:
269			_regenerateTile16(cache, tile, tileId, paletteId);
270			break;
271		case 3:
272			_regenerateTile256(cache, tile, tileId, paletteId);
273			break;
274		}
275		*status = desiredStatus;
276	}
277	if (memcmp(status, &entry[paletteId], sizeof(*status))) {
278		tile = _tileLookup(cache, tileId, paletteId);
279		entry[paletteId] = *status;
280	}
281	return tile;
282}