Core: Fix unitialized memory issues with graphics caches

This commit is contained in:
Vicki Pfau 2019-11-16 12:16:16 -08:00
parent 6900d130ae
commit 5e37df6cf5
6 changed files with 39 additions and 18 deletions

View File

@ -94,6 +94,7 @@ Emulation fixes:
- GBA Memory: Fix open bus from IWRAM (fixes mgba.io/i/1575)
Other fixes:
- 3DS: Fix screen darkening (fixes mgba.io/i/1562)
- Core: Fix uninitialized memory issues with graphics caches
- Vita: Fix analog controls (fixes mgba.io/i/1554)
- Qt: Fix fast forward mute being reset (fixes mgba.io/i/1574)

View File

@ -11,6 +11,7 @@ void mBitmapCacheInit(struct mBitmapCache* cache) {
// TODO: Reconfigurable cache for space savings
cache->cache = NULL;
cache->config = mBitmapCacheConfigurationFillShouldStore(0);
cache->sysConfig = 0;
cache->status = NULL;
cache->palette = NULL;
cache->buffer = 0;
@ -18,14 +19,18 @@ void mBitmapCacheInit(struct mBitmapCache* cache) {
static void _freeCache(struct mBitmapCache* cache) {
size_t size = mBitmapCacheSystemInfoGetHeight(cache->sysConfig) * mBitmapCacheSystemInfoGetBuffers(cache->sysConfig);
mappedMemoryFree(cache->cache, mBitmapCacheSystemInfoGetWidth(cache->sysConfig) * size * sizeof(color_t));
mappedMemoryFree(cache->status, size * sizeof(*cache->status));
if (cache->cache) {
mappedMemoryFree(cache->cache, mBitmapCacheSystemInfoGetWidth(cache->sysConfig) * size * sizeof(color_t));
cache->cache = NULL;
}
if (cache->status) {
mappedMemoryFree(cache->status, size * sizeof(*cache->status));
cache->status = NULL;
}
if (cache->palette) {
free(cache->palette);
cache->palette = NULL;
}
cache->cache = NULL;
cache->status = NULL;
cache->palette = NULL;
}
static void _redoCacheSize(struct mBitmapCache* cache) {

View File

@ -11,15 +11,20 @@ void mMapCacheInit(struct mMapCache* cache) {
// TODO: Reconfigurable cache for space savings
cache->cache = NULL;
cache->config = mMapCacheConfigurationFillShouldStore(0);
cache->sysConfig = 0;
cache->status = NULL;
}
static void _freeCache(struct mMapCache* cache) {
size_t tiles = (1 << mMapCacheSystemInfoGetTilesWide(cache->sysConfig)) * (1 << mMapCacheSystemInfoGetTilesHigh(cache->sysConfig));
mappedMemoryFree(cache->cache, 8 * 8 * sizeof(color_t) * tiles);
mappedMemoryFree(cache->status, tiles * sizeof(*cache->status));
cache->cache = NULL;
cache->status = NULL;
if (cache->cache) {
mappedMemoryFree(cache->cache, 8 * 8 * sizeof(color_t) * tiles);
cache->cache = NULL;
}
if (cache->status) {
mappedMemoryFree(cache->status, tiles * sizeof(*cache->status));
cache->status = NULL;
}
}
static void _redoCacheSize(struct mMapCache* cache) {

View File

@ -51,6 +51,9 @@ static void _redoCacheSize(struct mTileCache* cache) {
}
void mTileCacheConfigure(struct mTileCache* cache, mTileCacheConfiguration config) {
if (cache->config == config) {
return;
}
_freeCache(cache);
cache->config = config;
_redoCacheSize(cache);

View File

@ -12,11 +12,13 @@
void GBVideoCacheInit(struct mCacheSet* cache) {
mCacheSetInit(cache, 2, 0, 1);
mTileCacheConfiguration config = 0;
config = mTileCacheSystemInfoSetPaletteBPP(config, 1); // 2^(2^1) = 4 entries
config = mTileCacheSystemInfoSetPaletteCount(config, 4); // 16 palettes
config = mTileCacheSystemInfoSetMaxTiles(config, 1024);
mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 0), config, 0, 0);
mTileCacheSystemInfo sysconfig = 0;
mTileCacheConfiguration config = mTileCacheConfigurationFillShouldStore(0);
sysconfig = mTileCacheSystemInfoSetPaletteBPP(sysconfig, 1); // 2^(2^1) = 4 entries
sysconfig = mTileCacheSystemInfoSetPaletteCount(sysconfig, 4); // 16 palettes
sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 1024);
mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 0), sysconfig, 0, 0);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 0), config);
mMapCacheSetGetPointer(&cache->maps, 0)->tileCache = mTileCacheSetGetPointer(&cache->tiles, 0);
mMapCacheSetGetPointer(&cache->maps, 1)->tileCache = mTileCacheSetGetPointer(&cache->tiles, 0);

View File

@ -17,20 +17,20 @@ void GBAVideoCacheInit(struct mCacheSet* cache) {
sysconfig = mTileCacheSystemInfoSetPaletteBPP(sysconfig, 2); // 2^(2^2) = 16 entries
sysconfig = mTileCacheSystemInfoSetPaletteCount(sysconfig, 4); // 16 palettes
sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 2048);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 0), config);
mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 0), sysconfig, 0, 0);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 0), config);
sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 1024);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 2), config);
mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 2), sysconfig, 0x10000, 0x100);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 2), config);
sysconfig = mTileCacheSystemInfoSetPaletteBPP(sysconfig, 3); // 2^(2^3) = 256 entries
sysconfig = mTileCacheSystemInfoSetPaletteCount(sysconfig, 0); // 1 palettes
sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 2048);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 1), config);
mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 1), sysconfig, 0, 0);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 1), config);
sysconfig = mTileCacheSystemInfoSetMaxTiles(sysconfig, 1024);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 3), config);
mTileCacheConfigureSystem(mTileCacheSetGetPointer(&cache->tiles, 3), sysconfig, 0x10000, 0x100);
mTileCacheConfigure(mTileCacheSetGetPointer(&cache->tiles, 3), config);
mBitmapCacheSystemInfo bitConfig;
bitConfig = mBitmapCacheSystemInfoSetEntryBPP(0, 4);
@ -50,6 +50,11 @@ void GBAVideoCacheInit(struct mCacheSet* cache) {
mBitmapCacheConfigureSystem(mBitmapCacheSetGetPointer(&cache->bitmaps, 1), bitConfig);
mBitmapCacheSetGetPointer(&cache->bitmaps, 1)->bitsStart[0] = 0;
mBitmapCacheSetGetPointer(&cache->bitmaps, 1)->bitsStart[1] = 0xA000;
mMapCacheSetGetPointer(&cache->maps, 0)->context = NULL;
mMapCacheSetGetPointer(&cache->maps, 1)->context = NULL;
mMapCacheSetGetPointer(&cache->maps, 2)->context = NULL;
mMapCacheSetGetPointer(&cache->maps, 3)->context = NULL;
}
void GBAVideoCacheAssociate(struct mCacheSet* cache, struct GBAVideo* video) {