ExecutionContext.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. internal bool Running { get; private set; }
  54. public event EventHandler<EventArgs> Interrupt;
  55. public event EventHandler<InstExceptionEventArgs> Break;
  56. public event EventHandler<InstExceptionEventArgs> SupervisorCall;
  57. public event EventHandler<InstUndefinedEventArgs> Undefined;
  58. static ExecutionContext()
  59. {
  60. _hostTickFreq = 1.0 / Stopwatch.Frequency;
  61. _tickCounter = new Stopwatch();
  62. _tickCounter.Start();
  63. }
  64. public ExecutionContext(IJitMemoryAllocator allocator)
  65. {
  66. _nativeContext = new NativeContext(allocator);
  67. Running = true;
  68. _nativeContext.SetCounter(MinCountForCheck);
  69. }
  70. public ulong GetX(int index) => _nativeContext.GetX(index);
  71. public void SetX(int index, ulong value) => _nativeContext.SetX(index, value);
  72. public V128 GetV(int index) => _nativeContext.GetV(index);
  73. public void SetV(int index, V128 value) => _nativeContext.SetV(index, value);
  74. public bool GetPstateFlag(PState flag) => _nativeContext.GetPstateFlag(flag);
  75. public void SetPstateFlag(PState flag, bool value) => _nativeContext.SetPstateFlag(flag, value);
  76. public bool GetFPstateFlag(FPState flag) => _nativeContext.GetFPStateFlag(flag);
  77. public void SetFPstateFlag(FPState flag, bool value) => _nativeContext.SetFPStateFlag(flag, value);
  78. internal void CheckInterrupt()
  79. {
  80. if (_interrupted)
  81. {
  82. _interrupted = false;
  83. Interrupt?.Invoke(this, EventArgs.Empty);
  84. }
  85. _nativeContext.SetCounter(MinCountForCheck);
  86. }
  87. public void RequestInterrupt()
  88. {
  89. _interrupted = true;
  90. }
  91. internal void OnBreak(ulong address, int imm)
  92. {
  93. Break?.Invoke(this, new InstExceptionEventArgs(address, imm));
  94. }
  95. internal void OnSupervisorCall(ulong address, int imm)
  96. {
  97. SupervisorCall?.Invoke(this, new InstExceptionEventArgs(address, imm));
  98. }
  99. internal void OnUndefined(ulong address, int opCode)
  100. {
  101. Undefined?.Invoke(this, new InstUndefinedEventArgs(address, opCode));
  102. }
  103. public void StopRunning()
  104. {
  105. Running = false;
  106. _nativeContext.SetCounter(0);
  107. }
  108. public void Dispose()
  109. {
  110. _nativeContext.Dispose();
  111. }
  112. }
  113. }