StandardSteadyClockCore.cs 2.8 KB

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