2022-04-23 10:59:50 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2020 yuzu Emulator Project
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2020-02-09 21:53:22 +01:00
|
|
|
|
2023-03-02 01:43:00 +01:00
|
|
|
#include "common/steady_clock.h"
|
2020-02-09 21:53:22 +01:00
|
|
|
#include "common/uint128.h"
|
|
|
|
#include "common/wall_clock.h"
|
|
|
|
|
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
#include "common/x64/cpu_detect.h"
|
|
|
|
#include "common/x64/native_clock.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace Common {
|
|
|
|
|
2020-09-29 21:19:37 +02:00
|
|
|
class StandardWallClock final : public WallClock {
|
2020-02-09 21:53:22 +01:00
|
|
|
public:
|
2020-11-25 21:21:03 +01:00
|
|
|
explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_)
|
2023-03-02 01:43:00 +01:00
|
|
|
: WallClock{emulated_cpu_frequency_, emulated_clock_frequency_, false},
|
|
|
|
start_time{SteadyClock::Now()} {}
|
2020-02-09 21:53:22 +01:00
|
|
|
|
|
|
|
std::chrono::nanoseconds GetTimeNS() override {
|
2023-03-02 01:43:00 +01:00
|
|
|
return SteadyClock::Now() - start_time;
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::microseconds GetTimeUS() override {
|
2023-03-02 01:43:00 +01:00
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(GetTimeNS());
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::milliseconds GetTimeMS() override {
|
2023-03-02 01:43:00 +01:00
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(GetTimeNS());
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetClockCycles() override {
|
2023-03-02 01:43:00 +01:00
|
|
|
const u128 temp = Common::Multiply64Into128(GetTimeNS().count(), emulated_clock_frequency);
|
|
|
|
return Common::Divide128On32(temp, NS_RATIO).first;
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetCPUCycles() override {
|
2023-03-02 01:43:00 +01:00
|
|
|
const u128 temp = Common::Multiply64Into128(GetTimeNS().count(), emulated_cpu_frequency);
|
|
|
|
return Common::Divide128On32(temp, NS_RATIO).first;
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
2020-10-30 20:02:02 +01:00
|
|
|
void Pause([[maybe_unused]] bool is_paused) override {
|
2020-02-25 17:28:55 +01:00
|
|
|
// Do nothing in this clock type.
|
|
|
|
}
|
|
|
|
|
2020-02-09 21:53:22 +01:00
|
|
|
private:
|
2023-03-02 01:43:00 +01:00
|
|
|
SteadyClock::time_point start_time;
|
2020-02-09 21:53:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
|
2022-01-30 18:57:23 +01:00
|
|
|
std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
|
|
|
|
u64 emulated_clock_frequency) {
|
2020-02-09 21:53:22 +01:00
|
|
|
const auto& caps = GetCPUCaps();
|
|
|
|
u64 rtsc_frequency = 0;
|
|
|
|
if (caps.invariant_tsc) {
|
2022-07-06 19:42:01 +02:00
|
|
|
rtsc_frequency = caps.tsc_frequency ? caps.tsc_frequency : EstimateRDTSCFrequency();
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
2022-01-27 23:53:58 +01:00
|
|
|
|
2022-01-30 18:57:23 +01:00
|
|
|
// Fallback to StandardWallClock if the hardware TSC does not have the precision greater than:
|
|
|
|
// - A nanosecond
|
|
|
|
// - The emulated CPU frequency
|
|
|
|
// - The emulated clock counter frequency (CNTFRQ)
|
|
|
|
if (rtsc_frequency <= WallClock::NS_RATIO || rtsc_frequency <= emulated_cpu_frequency ||
|
|
|
|
rtsc_frequency <= emulated_clock_frequency) {
|
2020-02-10 18:33:13 +01:00
|
|
|
return std::make_unique<StandardWallClock>(emulated_cpu_frequency,
|
|
|
|
emulated_clock_frequency);
|
2020-02-09 21:53:22 +01:00
|
|
|
} else {
|
2020-02-10 18:33:13 +01:00
|
|
|
return std::make_unique<X64::NativeClock>(emulated_cpu_frequency, emulated_clock_frequency,
|
|
|
|
rtsc_frequency);
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2022-01-30 18:57:23 +01:00
|
|
|
std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
|
|
|
|
u64 emulated_clock_frequency) {
|
2020-02-10 16:20:40 +01:00
|
|
|
return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency);
|
2020-02-09 21:53:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2023-03-02 03:06:19 +01:00
|
|
|
std::unique_ptr<WallClock> CreateStandardWallClock(u64 emulated_cpu_frequency,
|
|
|
|
u64 emulated_clock_frequency) {
|
|
|
|
return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency);
|
|
|
|
}
|
|
|
|
|
2020-02-09 21:53:22 +01:00
|
|
|
} // namespace Common
|