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)
This commit is contained in:
parent
6cafa77f22
commit
e338ab0765
11 changed files with 48 additions and 42 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#include "stdafx.h"
|
||||
#include <random>
|
||||
#include <thread>
|
||||
#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<BaseMapper> mapper = MapperFactory::InitializeFromFile(shared_from_this(), romFile.GetFileName(), fileData);
|
||||
|
||||
RomData romData;
|
||||
shared_ptr<BaseMapper> 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<HdPackData> Console::GetHdData()
|
||||
{
|
||||
return _hdData;
|
||||
|
|
|
@ -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<Console>
|
||||
{
|
||||
|
@ -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<HdPackData> GetHdData();
|
||||
bool IsHdPpu();
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ public:
|
|||
Eeprom24C01(shared_ptr<Console> console)
|
||||
{
|
||||
_console = console;
|
||||
BaseMapper::InitializeRam(_console->GetSettings()->GetRamPowerOnState(), _romData, 128);
|
||||
_console->InitializeRam(_romData, 128);
|
||||
_console->GetBatteryManager()->LoadBattery(".eeprom128", _romData, 128);
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ public:
|
|||
Eeprom24C02(shared_ptr<Console> console)
|
||||
{
|
||||
_console = console;
|
||||
BaseMapper::InitializeRam(_console->GetSettings()->GetRamPowerOnState(), _romData, 256);
|
||||
_console->InitializeRam(_romData, 256);
|
||||
_console->GetBatteryManager()->LoadBattery(".eeprom256", _romData, 256);
|
||||
}
|
||||
|
||||
|
|
|
@ -656,7 +656,7 @@ BaseMapper* MapperFactory::GetMapperFromID(RomData &romData)
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
shared_ptr<BaseMapper> MapperFactory::InitializeFromFile(shared_ptr<Console> console, string romFilename, vector<uint8_t> &fileData)
|
||||
shared_ptr<BaseMapper> MapperFactory::InitializeFromFile(shared_ptr<Console> console, string romFilename, vector<uint8_t> &fileData, RomData &outRomData)
|
||||
{
|
||||
RomLoader loader;
|
||||
|
||||
|
@ -673,9 +673,7 @@ shared_ptr<BaseMapper> MapperFactory::InitializeFromFile(shared_ptr<Console> con
|
|||
shared_ptr<BaseMapper> mapper(GetMapperFromID(romData));
|
||||
|
||||
if(mapper) {
|
||||
mapper->SetConsole(console);
|
||||
mapper->Initialize(romData);
|
||||
console->GetNotificationManager()->RegisterNotificationListener(mapper);
|
||||
outRomData = romData;
|
||||
return mapper;
|
||||
}
|
||||
} else if(loader.GetRomData().BiosMissing) {
|
||||
|
|
|
@ -15,5 +15,5 @@ class MapperFactory
|
|||
static constexpr uint16_t FdsMapperID = 65535;
|
||||
static constexpr uint16_t NsfMapperID = 65534;
|
||||
|
||||
static shared_ptr<BaseMapper> InitializeFromFile(shared_ptr<Console> console, string romFilename, vector<uint8_t> &fileData);
|
||||
static shared_ptr<BaseMapper> InitializeFromFile(shared_ptr<Console> console, string romFilename, vector<uint8_t> &fileData, RomData &outRomData);
|
||||
};
|
||||
|
|
|
@ -38,7 +38,7 @@ void MemoryManager::SetMapper(shared_ptr<BaseMapper> mapper)
|
|||
void MemoryManager::Reset(bool softReset)
|
||||
{
|
||||
if(!softReset) {
|
||||
_mapper->InitializeRam(_internalRAM, InternalRAMSize);
|
||||
_console->InitializeRam(_internalRAM, InternalRAMSize);
|
||||
}
|
||||
|
||||
_mapper->Reset(softReset);
|
||||
|
|
|
@ -28,8 +28,8 @@ PPU::PPU(shared_ptr<Console> 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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue