ExecutionContext.cs 4.0 KB

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