Skip to content

Commit

Permalink
Merge pull request #12481 from AdmiralCurtiss/globals-systemtimers
Browse files Browse the repository at this point in the history
Core/SystemTimers: Refactor to class, move to System.
  • Loading branch information
lioncash committed Jan 5, 2024
2 parents 9c91b5e + 07c035e commit bfb417f
Show file tree
Hide file tree
Showing 35 changed files with 258 additions and 185 deletions.
8 changes: 5 additions & 3 deletions Source/Core/Core/DSP/Interpreter/DSPInterpreter.cpp
Expand Up @@ -257,7 +257,8 @@ void Interpreter::WriteControlRegister(u16 val)
val &= ~CR_INIT;
val |= CR_INIT_CODE;
// Number obtained from real hardware on a Wii, but it's not perfectly consistent
state.control_reg_init_code_clear_time = SystemTimers::GetFakeTimeBase() + 130;
state.control_reg_init_code_clear_time =
Core::System::GetInstance().GetSystemTimers().GetFakeTimeBase() + 130;
}

// update cr
Expand All @@ -269,10 +270,11 @@ u16 Interpreter::ReadControlRegister()
auto& state = m_dsp_core.DSPState();
if ((state.control_reg & CR_INIT_CODE) != 0)
{
if (SystemTimers::GetFakeTimeBase() >= state.control_reg_init_code_clear_time)
auto& system = Core::System::GetInstance();
if (system.GetSystemTimers().GetFakeTimeBase() >= state.control_reg_init_code_clear_time)
state.control_reg &= ~CR_INIT_CODE;
else
Core::System::GetInstance().GetCoreTiming().ForceExceptionCheck(50); // Keep checking
system.GetCoreTiming().ForceExceptionCheck(50); // Keep checking
}
return state.control_reg;
}
Expand Down
5 changes: 3 additions & 2 deletions Source/Core/Core/FifoPlayer/FifoPlayer.cpp
Expand Up @@ -410,8 +410,9 @@ FifoPlayer& FifoPlayer::GetInstance()
void FifoPlayer::WriteFrame(const FifoFrameInfo& frame, const AnalyzedFrameInfo& info)
{
// Core timing information
auto& vi = Core::System::GetInstance().GetVideoInterface();
m_CyclesPerFrame = static_cast<u64>(SystemTimers::GetTicksPerSecond()) *
auto& system = Core::System::GetInstance();
auto& vi = system.GetVideoInterface();
m_CyclesPerFrame = static_cast<u64>(system.GetSystemTimers().GetTicksPerSecond()) *
vi.GetTargetRefreshRateDenominator() / vi.GetTargetRefreshRateNumerator();
m_ElapsedCycles = 0;
m_FrameFifoSize = static_cast<u32>(frame.fifoData.size());
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/AudioInterface.cpp
Expand Up @@ -184,7 +184,7 @@ void AudioInterfaceManager::SetAISSampleRate(SampleRate sample_rate)
m_ais_sample_rate_divisor = Get48KHzSampleRateDivisor();
}

m_cpu_cycles_per_sample = static_cast<u64>(SystemTimers::GetTicksPerSecond()) *
m_cpu_cycles_per_sample = static_cast<u64>(m_system.GetSystemTimers().GetTicksPerSecond()) *
m_ais_sample_rate_divisor / Mixer::FIXED_SAMPLE_RATE_DIVIDEND;
SoundStream* sound_stream = m_system.GetSoundStream();
sound_stream->GetMixer()->SetStreamInputSampleRateDivisor(m_ais_sample_rate_divisor);
Expand Down
10 changes: 6 additions & 4 deletions Source/Core/Core/HW/DSPHLE/DSPHLE.cpp
Expand Up @@ -55,7 +55,7 @@ u32 DSPHLE::DSP_UpdateRate()
{
// AX HLE uses 3ms (Wii) or 5ms (GC) timing period
// But to be sure, just update the HLE every ms.
return SystemTimers::GetTicksPerSecond() / 1000;
return Core::System::GetInstance().GetSystemTimers().GetTicksPerSecond() / 1000;
}

void DSPHLE::SendMailToDSP(u32 mail)
Expand Down Expand Up @@ -221,7 +221,8 @@ u16 DSPHLE::DSP_WriteControlRegister(u16 value)
SetUCode(UCODE_INIT_AUDIO_SYSTEM);
temp.DSPInitCode = 1;
// Number obtained from real hardware on a Wii, but it's not perfectly consistent
m_control_reg_init_code_clear_time = SystemTimers::GetFakeTimeBase() + 130;
m_control_reg_init_code_clear_time =
Core::System::GetInstance().GetSystemTimers().GetFakeTimeBase() + 130;
}

