Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11632 from AdmiralCurtiss/pass-more-system
Pass System to Boot functions, CPUThreadGuard, IEXIDevice.
  • Loading branch information
lioncash committed Mar 8, 2023
2 parents c592c94 + a1c2651 commit 2856723
Show file tree
Hide file tree
Showing 54 changed files with 226 additions and 191 deletions.
12 changes: 5 additions & 7 deletions Source/Core/Core/Boot/Boot.cpp
Expand Up @@ -321,26 +321,24 @@ static const DiscIO::VolumeDisc* SetDisc(std::unique_ptr<DiscIO::VolumeDisc> dis
return pointer;
}

bool CBoot::DVDRead(const DiscIO::VolumeDisc& disc, u64 dvd_offset, u32 output_address, u32 length,
const DiscIO::Partition& partition)
bool CBoot::DVDRead(Core::System& system, const DiscIO::VolumeDisc& disc, u64 dvd_offset,
u32 output_address, u32 length, const DiscIO::Partition& partition)
{
std::vector<u8> buffer(length);
if (!disc.Read(dvd_offset, length, buffer.data(), partition))
return false;

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

bool CBoot::DVDReadDiscID(const DiscIO::VolumeDisc& disc, u32 output_address)
bool CBoot::DVDReadDiscID(Core::System& system, 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;

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

Expand Down Expand Up @@ -550,14 +548,14 @@ bool CBoot::BootUp(Core::System& system, const Core::CPUThreadGuard& guard,
// Because there is no TMD to get the requested system (IOS) version from,
// we default to IOS58, which is the version used by the Homebrew Channel.
SetupWiiMemory(system, IOS::HLE::IOSC::ConsoleType::Retail);
IOS::HLE::GetIOS()->BootIOS(Titles::IOS(58));
IOS::HLE::GetIOS()->BootIOS(system, Titles::IOS(58));
}
else
{
SetupGCMemory(system, guard);
}

if (!executable.reader->LoadIntoMemory())
if (!executable.reader->LoadIntoMemory(system))
{
PanicAlertFmtT("Failed to load the executable to memory.");
return false;
Expand Down
9 changes: 5 additions & 4 deletions Source/Core/Core/Boot/Boot.h
Expand Up @@ -171,9 +171,10 @@ class CBoot
static bool LoadMapFromFilename(const Core::CPUThreadGuard& guard);

private:
static bool DVDRead(const DiscIO::VolumeDisc& disc, u64 dvd_offset, u32 output_address,
u32 length, const DiscIO::Partition& partition);
static bool DVDReadDiscID(const DiscIO::VolumeDisc& disc, u32 output_address);
static bool DVDRead(Core::System& system, const DiscIO::VolumeDisc& disc, u64 dvd_offset,
u32 output_address, u32 length, const DiscIO::Partition& partition);
static bool DVDReadDiscID(Core::System& system, const DiscIO::VolumeDisc& disc,
u32 output_address);
static void RunFunction(Core::System& system, u32 address);

static void UpdateDebugger_MapLoaded();
Expand Down Expand Up @@ -213,7 +214,7 @@ class BootExecutableReader
virtual u32 GetEntryPoint() const = 0;
virtual bool IsValid() const = 0;
virtual bool IsWii() const = 0;
virtual bool LoadIntoMemory(bool only_in_mem1 = false) const = 0;
virtual bool LoadIntoMemory(Core::System& system, bool only_in_mem1 = false) const = 0;
virtual bool LoadSymbols(const Core::CPUThreadGuard& guard) const = 0;

protected:
Expand Down
18 changes: 9 additions & 9 deletions Source/Core/Core/Boot/Boot_BS2Emu.cpp
Expand Up @@ -44,10 +44,10 @@

namespace
{
void PresetTimeBaseTicks(const Core::CPUThreadGuard& guard)
void PresetTimeBaseTicks(Core::System& system, const Core::CPUThreadGuard& guard)
{
const u64 emulated_time =
ExpansionInterface::CEXIIPL::GetEmulatedTime(ExpansionInterface::CEXIIPL::GC_EPOCH);
ExpansionInterface::CEXIIPL::GetEmulatedTime(system, ExpansionInterface::CEXIIPL::GC_EPOCH);

const u64 time_base_ticks = emulated_time * 40500000ULL;

Expand Down Expand Up @@ -148,7 +148,7 @@ bool CBoot::RunApploader(Core::System& system, const Core::CPUThreadGuard& guard
INFO_LOG_FMT(BOOT, "Invalid apploader. Your disc image is probably corrupted.");
return false;
}
DVDRead(volume, offset + 0x20, 0x01200000, *size + *trailer, partition);
DVDRead(system, volume, offset + 0x20, 0x01200000, *size + *trailer, partition);

// TODO - Make Apploader(or just RunFunction()) debuggable!!!

Expand Down Expand Up @@ -195,7 +195,7 @@ bool CBoot::RunApploader(Core::System& system, const Core::CPUThreadGuard& guard

INFO_LOG_FMT(BOOT, "DVDRead: offset: {:08x} memOffset: {:08x} length: {}", dvd_offset,
ram_address, length);
DVDRead(volume, dvd_offset, ram_address, length, partition);
DVDRead(system, volume, dvd_offset, ram_address, length, partition);

DiscIO::Riivolution::ApplyApploaderMemoryPatches(guard, riivolution_patches, ram_address,
length);
Expand Down Expand Up @@ -248,7 +248,7 @@ void CBoot::SetupGCMemory(Core::System& system, const Core::CPUThreadGuard& guar
PowerPC::HostWrite_U32(guard, 0x4c000064, 0x80000800); // Write default FPU Handler: rfi
PowerPC::HostWrite_U32(guard, 0x4c000064, 0x80000C00); // Write default Syscall Handler: rfi

PresetTimeBaseTicks(guard);
PresetTimeBaseTicks(system, guard);

// HIO checks this
// PowerPC::HostWrite_U16(0x8200, 0x000030e6); // Console type
Expand Down Expand Up @@ -284,7 +284,7 @@ bool CBoot::EmulatedBS2_GC(Core::System& system, const Core::CPUThreadGuard& gua
auto& vertex_shader_manager = system.GetVertexShaderManager();
vertex_shader_manager.InvalidateXFRange(XFMEM_POSTMATRICES + 0x3d * 4, XFMEM_POSTMATRICES_END);

DVDReadDiscID(volume, 0x00000000);
DVDReadDiscID(system, volume, 0x00000000);

auto& memory = system.GetMemory();
bool streaming = memory.Read_U8(0x80000008);
Expand Down Expand Up @@ -554,7 +554,7 @@ bool CBoot::EmulatedBS2_Wii(Core::System& system, const Core::CPUThreadGuard& gu
const u64 ios = ios_override >= 0 ? Titles::IOS(static_cast<u32>(ios_override)) : tmd.GetIOSId();

const auto console_type = volume.GetTicket(data_partition).GetConsoleType();
if (!SetupWiiMemory(system, console_type) || !IOS::HLE::GetIOS()->BootIOS(ios))
if (!SetupWiiMemory(system, console_type) || !IOS::HLE::GetIOS()->BootIOS(system, ios))
return false;

auto di =
Expand All @@ -563,13 +563,13 @@ bool CBoot::EmulatedBS2_Wii(Core::System& system, const Core::CPUThreadGuard& gu
di->InitializeIfFirstTime();
di->ChangePartition(data_partition);

DVDReadDiscID(volume, 0x00000000);
DVDReadDiscID(system, volume, 0x00000000);

// This is some kind of consistency check that is compared to the 0x00
// values as the game boots. This location keeps the 4 byte ID for as long
// as the game is running. The 6 byte ID at 0x00 is overwritten sometime
// after this check during booting.
DVDRead(volume, 0, 0x3180, 4, partition);
DVDRead(system, volume, 0, 0x3180, 4, partition);

auto& ppc_state = system.GetPPCState();

Expand Down
8 changes: 3 additions & 5 deletions Source/Core/Core/Boot/DolReader.cpp
Expand Up @@ -110,15 +110,14 @@ bool DolReader::Initialize(const std::vector<u8>& buffer)
return true;
}

bool DolReader::LoadIntoMemory(bool only_in_mem1) const
bool DolReader::LoadIntoMemory(Core::System& system, bool only_in_mem1) const
{
if (!m_is_valid)
return false;

if (m_is_ancast)
return LoadAncastIntoMemory();
return LoadAncastIntoMemory(system);

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

// load all text (code) sections
Expand Down Expand Up @@ -149,7 +148,7 @@ bool DolReader::LoadIntoMemory(bool only_in_mem1) const
}

// On a real console this would be done in the Espresso bootrom
bool DolReader::LoadAncastIntoMemory() const
bool DolReader::LoadAncastIntoMemory(Core::System& system) const
{
// The ancast image will always be in data section 0
const auto& section = m_data_sections[0];
Expand Down Expand Up @@ -227,7 +226,6 @@ bool DolReader::LoadAncastIntoMemory() const
body_size))
return false;

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

// Copy the Ancast header to the emu
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/Boot/DolReader.h
Expand Up @@ -26,7 +26,7 @@ class DolReader final : public BootExecutableReader
bool IsWii() const override { return m_is_wii; }
bool IsAncast() const { return m_is_ancast; };
u32 GetEntryPoint() const override { return m_dolheader.entryPoint; }
bool LoadIntoMemory(bool only_in_mem1 = false) const override;
bool LoadIntoMemory(Core::System& system, bool only_in_mem1 = false) const override;
bool LoadSymbols(const Core::CPUThreadGuard& guard) const override { return false; }

private:
Expand Down Expand Up @@ -63,5 +63,5 @@ class DolReader final : public BootExecutableReader
// Copy sections to internal buffers
bool Initialize(const std::vector<u8>& buffer);

bool LoadAncastIntoMemory() const;
bool LoadAncastIntoMemory(Core::System& system) const;
};
3 changes: 1 addition & 2 deletions Source/Core/Core/Boot/ElfReader.cpp
Expand Up @@ -124,7 +124,7 @@ const char* ElfReader::GetSectionName(int section) const
}

// This is just a simple elf loader, good enough to load elfs generated by devkitPPC
bool ElfReader::LoadIntoMemory(bool only_in_mem1) const
bool ElfReader::LoadIntoMemory(Core::System& system, bool only_in_mem1) const
{
INFO_LOG_FMT(BOOT, "String section: {}", header->e_shstrndx);

Expand All @@ -136,7 +136,6 @@ 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.
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Boot/ElfReader.h
Expand Up @@ -35,7 +35,7 @@ class ElfReader final : public BootExecutableReader
ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); }
u32 GetEntryPoint() const override { return entryPoint; }
u32 GetFlags() const { return (u32)(header->e_flags); }
bool LoadIntoMemory(bool only_in_mem1 = false) const override;
bool LoadIntoMemory(Core::System& system, bool only_in_mem1 = false) const override;
bool LoadSymbols(const Core::CPUThreadGuard& guard) const override;
// TODO: actually check for validity.
bool IsValid() const override { return true; }
Expand Down
15 changes: 8 additions & 7 deletions Source/Core/Core/Core.cpp
Expand Up @@ -170,7 +170,7 @@ void OnFrameEnd()
if (s_memory_watcher)
{
ASSERT(IsCPUThread());
CPUThreadGuard guard;
CPUThreadGuard guard(Core::System::GetInstance());

s_memory_watcher->Step(guard);
}
Expand Down Expand Up @@ -533,7 +533,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi

HW::Init(NetPlay::IsNetPlayRunning() ? &(boot_session_data.GetNetplaySettings()->sram) : nullptr);

Common::ScopeGuard hw_guard{[] {
Common::ScopeGuard hw_guard{[&system] {
// We must set up this flag before executing HW::Shutdown()
s_hardware_initialized = false;
INFO_LOG_FMT(CONSOLE, "{}", StopMessage(false, "Shutting down HW"));
Expand All @@ -550,7 +550,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
PatchEngine::Shutdown();
HLE::Clear();

CPUThreadGuard guard;
CPUThreadGuard guard(system);
PowerPC::debug_interface.Clear(guard);
}};

Expand Down Expand Up @@ -603,7 +603,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi

{
ASSERT(IsCPUThread());
CPUThreadGuard guard;
CPUThreadGuard guard(system);
if (!CBoot::BootUp(system, guard, std::move(boot)))
return;
}
Expand Down Expand Up @@ -1058,16 +1058,17 @@ void UpdateInputGate(bool require_focus, bool require_full_focus)
ControlReference::SetInputGate(focus_passes && full_focus_passes);
}

CPUThreadGuard::CPUThreadGuard() : m_was_cpu_thread(IsCPUThread())
CPUThreadGuard::CPUThreadGuard(Core::System& system)
: m_system(system), m_was_cpu_thread(IsCPUThread())
{
if (!m_was_cpu_thread)
m_was_unpaused = PauseAndLock(Core::System::GetInstance(), true, true);
m_was_unpaused = PauseAndLock(system, true, true);
}

CPUThreadGuard::~CPUThreadGuard()
{
if (!m_was_cpu_thread)
PauseAndLock(Core::System::GetInstance(), false, m_was_unpaused);
PauseAndLock(m_system, false, m_was_unpaused);
}

} // namespace Core
3 changes: 2 additions & 1 deletion Source/Core/Core/Core.h
Expand Up @@ -108,7 +108,7 @@ enum class ConsoleType : u32
class CPUThreadGuard final
{
public:
CPUThreadGuard();
explicit CPUThreadGuard(Core::System& system);
~CPUThreadGuard();

CPUThreadGuard(const CPUThreadGuard&) = delete;
Expand All @@ -117,6 +117,7 @@ class CPUThreadGuard final
CPUThreadGuard& operator=(CPUThreadGuard&&) = delete;

private:
Core::System& m_system;
const bool m_was_cpu_thread;
bool m_was_unpaused = false;
};
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HLE/HLE.cpp
Expand Up @@ -169,7 +169,7 @@ void Execute(const Core::CPUThreadGuard& guard, u32 current_pc, u32 hook_index)
void ExecuteFromJIT(u32 current_pc, u32 hook_index)
{
ASSERT(Core::IsCPUThread());
Core::CPUThreadGuard guard;
Core::CPUThreadGuard guard(Core::System::GetInstance());
Execute(guard, current_pc, hook_index);
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/EXI/EXI.cpp
Expand Up @@ -149,7 +149,7 @@ void Init(const Sram* override_sram)
Memcard::HeaderData header_data;
Memcard::InitializeHeaderData(&header_data, flash_id, size_mbits, shift_jis, rtc_bias,
sram_language, format_time + i);
state.channels[i] = std::make_unique<CEXIChannel>(i, header_data);
state.channels[i] = std::make_unique<CEXIChannel>(system, i, header_data);
}
}

Expand Down
12 changes: 7 additions & 5 deletions Source/Core/Core/HW/EXI/EXI_Channel.cpp
Expand Up @@ -25,16 +25,17 @@ enum
EXI_READWRITE
};

CEXIChannel::CEXIChannel(u32 channel_id, const Memcard::HeaderData& memcard_header_data)
: m_channel_id(channel_id), m_memcard_header_data(memcard_header_data)
CEXIChannel::CEXIChannel(Core::System& system, u32 channel_id,
const Memcard::HeaderData& memcard_header_data)
: m_system(system), m_channel_id(channel_id), m_memcard_header_data(memcard_header_data)
{
if (m_channel_id == 0 || m_channel_id == 1)
m_status.EXTINT = 1;
if (m_channel_id == 1)
m_status.CHIP_SELECT = 1;

for (auto& device : m_devices)
device = EXIDevice_Create(EXIDeviceType::None, m_channel_id, m_memcard_header_data);
device = EXIDevice_Create(system, EXIDeviceType::None, m_channel_id, m_memcard_header_data);
}

CEXIChannel::~CEXIChannel()
Expand Down Expand Up @@ -170,7 +171,8 @@ void CEXIChannel::RemoveDevices()

void CEXIChannel::AddDevice(const EXIDeviceType device_type, const int device_num)
{
AddDevice(EXIDevice_Create(device_type, m_channel_id, m_memcard_header_data), device_num);
AddDevice(EXIDevice_Create(m_system, device_type, m_channel_id, m_memcard_header_data),
device_num);
}

void CEXIChannel::AddDevice(std::unique_ptr<IEXIDevice> device, const int device_num,
Expand Down Expand Up @@ -255,7 +257,7 @@ void CEXIChannel::DoState(PointerWrap& p)
else
{
std::unique_ptr<IEXIDevice> save_device =
EXIDevice_Create(type, m_channel_id, m_memcard_header_data);
EXIDevice_Create(m_system, type, m_channel_id, m_memcard_header_data);
save_device->DoState(p);
AddDevice(std::move(save_device), device_index, false);
}
Expand Down
5 changes: 4 additions & 1 deletion Source/Core/Core/HW/EXI/EXI_Channel.h
Expand Up @@ -25,7 +25,8 @@ enum class EXIDeviceType : int;
class CEXIChannel
{
public:
explicit CEXIChannel(u32 channel_id, const Memcard::HeaderData& memcard_header_data);
explicit CEXIChannel(Core::System& system, u32 channel_id,
const Memcard::HeaderData& memcard_header_data);
~CEXIChannel();

// get device
Expand Down Expand Up @@ -100,6 +101,8 @@ class CEXIChannel
};
};

Core::System& m_system;

// STATE_TO_SAVE
UEXI_STATUS m_status;
u32 m_dma_memory_address = 0;
Expand Down

0 comments on commit 2856723

Please sign in to comment.