From 5be92d3ee50fcdc04ec4d727cef5201fa8fba378 Mon Sep 17 00:00:00 2001 From: Brad Smith Date: Tue, 26 Jun 2018 22:11:01 -0400 Subject: [PATCH] Propagating memory initialization option to WRAM (#13) 1. Including WRAM in the RAM initialization options. Since it was usually initialized to zero by default, the "default" setting must do that as well, the explicit settings should follow the others. 2. unif.cpp extending memory randomization of 4-screen VRAM to match ines.cpp version, except it had no explicit initialization before: static array should initialize to 0, so its default will still do this 3. MALEE WRAM never initialized, replacing a few existing explicit (non gmalloc) WRAM clears with MemoryRand, removing a few memsets now unneeded because of gmalloc clear. --- src/boards/famicombox.cpp | 2 +- src/boards/malee.cpp | 1 + src/boards/mmc1.cpp | 7 +------ src/boards/mmc3.cpp | 4 ++-- src/boards/mmc5.cpp | 6 ++++++ src/boards/n106.cpp | 7 +++++-- src/fceu.cpp | 9 +++++---- src/fceu.h | 2 +- src/fds.cpp | 2 -- src/unif.cpp | 3 ++- src/utils/memory.cpp | 12 ++---------- src/utils/memory.h | 4 ++-- 12 files changed, 28 insertions(+), 31 deletions(-) diff --git a/src/boards/famicombox.cpp b/src/boards/famicombox.cpp index a47823fa..077931f9 100644 --- a/src/boards/famicombox.cpp +++ b/src/boards/famicombox.cpp @@ -70,7 +70,7 @@ static void SSSNROMPower(void) { regs[0] = regs[1] = regs[2] = regs[3] = regs[4] = regs[5] = regs[6] = 0; regs[7] = 0xff; Sync(); - memset(WRAM, 0x00, WRAMSIZE); + FCEU_MemoryRand(WRAM, WRAMSIZE, true); // SetWriteHandler(0x0000,0x1FFF,SSSNROMRamWrite); SetReadHandler(0x0800, 0x1FFF, CartBR); SetWriteHandler(0x0800, 0x1FFF, CartBW); diff --git a/src/boards/malee.cpp b/src/boards/malee.cpp index ca369a75..9bf4fa63 100644 --- a/src/boards/malee.cpp +++ b/src/boards/malee.cpp @@ -35,6 +35,7 @@ static void MALEEPower(void) { void MALEE_Init(CartInfo *info) { info->Power = MALEEPower; + FCEU_MemoryRand(WRAM,sizeof(WRAM),true); SetupCartPRGMapping(0x10, WRAM, 2048, 1); AddExState(WRAM, 2048, 0, "WRAM"); } diff --git a/src/boards/mmc1.cpp b/src/boards/mmc1.cpp index 09e1e497..bfe07a78 100644 --- a/src/boards/mmc1.cpp +++ b/src/boards/mmc1.cpp @@ -272,7 +272,7 @@ static void GenMMC1Power(void) { // clear non-battery-backed portion of WRAM if (NONBRAMSIZE) - FCEU_dwmemset(WRAM, 0, NONBRAMSIZE) + FCEU_MemoryRand(WRAM, NONBRAMSIZE, true); SetReadHandler(0x6000, 0x7FFF, MAWRAM); SetWriteHandler(0x6000, 0x7FFF, MBWRAM); @@ -303,11 +303,6 @@ static void GenMMC1Init(CartInfo *info, int prg, int chr, int wram, int bram) { if (WRAMSIZE) { WRAM = (uint8*)FCEU_gmalloc(WRAMSIZE); - //mbg 17-jun-08 - this shouldve been cleared to re-initialize save ram - //ch4 10-dec-08 - nope, this souldn't - //mbg 29-mar-09 - no time to debate this, we need to keep from breaking some old stuff. - //we really need to make up a policy for how compatibility and accuracy can be resolved. - memset(WRAM, 0, WRAMSIZE); SetupCartPRGMapping(0x10, WRAM, WRAMSIZE, 1); AddExState(WRAM, WRAMSIZE, 0, "WRAM"); if (bram) { diff --git a/src/boards/mmc3.cpp b/src/boards/mmc3.cpp index 0f795dd4..6a93ba92 100644 --- a/src/boards/mmc3.cpp +++ b/src/boards/mmc3.cpp @@ -279,11 +279,11 @@ void GenMMC3Power(void) { setprg8r(0x10, 0x6000, 0); } if (!(mmc3opts & 2)) - FCEU_dwmemset(WRAM, 0, WRAMSIZE); + FCEU_MemoryRand(WRAM, WRAMSIZE, true); } MMC3RegReset(); if (CHRRAM) - FCEU_dwmemset(CHRRAM, 0, CHRRAMSIZE); + FCEU_MemoryRand(CHRRAM, CHRRAMSIZE, true); } static void GenMMC3Close(void) { diff --git a/src/boards/mmc5.cpp b/src/boards/mmc5.cpp index 771ea7ec..e11ca083 100644 --- a/src/boards/mmc5.cpp +++ b/src/boards/mmc5.cpp @@ -958,6 +958,12 @@ static void GenMMC5_Init(CartInfo *info, int wsize, int battery) { MMC5fill = (uint8*)FCEU_gmalloc(1024); ExRAM = (uint8*)FCEU_gmalloc(1024); + // MMC5fill is and 8-bit tile index, and a 2-bit attribute implented as a mirrored nametable + u8 nval = MMC5fill[0x000]; + u8 aval = MMC5fill[0x3C0] & 3; aval = aval | (aval << 2) | (aval << 4) | (aval << 6); + FCEU_dwmemset(MMC5fill + 0x000, nval | (nval<<8) | (nval<<16) | (nval<<24), 0x3C0); + FCEU_dwmemset(MMC5fill + 0x3C0, aval | (aval<<8) | (aval<<16) | (aval<<24), 0x040); + AddExState(ExRAM, 1024, 0, "ERAM"); AddExState(&MMC5HackSPMode, 1, 0, "SPLM"); AddExState(&MMC5HackSPScroll, 1, 0, "SPLS"); diff --git a/src/boards/n106.cpp b/src/boards/n106.cpp index abe8de5b..76c93405 100644 --- a/src/boards/n106.cpp +++ b/src/boards/n106.cpp @@ -400,8 +400,8 @@ static void N106_Power(void) { FixCRR(); if (!battery) { - FCEU_dwmemset(WRAM, 0, 8192); - FCEU_dwmemset(IRAM, 0, 128); + FCEU_MemoryRand(WRAM, sizeof(WRAM), true); + FCEU_MemoryRand(IRAM, sizeof(IRAM), true); } for (x = 0x40; x < 0x80; x++) FixCache(x, IRAM[x]); @@ -419,6 +419,8 @@ void Mapper19_Init(CartInfo *info) { if (FSettings.SndRate) Mapper19_ESI(); + FCEU_MemoryRand(WRAM, sizeof(WRAM), true); + FCEU_MemoryRand(IRAM, sizeof(IRAM), true); AddExState(WRAM, 8192, 0, "WRAM"); AddExState(IRAM, 128, 0, "IRAM"); AddExState(N106_StateRegs, ~0, 0, 0); @@ -440,6 +442,7 @@ void Mapper210_Init(CartInfo *info) { is210 = 1; GameStateRestore = Mapper210_StateRestore; info->Power = N106_Power; + FCEU_MemoryRand(WRAM, sizeof(WRAM), true); AddExState(WRAM, 8192, 0, "WRAM"); AddExState(N106_StateRegs, ~0, 0, 0); } diff --git a/src/fceu.cpp b/src/fceu.cpp index 38f542c1..2ba2a748 100644 --- a/src/fceu.cpp +++ b/src/fceu.cpp @@ -798,8 +798,6 @@ void ResetNES(void) { int RAMInitSeed = 0; int RAMInitOption = 0; -// Note: this option does not currently apply to WRAM. -// Would it be appropriate to call FCEU_MemoryRand inside FCEU_gmalloc to initialize them? u64 splitmix64(u32 input) { u64 z = (input + 0x9e3779b97f4a7c15); @@ -842,7 +840,7 @@ u64 xoroshiro128plus_next() { return result; } -void FCEU_MemoryRand(uint8 *ptr, uint32 size) { +void FCEU_MemoryRand(uint8 *ptr, uint32 size, bool default_zero) { int x = 0; while (size) { @@ -850,7 +848,10 @@ void FCEU_MemoryRand(uint8 *ptr, uint32 size) { switch (RAMInitOption) { default: - case 0: v = (x & 4) ? 0xFF : 0x00; break; + case 0: + if (!default_zero) v = (x & 4) ? 0xFF : 0x00; + else v = 0x00; + break; case 1: v = 0xFF; break; case 2: v = 0x00; break; case 3: v = (u8)(xoroshiro128plus_next()); break; diff --git a/src/fceu.h b/src/fceu.h index bea3e656..1b76249b 100644 --- a/src/fceu.h +++ b/src/fceu.h @@ -22,7 +22,7 @@ extern char romNameWhenClosingEmulator[]; #define DECLFR(x) uint8 x (uint32 A) #define DECLFW(x) void x (uint32 A, uint8 V) -void FCEU_MemoryRand(uint8 *ptr, uint32 size); +void FCEU_MemoryRand(uint8 *ptr, uint32 size, bool default_zero=false); void SetReadHandler(int32 start, int32 end, readfunc func); void SetWriteHandler(int32 start, int32 end, writefunc func); writefunc GetWriteHandler(int32 a); diff --git a/src/fds.cpp b/src/fds.cpp index 0de068d9..463ba327 100644 --- a/src/fds.cpp +++ b/src/fds.cpp @@ -752,13 +752,11 @@ int FDSLoad(const char *name, FCEUFILE *fp) { CHRRAMSize = 8192; CHRRAM = (uint8*)FCEU_gmalloc(CHRRAMSize); - memset(CHRRAM, 0, CHRRAMSize); SetupCartCHRMapping(0, CHRRAM, CHRRAMSize, 1); AddExState(CHRRAM, CHRRAMSize, 0, "CHRR"); FDSRAMSize = 32768; FDSRAM = (uint8*)FCEU_gmalloc(FDSRAMSize); - memset(FDSRAM, 0, FDSRAMSize); SetupCartPRGMapping(1, FDSRAM, FDSRAMSize, 1); AddExState(FDSRAM, FDSRAMSize, 0, "FDSR"); diff --git a/src/unif.cpp b/src/unif.cpp index 627c9a3a..fe0cff04 100644 --- a/src/unif.cpp +++ b/src/unif.cpp @@ -114,6 +114,7 @@ static void MooMirroring(void) { if (mirrortodo < 0x4) SetupCartMirroring(mirrortodo, 1, 0); else if (mirrortodo == 0x4) { + FCEU_MemoryRand(exntar, sizeof(exntar), true); SetupCartMirroring(4, 1, exntar); AddExState(exntar, 2048, 0, "EXNR"); } else @@ -534,7 +535,7 @@ static int InitializeBoard(void) { CHRRAMSize = 256; else CHRRAMSize = 8; - CHRRAMSize <<= 10; + CHRRAMSize <<= 10; if ((UNIFchrrama = (uint8*)FCEU_malloc(CHRRAMSize))) { SetupCartCHRMapping(0, UNIFchrrama, CHRRAMSize, 1); AddExState(UNIFchrrama, CHRRAMSize, 0, "CHRR"); diff --git a/src/utils/memory.cpp b/src/utils/memory.cpp index 088c9a8d..80e072e1 100644 --- a/src/utils/memory.cpp +++ b/src/utils/memory.cpp @@ -39,11 +39,7 @@ void *FCEU_gmalloc(uint32 size) FCEU_PrintError("Error allocating memory! Doing a hard exit."); exit(1); } - //mbg 6/17/08 - sometimes this memory is used as RAM or somesuch without clearing first. - //this yields different behavior in debug and release modes. - //specifically, saveram wasnt getting cleared so the games thought their savefiles were initialized - //so we are going to clear it here. - memset(ret,0,size); + FCEU_MemoryRand((uint8*)ret,size,true); // initialize according to RAMInitOption, default zero return ret; } @@ -57,11 +53,7 @@ void *FCEU_malloc(uint32 size) FCEU_PrintError("Error allocating memory!"); return(0); } - //mbg 6/17/08 - sometimes this memory is used as RAM or somesuch without clearing first. - //this yields different behavior in debug and release modes. - //specifically, saveram wasnt getting cleared so the games thought their savefiles were initialized - //so we are going to clear it here. - memset(ret,0,size); + memset(ret,0,size); // initialize to 0 return ret; } diff --git a/src/utils/memory.h b/src/utils/memory.h index 34676d38..559c7bad 100644 --- a/src/utils/memory.h +++ b/src/utils/memory.h @@ -24,8 +24,8 @@ #define FCEU_dwmemset(d,c,n) {int _x; for(_x=n-4;_x>=0;_x-=4) *(uint32 *)&(d)[_x]=c;} -void *FCEU_malloc(uint32 size); -void *FCEU_gmalloc(uint32 size); +void *FCEU_malloc(uint32 size); // initialized to 0 +void *FCEU_gmalloc(uint32 size); // used by boards for WRAM etc, initialized to 0 (default) or other via RAMInitOption void FCEU_gfree(void *ptr); void FCEU_free(void *ptr); void FCEU_memmove(void *d, void *s, uint32 l);