Address comment

This commit is contained in:
Thog 2019-07-25 13:59:57 +02:00
parent 0b3bfe4b6f
commit 9d937ab93c
No known key found for this signature in database
GPG key ID: 0CD291558FAFDBC6
10 changed files with 70 additions and 76 deletions

View file

@ -759,19 +759,6 @@ namespace ChocolArm64.Memory
}
}
public unsafe T ReadStruct<T>(long position)
where T : struct
{
int size = Marshal.SizeOf<T>();
byte[] data = ReadBytes(position, size);
fixed (byte* ptr = data)
{
return Marshal.PtrToStructure<T>((IntPtr)ptr);
}
}
public void WriteSByte(long position, sbyte value)
{
WriteByte(position, (byte)value);
@ -965,21 +952,6 @@ namespace ChocolArm64.Memory
}
}
public unsafe void WriteStruct<T>(long position, T value)
where T : struct
{
long size = Marshal.SizeOf<T>();
byte[] data = new byte[size];
fixed (byte* ptr = data)
{
Marshal.StructureToPtr<T>(value, (IntPtr)ptr, false);
}
WriteBytes(position, data);
}
public void CopyBytes(long src, long dst, long size)
{
// Note: This will be moved later.

View file

@ -7,6 +7,8 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
[StructLayout(LayoutKind.Sequential)]
struct TimeSpanType
{
private const long NanoSecondsPerSecond = 1000000000;
public long NanoSeconds;
public TimeSpanType(long nanoSeconds)
@ -16,12 +18,12 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
public long ToSeconds()
{
return NanoSeconds / 1000000000;
return NanoSeconds / NanoSecondsPerSecond;
}
public static TimeSpanType FromSeconds(long seconds)
{
return new TimeSpanType(seconds * 1000000000);
return new TimeSpanType(seconds * NanoSecondsPerSecond);
}
public static TimeSpanType FromTicks(ulong ticks, ulong frequency)
@ -65,7 +67,6 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
public SteadyClockTimePoint SteadyTimePoint;
}
[StructLayout(LayoutKind.Sequential, Size = 0xD0)]
struct ClockSnapshot
{
@ -87,7 +88,6 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
public byte Type;
public ushort Unknown;
public static ResultCode GetCurrentTime(out long currentTime, SteadyClockTimePoint steadyClockTimePoint, SystemClockContext context)
{
currentTime = 0;

View file

@ -2,18 +2,18 @@
{
class EphemeralNetworkSystemClockCore : SystemClockCore
{
private static EphemeralNetworkSystemClockCore instance;
private static EphemeralNetworkSystemClockCore _instance;
public static EphemeralNetworkSystemClockCore Instance
{
get
{
if (instance == null)
if (_instance == null)
{
instance = new EphemeralNetworkSystemClockCore(TickBasedSteadyClockCore.Instance);
_instance = new EphemeralNetworkSystemClockCore(TickBasedSteadyClockCore.Instance);
}
return instance;
return _instance;
}
}

View file

@ -2,18 +2,18 @@
{
class StandardLocalSystemClockCore : SystemClockCore
{
private static StandardLocalSystemClockCore instance;
private static StandardLocalSystemClockCore _instance;
public static StandardLocalSystemClockCore Instance
{
get
{
if (instance == null)
if (_instance == null)
{
instance = new StandardLocalSystemClockCore(StandardSteadyClockCore.Instance);
_instance = new StandardLocalSystemClockCore(StandardSteadyClockCore.Instance);
}
return instance;
return _instance;
}
}

View file

@ -6,18 +6,18 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
{
private TimeSpanType _standardNetworkClockSufficientAccuracy;
private static StandardNetworkSystemClockCore instance;
private static StandardNetworkSystemClockCore _instance;
public static StandardNetworkSystemClockCore Instance
{
get
{
if (instance == null)
if (_instance == null)
{
instance = new StandardNetworkSystemClockCore(StandardSteadyClockCore.Instance);
_instance = new StandardNetworkSystemClockCore(StandardSteadyClockCore.Instance);
}
return instance;
return _instance;
}
}

View file

@ -11,18 +11,18 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
private TimeSpanType _testOffset;
private TimeSpanType _internalOffset;
private static StandardSteadyClockCore instance;
private static StandardSteadyClockCore _instance;
public static StandardSteadyClockCore Instance
{
get
{
if (instance == null)
if (_instance == null)
{
instance = new StandardSteadyClockCore();
_instance = new StandardSteadyClockCore();
}
return instance;
return _instance;
}
}

View file

@ -8,18 +8,18 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
private StandardNetworkSystemClockCore _networkSystemClockCore;
private bool _autoCorrectionEnabled;
private static StandardUserSystemClockCore instance;
private static StandardUserSystemClockCore _instance;
public static StandardUserSystemClockCore Instance
{
get
{
if (instance == null)
if (_instance == null)
{
instance = new StandardUserSystemClockCore(StandardLocalSystemClockCore.Instance, StandardNetworkSystemClockCore.Instance);
_instance = new StandardUserSystemClockCore(StandardLocalSystemClockCore.Instance, StandardNetworkSystemClockCore.Instance);
}
return instance;
return _instance;
}
}

View file

@ -4,8 +4,8 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
{
abstract class SystemClockCore
{
private SteadyClockCore _steadyClockCore;
private SystemClockContext _context;
private SteadyClockCore _steadyClockCore;
private SystemClockContext _context;
public SystemClockCore(SteadyClockCore steadyClockCore)
{

View file

@ -5,18 +5,18 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
{
class TickBasedSteadyClockCore : SteadyClockCore
{
private static TickBasedSteadyClockCore instance;
private static TickBasedSteadyClockCore _instance;
public static TickBasedSteadyClockCore Instance
{
get
{
if (instance == null)
if (_instance == null)
{
instance = new TickBasedSteadyClockCore();
_instance = new TickBasedSteadyClockCore();
}
return instance;
return _instance;
}
}
@ -26,7 +26,7 @@ namespace Ryujinx.HLE.HOS.Services.Time.Clock
{
SteadyClockTimePoint result = new SteadyClockTimePoint
{
TimePoint = 0,
TimePoint = 0,
ClockSourceId = GetClockSourceId()
};

View file

@ -5,6 +5,9 @@ using Ryujinx.HLE.HOS.Kernel.Threading;
using Ryujinx.HLE.HOS.Services.Time.Clock;
using Ryujinx.HLE.HOS.Services.Time.TimeZone;
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
namespace Ryujinx.HLE.HOS.Services.Time
{
@ -131,8 +134,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
// CalculateMonotonicSystemClockBaseTimePoint(nn::time::SystemClockContext) -> s64
public ResultCode CalculateMonotonicSystemClockBaseTimePoint(ServiceCtx context)
{
SystemClockContext otherContext = context.RequestData.ReadStruct<SystemClockContext>();
SystemClockContext otherContext = context.RequestData.ReadStruct<SystemClockContext>();
SteadyClockTimePoint currentTimePoint = StandardSteadyClockCore.Instance.GetCurrentTimePoint(context.Thread);
ResultCode result = ResultCode.TimeMismatch;
@ -140,8 +142,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
if (currentTimePoint.ClockSourceId == otherContext.SteadyTimePoint.ClockSourceId)
{
TimeSpanType ticksTimeSpan = TimeSpanType.FromTicks(context.Thread.Context.ThreadState.CntpctEl0, context.Thread.Context.ThreadState.CntfrqEl0);
long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds();
long baseTimePoint = otherContext.Offset + currentTimePoint.TimePoint - ticksTimeSpan.ToSeconds();
context.ResponseData.Write(baseTimePoint);
@ -157,9 +158,6 @@ namespace Ryujinx.HLE.HOS.Services.Time
{
byte type = context.RequestData.ReadByte();
long bufferPosition = context.Request.RecvListBuff[0].Position;
long bufferSize = context.Request.RecvListBuff[0].Size;
ResultCode result = StandardUserSystemClockCore.Instance.GetSystemClockContext(context.Thread, out SystemClockContext userContext);
if (result == ResultCode.Success)
@ -172,7 +170,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
if (result == ResultCode.Success)
{
context.Memory.WriteStruct(bufferPosition, clockSnapshot);
WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
}
}
}
@ -185,18 +183,17 @@ namespace Ryujinx.HLE.HOS.Services.Time
public ResultCode GetClockSnapshotFromSystemClockContext(ServiceCtx context)
{
byte type = context.RequestData.ReadByte();
context.RequestData.BaseStream.Position += 7;
SystemClockContext userContext = context.RequestData.ReadStruct<SystemClockContext>();
SystemClockContext networkContext = context.RequestData.ReadStruct<SystemClockContext>();
long bufferPosition = context.Request.RecvListBuff[0].Position;
long bufferSize = context.Request.RecvListBuff[0].Size;
ResultCode result = GetClockSnapshotFromSystemClockContextInternal(context.Thread, userContext, networkContext, type, out ClockSnapshot clockSnapshot);
if (result == ResultCode.Success)
{
context.Memory.WriteStruct(bufferPosition, clockSnapshot);
WriteClockSnapshotFromBuffer(context, context.Request.RecvListBuff[0], clockSnapshot);
}
return result;
@ -206,8 +203,9 @@ namespace Ryujinx.HLE.HOS.Services.Time
// CalculateStandardUserSystemClockDifferenceByUser(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
public ResultCode CalculateStandardUserSystemClockDifferenceByUser(ServiceCtx context)
{
ClockSnapshot clockSnapshotA = context.Memory.ReadStruct<ClockSnapshot>(context.Request.ExchangeBuff[0].Position);
ClockSnapshot clockSnapshotB = context.Memory.ReadStruct<ClockSnapshot>(context.Request.ExchangeBuff[1].Position);
ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[0]);
ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[1]);
TimeSpanType difference = TimeSpanType.FromSeconds(clockSnapshotB.UserContext.Offset - clockSnapshotA.UserContext.Offset);
if (clockSnapshotB.UserContext.SteadyTimePoint.ClockSourceId != clockSnapshotA.UserContext.SteadyTimePoint.ClockSourceId || (clockSnapshotB.IsAutomaticCorrectionEnabled && clockSnapshotA.IsAutomaticCorrectionEnabled))
@ -224,8 +222,8 @@ namespace Ryujinx.HLE.HOS.Services.Time
// CalculateSpanBetween(buffer<nn::time::sf::ClockSnapshot, 0x19>, buffer<nn::time::sf::ClockSnapshot, 0x19>) -> nn::TimeSpanType
public ResultCode CalculateSpanBetween(ServiceCtx context)
{
ClockSnapshot clockSnapshotA = context.Memory.ReadStruct<ClockSnapshot>(context.Request.ExchangeBuff[0].Position);
ClockSnapshot clockSnapshotB = context.Memory.ReadStruct<ClockSnapshot>(context.Request.ExchangeBuff[1].Position);
ClockSnapshot clockSnapshotA = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[0]);
ClockSnapshot clockSnapshotB = ReadClockSnapshotFromBuffer(context, context.Request.ExchangeBuff[1]);
TimeSpanType result;
@ -259,8 +257,7 @@ namespace Ryujinx.HLE.HOS.Services.Time
{
clockSnapshot = new ClockSnapshot();
SteadyClockCore steadyClockCore = StandardSteadyClockCore.Instance;
SteadyClockCore steadyClockCore = StandardSteadyClockCore.Instance;
SteadyClockTimePoint currentTimePoint = steadyClockCore.GetCurrentTimePoint(thread);
clockSnapshot.IsAutomaticCorrectionEnabled = StandardUserSystemClockCore.Instance.IsAutomaticCorrectionEnabled();
@ -306,5 +303,30 @@ namespace Ryujinx.HLE.HOS.Services.Time
return result;
}
private ClockSnapshot ReadClockSnapshotFromBuffer(ServiceCtx context, IpcBuffDesc ipcDesc)
{
Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>());
using (BinaryReader bufferReader = new BinaryReader(new MemoryStream(context.Memory.ReadBytes(ipcDesc.Position, ipcDesc.Size))))
{
return bufferReader.ReadStruct<ClockSnapshot>();
}
}
private void WriteClockSnapshotFromBuffer(ServiceCtx context, IpcRecvListBuffDesc ipcDesc, ClockSnapshot clockSnapshot)
{
Debug.Assert(ipcDesc.Size == Marshal.SizeOf<ClockSnapshot>());
MemoryStream memory = new MemoryStream((int)ipcDesc.Size);
using (BinaryWriter bufferWriter = new BinaryWriter(memory))
{
bufferWriter.WriteStruct(clockSnapshot);
}
context.Memory.WriteBytes(ipcDesc.Position, memory.ToArray());
memory.Dispose();
}
}
}