m_dsp_control.Hex = temp.Hex;
Expand All @@ -232,10 +233,11 @@ u16 DSPHLE::DSP_ReadControlRegister()
{
if (m_dsp_control.DSPInitCode != 0)
{
if (SystemTimers::GetFakeTimeBase() >= m_control_reg_init_code_clear_time)
auto& system = Core::System::GetInstance();
if (system.GetSystemTimers().GetFakeTimeBase() >= m_control_reg_init_code_clear_time)
m_dsp_control.DSPInitCode = 0;
else
Core::System::GetInstance().GetCoreTiming().ForceExceptionCheck(50); // Keep checking
system.GetCoreTiming().ForceExceptionCheck(50); // Keep checking
}
return m_dsp_control.Hex;
}
Expand Down
19 changes: 10 additions & 9 deletions Source/Core/Core/HW/DVD/DVDInterface.cpp
Expand Up @@ -234,7 +234,7 @@ void DVDInterface::DTKStreamingCallback(DIInterruptType interrupt_type,
}

// Read the next chunk of audio data asynchronously.
s64 ticks_to_dtk = SystemTimers::GetTicksPerSecond() * s64(m_pending_blocks) *
s64 ticks_to_dtk = m_system.GetSystemTimers().GetTicksPerSecond() * s64(m_pending_blocks) *
StreamADPCM::SAMPLES_PER_BLOCK * sample_rate_divisor /
Mixer::FIXED_SAMPLE_RATE_DIVIDEND;
ticks_to_dtk -= cycles_late;
Expand Down Expand Up @@ -474,7 +474,8 @@ void DVDInterface::ChangeDisc(const std::string& new_path)
EjectDisc(EjectCause::User);

m_disc_path_to_insert = new_path;
m_system.GetCoreTiming().ScheduleEvent(SystemTimers::GetTicksPerSecond(), m_insert_disc);
m_system.GetCoreTiming().ScheduleEvent(m_system.GetSystemTimers().GetTicksPerSecond(),
m_insert_disc);
Movie::SignalDiscChange(new_path);

for (size_t i = 0; i < m_auto_disc_change_paths.size(); ++i)
Expand Down Expand Up @@ -1090,7 +1091,7 @@ void DVDInterface::ExecuteCommand(ReplyType reply_type)
m_system.GetDVDThread().IsInsertedDiscRunning() && !m_auto_disc_change_paths.empty())
{
m_system.GetCoreTiming().ScheduleEvent(
force_eject ? 0 : SystemTimers::GetTicksPerSecond() / 2, m_auto_change_disc);
force_eject ? 0 : m_system.GetSystemTimers().GetTicksPerSecond() / 2, m_auto_change_disc);
OSD::AddMessage("Changing discs automatically...", OSD::Duration::NORMAL);
}
else if (force_eject)
Expand Down Expand Up @@ -1181,7 +1182,7 @@ void DVDInterface::ExecuteCommand(ReplyType reply_type)
{
// TODO: Needs testing to determine if MINIMUM_COMMAND_LATENCY_US is accurate for this
m_system.GetCoreTiming().ScheduleEvent(
MINIMUM_COMMAND_LATENCY_US * (SystemTimers::GetTicksPerSecond() / 1000000),
MINIMUM_COMMAND_LATENCY_US * (m_system.GetSystemTimers().GetTicksPerSecond() / 1000000),
m_finish_executing_command, PackFinishExecutingCommandUserdata(reply_type, interrupt_type));
}
}
Expand All @@ -1202,7 +1203,7 @@ void DVDInterface::PerformDecryptingRead(u32 position, u32 length, u32 output_ad
{
// TODO: Needs testing to determine if MINIMUM_COMMAND_LATENCY_US is accurate for this
m_system.GetCoreTiming().ScheduleEvent(
MINIMUM_COMMAND_LATENCY_US * (SystemTimers::GetTicksPerSecond() / 1000000),
MINIMUM_COMMAND_LATENCY_US * (m_system.GetSystemTimers().GetTicksPerSecond() / 1000000),
m_finish_executing_command, PackFinishExecutingCommandUserdata(reply_type, interrupt_type));
}
}
Expand All @@ -1219,7 +1220,7 @@ void DVDInterface::ForceOutOfBoundsRead(ReplyType reply_type)
// TODO: Needs testing to determine if MINIMUM_COMMAND_LATENCY_US is accurate for this
const DIInterruptType interrupt_type = DIInterruptType::DEINT;
m_system.GetCoreTiming().ScheduleEvent(
MINIMUM_COMMAND_LATENCY_US * (SystemTimers::GetTicksPerSecond() / 1000000),
MINIMUM_COMMAND_LATENCY_US * (m_system.GetSystemTimers().GetTicksPerSecond() / 1000000),
m_finish_executing_command, PackFinishExecutingCommandUserdata(reply_type, interrupt_type));
}

