Skip to content

Commit

Permalink
HW/Memmap: Refactor Memory to class, move to Core::System.
Browse files Browse the repository at this point in the history
  • Loading branch information
AdmiralCurtiss committed Dec 3, 2022
1 parent 7cd9a78 commit 839db59
Show file tree
Hide file tree
Showing 83 changed files with 2,221 additions and 1,360 deletions.
22 changes: 17 additions & 5 deletions Source/Core/Core/Boot/Boot.cpp
Expand Up @@ -55,6 +55,7 @@ namespace fs = std::filesystem;
#include "Core/PowerPC/PPCAnalyst.h"
#include "Core/PowerPC/PPCSymbolDB.h"
#include "Core/PowerPC/PowerPC.h"
#include "Core/System.h"

#include "DiscIO/Enums.h"
#include "DiscIO/GameModDescriptor.h"
Expand Down Expand Up @@ -348,7 +349,10 @@ bool CBoot::DVDRead(const DiscIO::VolumeDisc& disc, u64 dvd_offset, u32 output_a
std::vector<u8> buffer(length);
if (!disc.Read(dvd_offset, length, buffer.data(), partition))
return false;
Memory::CopyToEmu(output_address, buffer.data(), length);

auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();
memory.CopyToEmu(output_address, buffer.data(), length);
return true;
}

Expand All @@ -357,7 +361,11 @@ bool CBoot::DVDReadDiscID(const DiscIO::VolumeDisc& disc, u32 output_address)
std::array<u8, 0x20> buffer;
if (!disc.Read(0, buffer.size(), buffer.data(), DiscIO::PARTITION_NONE))
return false;
Memory::CopyToEmu(output_address, buffer.data(), buffer.size());

auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();
memory.CopyToEmu(output_address, buffer.data(), buffer.size());

// Transition out of the DiscIdNotRead state (which the drive should be in at this point,
// on the assumption that this is only used for the first read)
DVDInterface::SetDriveState(DVDInterface::DriveState::ReadyNoReadsMade);
Expand Down Expand Up @@ -455,8 +463,10 @@ bool CBoot::Load_BS2(const std::string& boot_rom_filename)
// copying the initial boot code to 0x81200000 is a hack.
// For now, HLE the first few instructions and start at 0x81200150
// to work around this.
Memory::CopyToEmu(0x01200000, data.data() + 0x100, 0x700);
Memory::CopyToEmu(0x01300000, data.data() + 0x820, 0x1AFE00);
auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();
memory.CopyToEmu(0x01200000, data.data() + 0x100, 0x700);
memory.CopyToEmu(0x01300000, data.data() + 0x820, 0x1AFE00);

PowerPC::ppcState.gpr[3] = 0xfff0001f;
PowerPC::ppcState.gpr[4] = 0x00002030;
Expand Down Expand Up @@ -491,9 +501,11 @@ static void CopyDefaultExceptionHandlers()
0x00000900, 0x00000C00, 0x00000D00, 0x00000F00,
0x00001300, 0x00001400, 0x00001700};

auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();
constexpr u32 RFI_INSTRUCTION = 0x4C000064;
for (const u32 address : EXCEPTION_HANDLER_ADDRESSES)
Memory::Write_U32(RFI_INSTRUCTION, address);
memory.Write_U32(RFI_INSTRUCTION, address);
}

// Third boot step after BootManager and Core. See Call schedule in BootManager.cpp
Expand Down
78 changes: 45 additions & 33 deletions Source/Core/Core/Boot/Boot_BS2Emu.cpp
Expand Up @@ -32,6 +32,7 @@
#include "Core/IOS/Uids.h"
#include "Core/PowerPC/MMU.h"
#include "Core/PowerPC/PowerPC.h"
#include "Core/System.h"

