Implement IsStandardNetworkSystemClockAccuracySufficient

Also use signed values for offsets and TimeSpanType
This commit is contained in:
Thog 2019-07-15 18:01:04 +02:00
commit 410e401e64
No known key found for this signature in database
GPG key ID: 0CD291558FAFDBC6
6 changed files with 73 additions and 11 deletions

View file

@ -201,6 +201,13 @@ namespace Ryujinx.HLE.HOS
// TODO: use "time!standard_steady_clock_rtc_update_interval_minutes" and implement a worker thread to be accurate. // TODO: use "time!standard_steady_clock_rtc_update_interval_minutes" and implement a worker thread to be accurate.
SteadyClockCore.Instance.ConfigureSetupValue(); SteadyClockCore.Instance.ConfigureSetupValue();
if (Services.Set.NxSettings.Settings.TryGetValue("time!standard_network_clock_sufficient_accuracy_minutes", out object standardNetworkClockSufficientAccuracyMinutes))
{
TimeSpanType standardNetworkClockSufficientAccuracy = new TimeSpanType((int)standardNetworkClockSufficientAccuracyMinutes * 60000000000);
StandardNetworkSystemClockCore.Instance.SetStandardNetworkClockSufficientAccuracy(standardNetworkClockSufficientAccuracy);
}
} }
public void LoadCart(string exeFsDir, string romFsFile = null) public void LoadCart(string exeFsDir, string romFsFile = null)

View file

@ -1,4 +1,5 @@
using Ryujinx.HLE.Utilities; using Ryujinx.HLE.Utilities;
using System;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Time.Clock namespace Ryujinx.HLE.HOS.Services.Time.Clock
@ -6,35 +7,56 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
struct TimeSpanType struct TimeSpanType
{ {
public ulong NanoSeconds; public long NanoSeconds;
public TimeSpanType(ulong nanoSeconds) public TimeSpanType(long nanoSeconds)
{ {
NanoSeconds = nanoSeconds; NanoSeconds = nanoSeconds;
} }
public ulong ToSeconds() public long ToSeconds()
{ {
return NanoSeconds / 1000000000; return NanoSeconds / 1000000000;
} }
public static TimeSpanType FromTicks(ulong ticks, ulong frequency) public static TimeSpanType FromTicks(ulong ticks, ulong frequency)
{ {
return new TimeSpanType(ticks * 1000000000 / frequency); return new TimeSpanType((long)ticks * 1000000000 / (long)frequency);
} }
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
struct SteadyClockTimePoint struct SteadyClockTimePoint
{ {
public ulong TimePoint; public long TimePoint;
public UInt128 ClockSourceId; public UInt128 ClockSourceId;
public ResultCode GetSpanBetween(SteadyClockTimePoint other, out long outSpan)
{
outSpan = 0;
if (ClockSourceId == other.ClockSourceId)
{
try
{
outSpan = checked(other.TimePoint - TimePoint);
return ResultCode.Success;
}
catch (OverflowException)
{
return ResultCode.Overflow;
}
}
return ResultCode.Overflow;
}
} }
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
struct SystemClockContext struct SystemClockContext
{ {
public ulong Offset; public long Offset;
public SteadyClockTimePoint SteadyTimePoint; public SteadyClockTimePoint SteadyTimePoint;
} }
} }

View file

