core_timing.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <limits>
  19. #include <string>
  20. #include "common/common_types.h"
  21. #include "common/logging/log.h"
  22. // The below clock rate is based on Switch's clockspeed being widely known as 1.020GHz
  23. // The exact value used is of course unverified.
  24. constexpr u64 BASE_CLOCK_RATE = 1019215872; // Switch clock speed is 1020MHz un/docked
  25. constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / BASE_CLOCK_RATE;
  26. inline s64 msToCycles(int ms) {
  27. // since ms is int there is no way to overflow
  28. return BASE_CLOCK_RATE * static_cast<s64>(ms) / 1000;
  29. }
  30. inline s64 msToCycles(float ms) {
  31. return static_cast<s64>(BASE_CLOCK_RATE * (0.001f) * ms);
  32. }
  33. inline s64 msToCycles(double ms) {
  34. return static_cast<s64>(BASE_CLOCK_RATE * (0.001) * ms);
  35. }
  36. inline s64 usToCycles(float us) {
  37. return static_cast<s64>(BASE_CLOCK_RATE * (0.000001f) * us);
  38. }
  39. inline s64 usToCycles(int us) {
  40. return (BASE_CLOCK_RATE * static_cast<s64>(us) / 1000000);
  41. }
  42. inline s64 usToCycles(s64 us) {
  43. if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
  44. LOG_ERROR(Core_Timing, "Integer overflow, use max value");
  45. return std::numeric_limits<s64>::max();
  46. }
  47. if (us > MAX_VALUE_TO_MULTIPLY) {
  48. LOG_DEBUG(Core_Timing, "Time very big, do rounding");
  49. return BASE_CLOCK_RATE * (us / 1000000);
  50. }
  51. return (BASE_CLOCK_RATE * us) / 1000000;
  52. }
  53. inline s64 usToCycles(u64 us) {
  54. if (us / 1000000 > MAX_VALUE_TO_MULTIPLY) {
  55. LOG_ERROR(Core_Timing, "Integer overflow, use max value");
  56. return std::numeric_limits<s64>::max();
  57. }
  58. if (us > MAX_VALUE_TO_MULTIPLY) {
  59. LOG_DEBUG(Core_Timing, "Time very big, do rounding");
  60. return BASE_CLOCK_RATE * static_cast<s64>(us / 1000000);
  61. }
  62. return (BASE_CLOCK_RATE * static_cast<s64>(us)) / 1000000;
  63. }
  64. inline s64 nsToCycles(float ns) {
  65. return static_cast<s64>(BASE_CLOCK_RATE * (0.000000001f) * ns);
  66. }
  67. inline s64 nsToCycles(int ns) {
  68. return BASE_CLOCK_RATE * static_cast<s64>(ns) / 1000000000;
  69. }
  70. inline s64 nsToCycles(s64 ns) {
  71. if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
  72. LOG_ERROR(Core_Timing, "Integer overflow, use max value");
  73. return std::numeric_limits<s64>::max();
  74. }
  75. if (ns > MAX_VALUE_TO_MULTIPLY) {
  76. LOG_DEBUG(Core_Timing, "Time very big, do rounding");
  77. return BASE_CLOCK_RATE * (ns / 1000000000);
  78. }
  79. return (BASE_CLOCK_RATE * ns) / 1000000000;
  80. }
  81. inline s64 nsToCycles(u64 ns) {
  82. if (ns / 1000000000 > MAX_VALUE_TO_MULTIPLY) {
  83. LOG_ERROR(Core_Timing, "Integer overflow, use max value");
  84. return std::numeric_limits<s64>::max();
  85. }
  86. if (ns > MAX_VALUE_TO_MULTIPLY) {
  87. LOG_DEBUG(Core_Timing, "Time very big, do rounding");
  88. return BASE_CLOCK_RATE * (static_cast<s64>(ns) / 1000000000);
  89. }
  90. return (BASE_CLOCK_RATE * static_cast<s64>(ns)) / 1000000000;
  91. }
  92. inline u64 cyclesToNs(s64 cycles) {
  93. return cycles * 1000000000 / BASE_CLOCK_RATE;
  94. }
  95. inline s64 cyclesToUs(s64 cycles) {
  96. return cycles * 1000000 / BASE_CLOCK_RATE;
  97. }
  98. inline u64 cyclesToMs(s64 cycles) {
  99. return cycles * 1000 / BASE_CLOCK_RATE;
  100. }
  101. namespace CoreTiming {
  102. /**
  103. * CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
  104. * required to end slice -1 and start slice 0 before the first cycle of code is executed.
  105. */
  106. void Init();
  107. void Shutdown();
  108. typedef std::function<void(u64 userdata, int cycles_late)> TimedCallback;
  109. /**
  110. * This should only be called from the emu thread, if you are calling it any other thread, you are
  111. * doing something evil
  112. */
  113. u64 GetTicks();
  114. u64 GetIdleTicks();
  115. void AddTicks(u64 ticks);
  116. struct EventType;
  117. /**
  118. * Returns the event_type identifier. if name is not unique, it will assert.
  119. */
  120. EventType* RegisterEvent(const std::string& name, TimedCallback callback);
  121. void UnregisterAllEvents();
  122. /**
  123. * After the first Advance, the slice lengths and the downcount will be reduced whenever an event
  124. * is scheduled earlier than the current values.
  125. * Scheduling from a callback will not update the downcount until the Advance() completes.
  126. */
  127. void ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata = 0);
  128. /**
  129. * This is to be called when outside of hle threads, such as the graphics thread, wants to
  130. * schedule things to be executed on the main thread.
  131. * Not that this doesn't change slice_length and thus events scheduled by this might be called
  132. * with a delay of up to MAX_SLICE_LENGTH
  133. */
  134. void ScheduleEventThreadsafe(s64 cycles_into_future, const EventType* event_type, u64 userdata);
  135. void UnscheduleEvent(const EventType* event_type, u64 userdata);
  136. /// We only permit one event of each type in the queue at a time.
  137. void RemoveEvent(const EventType* event_type);
  138. void RemoveNormalAndThreadsafeEvent(const EventType* event_type);
  139. /** Advance must be called at the beginning of dispatcher loops, not the end. Advance() ends
  140. * the previous timing slice and begins the next one, you must Advance from the previous
  141. * slice to the current one before executing any cycles. CoreTiming starts in slice -1 so an
  142. * Advance() is required to initialize the slice length before the first cycle of emulated
  143. * instructions is executed.
  144. */
  145. void Advance();
  146. void MoveEvents();
  147. /// Pretend that the main CPU has executed enough cycles to reach the next event.
  148. void Idle();
  149. /// Clear all pending events. This should ONLY be done on exit.
  150. void ClearPendingEvents();
  151. void ForceExceptionCheck(s64 cycles);
  152. u64 GetGlobalTimeUs();
  153. int GetDowncount();
  154. } // namespace CoreTiming