Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #11741 from AdmiralCurtiss/ppc-class
PowerPC: Refactor to class, move to System.
  • Loading branch information
JosJuice committed Apr 10, 2023
2 parents ebbbdc4 + 2384358 commit 4c80c99
Show file tree
Hide file tree
Showing 50 changed files with 712 additions and 538 deletions.
19 changes: 11 additions & 8 deletions Source/Core/Common/Debug/CodeTrace.cpp
Expand Up @@ -126,16 +126,18 @@ InstructionAttributes CodeTrace::GetInstructionAttributes(const TraceOutput& ins
TraceOutput CodeTrace::SaveCurrentInstruction(const Core::CPUThreadGuard& guard) const
{
auto& system = guard.GetSystem();
auto& ppc_state = system.GetPPCState();
auto& power_pc = system.GetPowerPC();
auto& ppc_state = power_pc.GetPPCState();
auto& debug_interface = power_pc.GetDebugInterface();

// Quickly save instruction and memory target for fast logging.
TraceOutput output;
const std::string instr = PowerPC::debug_interface.Disassemble(&guard, ppc_state.pc);
const std::string instr = debug_interface.Disassemble(&guard, ppc_state.pc);
output.instruction = instr;
output.address = ppc_state.pc;

if (IsInstructionLoadStore(output.instruction))
output.memory_target = PowerPC::debug_interface.GetMemoryAddressFromInstruction(instr);
output.memory_target = debug_interface.GetMemoryAddressFromInstruction(instr);

return output;
}
Expand Down Expand Up @@ -189,16 +191,17 @@ AutoStepResults CodeTrace::AutoStepping(const Core::CPUThreadGuard& guard, bool
else if (stop_on == AutoStop::Changed)
stop_condition = HitType::ACTIVE;

PowerPC::breakpoints.ClearAllTemporary();
auto& power_pc = guard.GetSystem().GetPowerPC();
power_pc.GetBreakPoints().ClearAllTemporary();
using clock = std::chrono::steady_clock;
clock::time_point timeout = clock::now() + std::chrono::seconds(4);

PowerPC::CoreMode old_mode = PowerPC::GetMode();
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
PowerPC::CoreMode old_mode = power_pc.GetMode();
power_pc.SetMode(PowerPC::CoreMode::Interpreter);

do
{
PowerPC::SingleStep();
power_pc.SingleStep();

pc_instr = SaveCurrentInstruction(guard);
hit = TraceLogic(pc_instr);
Expand All @@ -210,7 +213,7 @@ AutoStepResults CodeTrace::AutoStepping(const Core::CPUThreadGuard& guard, bool
if (clock::now() >= timeout)
results.timed_out = true;

PowerPC::SetMode(old_mode);
power_pc.SetMode(old_mode);
m_recording = false;

results.reg_tracked = m_reg_autotrack;
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/Core/Boot/Boot_BS2Emu.cpp
Expand Up @@ -57,13 +57,14 @@ void PresetTimeBaseTicks(Core::System& system, const Core::CPUThreadGuard& guard

void CBoot::RunFunction(Core::System& system, u32 address)
{
auto& ppc_state = system.GetPPCState();
auto& power_pc = system.GetPowerPC();
auto& ppc_state = power_pc.GetPPCState();

ppc_state.pc = address;
LR(ppc_state) = 0x00;

while (ppc_state.pc != 0x00)
PowerPC::SingleStep();
power_pc.SingleStep();
}

void CBoot::SetupMSR(PowerPC::PowerPCState& ppc_state)
Expand Down
18 changes: 9 additions & 9 deletions Source/Core/Core/Core.cpp
Expand Up @@ -435,23 +435,23 @@ static void FifoPlayerThread(const std::optional<std::string>& savestate_path,
{
DeclareAsCPUThread();

if (Core::System::GetInstance().IsDualCoreMode())
auto& system = Core::System::GetInstance();
if (system.IsDualCoreMode())
Common::SetCurrentThreadName("FIFO player thread");
else
Common::SetCurrentThreadName("FIFO-GPU thread");

// Enter CPU run loop. When we leave it - we are done.
if (auto cpu_core = FifoPlayer::GetInstance().GetCPUCore())
{
PowerPC::InjectExternalCPUCore(cpu_core.get());
system.GetPowerPC().InjectExternalCPUCore(cpu_core.get());
s_is_started = true;

CPUSetInitialExecutionState();
auto& system = Core::System::GetInstance();
system.GetCPU().Run();

s_is_started = false;
PowerPC::InjectExternalCPUCore(nullptr);
system.GetPowerPC().InjectExternalCPUCore(nullptr);
FifoPlayer::GetInstance().Close();
}
else
Expand Down Expand Up @@ -552,7 +552,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
HLE::Clear();

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

VideoBackendBase::PopulateBackendInfo();
Expand Down Expand Up @@ -590,7 +590,7 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
system.GetCPU().Break();

// Load GCM/DOL/ELF whatever ... we boot with the interpreter core
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
system.GetPowerPC().SetMode(PowerPC::CoreMode::Interpreter);

// Determine the CPU thread function
void (*cpuThreadFunc)(const std::optional<std::string>& savestate_path, bool delete_savestate);
Expand Down Expand Up @@ -628,11 +628,11 @@ static void EmuThread(std::unique_ptr<BootParameters> boot, WindowSystemInfo wsi
// Setup our core
if (Config::Get(Config::MAIN_CPU_CORE) != PowerPC::CPUCore::Interpreter)
{
PowerPC::SetMode(PowerPC::CoreMode::JIT);
system.GetPowerPC().SetMode(PowerPC::CoreMode::JIT);
}
else
{
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
system.GetPowerPC().SetMode(PowerPC::CoreMode::Interpreter);
}

UpdateTitle();
Expand Down Expand Up @@ -901,7 +901,7 @@ void UpdateTitle()
{
// Settings are shown the same for both extended and summary info
const std::string SSettings = fmt::format(
"{} {} | {} | {}", PowerPC::GetCPUName(),
"{} {} | {} | {}", Core::System::GetInstance().GetPowerPC().GetCPUName(),
Core::System::GetInstance().IsDualCoreMode() ? "DC" : "SC", g_video_backend->GetDisplayName(),
Config::Get(Config::MAIN_DSP_HLE) ? "HLE" : "LLE");

Expand Down
8 changes: 4 additions & 4 deletions Source/Core/Core/CoreTiming.cpp
Expand Up @@ -303,8 +303,8 @@ void CoreTimingManager::MoveEvents()

void CoreTimingManager::Advance()
{
auto& system = m_system;
auto& ppc_state = m_system.GetPPCState();
auto& power_pc = m_system.GetPowerPC();
auto& ppc_state = power_pc.GetPPCState();

MoveEvents();

Expand All @@ -323,7 +323,7 @@ void CoreTimingManager::Advance()
m_event_queue.pop_back();

Throttle(evt.time);
evt.type->callback(system, evt.userdata, m_globals.global_timer - evt.time);
evt.type->callback(m_system, evt.userdata, m_globals.global_timer - evt.time);
}

m_is_global_timer_sane = false;
Expand All @@ -341,7 +341,7 @@ void CoreTimingManager::Advance()
// It's important to do this after processing events otherwise any exceptions will be delayed
// until the next slice:
// Pokemon Box refuses to boot if the first exception from the audio DMA is received late
PowerPC::CheckExternalExceptions();
power_pc.CheckExternalExceptions();
}

void CoreTimingManager::Throttle(const s64 target_cycle)
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/Debugger/Debugger_SymbolMap.cpp
Expand Up @@ -32,7 +32,7 @@ void AddAutoBreakpoints()
{
Common::Symbol* symbol = g_symbolDB.GetSymbolFromName(bp);
if (symbol)
PowerPC::breakpoints.Add(symbol->address, false);
Core::System::GetInstance().GetPowerPC().GetBreakPoints().Add(symbol->address, false);
}
#endif
#endif
Expand Down
28 changes: 15 additions & 13 deletions Source/Core/Core/Debugger/PPCDebugInterface.cpp
Expand Up @@ -36,6 +36,7 @@ void ApplyMemoryPatch(const Core::CPUThreadGuard& guard, Common::Debug::MemoryPa
if (!PowerPC::MMU::HostIsRAMAddress(guard, address))
return;

auto& power_pc = guard.GetSystem().GetPowerPC();
for (u32 offset = 0; offset < size; ++offset)
{
if (store_existing_value)
Expand All @@ -50,11 +51,11 @@ void ApplyMemoryPatch(const Core::CPUThreadGuard& guard, Common::Debug::MemoryPa
}

if (((address + offset) % 4) == 3)
PowerPC::ScheduleInvalidateCacheThreadSafe(Common::AlignDown(address + offset, 4));
power_pc.ScheduleInvalidateCacheThreadSafe(Common::AlignDown(address + offset, 4));
}
if (((address + size) % 4) != 0)
{
PowerPC::ScheduleInvalidateCacheThreadSafe(
power_pc.ScheduleInvalidateCacheThreadSafe(
Common::AlignDown(address + static_cast<u32>(size), 4));
}
}
Expand Down Expand Up @@ -347,40 +348,41 @@ bool PPCDebugInterface::IsAlive() const

bool PPCDebugInterface::IsBreakpoint(u32 address) const
{
return PowerPC::breakpoints.IsAddressBreakPoint(address);
return m_system.GetPowerPC().GetBreakPoints().IsAddressBreakPoint(address);
}

void PPCDebugInterface::SetBreakpoint(u32 address)
{
PowerPC::breakpoints.Add(address);
m_system.GetPowerPC().GetBreakPoints().Add(address);
}

void PPCDebugInterface::ClearBreakpoint(u32 address)
{
PowerPC::breakpoints.Remove(address);
m_system.GetPowerPC().GetBreakPoints().Remove(address);
}

void PPCDebugInterface::ClearAllBreakpoints()
{
PowerPC::breakpoints.Clear();
m_system.GetPowerPC().GetBreakPoints().Clear();
}

void PPCDebugInterface::ToggleBreakpoint(u32 address)
{
if (PowerPC::breakpoints.IsAddressBreakPoint(address))
PowerPC::breakpoints.Remove(address);
auto& breakpoints = m_system.GetPowerPC().GetBreakPoints();
if (breakpoints.IsAddressBreakPoint(address))
breakpoints.Remove(address);
else
PowerPC::breakpoints.Add(address);
breakpoints.Add(address);
}

void PPCDebugInterface::ClearAllMemChecks()
{
PowerPC::memchecks.Clear();
m_system.GetPowerPC().GetMemChecks().Clear();
}

bool PPCDebugInterface::IsMemCheck(u32 address, size_t size) const
{
return PowerPC::memchecks.GetMemCheck(address, size) != nullptr;
return m_system.GetPowerPC().GetMemChecks().GetMemCheck(address, size) != nullptr;
}

void PPCDebugInterface::ToggleMemCheck(u32 address, bool read, bool write, bool log)
Expand All @@ -397,11 +399,11 @@ void PPCDebugInterface::ToggleMemCheck(u32 address, bool read, bool write, bool
MemCheck.log_on_hit = log;
MemCheck.break_on_hit = true;

PowerPC::memchecks.Add(std::move(MemCheck));
m_system.GetPowerPC().GetMemChecks().Add(std::move(MemCheck));
}
else
{
PowerPC::memchecks.Remove(address);
m_system.GetPowerPC().GetMemChecks().Remove(address);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HLE/HLE.cpp
Expand Up @@ -203,7 +203,7 @@ HookFlag GetHookFlagsByIndex(u32 index)
bool IsEnabled(HookFlag flag)
{
return flag != HLE::HookFlag::Debug || Config::Get(Config::MAIN_ENABLE_DEBUGGING) ||
PowerPC::GetMode() == PowerPC::CoreMode::Interpreter;
Core::System::GetInstance().GetPowerPC().GetMode() == PowerPC::CoreMode::Interpreter;
}

u32 UnPatch(Core::System& system, std::string_view patch_name)
Expand Down
30 changes: 16 additions & 14 deletions Source/Core/Core/HW/CPU.cpp
Expand Up @@ -19,19 +19,21 @@

namespace CPU
{
CPUManager::CPUManager() = default;
CPUManager::CPUManager(Core::System& system) : m_system(system)
{
}
CPUManager::~CPUManager() = default;

void CPUManager::Init(PowerPC::CPUCore cpu_core)
{
PowerPC::Init(cpu_core);
m_system.GetPowerPC().Init(cpu_core);
m_state = State::Stepping;
}

void CPUManager::Shutdown()
{
Stop();
PowerPC::Shutdown();
m_system.GetPowerPC().Shutdown();
}

// Requires holding m_state_change_lock
Expand Down Expand Up @@ -62,11 +64,11 @@ void CPUManager::ExecutePendingJobs(std::unique_lock<std::mutex>& state_lock)

void CPUManager::Run()
{
auto& system = Core::System::GetInstance();
auto& power_pc = m_system.GetPowerPC();

// Updating the host CPU's rounding mode must be done on the CPU thread.
// We can't rely on PowerPC::Init doing it, since it's called from EmuThread.
PowerPC::RoundingModeUpdated();
PowerPC::RoundingModeUpdated(power_pc.GetPPCState());

std::unique_lock state_lock(m_state_change_lock);
while (m_state != State::PowerDown)
Expand All @@ -85,22 +87,22 @@ void CPUManager::Run()
// SingleStep so that the "continue", "step over" and "step out" debugger functions
// work when the PC is at a breakpoint at the beginning of the block
// If watchpoints are enabled, any instruction could be a breakpoint.
if (PowerPC::GetMode() != PowerPC::CoreMode::Interpreter)
if (power_pc.GetMode() != PowerPC::CoreMode::Interpreter)
{
if (PowerPC::breakpoints.IsAddressBreakPoint(system.GetPPCState().pc) ||
PowerPC::memchecks.HasAny())
if (power_pc.GetBreakPoints().IsAddressBreakPoint(power_pc.GetPPCState().pc) ||
power_pc.GetMemChecks().HasAny())
{
m_state = State::Stepping;
PowerPC::CoreMode old_mode = PowerPC::GetMode();
PowerPC::SetMode(PowerPC::CoreMode::Interpreter);
PowerPC::SingleStep();
PowerPC::SetMode(old_mode);
PowerPC::CoreMode old_mode = power_pc.GetMode();
power_pc.SetMode(PowerPC::CoreMode::Interpreter);
power_pc.SingleStep();
power_pc.SetMode(old_mode);
m_state = State::Running;
}
}

// Enter a fast runloop
PowerPC::RunLoop();
power_pc.RunLoop();

state_lock.lock();
m_state_cpu_thread_active = false;
Expand Down Expand Up @@ -147,7 +149,7 @@ void CPUManager::Run()
m_state_cpu_thread_active = true;
state_lock.unlock();

PowerPC::SingleStep();
power_pc.SingleStep();

state_lock.lock();
m_state_cpu_thread_active = false;
Expand Down
9 changes: 7 additions & 2 deletions Source/Core/Core/HW/CPU.h
Expand Up @@ -12,7 +12,10 @@ namespace Common
{
class Event;
}

namespace Core
{
class System;
}
namespace PowerPC
{
enum class CPUCore;
Expand All @@ -30,7 +33,7 @@ enum class State
class CPUManager
{
public:
CPUManager();
explicit CPUManager(Core::System& system);
CPUManager(const CPUManager& other) = delete;
CPUManager(CPUManager&& other) = delete;
CPUManager& operator=(const CPUManager& other) = delete;
Expand Down Expand Up @@ -130,5 +133,7 @@ class CPUManager
bool m_state_cpu_step_instruction = false;
Common::Event* m_state_cpu_step_instruction_sync = nullptr;
std::queue<std::function<void()>> m_pending_jobs;

Core::System& m_system;
};
} // namespace CPU
5 changes: 3 additions & 2 deletions Source/Core/Core/HW/EXI/EXI_DeviceEthernet.cpp
Expand Up @@ -448,7 +448,7 @@ void CEXIETHERNET::SendFromDirectFIFO()
const u8* frame = tx_fifo.get();
const u16 size = Common::BitCastPtr<u16>(&mBbaMem[BBA_TXFIFOCNT]);
if (m_network_interface->SendFrame(frame, size))
PowerPC::debug_interface.NetworkLogger()->LogBBA(frame, size);
m_system.GetPowerPC().GetDebugInterface().NetworkLogger()->LogBBA(frame, size);
}

void CEXIETHERNET::SendFromPacketBuffer()
Expand Down Expand Up @@ -561,7 +561,8 @@ bool CEXIETHERNET::RecvHandlePacket()
INFO_LOG_FMT(SP1, "{:x} {:x} {:x} {:x}", page_ptr(BBA_BP), page_ptr(BBA_RRP), page_ptr(BBA_RWP),
page_ptr(BBA_RHBP));
#endif
PowerPC::debug_interface.NetworkLogger()->LogBBA(mRecvBuffer.get(), mRecvBufferLength);
m_system.GetPowerPC().GetDebugInterface().NetworkLogger()->LogBBA(mRecvBuffer.get(),
mRecvBufferLength);
write_ptr = &mBbaMem[page_ptr(BBA_RWP) << 8];

descriptor = (Descriptor*)write_ptr;
Expand Down

0 comments on commit 4c80c99

Please sign in to comment.