core_timing.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. /**
  6. * This is a system to schedule events into the emulated machine's future. Time is measured
  7. * in main CPU clock cycles.
  8. *
  9. * To schedule an event, you first have to register its type. This is where you pass in the
  10. * callback. You then schedule events using the type id you get back.
  11. *
  12. * The int cyclesLate that the callbacks get is how many cycles late it was.
  13. * So to schedule a new event on a regular basis:
  14. * inside callback:
  15. * ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
  16. */
  17. #include <functional>
  18. #include <string>
  19. #include "common/common_types.h"
  20. namespace CoreTiming {
  21. // The below clock rate is based on Switch's clockspeed being widely known as 1.020GHz
  22. // The exact value used is of course unverified.
  23. constexpr u64 BASE_CLOCK_RATE = 1019215872; // Switch clock speed is 1020MHz un/docked
  24. inline s64 msToCycles(int ms) {
  25. // since ms is int there is no way to overflow
  26. return BASE_CLOCK_RATE * static_cast<s64>(ms) / 1000;
  27. }
  28. inline s64 msToCycles(float ms) {
  29. return static_cast<s64>(BASE_CLOCK_RATE * (0.001f) * ms);
  30. }
  31. inline s64 msToCycles(double ms) {
  32. return static_cast<s64>(BASE_CLOCK_RATE * (0.001) * ms);
  33. }
  34. inline s64 usToCycles(float us) {
  35. return static_cast<s64>(BASE_CLOCK_RATE * (0.000001f) * us);
  36. }
  37. inline s64 usToCycles(int us) {
  38. return (BASE_CLOCK_RATE * static_cast<s64>(us) / 1000000);
  39. }
  40. s64 usToCycles(s64 us);
  41. s64 usToCycles(u64 us);
  42. inline s64 nsToCycles(float ns) {
  43. return static_cast<s64>(BASE_CLOCK_RATE * (0.000000001f) * ns);
  44. }
  45. inline s64 nsToCycles(int ns) {
  46. return BASE_CLOCK_RATE * static_cast<s64>(ns) / 1000000000;
  47. }
  48. s64 nsToCycles(s64 ns);
  49. s64 nsToCycles(u64 ns);
  50. inline u64 cyclesToNs(s64 cycles) {
  51. return cycles * 1000000000 / BASE_CLOCK_RATE;
  52. }
  53. inline s64 cyclesToUs(s64 cycles) {
  54. return cycles * 1000000 / BASE_CLOCK_RATE;
  55. }
  56. inline u64 cyclesToMs(s64 cycles) {
  57. return cycles * 1000 / BASE_CLOCK_RATE;
  58. }
  59. /**
  60. * CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
  61. * required to end slice -1 and start slice 0 before the first cycle of code is executed.
  62. */
  63. void Init();
  64. void Shutdown();
  65. typedef std::function<void(u64 userdata, int cycles_late)> TimedCallback;
  66. /**
  67. * This should only be called from the emu thread, if you are calling it any other thread, you are
  68. * doing something evil
  69. */
  70. u64 GetTicks();
  71. u64 GetIdleTicks();
  72. void AddTicks(u64 ticks);
  73. struct EventType;
  74. /**
  75. * Returns the event_type identifier. if name is not unique, it will assert.
  76. */
  77. EventType* RegisterEvent(const std::string& name, TimedCallback callback);
  78. void UnregisterAllEvents();
  79. /**
  80. * After the first Advance, the slice lengths and the downcount will be reduced whenever an event
  81. * is scheduled earlier than the current values.
  82. * Scheduling from a callback will not update the downcount until the Advance() completes.
  83. */
  84. void ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata = 0);
  85. /**
  86. * This is to be called when outside of hle threads, such as the graphics thread, wants to
  87. * schedule things to be executed on the main thread.
  88. * Not that this doesn't change slice_length and thus events scheduled by this might be called
  89. * with a delay of up to MAX_SLICE_LENGTH
  90. */
  91. void ScheduleEventThreadsafe(s64 cycles_into_future, const EventType* event_type, u64 userdata);
  92. void UnscheduleEvent(const EventType* event_type, u64 userdata);
  93. /// We only permit one event of each type in the queue at a time.
  94. void RemoveEvent(const EventType* event_type);
  95. void RemoveNormalAndThreadsafeEvent(const EventType* event_type);
  96. /** Advance must be called at the beginning of dispatcher loops, not the end. Advance() ends
  97. * the previous timing slice and begins the next one, you must Advance from the previous
  98. * slice to the current one before executing any cycles. CoreTiming starts in slice -1 so an
  99. * Advance() is required to initialize the slice length before the first cycle of emulated
  100. * instructions is executed.
  101. */
  102. void Advance();
  103. void MoveEvents();
  104. /// Pretend that the main CPU has executed enough cycles to reach the next event.
  105. void Idle();
  106. /// Clear all pending events. This should ONLY be done on exit.
  107. void ClearPendingEvents();
  108. void ForceExceptionCheck(s64 cycles);
  109. u64 GetGlobalTimeUs();
  110. int GetDowncount();
  111. } // namespace CoreTiming