From 41ad0490f088f4cd73a3165e55574106e84ce946 Mon Sep 17 00:00:00 2001 From: "Admiral H. Curtiss" Date: Tue, 10 Jan 2023 04:47:34 +0100 Subject: [PATCH] CoreTiming: Avoid ppcState global. --- Source/Core/Core/CoreTiming.cpp | 33 ++++++++++++++++++++------------- Source/Core/Core/CoreTiming.h | 4 ++++ Source/Core/Core/System.cpp | 5 ++++- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Source/Core/Core/CoreTiming.cpp b/Source/Core/Core/CoreTiming.cpp index 1edfa46171..4acdebea70 100644 --- a/Source/Core/Core/CoreTiming.cpp +++ b/Source/Core/Core/CoreTiming.cpp @@ -44,6 +44,10 @@ static void EmptyTimedCallback(Core::System& system, u64 userdata, s64 cyclesLat { } +CoreTimingManager::CoreTimingManager(Core::System& system) : m_system(system) +{ +} + // Changing the CPU speed in Dolphin isn't actually done by changing the physical clock rate, // but by changing the amount of work done in a particular amount of time. This tends to be more // compatible because it stops the games from actually knowing directly that the clock rate has @@ -90,7 +94,7 @@ void CoreTimingManager::Init() m_last_oc_factor = m_config_oc_factor; m_globals.last_OC_factor_inverted = m_config_oc_inv_factor; - PowerPC::ppcState.downcount = CyclesToDowncount(MAX_SLICE_LENGTH); + m_system.GetPPCState().downcount = CyclesToDowncount(MAX_SLICE_LENGTH); m_globals.slice_length = MAX_SLICE_LENGTH; m_globals.global_timer = 0; m_idled_cycles = 0; @@ -195,7 +199,7 @@ u64 CoreTimingManager::GetTicks() const u64 ticks = static_cast(m_globals.global_timer); if (!m_is_global_timer_sane) { - int downcount = DowncountToCycles(PowerPC::ppcState.downcount); + int downcount = DowncountToCycles(m_system.GetPPCState().downcount); ticks += m_globals.slice_length - downcount; } return ticks; @@ -277,13 +281,13 @@ void CoreTimingManager::RemoveAllEvents(EventType* event_type) void CoreTimingManager::ForceExceptionCheck(s64 cycles) { cycles = std::max(0, cycles); - if (DowncountToCycles(PowerPC::ppcState.downcount) > cycles) + auto& ppc_state = m_system.GetPPCState(); + if (DowncountToCycles(ppc_state.downcount) > cycles) { // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int here. // Account for cycles already executed by adjusting the m_globals.slice_length - m_globals.slice_length -= - DowncountToCycles(PowerPC::ppcState.downcount) - static_cast(cycles); - PowerPC::ppcState.downcount = CyclesToDowncount(static_cast(cycles)); + m_globals.slice_length -= DowncountToCycles(ppc_state.downcount) - static_cast(cycles); + ppc_state.downcount = CyclesToDowncount(static_cast(cycles)); } } @@ -299,11 +303,12 @@ void CoreTimingManager::MoveEvents() void CoreTimingManager::Advance() { - auto& system = Core::System::GetInstance(); + auto& system = m_system; + auto& ppc_state = m_system.GetPPCState(); MoveEvents(); - int cyclesExecuted = m_globals.slice_length - DowncountToCycles(PowerPC::ppcState.downcount); + int cyclesExecuted = m_globals.slice_length - DowncountToCycles(ppc_state.downcount); m_globals.global_timer += cyclesExecuted; m_last_oc_factor = m_config_oc_factor; m_globals.last_OC_factor_inverted = m_config_oc_inv_factor; @@ -330,7 +335,7 @@ void CoreTimingManager::Advance() std::min(m_event_queue.front().time - m_globals.global_timer, MAX_SLICE_LENGTH)); } - PowerPC::ppcState.downcount = CyclesToDowncount(m_globals.slice_length); + ppc_state.downcount = CyclesToDowncount(m_globals.slice_length); // Check for any external exceptions. // It's important to do this after processing events otherwise any exceptions will be delayed @@ -438,18 +443,20 @@ void CoreTimingManager::AdjustEventQueueTimes(u32 new_ppc_clock, u32 old_ppc_clo void CoreTimingManager::Idle() { + auto& system = m_system; + auto& ppc_state = m_system.GetPPCState(); + if (m_config_sync_on_skip_idle) { // When the FIFO is processing data we must not advance because in this way // the VI will be desynchronized. So, We are waiting until the FIFO finish and // while we process only the events required by the FIFO. - auto& system = Core::System::GetInstance(); system.GetFifo().FlushGpu(system); } - PowerPC::UpdatePerformanceMonitor(PowerPC::ppcState.downcount, 0, 0, PowerPC::ppcState); - m_idled_cycles += DowncountToCycles(PowerPC::ppcState.downcount); - PowerPC::ppcState.downcount = 0; + PowerPC::UpdatePerformanceMonitor(ppc_state.downcount, 0, 0, ppc_state); + m_idled_cycles += DowncountToCycles(ppc_state.downcount); + ppc_state.downcount = 0; } std::string CoreTimingManager::GetScheduledEventsSummary() const diff --git a/Source/Core/Core/CoreTiming.h b/Source/Core/Core/CoreTiming.h index d028550696..1ddb13f948 100644 --- a/Source/Core/Core/CoreTiming.h +++ b/Source/Core/Core/CoreTiming.h @@ -75,6 +75,8 @@ void GlobalIdle(); class CoreTimingManager { public: + explicit CoreTimingManager(Core::System& system); + // CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is // required to end slice -1 and start slice 0 before the first cycle of code is executed. void Init(); @@ -151,6 +153,8 @@ public: private: Globals m_globals; + Core::System& m_system; + // unordered_map stores each element separately as a linked list node so pointers to elements // remain stable regardless of rehashes/resizing. std::unordered_map m_event_types; diff --git a/Source/Core/Core/System.cpp b/Source/Core/Core/System.cpp index 85bbb94481..e007ab0ffd 100644 --- a/Source/Core/Core/System.cpp +++ b/Source/Core/Core/System.cpp @@ -33,7 +33,10 @@ namespace Core { struct System::Impl { - explicit Impl(System& system) : m_gp_fifo(system), m_ppc_state(PowerPC::ppcState) {} + explicit Impl(System& system) + : m_core_timing(system), m_gp_fifo(system), m_ppc_state(PowerPC::ppcState) + { + } std::unique_ptr m_sound_stream; bool m_sound_stream_running = false;