core_timing.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2013 Dolphin Emulator Project
  2. // Licensed under GPLv2
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. // This is a system to schedule events into the emulated machine's future. Time is measured
  6. // in main CPU clock cycles.
  7. // To schedule an event, you first have to register its type. This is where you pass in the
  8. // callback. You then schedule events using the type id you get back.
  9. // See HW/SystemTimers.cpp for the main part of Dolphin's usage of this scheduler.
  10. // The int cyclesLate that the callbacks get is how many cycles late it was.
  11. // So to schedule a new event on a regular basis:
  12. // inside callback:
  13. // ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
  14. #include "common/common.h"
  15. class PointerWrap;
  16. extern int g_clock_rate_arm11;
  17. inline s64 msToCycles(int ms) {
  18. return g_clock_rate_arm11 / 1000 * ms;
  19. }
  20. inline s64 msToCycles(float ms) {
  21. return (s64)(g_clock_rate_arm11 * ms * (0.001f));
  22. }
  23. inline s64 msToCycles(double ms) {
  24. return (s64)(g_clock_rate_arm11 * ms * (0.001));
  25. }
  26. inline s64 usToCycles(float us) {
  27. return (s64)(g_clock_rate_arm11 * us * (0.000001f));
  28. }
  29. inline s64 usToCycles(int us) {
  30. return (g_clock_rate_arm11 / 1000000 * (s64)us);
  31. }
  32. inline s64 usToCycles(s64 us) {
  33. return (g_clock_rate_arm11 / 1000000 * us);
  34. }
  35. inline s64 usToCycles(u64 us) {
  36. return (s64)(g_clock_rate_arm11 / 1000000 * us);
  37. }
  38. inline s64 cyclesToUs(s64 cycles) {
  39. return cycles / (g_clock_rate_arm11 / 1000000);
  40. }
  41. namespace CoreTiming {
  42. void Init();
  43. void Shutdown();
  44. typedef void(*TimedCallback)(u64 userdata, int cyclesLate);
  45. u64 GetTicks();
  46. u64 GetIdleTicks();
  47. // Returns the event_type identifier.
  48. int RegisterEvent(const char *name, TimedCallback callback);
  49. // For save states.
  50. void RestoreRegisterEvent(int event_type, const char *name, TimedCallback callback);
  51. void UnregisterAllEvents();
  52. // userdata MAY NOT CONTAIN POINTERS. userdata might get written and reloaded from disk,
  53. // when we implement state saves.
  54. void ScheduleEvent(s64 cyclesIntoFuture, int event_type, u64 userdata = 0);
  55. void ScheduleEvent_Threadsafe(s64 cyclesIntoFuture, int event_type, u64 userdata = 0);
  56. void ScheduleEvent_Threadsafe_Immediate(int event_type, u64 userdata = 0);
  57. s64 UnscheduleEvent(int event_type, u64 userdata);
  58. s64 UnscheduleThreadsafeEvent(int event_type, u64 userdata);
  59. void RemoveEvent(int event_type);
  60. void RemoveThreadsafeEvent(int event_type);
  61. void RemoveAllEvents(int event_type);
  62. bool IsScheduled(int event_type);
  63. void Advance();
  64. void MoveEvents();
  65. void ProcessFifoWaitEvents();
  66. // Pretend that the main CPU has executed enough cycles to reach the next event.
  67. void Idle(int maxIdle = 0);
  68. // Clear all pending events. This should ONLY be done on exit or state load.
  69. void ClearPendingEvents();
  70. void LogPendingEvents();
  71. // Warning: not included in save states.
  72. void RegisterAdvanceCallback(void(*callback)(int cyclesExecuted));
  73. std::string GetScheduledEventsSummary();
  74. void DoState(PointerWrap &p);
  75. void SetClockFrequencyMHz(int cpuMhz);
  76. int GetClockFrequencyMHz();
  77. extern int slicelength;
  78. } // namespace