core_timing.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. /**
  23. * CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
  24. * required to end slice -1 and start slice 0 before the first cycle of code is executed.
  25. */
  26. void Init();
  27. void Shutdown();
  28. typedef std::function<void(u64 userdata, int cycles_late)> TimedCallback;
  29. /**
  30. * This should only be called from the emu thread, if you are calling it any other thread, you are
  31. * doing something evil
  32. */
  33. u64 GetTicks();
  34. u64 GetIdleTicks();
  35. void AddTicks(u64 ticks);
  36. struct EventType;
  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. /// We only permit one event of each type in the queue at a time.
  57. void RemoveEvent(const EventType* event_type);
  58. void RemoveNormalAndThreadsafeEvent(const EventType* event_type);
  59. /** Advance must be called at the beginning of dispatcher loops, not the end. Advance() ends
  60. * the previous timing slice and begins the next one, you must Advance from the previous
  61. * slice to the current one before executing any cycles. CoreTiming starts in slice -1 so an
  62. * Advance() is required to initialize the slice length before the first cycle of emulated
  63. * instructions is executed.
  64. */
  65. void Advance();
  66. void MoveEvents();
  67. /// Pretend that the main CPU has executed enough cycles to reach the next event.
  68. void Idle();
  69. /// Clear all pending events. This should ONLY be done on exit.
  70. void ClearPendingEvents();
  71. void ForceExceptionCheck(s64 cycles);
  72. std::chrono::microseconds GetGlobalTimeUs();
  73. int GetDowncount();
  74. } // namespace CoreTiming