native_clock.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <optional>
  6. #include "common/spin_lock.h"
  7. #include "common/wall_clock.h"
  8. namespace Common {
  9. namespace X64 {
  10. class NativeClock : public WallClock {
  11. public:
  12. NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, u64 rtsc_frequency);
  13. std::chrono::nanoseconds GetTimeNS() override;
  14. std::chrono::microseconds GetTimeUS() override;
  15. std::chrono::milliseconds GetTimeMS() override;
  16. u64 GetClockCycles() override;
  17. u64 GetCPUCycles() override;
  18. void Pause(bool is_paused) override;
  19. private:
  20. u64 GetRTSC();
  21. /// value used to reduce the native clocks accuracy as some apss rely on
  22. /// undefined behavior where the level of accuracy in the clock shouldn't
  23. /// be higher.
  24. static constexpr u64 inaccuracy_mask = ~(0x400 - 1);
  25. SpinLock rtsc_serialize{};
  26. u64 last_measure{};
  27. u64 accumulated_ticks{};
  28. u64 rtsc_frequency;
  29. };
  30. } // namespace X64
  31. u64 EstimateRDTSCFrequency();
  32. } // namespace Common