core_timing.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <chrono>
  18. #include <functional>
  19. #include <string>
  20. #include "common/common_types.h"
  21. namespace CoreTiming {
  22. struct EventType;
  23. using TimedCallback = std::function<void(u64 userdata, int cycles_late)>;
  24. /**
  25. * CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
  26. * required to end slice -1 and start slice 0 before the first cycle of code is executed.
  27. */
  28. void Init();
  29. void Shutdown();
  30. /**
  31. * This should only be called from the emu thread, if you are calling it any other thread, you are
  32. * doing something evil
  33. */
  34. u64 GetTicks();
  35. u64 GetIdleTicks();
  36. void AddTicks(u64 ticks);
  37. /**
  38. * Returns the event_type identifier. if name is not unique, it will assert.
  39. */
  40. EventType* RegisterEvent(const std::string& name, TimedCallback callback);
  41. void UnregisterAllEvents();
  42. /**
  43. * After the first Advance, the slice lengths and the downcount will be reduced whenever an event
  44. * is scheduled earlier than the current values.
  45. * Scheduling from a callback will not update the downcount until the Advance() completes.
  46. */
  47. void ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata = 0);
  48. /**
  49. * This is to be called when outside of hle threads, such as the graphics thread, wants to
  50. * schedule things to be executed on the main thread.
  51. * Not that this doesn't change slice_length and thus events scheduled by this might be called
  52. * with a delay of up to MAX_SLICE_LENGTH
  53. */
  54. void ScheduleEventThreadsafe(s64 cycles_into_future, const EventType* event_type, u64 userdata);
  55. void UnscheduleEvent(const EventType* event_type, u64 userdata);
  56. void UnscheduleEventThreadsafe(const EventType* event_type, u64 userdata);
  57. /// We only permit one event of each type in the queue at a time.
  58. void RemoveEvent(const EventType* event_type);
  59. void RemoveNormalAndThreadsafeEvent(const EventType* event_type);
  60. /** Advance must be called at the beginning of dispatcher loops, not the end. Advance() ends
  61. * the previous timing slice and begins the next one, you must Advance from the previous
  62. * slice to the current one before executing any cycles. CoreTiming starts in slice -1 so an
  63. * Advance() is required to initialize the slice length before the first cycle of emulated
  64. * instructions is executed.
  65. */
  66. void Advance();
  67. void MoveEvents();
  68. /// Pretend that the main CPU has executed enough cycles to reach the next event.
  69. void Idle();
  70. /// Clear all pending events. This should ONLY be done on exit.
  71. void ClearPendingEvents();
  72. void ForceExceptionCheck(s64 cycles);
  73. std::chrono::microseconds GetGlobalTimeUs();
  74. int GetDowncount();
  75. } // namespace CoreTiming