From e338ab0765d6b488a67743dc2a031afde9a057a1 Mon Sep 17 00:00:00 2001 From: Sour Date: Sat, 22 Jun 2019 16:33:59 -0400 Subject: [PATCH] Fixed init order when loading a game to prevent potential crashes (or incorrect behavior) in some scenarios (mostly if using the random mapper state option) --- Core/BaseMapper.cpp | 30 ++++-------------------------- Core/BaseMapper.h | 3 --- Core/Console.cpp | 31 ++++++++++++++++++++++++++++++- Core/Console.h | 4 ++++ Core/Eeprom24C01.h | 2 +- Core/Eeprom24C02.h | 2 +- Core/MapperFactory.cpp | 6 ++---- Core/MapperFactory.h | 2 +- Core/MemoryManager.cpp | 2 +- Core/PPU.cpp | 4 ++-- Core/UnlDripGame.h | 4 ++-- 11 files changed, 48 insertions(+), 42 deletions(-) diff --git a/Core/BaseMapper.cpp b/Core/BaseMapper.cpp index eb6c711c..25f997f6 100644 --- a/Core/BaseMapper.cpp +++ b/Core/BaseMapper.cpp @@ -372,28 +372,6 @@ void BaseMapper::SelectCHRPage(uint16_t slot, uint16_t page, ChrMemoryType memor SetPpuMemoryMapping(startAddr, endAddr, page, memoryType); } -void BaseMapper::InitializeRam(void* data, uint32_t length) -{ - InitializeRam(_console->GetSettings()->GetRamPowerOnState(), data, length); -} - -void BaseMapper::InitializeRam(RamPowerOnState powerOnState, void* data, uint32_t length) -{ - switch(powerOnState) { - default: - case RamPowerOnState::AllZeros: memset(data, 0, length); break; - case RamPowerOnState::AllOnes: memset(data, 0xFF, length); break; - case RamPowerOnState::Random: - std::random_device rd; - std::mt19937 mt(rd()); - std::uniform_int_distribution<> dist(0, 255); - for(uint32_t i = 0; i < length; i++) { - ((uint8_t*)data)[i] = dist(mt); - } - break; - } -} - uint8_t BaseMapper::GetPowerOnByte(uint8_t defaultValue) { if(_console->GetSettings()->CheckFlag(EmulationFlags::RandomizeMapperPowerOnState)) { @@ -467,7 +445,7 @@ void BaseMapper::InitializeChrRam(int32_t chrRamSize) _chrRamSize = chrRamSize >= 0 ? chrRamSize : defaultRamSize; if(_chrRamSize > 0) { _chrRam = new uint8_t[_chrRamSize]; - InitializeRam(_chrRam, _chrRamSize); + _console->InitializeRam(_chrRam, _chrRamSize); } } @@ -607,12 +585,12 @@ void BaseMapper::Initialize(RomData &romData) _saveRam = new uint8_t[_saveRamSize]; _workRam = new uint8_t[_workRamSize]; - InitializeRam(_saveRam, _saveRamSize); - InitializeRam(_workRam, _workRamSize); + _console->InitializeRam(_saveRam, _saveRamSize); + _console->InitializeRam(_workRam, _workRamSize); _nametableCount = 2; _nametableRam = new uint8_t[BaseMapper::NametableSize*BaseMapper::NametableCount]; - InitializeRam(_nametableRam, BaseMapper::NametableSize*BaseMapper::NametableCount); + _console->InitializeRam(_nametableRam, BaseMapper::NametableSize*BaseMapper::NametableCount); for(int i = 0; i < 0x100; i++) { //Allow us to map a different page every 256 bytes diff --git a/Core/BaseMapper.h b/Core/BaseMapper.h index 0ef7e6e4..04b5f86b 100644 --- a/Core/BaseMapper.h +++ b/Core/BaseMapper.h @@ -200,9 +200,6 @@ public: uint8_t DebugReadVRAM(uint16_t addr, bool disableSideEffects = true); - void InitializeRam(void* data, uint32_t length); - static void InitializeRam(RamPowerOnState powerOnState, void* data, uint32_t length); - void CopyChrTile(uint32_t address, uint8_t *dest); //Debugger Helper Functions diff --git a/Core/Console.cpp b/Core/Console.cpp index baa4953e..270b7254 100644 --- a/Core/Console.cpp +++ b/Core/Console.cpp @@ -1,4 +1,5 @@ #include "stdafx.h" +#include #include #include "Console.h" #include "CPU.h" @@ -277,7 +278,9 @@ bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile) romFile.ReadFile(fileData); _batteryManager->Initialize(FolderUtilities::GetFilename(romFile.GetFileName(), false)); - shared_ptr mapper = MapperFactory::InitializeFromFile(shared_from_this(), romFile.GetFileName(), fileData); + + RomData romData; + shared_ptr mapper = MapperFactory::InitializeFromFile(shared_from_this(), romFile.GetFileName(), fileData, romData); if(mapper) { _soundMixer->StopAudio(true); @@ -364,6 +367,10 @@ bool Console::Initialize(VirtualFile &romFile, VirtualFile &patchFile) _ppu.reset(new PPU(shared_from_this())); } + _mapper->SetConsole(shared_from_this()); + _mapper->Initialize(romData); + GetNotificationManager()->RegisterNotificationListener(_mapper); + _memoryManager->SetMapper(_mapper); _memoryManager->RegisterIODevice(_ppu.get()); _memoryManager->RegisterIODevice(_apu.get()); @@ -1125,6 +1132,28 @@ int32_t Console::GetStopCode() return _stopCode; } +void Console::InitializeRam(void* data, uint32_t length) +{ + InitializeRam(_settings->GetRamPowerOnState(), data, length); +} + +void Console::InitializeRam(RamPowerOnState powerOnState, void* data, uint32_t length) +{ + switch(powerOnState) { + default: + case RamPowerOnState::AllZeros: memset(data, 0, length); break; + case RamPowerOnState::AllOnes: memset(data, 0xFF, length); break; + case RamPowerOnState::Random: + std::random_device rd; + std::mt19937 mt(rd()); + std::uniform_int_distribution<> dist(0, 255); + for(uint32_t i = 0; i < length; i++) { + ((uint8_t*)data)[i] = dist(mt); + } + break; + } +} + shared_ptr Console::GetHdData() { return _hdData; diff --git a/Core/Console.h b/Core/Console.h index b3582d32..d1f39cd6 100644 --- a/Core/Console.h +++ b/Core/Console.h @@ -39,6 +39,7 @@ enum class ScaleFilterType; enum class ConsoleFeatures; enum class DebugMemoryType; enum class EventType; +enum class RamPowerOnState; class Console : public std::enable_shared_from_this { @@ -214,6 +215,9 @@ public: bool IsDebuggerAttached(); + void InitializeRam(void* data, uint32_t length); + static void InitializeRam(RamPowerOnState powerOnState, void* data, uint32_t length); + shared_ptr GetHdData(); bool IsHdPpu(); diff --git a/Core/Eeprom24C01.h b/Core/Eeprom24C01.h index b34b01f2..dbc68a36 100644 --- a/Core/Eeprom24C01.h +++ b/Core/Eeprom24C01.h @@ -27,7 +27,7 @@ public: Eeprom24C01(shared_ptr console) { _console = console; - BaseMapper::InitializeRam(_console->GetSettings()->GetRamPowerOnState(), _romData, 128); + _console->InitializeRam(_romData, 128); _console->GetBatteryManager()->LoadBattery(".eeprom128", _romData, 128); } diff --git a/Core/Eeprom24C02.h b/Core/Eeprom24C02.h index 1bfb1a98..15840ffd 100644 --- a/Core/Eeprom24C02.h +++ b/Core/Eeprom24C02.h @@ -27,7 +27,7 @@ public: Eeprom24C02(shared_ptr console) { _console = console; - BaseMapper::InitializeRam(_console->GetSettings()->GetRamPowerOnState(), _romData, 256); + _console->InitializeRam(_romData, 256); _console->GetBatteryManager()->LoadBattery(".eeprom256", _romData, 256); } diff --git a/Core/MapperFactory.cpp b/Core/MapperFactory.cpp index e6a887f7..3c700f40 100644 --- a/Core/MapperFactory.cpp +++ b/Core/MapperFactory.cpp @@ -656,7 +656,7 @@ BaseMapper* MapperFactory::GetMapperFromID(RomData &romData) return nullptr; } -shared_ptr MapperFactory::InitializeFromFile(shared_ptr console, string romFilename, vector &fileData) +shared_ptr MapperFactory::InitializeFromFile(shared_ptr console, string romFilename, vector &fileData, RomData &outRomData) { RomLoader loader; @@ -673,9 +673,7 @@ shared_ptr MapperFactory::InitializeFromFile(shared_ptr con shared_ptr mapper(GetMapperFromID(romData)); if(mapper) { - mapper->SetConsole(console); - mapper->Initialize(romData); - console->GetNotificationManager()->RegisterNotificationListener(mapper); + outRomData = romData; return mapper; } } else if(loader.GetRomData().BiosMissing) { diff --git a/Core/MapperFactory.h b/Core/MapperFactory.h index 3b3cc21d..f33f44ee 100644 --- a/Core/MapperFactory.h +++ b/Core/MapperFactory.h @@ -15,5 +15,5 @@ class MapperFactory static constexpr uint16_t FdsMapperID = 65535; static constexpr uint16_t NsfMapperID = 65534; - static shared_ptr InitializeFromFile(shared_ptr console, string romFilename, vector &fileData); + static shared_ptr InitializeFromFile(shared_ptr console, string romFilename, vector &fileData, RomData &outRomData); }; diff --git a/Core/MemoryManager.cpp b/Core/MemoryManager.cpp index bfc28ab0..dfe956cc 100644 --- a/Core/MemoryManager.cpp +++ b/Core/MemoryManager.cpp @@ -38,7 +38,7 @@ void MemoryManager::SetMapper(shared_ptr mapper) void MemoryManager::Reset(bool softReset) { if(!softReset) { - _mapper->InitializeRam(_internalRAM, InternalRAMSize); + _console->InitializeRam(_internalRAM, InternalRAMSize); } _mapper->Reset(softReset); diff --git a/Core/PPU.cpp b/Core/PPU.cpp index abb891a0..c1f7e4db 100644 --- a/Core/PPU.cpp +++ b/Core/PPU.cpp @@ -28,8 +28,8 @@ PPU::PPU(shared_ptr console) 0x09, 0x01, 0x34, 0x03, 0x00, 0x04, 0x00, 0x14, 0x08, 0x3A, 0x00, 0x02, 0x00, 0x20, 0x2C, 0x08 }; memcpy(_paletteRAM, paletteRamBootValues, sizeof(_paletteRAM)); - _console->GetMapper()->InitializeRam(_spriteRAM, 0x100); - _console->GetMapper()->InitializeRam(_secondarySpriteRAM, 0x20); + _console->InitializeRam(_spriteRAM, 0x100); + _console->InitializeRam(_secondarySpriteRAM, 0x20); Reset(); } diff --git a/Core/UnlDripGame.h b/Core/UnlDripGame.h index 497e2b47..fd80a0cb 100644 --- a/Core/UnlDripGame.h +++ b/Core/UnlDripGame.h @@ -35,8 +35,8 @@ protected: _wramWriteEnabled = false; _lastNametableFetchAddr = 0; - InitializeRam(_extendedAttributes[0], 0x400); - InitializeRam(_extendedAttributes[1], 0x400); + _console->InitializeRam(_extendedAttributes[0], 0x400); + _console->InitializeRam(_extendedAttributes[1], 0x400); SelectPRGPage(1, -1);