#include "DiscIO/Enums.h"
#include "DiscIO/RiivolutionPatcher.h"
Expand Down Expand Up @@ -212,11 +213,14 @@ bool CBoot::RunApploader(bool is_wii, const DiscIO::VolumeDisc& volume,

void CBoot::SetupGCMemory()
{
auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();

// Booted from bootrom. 0xE5207C22 = booted from jtag
PowerPC::HostWrite_U32(0x0D15EA5E, 0x80000020);

// Physical Memory Size (24MB on retail)
PowerPC::HostWrite_U32(Memory::GetRamSizeReal(), 0x80000028);
PowerPC::HostWrite_U32(memory.GetRamSizeReal(), 0x80000028);

// Console type - DevKit (retail ID == 0x00000003) see YAGCD 4.2.1.1.2
// TODO: determine why some games fail when using a retail ID.
Expand Down Expand Up @@ -271,10 +275,12 @@ bool CBoot::EmulatedBS2_GC(const DiscIO::VolumeDisc& volume,

DVDReadDiscID(volume, 0x00000000);

bool streaming = Memory::Read_U8(0x80000008);
auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();
bool streaming = memory.Read_U8(0x80000008);
if (streaming)
{
u8 streaming_size = Memory::Read_U8(0x80000009);
u8 streaming_size = memory.Read_U8(0x80000009);
// If the streaming buffer size is 0, then BS2 uses a default size of 10 instead.
// No known game uses a size other than the default.
if (streaming_size == 0)
Expand Down Expand Up @@ -412,8 +418,11 @@ bool CBoot::SetupWiiMemory(IOS::HLE::IOSC::ConsoleType console_type)
return false;
}

auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();

// Write the 256 byte setting.txt to memory.
Memory::CopyToEmu(0x3800, gen.GetBytes().data(), gen.GetBytes().size());
memory.CopyToEmu(0x3800, gen.GetBytes().data(), gen.GetBytes().size());

INFO_LOG_FMT(BOOT, "Setup Wii Memory...");

Expand All @@ -427,28 +436,28 @@ bool CBoot::SetupWiiMemory(IOS::HLE::IOSC::ConsoleType console_type)
0x80000060 Copyright code
*/

Memory::Write_U32(0x0D15EA5E, 0x00000020); // Another magic word
Memory::Write_U32(0x00000001, 0x00000024); // Unknown
Memory::Write_U32(Memory::GetRamSizeReal(), 0x00000028); // MEM1 size 24MB
memory.Write_U32(0x0D15EA5E, 0x00000020); // Another magic word
memory.Write_U32(0x00000001, 0x00000024); // Unknown
memory.Write_U32(memory.GetRamSizeReal(), 0x00000028); // MEM1 size 24MB
const Core::ConsoleType board_model = console_type == IOS::HLE::IOSC::ConsoleType::RVT ?
Core::ConsoleType::NDEV2_1 :
Core::ConsoleType::RVL_Retail3;
Memory::Write_U32(static_cast<u32>(board_model), 0x0000002c); // Board Model
Memory::Write_U32(0x00000000, 0x00000030); // Init
Memory::Write_U32(0x817FEC60, 0x00000034); // Init
memory.Write_U32(static_cast<u32>(board_model), 0x0000002c); // Board Model
memory.Write_U32(0x00000000, 0x00000030); // Init
memory.Write_U32(0x817FEC60, 0x00000034); // Init
// 38, 3C should get start, size of FST through apploader
Memory::Write_U32(0x8008f7b8, 0x000000e4); // Thread Init
Memory::Write_U32(Memory::GetRamSizeReal(), 0x000000f0); // "Simulated memory size" (debug mode?)
Memory::Write_U32(0x8179b500, 0x000000f4); // __start
Memory::Write_U32(0x0e7be2c0, 0x000000f8); // Bus speed
Memory::Write_U32(0x2B73A840, 0x000000fc); // CPU speed
Memory::Write_U16(0x0000, 0x000030e6); // Console type
Memory::Write_U32(0x00000000, 0x000030c0); // EXI
Memory::Write_U32(0x00000000, 0x000030c4); // EXI
Memory::Write_U32(0x00000000, 0x000030dc); // Time
Memory::Write_U32(0xffffffff, 0x000030d8); // Unknown, set by any official NAND title
Memory::Write_U16(0x8201, 0x000030e6); // Dev console / debug capable
Memory::Write_U32(0x00000000, 0x000030f0); // Apploader
memory.Write_U32(0x8008f7b8, 0x000000e4); // Thread Init
memory.Write_U32(memory.GetRamSizeReal(), 0x000000f0); // "Simulated memory size" (debug mode?)
memory.Write_U32(0x8179b500, 0x000000f4); // __start
memory.Write_U32(0x0e7be2c0, 0x000000f8); // Bus speed
memory.Write_U32(0x2B73A840, 0x000000fc); // CPU speed
memory.Write_U16(0x0000, 0x000030e6); // Console type
memory.Write_U32(0x00000000, 0x000030c0); // EXI
memory.Write_U32(0x00000000, 0x000030c4); // EXI
memory.Write_U32(0x00000000, 0x000030dc); // Time
memory.Write_U32(0xffffffff, 0x000030d8); // Unknown, set by any official NAND title
memory.Write_U16(0x8201, 0x000030e6); // Dev console / debug capable
memory.Write_U32(0x00000000, 0x000030f0); // Apploader

// During the boot process, 0x315c is first set to 0xdeadbeef by IOS
// in the boot_ppc syscall. The value is then partly overwritten by SDK titles.
Expand All @@ -457,19 +466,19 @@ bool CBoot::SetupWiiMemory(IOS::HLE::IOSC::ConsoleType console_type)
//
// 0x0113 appears to mean v1.13, which is the latest version.
// It is fine to always use the latest value as apploaders work with all versions.
Memory::Write_U16(0x0113, 0x0000315e);
memory.Write_U16(0x0113, 0x0000315e);

Memory::Write_U8(0x80, 0x0000315c); // OSInit
Memory::Write_U16(0x0000, 0x000030e0); // PADInit
Memory::Write_U32(0x80000000, 0x00003184); // GameID Address
memory.Write_U8(0x80, 0x0000315c); // OSInit
memory.Write_U16(0x0000, 0x000030e0); // PADInit
memory.Write_U32(0x80000000, 0x00003184); // GameID Address

// Fake the VI Init of the IPL
Memory::Write_U32(DiscIO::IsNTSC(SConfig::GetInstance().m_region) ? 0 : 1, 0x000000CC);
memory.Write_U32(DiscIO::IsNTSC(SConfig::GetInstance().m_region) ? 0 : 1, 0x000000CC);

// Clear exception handler. Why? Don't we begin with only zeros?
for (int i = 0x3000; i <= 0x3038; i += 4)
{
Memory::Write_U32(0x00000000, i);
memory.Write_U32(0x00000000, i);
}

return true;
Expand Down Expand Up @@ -513,6 +522,9 @@ bool CBoot::EmulatedBS2_Wii(const DiscIO::VolumeDisc& volume,
state->discstate = 0x01;
});

auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();

// The system menu clears the RTC flags.
// However, the system menu also updates the disc cache when this happens; see
// https://wiibrew.org/wiki/MX23L4005#DI and
Expand All @@ -526,8 +538,8 @@ bool CBoot::EmulatedBS2_Wii(const DiscIO::VolumeDisc& volume,
// When launching the disc game, it copies the partition type and offset to 0x3194
// and 0x3198 respectively.
const DiscIO::Partition data_partition = volume.GetGamePartition();
Memory::Write_U32(0, 0x3194);
Memory::Write_U32(static_cast<u32>(data_partition.offset >> 2), 0x3198);
memory.Write_U32(0, 0x3194);
memory.Write_U32(static_cast<u32>(data_partition.offset >> 2), 0x3198);

const s32 ios_override = Config::Get(Config::MAIN_OVERRIDE_BOOT_IOS);
const u64 ios = ios_override >= 0 ? Titles::IOS(static_cast<u32>(ios_override)) : tmd.GetIOSId();
Expand All @@ -554,9 +566,9 @@ bool CBoot::EmulatedBS2_Wii(const DiscIO::VolumeDisc& volume,
SetupHID(/*is_wii*/ true);
SetupBAT(/*is_wii*/ true);

Memory::Write_U32(0x4c000064, 0x00000300); // Write default DSI Handler: rfi
Memory::Write_U32(0x4c000064, 0x00000800); // Write default FPU Handler: rfi
Memory::Write_U32(0x4c000064, 0x00000C00); // Write default Syscall Handler: rfi
memory.Write_U32(0x4c000064, 0x00000300); // Write default DSI Handler: rfi
memory.Write_U32(0x4c000064, 0x00000800); // Write default FPU Handler: rfi
memory.Write_U32(0x4c000064, 0x00000C00); // Write default Syscall Handler: rfi

PowerPC::ppcState.gpr[1] = 0x816ffff0; // StackPointer

Expand Down
27 changes: 19 additions & 8 deletions Source/Core/Core/Boot/DolReader.cpp
Expand Up @@ -12,6 +12,7 @@
#include "Common/Swap.h"
#include "Core/Boot/AncastTypes.h"
#include "Core/HW/Memmap.h"
#include "Core/System.h"

DolReader::DolReader(std::vector<u8> buffer) : BootExecutableReader(std::move(buffer))
{
Expand Down Expand Up @@ -117,25 +118,32 @@ bool DolReader::LoadIntoMemory(bool only_in_mem1) const
if (m_is_ancast)
return LoadAncastIntoMemory();

auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();

// load all text (code) sections
for (size_t i = 0; i < m_text_sections.size(); ++i)
{
if (!m_text_sections[i].empty() &&
!(only_in_mem1 &&
m_dolheader.textAddress[i] + m_text_sections[i].size() >= Memory::GetRamSizeReal()))
m_dolheader.textAddress[i] + m_text_sections[i].size() >= memory.GetRamSizeReal()))
{
Memory::CopyToEmu(m_dolheader.textAddress[i], m_text_sections[i].data(),
m_text_sections[i].size());
memory.CopyToEmu(m_dolheader.textAddress[i], m_text_sections[i].data(),
m_text_sections[i].size());
}
}

// load all data sections
for (size_t i = 0; i < m_data_sections.size(); ++i)
{
if (!m_data_sections[i].empty() &&
!(only_in_mem1 &&
m_dolheader.dataAddress[i] + m_data_sections[i].size() >= Memory::GetRamSizeReal()))
m_dolheader.dataAddress[i] + m_data_sections[i].size() >= memory.GetRamSizeReal()))
{
Memory::CopyToEmu(m_dolheader.dataAddress[i], m_data_sections[i].data(),
m_data_sections[i].size());
memory.CopyToEmu(m_dolheader.dataAddress[i], m_data_sections[i].data(),
m_data_sections[i].size());
}
}

return true;
}
Expand Down Expand Up @@ -219,11 +227,14 @@ bool DolReader::LoadAncastIntoMemory() const
body_size))
return false;

auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();

// Copy the Ancast header to the emu
Memory::CopyToEmu(section_address, header, sizeof(EspressoAncastHeader));
memory.CopyToEmu(section_address, header, sizeof(EspressoAncastHeader));

// Copy the decrypted body to the emu
Memory::CopyToEmu(section_address + sizeof(EspressoAncastHeader), decrypted.data(), body_size);
memory.CopyToEmu(section_address + sizeof(EspressoAncastHeader), decrypted.data(), body_size);

return true;
}
10 changes: 7 additions & 3 deletions Source/Core/Core/Boot/ElfReader.cpp
Expand Up @@ -14,6 +14,7 @@

#include "Core/HW/Memmap.h"
#include "Core/PowerPC/PPCSymbolDB.h"
#include "Core/System.h"

static void bswap(u32& w)
{
Expand Down Expand Up @@ -135,6 +136,9 @@ bool ElfReader::LoadIntoMemory(bool only_in_mem1) const

INFO_LOG_FMT(BOOT, "{} segments:", header->e_phnum);

auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();

// Copy segments into ram.
for (int i = 0; i < header->e_phnum; i++)
{
Expand All @@ -150,12 +154,12 @@ bool ElfReader::LoadIntoMemory(bool only_in_mem1) const
u32 srcSize = p->p_filesz;
u32 dstSize = p->p_memsz;

if (only_in_mem1 && p->p_vaddr >= Memory::GetRamSizeReal())
if (only_in_mem1 && p->p_vaddr >= memory.GetRamSizeReal())
continue;

Memory::CopyToEmu(writeAddr, src, srcSize);
memory.CopyToEmu(writeAddr, src, srcSize);
if (srcSize < dstSize)
Memory::Memset(writeAddr + srcSize, 0, dstSize - srcSize); // zero out bss
memory.Memset(writeAddr + srcSize, 0, dstSize - srcSize); // zero out bss

INFO_LOG_FMT(BOOT, "Loadable Segment Copied to {:08x}, size {:08x}", writeAddr, p->p_memsz);
}
Expand Down
16 changes: 10 additions & 6 deletions Source/Core/Core/FifoPlayer/FifoDataFile.cpp
Expand Up @@ -13,6 +13,7 @@
#include "Common/MsgHandler.h"
#include "Core/Config/MainSettings.h"
#include "Core/HW/Memmap.h"
#include "Core/System.h"

constexpr u32 FILE_ID = 0x0d01f1f0;
constexpr u32 VERSION_NUMBER = 5;
Expand Down Expand Up @@ -161,8 +162,10 @@ bool FifoDataFile::Save(const std::string& filename)

header.flags = m_Flags;

header.mem1_size = Memory::GetRamSizeReal();
header.mem2_size = Memory::GetExRamSizeReal();
auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();
header.mem1_size = memory.GetRamSizeReal();
header.mem2_size = memory.GetExRamSizeReal();

file.Seek(0, File::SeekOrigin::Begin);
file.WriteBytes(&header, sizeof(FileHeader));
Expand Down Expand Up @@ -267,14 +270,15 @@ std::unique_ptr<FifoDataFile> FifoDataFile::Load(const std::string& filename, bo
// It should be noted, however, that Dolphin *will still crash* from the nullptr being returned
// in a non-flagsOnly context, so if this code becomes necessary, it should be moved above the
// prior conditional.
if (header.mem1_size != Memory::GetRamSizeReal() ||
header.mem2_size != Memory::GetExRamSizeReal())
auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();
if (header.mem1_size != memory.GetRamSizeReal() || header.mem2_size != memory.GetExRamSizeReal())
{
CriticalAlertFmtT("Emulated memory size mismatch!\n"
"Current: MEM1 {0:08X} ({1} MiB), MEM2 {2:08X} ({3} MiB)\n"
"DFF: MEM1 {4:08X} ({5} MiB), MEM2 {6:08X} ({7} MiB)",
Memory::GetRamSizeReal(), Memory::GetRamSizeReal() / 0x100000,
Memory::GetExRamSizeReal(), Memory::GetExRamSizeReal() / 0x100000,
memory.GetRamSizeReal(), memory.GetRamSizeReal() / 0x100000,
memory.GetExRamSizeReal(), memory.GetExRamSizeReal() / 0x100000,
header.mem1_size, header.mem1_size / 0x100000, header.mem2_size,
header.mem2_size / 0x100000);
return nullptr;
Expand Down
6 changes: 4 additions & 2 deletions Source/Core/Core/FifoPlayer/FifoPlayer.cpp
Expand Up @@ -494,12 +494,14 @@ void FifoPlayer::WriteAllMemoryUpdates()

void FifoPlayer::WriteMemory(const MemoryUpdate& memUpdate)
{
auto& system = Core::System::GetInstance();
auto& memory = system.GetMemory();
u8* mem = nullptr;

if (memUpdate.address & 0x10000000)
mem = &Memory::m_pEXRAM[memUpdate.address & Memory::GetExRamMask()];
mem = &memory.GetEXRAM()[memUpdate.address & memory.GetExRamMask()];
else
mem = &Memory::m_pRAM[memUpdate.address & Memory::GetRamMask()];
mem = &memory.GetRAM()[memUpdate.address & memory.GetRamMask()];

std::copy(memUpdate.data.begin(), memUpdate.data.end(), mem);
}
Expand Down

0 comments on commit 839db59

Please sign in to comment.