core_timing.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2012- PPSSPP Project / Dolphin Project.
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <string>
  6. // This is a system to schedule events into the emulated machine's future. Time is measured
  7. // in main CPU clock cycles.
  8. // To schedule an event, you first have to register its type. This is where you pass in the
  9. // callback. You then schedule events using the type id you get back.
  10. // See HW/SystemTimers.cpp for the main part of Dolphin's usage of this scheduler.
  11. // The int cycles_late that the callbacks get is how many cycles late it was.
  12. // So to schedule a new event on a regular basis:
  13. // inside callback:
  14. // ScheduleEvent(periodInCycles - cycles_late, callback, "whatever")
  15. #include <functional>
  16. #include "common/common_types.h"
  17. extern int g_clock_rate_arm11;
  18. inline s64 msToCycles(int ms) {
  19. return g_clock_rate_arm11 / 1000 * ms;
  20. }
  21. inline s64 msToCycles(float ms) {
  22. return (s64)(g_clock_rate_arm11 * ms * (0.001f));
  23. }
  24. inline s64 msToCycles(double ms) {
  25. return (s64)(g_clock_rate_arm11 * ms * (0.001));
  26. }
  27. inline s64 usToCycles(float us) {
  28. return (s64)(g_clock_rate_arm11 * us * (0.000001f));
  29. }
  30. inline s64 usToCycles(int us) {
  31. return (g_clock_rate_arm11 / 1000000 * (s64)us);
  32. }
  33. inline s64 usToCycles(s64 us) {
  34. return (g_clock_rate_arm11 / 1000000 * us);
  35. }
  36. inline s64 usToCycles(u64 us) {
  37. return (s64)(g_clock_rate_arm11 / 1000000 * us);
  38. }
  39. inline s64 cyclesToUs(s64 cycles) {
  40. return cycles / (g_clock_rate_arm11 / 1000000);
  41. }
  42. inline u64 cyclesToMs(s64 cycles) {
  43. return cycles / (g_clock_rate_arm11 / 1000);
  44. }
  45. namespace CoreTiming
  46. {
  47. void Init();
  48. void Shutdown();
  49. typedef void(*MHzChangeCallback)();
  50. typedef std::function<void(u64 userdata, int cycles_late)> TimedCallback;
  51. u64 GetTicks();
  52. u64 GetIdleTicks();
  53. u64 GetGlobalTimeUs();
  54. /**
  55. * Registers an event type with the specified name and callback
  56. * @param name Name of the event type
  57. * @param callback Function that will execute when this event fires
  58. * @returns An identifier for the event type that was registered
  59. */
  60. int RegisterEvent(const char* name, TimedCallback callback);
  61. /// For save states.
  62. void RestoreRegisterEvent(int event_type, const char *name, TimedCallback callback);
  63. void UnregisterAllEvents();
  64. /// userdata MAY NOT CONTAIN POINTERS. userdata might get written and reloaded from disk,
  65. /// when we implement state saves.
  66. /**
  67. * Schedules an event to run after the specified number of cycles,
  68. * with an optional parameter to be passed to the callback handler.
  69. * This must be run ONLY from within the cpu thread.
  70. * @param cycles_into_future The number of cycles after which this event will be fired
  71. * @param event_type The event type to fire, as returned from RegisterEvent
  72. * @param userdata Optional parameter to pass to the callback when fired
  73. */
  74. void ScheduleEvent(s64 cycles_into_future, int event_type, u64 userdata = 0);
  75. void ScheduleEvent_Threadsafe(s64 cycles_into_future, int event_type, u64 userdata = 0);
  76. void ScheduleEvent_Threadsafe_Immediate(int event_type, u64 userdata = 0);
  77. /**
  78. * Unschedules an event with the specified type and userdata
  79. * @param event_type The type of event to unschedule, as returned from RegisterEvent
  80. * @param userdata The userdata that identifies this event, as passed to ScheduleEvent
  81. * @returns The remaining ticks until the next invocation of the event callback
  82. */
  83. s64 UnscheduleEvent(int event_type, u64 userdata);
  84. s64 UnscheduleThreadsafeEvent(int event_type, u64 userdata);
  85. void RemoveEvent(int event_type);
  86. void RemoveThreadsafeEvent(int event_type);
  87. void RemoveAllEvents(int event_type);
  88. bool IsScheduled(int event_type);
  89. /// Runs any pending events and updates downcount for the next slice of cycles
  90. void Advance();
  91. void MoveEvents();
  92. void ProcessFifoWaitEvents();
  93. void ForceCheck();
  94. /// Pretend that the main CPU has executed enough cycles to reach the next event.
  95. void Idle(int maxIdle = 0);
  96. /// Clear all pending events. This should ONLY be done on exit or state load.
  97. void ClearPendingEvents();
  98. void LogPendingEvents();
  99. /// Warning: not included in save states.
  100. void RegisterAdvanceCallback(void(*callback)(int cycles_executed));
  101. void RegisterMHzChangeCallback(MHzChangeCallback callback);
  102. std::string GetScheduledEventsSummary();
  103. void SetClockFrequencyMHz(int cpu_mhz);
  104. int GetClockFrequencyMHz();
  105. extern int g_slice_length;
  106. } // namespace