From 9ed560de2f0f6867e36cf06baa0a0edbd7af171c Mon Sep 17 00:00:00 2001 From: BearOso Date: Mon, 9 May 2022 15:32:07 -0500 Subject: [PATCH] Make SNES memory allocations static. --- libretro/libretro.cpp | 2 +- memmap.cpp | 47 ++++++++----------------------------------- memmap.h | 9 +++++---- snapshot.cpp | 14 ++++++------- 4 files changed, 21 insertions(+), 51 deletions(-) diff --git a/libretro/libretro.cpp b/libretro/libretro.cpp index 482d4f3c..b9c8abd2 100644 --- a/libretro/libretro.cpp +++ b/libretro/libretro.cpp @@ -1147,7 +1147,7 @@ bool retro_load_game(const struct retro_game_info *game) if (randomize_memory) { srand(time(NULL)); - for(int lcv = 0; lcv < 0x20000; lcv++) + for(int lcv = 0; lcv < sizeof(Memory.RAM); lcv++) Memory.RAM[lcv] = rand() % 256; } } diff --git a/memmap.cpp b/memmap.cpp index 955a6f8b..b1a6aff0 100644 --- a/memmap.cpp +++ b/memmap.cpp @@ -893,13 +893,6 @@ static void S9xDeinterleaveGD24 (int size, uint8 *base) bool8 CMemory::Init (void) { - // TODO: If these change size, check other locations in the code that also - // have the fixed size. In the future, make this a static allocation. - RAM = (uint8 *) malloc(0x20000); - SRAM = (uint8 *) malloc(0x80000); - VRAM = (uint8 *) malloc(0x10000); - ROM = (uint8 *) malloc(MAX_ROM_SIZE + 0x200 + 0x8000); - IPPU.TileCache[TILE_2BIT] = (uint8 *) malloc(MAX_2BIT_TILES * 64); IPPU.TileCache[TILE_4BIT] = (uint8 *) malloc(MAX_4BIT_TILES * 64); IPPU.TileCache[TILE_8BIT] = (uint8 *) malloc(MAX_8BIT_TILES * 64); @@ -916,8 +909,7 @@ bool8 CMemory::Init (void) IPPU.TileCached[TILE_4BIT_EVEN] = (uint8 *) malloc(MAX_4BIT_TILES); IPPU.TileCached[TILE_4BIT_ODD] = (uint8 *) malloc(MAX_4BIT_TILES); - if (!RAM || !SRAM || !VRAM || !ROM || - !IPPU.TileCache[TILE_2BIT] || + if (!IPPU.TileCache[TILE_2BIT] || !IPPU.TileCache[TILE_4BIT] || !IPPU.TileCache[TILE_8BIT] || !IPPU.TileCache[TILE_2BIT_EVEN] || @@ -936,10 +928,10 @@ bool8 CMemory::Init (void) return (FALSE); } - memset(RAM, 0, 0x20000); - memset(SRAM, 0, 0x80000); - memset(VRAM, 0, 0x10000); - memset(ROM, 0, MAX_ROM_SIZE + 0x200 + 0x8000); + memset(RAM, 0, sizeof(RAM)); + memset(SRAM, 0, sizeof(SRAM)); + memset(VRAM, 0, sizeof(VRAM)); + memset(ROMStorage, 0, sizeof(ROMStorage)); memset(IPPU.TileCache[TILE_2BIT], 0, MAX_2BIT_TILES * 64); memset(IPPU.TileCache[TILE_4BIT], 0, MAX_4BIT_TILES * 64); @@ -960,12 +952,12 @@ bool8 CMemory::Init (void) // FillRAM uses first 32K of ROM image area, otherwise space just // wasted. Might be read by the SuperFX code. - FillRAM = ROM; + FillRAM = &ROMStorage[0]; // Add 0x8000 to ROM image pointer to stop SuperFX code accessing // unallocated memory (can cause crash on some ports). - ROM += 0x8000; + ROM = &ROMStorage[0x8000]; C4RAM = ROM + 0x400000 + 8192 * 8; // C4 OBC1RAM = ROM + 0x400000; // OBC1 @@ -985,30 +977,7 @@ bool8 CMemory::Init (void) void CMemory::Deinit (void) { - if (RAM) - { - free(RAM); - RAM = NULL; - } - - if (SRAM) - { - free(SRAM); - SRAM = NULL; - } - - if (VRAM) - { - free(VRAM); - VRAM = NULL; - } - - if (ROM) - { - ROM -= 0x8000; - free(ROM); - ROM = NULL; - } + ROM = NULL; for (int t = 0; t < 7; t++) { diff --git a/memmap.h b/memmap.h index 30f4e0ca..5cfabe4b 100644 --- a/memmap.h +++ b/memmap.h @@ -55,10 +55,11 @@ struct CMemory uint8 NSRTHeader[32]; int32 HeaderCount; - uint8 *RAM; - uint8 *ROM; - uint8 *SRAM; - uint8 *VRAM; + uint8 RAM[0x20000]; + uint8 ROMStorage[MAX_ROM_SIZE + 0x200 + 0x8000]; + uint8 *ROM; + uint8 SRAM[0x80000]; + uint8 VRAM[0x10000]; uint8 *FillRAM; uint8 *BWRAM; uint8 *C4RAM; diff --git a/snapshot.cpp b/snapshot.cpp index 3175c7d6..834c1365 100644 --- a/snapshot.cpp +++ b/snapshot.cpp @@ -1189,11 +1189,11 @@ void S9xFreezeToStream (STREAM stream) dma_snap.dma[d] = DMA[d]; FreezeStruct(stream, "DMA", &dma_snap, SnapDMA, COUNT(SnapDMA)); - FreezeBlock (stream, "VRA", Memory.VRAM, 0x10000); + FreezeBlock (stream, "VRA", Memory.VRAM, sizeof(Memory.VRAM)); - FreezeBlock (stream, "RAM", Memory.RAM, 0x20000); + FreezeBlock (stream, "RAM", Memory.RAM, sizeof(Memory.RAM)); - FreezeBlock (stream, "SRA", Memory.SRAM, 0x80000); + FreezeBlock (stream, "SRA", Memory.SRAM, sizeof(Memory.SRAM)); FreezeBlock (stream, "FIL", Memory.FillRAM, 0x8000); @@ -1391,16 +1391,16 @@ int S9xUnfreezeFromStream (STREAM stream) break; if (fast) - result = UnfreezeBlock(stream, "RAM", Memory.RAM, 0x20000); + result = UnfreezeBlock(stream, "RAM", Memory.RAM, sizeof(Memory.RAM)); else - result = UnfreezeBlockCopy(stream, "RAM", &local_ram, 0x20000); + result = UnfreezeBlockCopy(stream, "RAM", &local_ram, sizeof(Memory.RAM)); if (result != SUCCESS) break; if (fast) - result = UnfreezeBlock(stream, "SRA", Memory.SRAM, 0x80000); + result = UnfreezeBlock(stream, "SRA", Memory.SRAM, sizeof(Memory.SRAM)); else - result = UnfreezeBlockCopy (stream, "SRA", &local_sram, 0x80000); + result = UnfreezeBlockCopy (stream, "SRA", &local_sram, sizeof(Memory.SRAM)); if (result != SUCCESS) break;