core_timing.h 4.3 KB

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