2020-02-09 20:53:22 +00:00
|
|
|
// Copyright 2020 yuzu Emulator Project
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2021-02-15 22:51:43 +00:00
|
|
|
#include <cstdint>
|
|
|
|
|
2020-02-09 20:53:22 +00: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 {
|
|
|
|
|
|
|
|
using base_timer = std::chrono::steady_clock;
|
|
|
|
using base_time_point = std::chrono::time_point<base_timer>;
|
|
|
|
|
2020-09-29 19:19:37 +00:00
|
|
|
class StandardWallClock final : public WallClock {
|
2020-02-09 20:53:22 +00:00
|
|
|
public:
|
2020-11-25 20:21:03 +00:00
|
|
|
explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_)
|
2021-02-15 22:51:43 +00:00
|
|
|
: WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false),
|
|
|
|
emulated_clock_factor{GetFixedPoint64Factor(emulated_clock_frequency, 1000000000)},
|
|
|
|
emulated_cpu_factor{GetFixedPoint64Factor(emulated_cpu_frequency, 1000000000)} {
|
2020-02-09 20:53:22 +00:00
|
|
|
start_time = base_timer::now();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::nanoseconds GetTimeNS() override {
|
|
|
|
base_time_point current = base_timer::now();
|
|
|
|
auto elapsed = current - start_time;
|
|
|
|
return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::microseconds GetTimeUS() override {
|
|
|
|
base_time_point current = base_timer::now();
|
|
|
|
auto elapsed = current - start_time;
|
|
|
|
return std::chrono::duration_cast<std::chrono::microseconds>(elapsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::chrono::milliseconds GetTimeMS() override {
|
|
|
|
base_time_point current = base_timer::now();
|
|
|
|
auto elapsed = current - start_time;
|
|
|
|
return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed);
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetClockCycles() override {
|
2021-02-15 22:51:43 +00:00
|
|
|
return MultiplyHigh(GetTimeNS().count(), emulated_clock_factor);
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 GetCPUCycles() override {
|
2021-02-15 22:51:43 +00:00
|
|
|
return MultiplyHigh(GetTimeNS().count(), emulated_cpu_factor);
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-30 19:02:02 +00:00
|
|
|
void Pause([[maybe_unused]] bool is_paused) override {
|
2020-02-25 16:28:55 +00:00
|
|
|
// Do nothing in this clock type.
|
|
|
|
}
|
|
|
|
|
2020-02-09 20:53:22 +00:00
|
|
|
private:
|
|
|
|
base_time_point start_time;
|
2021-02-15 22:51:43 +00:00
|
|
|
const u64 emulated_clock_factor;
|
|
|
|
const u64 emulated_cpu_factor;
|
2020-02-09 20:53:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef ARCHITECTURE_x86_64
|
|
|
|
|
2020-02-10 17:33:13 +00:00
|
|
|
std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
|
|
|
|
u32 emulated_clock_frequency) {
|
2020-02-09 20:53:22 +00:00
|
|
|
const auto& caps = GetCPUCaps();
|
|
|
|
u64 rtsc_frequency = 0;
|
|
|
|
if (caps.invariant_tsc) {
|
2020-05-29 21:37:37 +00:00
|
|
|
rtsc_frequency = EstimateRDTSCFrequency();
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
if (rtsc_frequency == 0) {
|
2020-02-10 17:33:13 +00:00
|
|
|
return std::make_unique<StandardWallClock>(emulated_cpu_frequency,
|
|
|
|
emulated_clock_frequency);
|
2020-02-09 20:53:22 +00:00
|
|
|
} else {
|
2020-02-10 17:33:13 +00:00
|
|
|
return std::make_unique<X64::NativeClock>(emulated_cpu_frequency, emulated_clock_frequency,
|
|
|
|
rtsc_frequency);
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2020-02-10 17:33:13 +00:00
|
|
|
std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
|
|
|
|
u32 emulated_clock_frequency) {
|
2020-02-10 15:20:40 +00:00
|
|
|
return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency);
|
2020-02-09 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // namespace Common
|