ExecutionContext.cs 4.1 KB

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