Expand Down Expand Up @@ -1321,7 +1322,7 @@ void DVDInterface::ScheduleReads(u64 offset, u32 length, const DiscIO::Partition

auto& core_timing = m_system.GetCoreTiming();
const u64 current_time = core_timing.GetTicks();
const u32 ticks_per_second = SystemTimers::GetTicksPerSecond();
const u32 ticks_per_second = m_system.GetSystemTimers().GetTicksPerSecond();
auto& dvd_thread = m_system.GetDVDThread();
const bool wii_disc = dvd_thread.GetDiscType() == DiscIO::Platform::WiiDisc;

Expand Down Expand Up @@ -1400,7 +1401,7 @@ void DVDInterface::ScheduleReads(u64 offset, u32 length, const DiscIO::Partition
length, output_address);

s64 ticks_until_completion =
READ_COMMAND_LATENCY_US * (SystemTimers::GetTicksPerSecond() / 1000000);
READ_COMMAND_LATENCY_US * (m_system.GetSystemTimers().GetTicksPerSecond() / 1000000);

u32 buffered_blocks = 0;
u32 unbuffered_blocks = 0;
Expand Down Expand Up @@ -1528,7 +1529,7 @@ void DVDInterface::ScheduleReads(u64 offset, u32 length, const DiscIO::Partition
"Schedule reads: ECC blocks unbuffered={}, buffered={}, "
"ticks={}, time={} us",
unbuffered_blocks, buffered_blocks, ticks_until_completion,
ticks_until_completion * 1000000 / SystemTimers::GetTicksPerSecond());
ticks_until_completion * 1000000 / m_system.GetSystemTimers().GetTicksPerSecond());
}

} // namespace DVD
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/DVD/DVDThread.cpp
Expand Up @@ -301,7 +301,7 @@ void DVDThread::FinishRead(u64 id, s64 cycles_late)
request.realtime_done_us - request.realtime_started_us,
Common::Timer::NowUs() - request.realtime_started_us,
(m_system.GetCoreTiming().GetTicks() - request.time_started_ticks) /
(SystemTimers::GetTicksPerSecond() / 1000000));
(m_system.GetSystemTimers().GetTicksPerSecond() / 1000000));

auto& dvd_interface = m_system.GetDVDInterface();
DVD::DIInterruptType interrupt;
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/HW/EXI/EXI.cpp
Expand Up @@ -208,9 +208,9 @@ void ExpansionInterfaceManager::ChangeDevice(u8 channel, u8 device_num, EXIDevic
core_timing.ScheduleEvent(0, m_event_type_change_device,
((u64)channel << 32) | ((u64)EXIDeviceType::None << 16) | device_num,
from_thread);
core_timing.ScheduleEvent(SystemTimers::GetTicksPerSecond(), m_event_type_change_device,
((u64)channel << 32) | ((u64)device_type << 16) | device_num,
from_thread);
core_timing.ScheduleEvent(
m_system.GetSystemTimers().GetTicksPerSecond(), m_event_type_change_device,
((u64)channel << 32) | ((u64)device_type << 16) | device_num, from_thread);
}

CEXIChannel* ExpansionInterfaceManager::GetChannel(u32 index)
Expand Down
7 changes: 4 additions & 3 deletions Source/Core/Core/HW/EXI/EXI_DeviceIPL.cpp
Expand Up @@ -411,19 +411,20 @@ u32 CEXIIPL::GetEmulatedTime(Core::System& system, u32 epoch)
ltime = Movie::GetRecordingStartTime();

// let's keep time moving forward, regardless of what it starts at
ltime += system.GetCoreTiming().GetTicks() / SystemTimers::GetTicksPerSecond();
ltime += system.GetCoreTiming().GetTicks() / system.GetSystemTimers().GetTicksPerSecond();
}
else if (NetPlay::IsNetPlayRunning())
{
ltime = NetPlay_GetEmulatedTime();

// let's keep time moving forward, regardless of what it starts at
ltime += system.GetCoreTiming().GetTicks() / SystemTimers::GetTicksPerSecond();
ltime += system.GetCoreTiming().GetTicks() / system.GetSystemTimers().GetTicksPerSecond();
}
else
{
ASSERT(!Core::WantsDeterminism());
ltime = Common::Timer::GetLocalTimeSinceJan1970() - SystemTimers::GetLocalTimeRTCOffset();
ltime = Common::Timer::GetLocalTimeSinceJan1970() -
system.GetSystemTimers().GetLocalTimeRTCOffset();
}

return static_cast<u32>(ltime) - epoch;
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/HW/EXI/EXI_DeviceMemoryCard.cpp
Expand Up @@ -532,7 +532,7 @@ void CEXIMemoryCard::DMARead(u32 addr, u32 size)

// Schedule transfer complete later based on read speed
m_system.GetCoreTiming().ScheduleEvent(
size * (SystemTimers::GetTicksPerSecond() / MC_TRANSFER_RATE_READ),
size * (m_system.GetSystemTimers().GetTicksPerSecond() / MC_TRANSFER_RATE_READ),
s_et_transfer_complete[m_card_slot], static_cast<u64>(m_card_slot));
}

Expand All @@ -550,7 +550,7 @@ void CEXIMemoryCard::DMAWrite(u32 addr, u32 size)

// Schedule transfer complete later based on write speed
m_system.GetCoreTiming().ScheduleEvent(
size * (SystemTimers::GetTicksPerSecond() / MC_TRANSFER_RATE_WRITE),
size * (m_system.GetSystemTimers().GetTicksPerSecond() / MC_TRANSFER_RATE_WRITE),
s_et_transfer_complete[m_card_slot], static_cast<u64>(m_card_slot));
}
} // namespace ExpansionInterface
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/EXI/EXI_DeviceMic.cpp
Expand Up @@ -264,7 +264,7 @@ void CEXIMic::SetCS(int cs)

