ExecutionContext.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 long TpidrEl0 { get; set; }
  30. public long Tpidr { get; set; }
  31. public FPCR Fpcr { get; set; }
  32. public FPSR Fpsr { get; set; }
  33. public FPCR StandardFpcrValue => (Fpcr & (FPCR.Ahp)) | FPCR.Dn | FPCR.Fz;
  34. public bool IsAarch32 { get; set; }
  35. internal ExecutionMode ExecutionMode
  36. {
  37. get
  38. {
  39. if (IsAarch32)
  40. {
  41. return GetPstateFlag(PState.TFlag)
  42. ? ExecutionMode.Aarch32Thumb
  43. : ExecutionMode.Aarch32Arm;
  44. }
  45. else
  46. {
  47. return ExecutionMode.Aarch64;
  48. }
  49. }
  50. }
  51. internal bool Running { get; private set; }
  52. public event EventHandler<EventArgs> Interrupt;
  53. public event EventHandler<InstExceptionEventArgs> Break;
  54. public event EventHandler<InstExceptionEventArgs> SupervisorCall;
  55. public event EventHandler<InstUndefinedEventArgs> Undefined;
  56. static ExecutionContext()
  57. {
  58. _hostTickFreq = 1.0 / Stopwatch.Frequency;
  59. _tickCounter = new Stopwatch();
  60. _tickCounter.Start();
  61. }
  62. public ExecutionContext(IJitMemoryAllocator allocator)
  63. {
  64. _nativeContext = new NativeContext(allocator);
  65. Running = true;
  66. _nativeContext.SetCounter(MinCountForCheck);
  67. }
  68. public ulong GetX(int index) => _nativeContext.GetX(index);
  69. public void SetX(int index, ulong value) => _nativeContext.SetX(index, value);
  70. public V128 GetV(int index) => _nativeContext.GetV(index);
  71. public void SetV(int index, V128 value) => _nativeContext.SetV(index, value);
  72. public bool GetPstateFlag(PState flag) => _nativeContext.GetPstateFlag(flag);
  73. public void SetPstateFlag(PState flag, bool value) => _nativeContext.SetPstateFlag(flag, value);
  74. public bool GetFPstateFlag(FPState flag) => _nativeContext.GetFPStateFlag(flag);
  75. public void SetFPstateFlag(FPState flag, bool value) => _nativeContext.SetFPStateFlag(flag, value);
  76. internal void CheckInterrupt()
  77. {
  78. if (_interrupted)
  79. {
  80. _interrupted = false;
  81. Interrupt?.Invoke(this, EventArgs.Empty);
  82. }
  83. _nativeContext.SetCounter(MinCountForCheck);
  84. }
  85. public void RequestInterrupt()
  86. {
  87. _interrupted = true;
  88. }
  89. internal void OnBreak(ulong address, int imm)
  90. {
  91. Break?.Invoke(this, new InstExceptionEventArgs(address, imm));
  92. }
  93. internal void OnSupervisorCall(ulong address, int imm)
  94. {
  95. SupervisorCall?.Invoke(this, new InstExceptionEventArgs(address, imm));
  96. }
  97. internal void OnUndefined(ulong address, int opCode)
  98. {
  99. Undefined?.Invoke(this, new InstUndefinedEventArgs(address, opCode));
  100. }
  101. public void StopRunning()
  102. {
  103. Running = false;
  104. _nativeContext.SetCounter(0);
  105. }
  106. public void Dispose()
  107. {
  108. _nativeContext.Dispose();
  109. }
  110. }
  111. }