ExecutionContext.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. using ARMeilleure.Memory;
  2. using System;
  3. using System.Diagnostics;
  4. namespace ARMeilleure.State
  5. {
  6. public class ExecutionContext
  7. {
  8. private const int MinCountForCheck = 4000;
  9. private NativeContext _nativeContext;
  10. internal IntPtr NativeContextPtr => _nativeContext.BasePtr;
  11. private bool _interrupted;
  12. private static Stopwatch _tickCounter;
  13. private static double _hostTickFreq;
  14. public uint CtrEl0 => 0x8444c004;
  15. public uint DczidEl0 => 0x00000004;
  16. public ulong CntfrqEl0 { get; set; }
  17. public ulong CntpctEl0
  18. {
  19. get
  20. {
  21. double ticks = _tickCounter.ElapsedTicks * _hostTickFreq;
  22. return (ulong)(ticks * CntfrqEl0);
  23. }
  24. }
  25. // CNTVCT_EL0 = CNTPCT_EL0 - CNTVOFF_EL2
  26. // Since EL2 isn't implemented, CNTVOFF_EL2 = 0
  27. public ulong CntvctEl0 => CntpctEl0;
  28. public static TimeSpan ElapsedTime => _tickCounter.Elapsed;
  29. public static long ElapsedTicks => _tickCounter.ElapsedTicks;
  30. public static double TickFrequency => _hostTickFreq;
  31. public long TpidrEl0 { get; set; }
  32. public long Tpidr { get; set; }
  33. public FPCR Fpcr { get; set; }
  34. public FPSR Fpsr { get; set; }
  35. public FPCR StandardFpcrValue => (Fpcr & (FPCR.Ahp)) | FPCR.Dn | FPCR.Fz;
  36. public bool IsAarch32 { get; set; }
  37. internal ExecutionMode ExecutionMode
  38. {
  39. get
  40. {
  41. if (IsAarch32)
  42. {
  43. return GetPstateFlag(PState.TFlag)
  44. ? ExecutionMode.Aarch32Thumb
  45. : ExecutionMode.Aarch32Arm;
  46. }
  47. else
  48. {
  49. return ExecutionMode.Aarch64;
  50. }
  51. }
  52. }
  53. public bool Running
  54. {
  55. get => _nativeContext.GetRunning();
  56. private set => _nativeContext.SetRunning(value);
  57. }
  58. public event EventHandler<EventArgs> Interrupt;
  59. public event EventHandler<InstExceptionEventArgs> Break;
  60. public event EventHandler<InstExceptionEventArgs> SupervisorCall;
  61. public event EventHandler<InstUndefinedEventArgs> Undefined;
  62. static ExecutionContext()
  63. {
  64. _hostTickFreq = 1.0 / Stopwatch.Frequency;
  65. _tickCounter = new Stopwatch();
  66. _tickCounter.Start();
  67. }
  68. public ExecutionContext(IJitMemoryAllocator allocator)
  69. {
  70. _nativeContext = new NativeContext(allocator);
  71. Running = true;
  72. _nativeContext.SetCounter(MinCountForCheck);
  73. }
  74. public ulong GetX(int index) => _nativeContext.GetX(index);
  75. public void SetX(int index, ulong value) => _nativeContext.SetX(index, value);
  76. public V128 GetV(int index) => _nativeContext.GetV(index);
  77. public void SetV(int index, V128 value) => _nativeContext.SetV(index, value);
  78. public bool GetPstateFlag(PState flag) => _nativeContext.GetPstateFlag(flag);
  79. public void SetPstateFlag(PState flag, bool value) => _nativeContext.SetPstateFlag(flag, value);
  80. public bool GetFPstateFlag(FPState flag) => _nativeContext.GetFPStateFlag(flag);
  81. public void SetFPstateFlag(FPState flag, bool value) => _nativeContext.SetFPStateFlag(flag, value);
  82. internal void CheckInterrupt()
  83. {
  84. if (_interrupted)
  85. {
  86. _interrupted = false;
  87. Interrupt?.Invoke(this, EventArgs.Empty);
  88. }
  89. _nativeContext.SetCounter(MinCountForCheck);
  90. }
  91. public void RequestInterrupt()
  92. {
  93. _interrupted = true;
  94. }
  95. internal void OnBreak(ulong address, int imm)
  96. {
  97. Break?.Invoke(this, new InstExceptionEventArgs(address, imm));
  98. }
  99. internal void OnSupervisorCall(ulong address, int imm)
  100. {
  101. SupervisorCall?.Invoke(this, new InstExceptionEventArgs(address, imm));
  102. }
  103. internal void OnUndefined(ulong address, int opCode)
  104. {
  105. Undefined?.Invoke(this, new InstUndefinedEventArgs(address, opCode));
  106. }
  107. public void StopRunning()
  108. {
  109. Running = false;
  110. _nativeContext.SetCounter(0);
  111. }
  112. public static void SuspendCounter()
  113. {
  114. _tickCounter.Stop();
  115. }
  116. public static void ResumeCounter()
  117. {
  118. _tickCounter.Start();
  119. }
  120. public void Dispose()
  121. {
  122. _nativeContext.Dispose();
  123. }
  124. }
  125. }