SteadyClockCore.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Ryujinx.HLE.HOS.Kernel.Threading;
  2. using Ryujinx.HLE.HOS.Services.Bpc;
  3. using Ryujinx.HLE.Utilities;
  4. using System;
  5. namespace Ryujinx.HLE.HOS.Services.Time.Clock
  6. {
  7. class SteadyClockCore
  8. {
  9. private long _setupValue;
  10. private ResultCode _setupResultCode;
  11. private bool _isRtcResetDetected;
  12. private TimeSpanType _testOffset;
  13. private TimeSpanType _internalOffset;
  14. private UInt128 _clockSourceId;
  15. private static SteadyClockCore instance;
  16. public static SteadyClockCore Instance
  17. {
  18. get
  19. {
  20. if (instance == null)
  21. {
  22. instance = new SteadyClockCore();
  23. }
  24. return instance;
  25. }
  26. }
  27. private SteadyClockCore()
  28. {
  29. _testOffset = new TimeSpanType(0);
  30. _internalOffset = new TimeSpanType(0);
  31. _clockSourceId = new UInt128(Guid.NewGuid().ToByteArray());
  32. }
  33. private SteadyClockTimePoint GetTimePoint(KThread thread)
  34. {
  35. SteadyClockTimePoint result = new SteadyClockTimePoint
  36. {
  37. TimePoint = 0,
  38. ClockSourceId = _clockSourceId
  39. };
  40. TimeSpanType ticksTimeSpan = TimeSpanType.FromTicks(thread.Context.ThreadState.CntpctEl0, thread.Context.ThreadState.CntfrqEl0);
  41. result.TimePoint = _setupValue + ticksTimeSpan.ToSeconds();
  42. return result;
  43. }
  44. public UInt128 GetClockSourceId()
  45. {
  46. return _clockSourceId;
  47. }
  48. public SteadyClockTimePoint GetCurrentTimePoint(KThread thread)
  49. {
  50. SteadyClockTimePoint result = GetTimePoint(thread);
  51. result.TimePoint += _testOffset.ToSeconds();
  52. result.TimePoint += _internalOffset.ToSeconds();
  53. return result;
  54. }
  55. public TimeSpanType GetTestOffset()
  56. {
  57. return _testOffset;
  58. }
  59. public void SetTestOffset(TimeSpanType testOffset)
  60. {
  61. _testOffset = testOffset;
  62. }
  63. public ResultCode GetRtcValue(out ulong rtcValue)
  64. {
  65. return (ResultCode)IRtcManager.GetExternalRtcValue(out rtcValue);
  66. }
  67. public bool IsRtcResetDetected()
  68. {
  69. return _isRtcResetDetected;
  70. }
  71. public ResultCode GetSetupResultCode()
  72. {
  73. return _setupResultCode;
  74. }
  75. public TimeSpanType GetInternalOffset()
  76. {
  77. return _internalOffset;
  78. }
  79. public void SetInternalOffset(TimeSpanType internalOffset)
  80. {
  81. _internalOffset = internalOffset;
  82. }
  83. public ResultCode GetSetupResultValue()
  84. {
  85. return _setupResultCode;
  86. }
  87. public void ConfigureSetupValue()
  88. {
  89. int retry = 0;
  90. ResultCode result = ResultCode.Success;
  91. while (retry < 20)
  92. {
  93. result = (ResultCode)IRtcManager.GetExternalRtcValue(out ulong rtcValue);
  94. if (result == ResultCode.Success)
  95. {
  96. _setupValue = (long)rtcValue;
  97. break;
  98. }
  99. retry++;
  100. }
  101. _setupResultCode = result;
  102. }
  103. }
  104. }