@ -1,4 +1,5 @@
using Ryujinx.HLE.HOS.Kernel.Threading; using System;
using Ryujinx.HLE.HOS.Kernel.Threading;
namespace Ryujinx.HLE.HOS.Services.Time.Clock namespace Ryujinx.HLE.HOS.Services.Time.Clock
{ {
@ -6,6 +7,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
{ {
private SteadyClockCore _steadyClockCore; private SteadyClockCore _steadyClockCore;
private SystemClockContext _context; private SystemClockContext _context;
private TimeSpanType _standardNetworkClockSufficientAccuracy;
private static StandardNetworkSystemClockCore instance; private static StandardNetworkSystemClockCore instance;
@ -28,6 +30,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
_context = new SystemClockContext(); _context = new SystemClockContext();
_context.SteadyTimePoint.ClockSourceId = steadyClockCore.GetClockSourceId(); _context.SteadyTimePoint.ClockSourceId = steadyClockCore.GetClockSourceId();
_standardNetworkClockSufficientAccuracy = new TimeSpanType(0);
} }
public override ResultCode Flush(SystemClockContext context) public override ResultCode Flush(SystemClockContext context)
@ -55,5 +58,26 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
return ResultCode.Success; return ResultCode.Success;
} }
public bool IsStandardNetworkSystemClockAccuracySufficient(KThread thread)
{
SteadyClockCore steadyClockCore = GetSteadyClockCore();
SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(thread);
bool isStandardNetworkClockSufficientAccuracy = false;
if (_context.SteadyTimePoint.GetSpanBetween(currentTimePoint, out long outSpan) == ResultCode.Success)
{
isStandardNetworkClockSufficientAccuracy = outSpan * 1000000000 < _standardNetworkClockSufficientAccuracy.NanoSeconds;
}
return isStandardNetworkClockSufficientAccuracy;
}
public void SetStandardNetworkClockSufficientAccuracy(TimeSpanType standardNetworkClockSufficientAccuracy)
{
_standardNetworkClockSufficientAccuracy = standardNetworkClockSufficientAccuracy;
}
} }
} }

View file

@ -7,7 +7,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
{ {
class SteadyClockCore class SteadyClockCore
{ {
private ulong _setupValue; private long _setupValue;
private ResultCode _setupResultCode; private ResultCode _setupResultCode;
private bool _isRtcResetDetected; private bool _isRtcResetDetected;
private TimeSpanType _testOffset; private TimeSpanType _testOffset;
@ -118,7 +118,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
if (result == ResultCode.Success) if (result == ResultCode.Success)
{ {
_setupValue = rtcValue; _setupValue = (long)rtcValue;
break; break;
} }

View file

@ -106,6 +106,15 @@ namespace Ryujinx.HLE.HOS.Services.Time
return StandardUserSystemClockCore.Instance.SetAutomaticCorrectionEnabled(context.Thread, autoCorrectionEnabled); return StandardUserSystemClockCore.Instance.SetAutomaticCorrectionEnabled(context.Thread, autoCorrectionEnabled);
} }
[Command(200)] // 3.0.0+
// IsStandardNetworkSystemClockAccuracySufficient() -> bool
public ResultCode IsStandardNetworkSystemClockAccuracySufficient(ServiceCtx context)
{
context.ResponseData.Write(StandardNetworkSystemClockCore.Instance.IsStandardNetworkSystemClockAccuracySufficient(context.Thread));
return ResultCode.Success;
}
[Command(300)] // 4.0.0+ [Command(300)] // 4.0.0+
// CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> u64 // CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> u64
public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context) public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context)

View file

@ -29,7 +29,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
if (currentTimePoint.ClockSourceId == clockContext.SteadyTimePoint.ClockSourceId) if (currentTimePoint.ClockSourceId == clockContext.SteadyTimePoint.ClockSourceId)
{ {
ulong posixTime = clockContext.Offset + currentTimePoint.TimePoint; long posixTime = clockContext.Offset + currentTimePoint.TimePoint;
context.ResponseData.Write(posixTime); context.ResponseData.Write(posixTime);
@ -49,7 +49,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
return ResultCode.PermissionDenied; return ResultCode.PermissionDenied;
} }
ulong posixTime = context.RequestData.ReadUInt64(); long posixTime = context.RequestData.ReadInt64();
SteadyClockCore steadyClockCore = _clockCore.GetSteadyClockCore(); SteadyClockCore steadyClockCore = _clockCore.GetSteadyClockCore();
SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(context.Thread); SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(context.Thread);