void CEXIMic::UpdateNextInterruptTicks()
{
int diff = (SystemTimers::GetTicksPerSecond() / sample_rate) * buff_size_samples;
int diff = (m_system.GetSystemTimers().GetTicksPerSecond() / sample_rate) * buff_size_samples;
next_int_ticks = m_system.GetCoreTiming().GetTicks() + diff;
m_system.GetExpansionInterface().ScheduleUpdateInterrupts(CoreTiming::FromThread::CPU, diff);
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/GBACore.cpp
Expand Up @@ -568,7 +568,7 @@ void Core::RunUntil(u64 gc_ticks)
if (static_cast<s64>(gc_ticks - m_last_gc_ticks) <= 0)
return;

const u64 gc_frequency = SystemTimers::GetTicksPerSecond();
const u64 gc_frequency = ::Core::System::GetInstance().GetSystemTimers().GetTicksPerSecond();
const u32 core_frequency = GetCoreFrequency(m_core);

mTimingSchedule(m_core->timing, &m_event,
Expand Down
6 changes: 3 additions & 3 deletions Source/Core/Core/HW/HW.cpp
Expand Up @@ -34,7 +34,7 @@ namespace HW
void Init(Core::System& system, const Sram* override_sram)
{
system.GetCoreTiming().Init();
SystemTimers::PreInit();
system.GetSystemTimers().PreInit();

State::Init();

Expand All @@ -52,7 +52,7 @@ void Init(Core::System& system, const Sram* override_sram)
system.GetDVDInterface().Init();
system.GetGPFifo().Init();
system.GetCPU().Init(Config::Get(Config::MAIN_CPU_CORE));
SystemTimers::Init();
system.GetSystemTimers().Init();

if (SConfig::GetInstance().bWii)
{
Expand All @@ -67,7 +67,7 @@ void Shutdown(Core::System& system)
IOS::HLE::Shutdown(); // Depends on Memory
IOS::Shutdown();

SystemTimers::Shutdown();
system.GetSystemTimers().Shutdown();
system.GetCPU().Shutdown();
system.GetDVDInterface().Shutdown();
system.GetDSP().Shutdown();
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/HW/ProcessorInterface.cpp
Expand Up @@ -262,8 +262,8 @@ void ProcessorInterfaceManager::ResetButton_Tap()
core_timing.ScheduleEvent(0, m_event_type_toggle_reset_button, true, CoreTiming::FromThread::ANY);
core_timing.ScheduleEvent(0, m_event_type_ios_notify_reset_button, 0,
CoreTiming::FromThread::ANY);
core_timing.ScheduleEvent(SystemTimers::GetTicksPerSecond() / 2, m_event_type_toggle_reset_button,
false, CoreTiming::FromThread::ANY);
core_timing.ScheduleEvent(m_system.GetSystemTimers().GetTicksPerSecond() / 2,
m_event_type_toggle_reset_button, false, CoreTiming::FromThread::ANY);
}

void ProcessorInterfaceManager::PowerButton_Tap()
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/SI/SI.cpp
Expand Up @@ -525,7 +525,7 @@ void SerialInterfaceManager::ChangeDeviceDeterministic(SIDevices device, int cha

// Prevent additional device changes on this channel for one second.
m_channel[channel].has_recent_device_change = true;
m_system.GetCoreTiming().ScheduleEvent(SystemTimers::GetTicksPerSecond(),
m_system.GetCoreTiming().ScheduleEvent(m_system.GetSystemTimers().GetTicksPerSecond(),
m_event_type_change_device, channel);
}

Expand Down
9 changes: 5 additions & 4 deletions Source/Core/Core/HW/SI/SI_Device.cpp
Expand Up @@ -25,6 +25,7 @@
#include "Core/HW/SI/SI_DeviceKeyboard.h"
#include "Core/HW/SI/SI_DeviceNull.h"
#include "Core/HW/SystemTimers.h"
#include "Core/System.h"

namespace SerialInterface
{
Expand Down Expand Up @@ -142,10 +143,10 @@ int SIDevice_GetGBATransferTime(EBufferCommands cmd)
}
}

u64 cycles =
(gba_bytes_transferred * 8 * SystemTimers::GetTicksPerSecond() / GBA_BITS_PER_SECOND) +
(gc_bytes_transferred * 8 * SystemTimers::GetTicksPerSecond() / GC_BITS_PER_SECOND) +
(stop_bits_ns * SystemTimers::GetTicksPerSecond() / 1000000000LL);
const u32 ticks_per_second = Core::System::GetInstance().GetSystemTimers().GetTicksPerSecond();
const u64 cycles = (gba_bytes_transferred * 8 * ticks_per_second / GBA_BITS_PER_SECOND) +
(gc_bytes_transferred * 8 * ticks_per_second / GC_BITS_PER_SECOND) +
(stop_bits_ns * ticks_per_second / 1000000000LL);
return static_cast<int>(cycles);
}

Expand Down
4 changes: 2 additions & 2 deletions Source/Core/Core/HW/SI/SI_DeviceGBA.cpp
Expand Up @@ -154,14 +154,14 @@ void GBASockServer::ClockSync(Core::System& system)
{
s_num_connected++;
m_last_time_slice = core_timing.GetTicks();
time_slice = (u32)(SystemTimers::GetTicksPerSecond() / 60);
time_slice = (u32)(system.GetSystemTimers().GetTicksPerSecond() / 60);
}
else
{
time_slice = (u32)(core_timing.GetTicks() - m_last_time_slice);
}

time_slice = (u32)((u64)time_slice * 16777216 / SystemTimers::GetTicksPerSecond());
time_slice = (u32)((u64)time_slice * 16777216 / system.GetSystemTimers().GetTicksPerSecond());
m_last_time_slice = core_timing.GetTicks();
char bytes[4] = {0, 0, 0, 0};
bytes[0] = (time_slice >> 24) & 0xff;
Expand Down
2 changes: 1 addition & 1 deletion Source/Core/Core/HW/SI/SI_DeviceGBAEmu.cpp
Expand Up @@ -24,7 +24,7 @@ namespace SerialInterface
{
static s64 GetSyncInterval()
{
return SystemTimers::GetTicksPerSecond() / 1000;
return Core::System::GetInstance().GetSystemTimers().GetTicksPerSecond() / 1000;
}

CSIDevice_GBAEmu::CSIDevice_GBAEmu(Core::System& system, SIDevices device, int device_number)
Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/HW/SI/SI_DeviceGCController.cpp
Expand Up @@ -264,7 +264,8 @@ CSIDevice_GCController::HandleButtonCombos(const GCPadStatus& pad_status)
if (m_last_button_combo != COMBO_NONE)
{
const u64 current_time = m_system.GetCoreTiming().GetTicks();
if (u32(current_time - m_timer_button_combo_start) > SystemTimers::GetTicksPerSecond() * 3)
const u32 ticks_per_second = m_system.GetSystemTimers().GetTicksPerSecond();
if (u32(current_time - m_timer_button_combo_start) > ticks_per_second * 3)
{
if (m_last_button_combo == COMBO_RESET)
{
Expand Down

0 comments on commit bfb417f

Please sign in to comment.