Refactor CPU interface to allow the implementation of other CPU emulators (#3362)
* Refactor CPU interface * Use IExecutionContext interface on SVC handler, change how CPU interrupts invokes the handlers * Make CpuEngine take a ITickSource rather than returning one The previous implementation had the scenario where the CPU engine had to implement the tick source in mind, like for example, when we have a hypervisor and the game can read CNTPCT on the host directly. However given that we need to do conversion due to different frequencies anyway, it's not worth it. It's better to just let the user pass the tick source and redirect any reads to CNTPCT to the user tick source * XML docs for the public interfaces * PPTC invalidation due to NativeInterface function name changes * Fix build of the CPU tests * PR feedback
This commit is contained in:
parent
9827dc35e1
commit
0c87bf9ea4
64 changed files with 877 additions and 322 deletions
|
@ -1,37 +0,0 @@
|
|||
using ARMeilleure.Memory;
|
||||
using ARMeilleure.State;
|
||||
using ARMeilleure.Translation;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
{
|
||||
public class CpuContext
|
||||
{
|
||||
private readonly Translator _translator;
|
||||
|
||||
public CpuContext(IMemoryManager memory, bool for64Bit)
|
||||
{
|
||||
_translator = new Translator(new JitMemoryAllocator(), memory, for64Bit);
|
||||
memory.UnmapEvent += UnmapHandler;
|
||||
}
|
||||
|
||||
private void UnmapHandler(ulong address, ulong size)
|
||||
{
|
||||
_translator.InvalidateJitCacheRegion(address, size);
|
||||
}
|
||||
|
||||
public static ExecutionContext CreateExecutionContext()
|
||||
{
|
||||
return new ExecutionContext(new JitMemoryAllocator());
|
||||
}
|
||||
|
||||
public void Execute(ExecutionContext context, ulong address)
|
||||
{
|
||||
_translator.Execute(context, address);
|
||||
}
|
||||
|
||||
public void InvalidateCacheRegion(ulong address, ulong size)
|
||||
{
|
||||
_translator.InvalidateJitCacheRegion(address, size);
|
||||
}
|
||||
}
|
||||
}
|
64
Ryujinx.Cpu/ExceptionCallbacks.cs
Normal file
64
Ryujinx.Cpu/ExceptionCallbacks.cs
Normal file
|
@ -0,0 +1,64 @@
|
|||
namespace Ryujinx.Cpu
|
||||
{
|
||||
/// <summary>
|
||||
/// Exception callback without any additional arguments.
|
||||
/// </summary>
|
||||
/// <param name="context">Context for the thread where the exception was triggered</param>
|
||||
public delegate void ExceptionCallbackNoArgs(IExecutionContext context);
|
||||
|
||||
/// <summary>
|
||||
/// Exception callback.
|
||||
/// </summary>
|
||||
/// <param name="context">Context for the thread where the exception was triggered</param>
|
||||
/// <param name="address">Address of the instruction that caused the exception</param>
|
||||
/// <param name="imm">Immediate value of the instruction that caused the exception, or for undefined instruction, the instruction itself</param>
|
||||
public delegate void ExceptionCallback(IExecutionContext context, ulong address, int imm);
|
||||
|
||||
/// <summary>
|
||||
/// Stores handlers for the various CPU exceptions.
|
||||
/// </summary>
|
||||
public struct ExceptionCallbacks
|
||||
{
|
||||
/// <summary>
|
||||
/// Handler for CPU interrupts triggered using <see cref="IExecutionContext.RequestInterrupt"/>.
|
||||
/// </summary>
|
||||
public readonly ExceptionCallbackNoArgs InterruptCallback;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for CPU software interrupts caused by the Arm BRK instruction.
|
||||
/// </summary>
|
||||
public readonly ExceptionCallback BreakCallback;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for CPU software interrupts caused by the Arm SVC instruction.
|
||||
/// </summary>
|
||||
public readonly ExceptionCallback SupervisorCallback;
|
||||
|
||||
/// <summary>
|
||||
/// Handler for CPU software interrupts caused by any undefined Arm instruction.
|
||||
/// </summary>
|
||||
public readonly ExceptionCallback UndefinedCallback;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new exception callbacks structure.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// All handlers are optional, and if null, the CPU will just continue executing as if nothing happened.
|
||||
/// </remarks>
|
||||
/// <param name="interruptCallback">Handler for CPU interrupts triggered using <see cref="IExecutionContext.RequestInterrupt"/></param>
|
||||
/// <param name="breakCallback">Handler for CPU software interrupts caused by the Arm BRK instruction</param>
|
||||
/// <param name="supervisorCallback">Handler for CPU software interrupts caused by the Arm SVC instruction</param>
|
||||
/// <param name="undefinedCallback">Handler for CPU software interrupts caused by any undefined Arm instruction</param>
|
||||
public ExceptionCallbacks(
|
||||
ExceptionCallbackNoArgs interruptCallback = null,
|
||||
ExceptionCallback breakCallback = null,
|
||||
ExceptionCallback supervisorCallback = null,
|
||||
ExceptionCallback undefinedCallback = null)
|
||||
{
|
||||
InterruptCallback = interruptCallback;
|
||||
BreakCallback = breakCallback;
|
||||
SupervisorCallback = supervisorCallback;
|
||||
UndefinedCallback = undefinedCallback;
|
||||
}
|
||||
}
|
||||
}
|
39
Ryujinx.Cpu/ICpuContext.cs
Normal file
39
Ryujinx.Cpu/ICpuContext.cs
Normal file
|
@ -0,0 +1,39 @@
|
|||
namespace Ryujinx.Cpu
|
||||
{
|
||||
/// <summary>
|
||||
/// CPU context interface.
|
||||
/// </summary>
|
||||
public interface ICpuContext
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new execution context that will store thread CPU register state when executing guest code.
|
||||
/// </summary>
|
||||
/// <param name="exceptionCallbacks">Optional functions to be called when the CPU receives an interrupt</param>
|
||||
/// <returns>Execution context</returns>
|
||||
IExecutionContext CreateExecutionContext(ExceptionCallbacks exceptionCallbacks);
|
||||
|
||||
/// <summary>
|
||||
/// Starts executing code at a specified entry point address.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This function only returns when the execution is stopped, by calling <see cref="IExecutionContext.StopRunning"/>.
|
||||
/// </remarks>
|
||||
/// <param name="context">Execution context to be used for this run</param>
|
||||
/// <param name="address">Entry point address</param>
|
||||
void Execute(IExecutionContext context, ulong address);
|
||||
|
||||
/// <summary>
|
||||
/// Invalidates the instruction cache for a given memory region.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This should be called if code is modified to make the CPU emulator aware of the modifications,
|
||||
/// otherwise it might run stale code which will lead to errors and crashes.
|
||||
/// Calling this function is not necessary if the code memory was modified by guest code,
|
||||
/// as the expectation is that it will do it on its own using the appropriate cache invalidation instructions,
|
||||
/// except on Arm32 where those instructions can't be used in unprivileged mode.
|
||||
/// </remarks>
|
||||
/// <param name="address">Address of the region to be invalidated</param>
|
||||
/// <param name="size">Size of the region to be invalidated</param>
|
||||
void InvalidateCacheRegion(ulong address, ulong size);
|
||||
}
|
||||
}
|
18
Ryujinx.Cpu/ICpuEngine.cs
Normal file
18
Ryujinx.Cpu/ICpuEngine.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using ARMeilleure.Memory;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
{
|
||||
/// <summary>
|
||||
/// CPU execution engine interface.
|
||||
/// </summary>
|
||||
public interface ICpuEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new CPU context that can be used to run code for multiple threads sharing an address space.
|
||||
/// </summary>
|
||||
/// <param name="memoryManager">Memory manager for the address space of the context</param>
|
||||
/// <param name="for64Bit">Indicates if the context will be used to run 64-bit or 32-bit Arm code</param>
|
||||
/// <returns>CPU context</returns>
|
||||
ICpuContext CreateCpuContext(IMemoryManager memoryManager, bool for64Bit);
|
||||
}
|
||||
}
|
112
Ryujinx.Cpu/IExecutionContext.cs
Normal file
112
Ryujinx.Cpu/IExecutionContext.cs
Normal file
|
@ -0,0 +1,112 @@
|
|||
using ARMeilleure.State;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
{
|
||||
/// <summary>
|
||||
/// CPU register state interface.
|
||||
/// </summary>
|
||||
public interface IExecutionContext : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// Current Program Counter.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// In some implementations, this value might not be accurate and might not point to the last instruction executed.
|
||||
/// </remarks>
|
||||
ulong Pc { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Thread ID Register (EL0).
|
||||
/// </summary>
|
||||
long TpidrEl0 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Thread ID Register (read-only) (EL0).
|
||||
/// </summary>
|
||||
long TpidrroEl0 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Processor State register.
|
||||
/// </summary>
|
||||
uint Pstate { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Floating-point Control Register.
|
||||
/// </summary>
|
||||
uint Fpcr { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Floating-point Status Register.
|
||||
/// </summary>
|
||||
uint Fpsr { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whenever the CPU is running 64-bit (AArch64 mode) or 32-bit (AArch32 mode) code.
|
||||
/// </summary>
|
||||
bool IsAarch32 { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whenever the CPU is still running code.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Even if this is false, the guest code might be still exiting.
|
||||
/// One must not assume that the code is no longer running from this property alone.
|
||||
/// </remarks>
|
||||
bool Running { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of a general purpose register.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The special <paramref name="index"/> of 31 can be used to access the SP (Stack Pointer) register.
|
||||
/// </remarks>
|
||||
/// <param name="index">Index of the register, in the range 0-31 (inclusive)</param>
|
||||
/// <returns>The register value</returns>
|
||||
ulong GetX(int index);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of a general purpose register.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The special <paramref name="index"/> of 31 can be used to access the SP (Stack Pointer) register.
|
||||
/// </remarks>
|
||||
/// <param name="index">Index of the register, in the range 0-31 (inclusive)</param>
|
||||
/// <param name="value">Value to be set</param>
|
||||
void SetX(int index, ulong value);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of a FP/SIMD register.
|
||||
/// </summary>
|
||||
/// <param name="index">Index of the register, in the range 0-31 (inclusive)</param>
|
||||
/// <returns>The register value</returns>
|
||||
V128 GetV(int index);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of a FP/SIMD register.
|
||||
/// </summary>
|
||||
/// <param name="index">Index of the register, in the range 0-31 (inclusive)</param>
|
||||
/// <param name="value">Value to be set</param>
|
||||
void SetV(int index, V128 value);
|
||||
|
||||
/// <summary>
|
||||
/// Requests the thread to stop running temporarily and call <see cref="ExceptionCallbacks.InterruptCallback"/>.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The thread might not pause immediately.
|
||||
/// One must not assume that guest code is no longer being executed by the thread after calling this function.
|
||||
/// </remarks>
|
||||
void RequestInterrupt();
|
||||
|
||||
/// <summary>
|
||||
/// Requests the thread to stop running guest code and return as soon as possible.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The thread might not stop immediately.
|
||||
/// One must not assume that guest code is no longer being executed by the thread after calling this function.
|
||||
/// After a thread has been stopped, it can't be restarted with the same <see cref="IExecutionContext"/>.
|
||||
/// If you only need to pause the thread temporarily, use <see cref="RequestInterrupt"/> instead.
|
||||
/// </remarks>
|
||||
void StopRunning();
|
||||
}
|
||||
}
|
31
Ryujinx.Cpu/ITickSource.cs
Normal file
31
Ryujinx.Cpu/ITickSource.cs
Normal file
|
@ -0,0 +1,31 @@
|
|||
using ARMeilleure.State;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
{
|
||||
/// <summary>
|
||||
/// Tick source interface.
|
||||
/// </summary>
|
||||
public interface ITickSource : ICounter
|
||||
{
|
||||
/// <summary>
|
||||
/// Time elapsed since the counter was created.
|
||||
/// </summary>
|
||||
TimeSpan ElapsedTime { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Time elapsed since the counter was created, in seconds.
|
||||
/// </summary>
|
||||
double ElapsedSeconds { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Stops counting.
|
||||
/// </summary>
|
||||
void Suspend();
|
||||
|
||||
/// <summary>
|
||||
/// Resumes counting after a call to <see cref="Suspend"/>.
|
||||
/// </summary>
|
||||
void Resume();
|
||||
}
|
||||
}
|
41
Ryujinx.Cpu/Jit/JitCpuContext.cs
Normal file
41
Ryujinx.Cpu/Jit/JitCpuContext.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using ARMeilleure.Memory;
|
||||
using ARMeilleure.Translation;
|
||||
|
||||
namespace Ryujinx.Cpu.Jit
|
||||
{
|
||||
class JitCpuContext : ICpuContext
|
||||
{
|
||||
private readonly ITickSource _tickSource;
|
||||
private readonly Translator _translator;
|
||||
|
||||
public JitCpuContext(ITickSource tickSource, IMemoryManager memory, bool for64Bit)
|
||||
{
|
||||
_tickSource = tickSource;
|
||||
_translator = new Translator(new JitMemoryAllocator(), memory, for64Bit);
|
||||
memory.UnmapEvent += UnmapHandler;
|
||||
}
|
||||
|
||||
private void UnmapHandler(ulong address, ulong size)
|
||||
{
|
||||
_translator.InvalidateJitCacheRegion(address, size);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public IExecutionContext CreateExecutionContext(ExceptionCallbacks exceptionCallbacks)
|
||||
{
|
||||
return new JitExecutionContext(new JitMemoryAllocator(), _tickSource, exceptionCallbacks);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Execute(IExecutionContext context, ulong address)
|
||||
{
|
||||
_translator.Execute(((JitExecutionContext)context).Impl, address);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void InvalidateCacheRegion(ulong address, ulong size)
|
||||
{
|
||||
_translator.InvalidateJitCacheRegion(address, size);
|
||||
}
|
||||
}
|
||||
}
|
20
Ryujinx.Cpu/Jit/JitEngine.cs
Normal file
20
Ryujinx.Cpu/Jit/JitEngine.cs
Normal file
|
@ -0,0 +1,20 @@
|
|||
using ARMeilleure.Memory;
|
||||
|
||||
namespace Ryujinx.Cpu.Jit
|
||||
{
|
||||
public class JitEngine : ICpuEngine
|
||||
{
|
||||
private readonly ITickSource _tickSource;
|
||||
|
||||
public JitEngine(ITickSource tickSource)
|
||||
{
|
||||
_tickSource = tickSource;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ICpuContext CreateCpuContext(IMemoryManager memoryManager, bool for64Bit)
|
||||
{
|
||||
return new JitCpuContext(_tickSource, memoryManager, for64Bit);
|
||||
}
|
||||
}
|
||||
}
|
123
Ryujinx.Cpu/Jit/JitExecutionContext.cs
Normal file
123
Ryujinx.Cpu/Jit/JitExecutionContext.cs
Normal file
|
@ -0,0 +1,123 @@
|
|||
using ARMeilleure.Memory;
|
||||
using ARMeilleure.State;
|
||||
|
||||
namespace Ryujinx.Cpu.Jit
|
||||
{
|
||||
class JitExecutionContext : IExecutionContext
|
||||
{
|
||||
private readonly ExecutionContext _impl;
|
||||
internal ExecutionContext Impl => _impl;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ulong Pc => _impl.Pc;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public long TpidrEl0
|
||||
{
|
||||
get => _impl.TpidrEl0;
|
||||
set => _impl.TpidrEl0 = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public long TpidrroEl0
|
||||
{
|
||||
get => _impl.TpidrroEl0;
|
||||
set => _impl.TpidrroEl0 = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public uint Pstate
|
||||
{
|
||||
get => _impl.Pstate;
|
||||
set => _impl.Pstate = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public uint Fpcr
|
||||
{
|
||||
get => (uint)_impl.Fpcr;
|
||||
set => _impl.Fpcr = (FPCR)value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public uint Fpsr
|
||||
{
|
||||
get => (uint)_impl.Fpsr;
|
||||
set => _impl.Fpsr = (FPSR)value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool IsAarch32
|
||||
{
|
||||
get => _impl.IsAarch32;
|
||||
set => _impl.IsAarch32 = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public bool Running => _impl.Running;
|
||||
|
||||
private readonly ExceptionCallbacks _exceptionCallbacks;
|
||||
|
||||
public JitExecutionContext(IJitMemoryAllocator allocator, ICounter counter, ExceptionCallbacks exceptionCallbacks)
|
||||
{
|
||||
_impl = new ExecutionContext(
|
||||
allocator,
|
||||
counter,
|
||||
InterruptHandler,
|
||||
BreakHandler,
|
||||
SupervisorCallHandler,
|
||||
UndefinedHandler);
|
||||
|
||||
_exceptionCallbacks = exceptionCallbacks;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ulong GetX(int index) => _impl.GetX(index);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void SetX(int index, ulong value) => _impl.SetX(index, value);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public V128 GetV(int index) => _impl.GetV(index);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void SetV(int index, V128 value) => _impl.SetV(index, value);
|
||||
|
||||
private void InterruptHandler(ExecutionContext context)
|
||||
{
|
||||
_exceptionCallbacks.InterruptCallback?.Invoke(this);
|
||||
}
|
||||
|
||||
private void BreakHandler(ExecutionContext context, ulong address, int imm)
|
||||
{
|
||||
_exceptionCallbacks.BreakCallback?.Invoke(this, address, imm);
|
||||
}
|
||||
|
||||
private void SupervisorCallHandler(ExecutionContext context, ulong address, int imm)
|
||||
{
|
||||
_exceptionCallbacks.SupervisorCallback?.Invoke(this, address, imm);
|
||||
}
|
||||
|
||||
private void UndefinedHandler(ExecutionContext context, ulong address, int opCode)
|
||||
{
|
||||
_exceptionCallbacks.UndefinedCallback?.Invoke(this, address, opCode);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void RequestInterrupt()
|
||||
{
|
||||
_impl.RequestInterrupt();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void StopRunning()
|
||||
{
|
||||
_impl.StopRunning();
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_impl.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,9 +1,9 @@
|
|||
using ARMeilleure.Memory;
|
||||
using Ryujinx.Memory;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
namespace Ryujinx.Cpu.Jit
|
||||
{
|
||||
class JitMemoryAllocator : IJitMemoryAllocator
|
||||
public class JitMemoryAllocator : IJitMemoryAllocator
|
||||
{
|
||||
public IJitMemoryBlock Allocate(ulong size) => new JitMemoryBlock(size, MemoryAllocationFlags.None);
|
||||
public IJitMemoryBlock Reserve(ulong size) => new JitMemoryBlock(size, MemoryAllocationFlags.Reserve);
|
|
@ -2,9 +2,9 @@
|
|||
using Ryujinx.Memory;
|
||||
using System;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
namespace Ryujinx.Cpu.Jit
|
||||
{
|
||||
class JitMemoryBlock : IJitMemoryBlock
|
||||
public class JitMemoryBlock : IJitMemoryBlock
|
||||
{
|
||||
private readonly MemoryBlock _impl;
|
||||
|
|
@ -10,7 +10,7 @@ using System.Runtime.CompilerServices;
|
|||
using System.Runtime.InteropServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
namespace Ryujinx.Cpu.Jit
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a CPU memory manager.
|
|
@ -8,12 +8,12 @@ using System.Collections.Generic;
|
|||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
namespace Ryujinx.Cpu.Jit
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a CPU memory manager which maps guest virtual memory directly onto a host virtual region.
|
||||
/// </summary>
|
||||
public class MemoryManagerHostMapped : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
||||
public sealed class MemoryManagerHostMapped : MemoryManagerBase, IMemoryManager, IVirtualMemoryManagerTracked, IWritableBlock
|
||||
{
|
||||
public const int PageBits = 12;
|
||||
public const int PageSize = 1 << PageBits;
|
45
Ryujinx.Cpu/TickSource.cs
Normal file
45
Ryujinx.Cpu/TickSource.cs
Normal file
|
@ -0,0 +1,45 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Ryujinx.Cpu
|
||||
{
|
||||
public class TickSource : ITickSource
|
||||
{
|
||||
private static Stopwatch _tickCounter;
|
||||
|
||||
private static double _hostTickFreq;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ulong Frequency { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public ulong Counter => (ulong)(ElapsedSeconds * Frequency);
|
||||
|
||||
/// <inheritdoc/>
|
||||
public TimeSpan ElapsedTime => _tickCounter.Elapsed;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public double ElapsedSeconds => _tickCounter.ElapsedTicks * _hostTickFreq;
|
||||
|
||||
public TickSource(ulong frequency)
|
||||
{
|
||||
Frequency = frequency;
|
||||
_hostTickFreq = 1.0 / Stopwatch.Frequency;
|
||||
|
||||
_tickCounter = new Stopwatch();
|
||||
_tickCounter.Start();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Suspend()
|
||||
{
|
||||
_tickCounter.Stop();
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Resume()
|
||||
{
|
||||
_tickCounter.Start();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue