ExecutionContext.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using ARMeilleure.Memory;
  2. using System;
  3. namespace ARMeilleure.State
  4. {
  5. public class ExecutionContext
  6. {
  7. private const int MinCountForCheck = 4000;
  8. private NativeContext _nativeContext;
  9. internal IntPtr NativeContextPtr => _nativeContext.BasePtr;
  10. private bool _interrupted;
  11. private readonly ICounter _counter;
  12. public ulong Pc => _nativeContext.GetPc();
  13. public uint CtrEl0 => 0x8444c004;
  14. public uint DczidEl0 => 0x00000004;
  15. public ulong CntfrqEl0 => _counter.Frequency;
  16. public ulong CntpctEl0 => _counter.Counter;
  17. // CNTVCT_EL0 = CNTPCT_EL0 - CNTVOFF_EL2
  18. // Since EL2 isn't implemented, CNTVOFF_EL2 = 0
  19. public ulong CntvctEl0 => CntpctEl0;
  20. public long TpidrEl0 { get; set; }
  21. public long TpidrroEl0 { get; set; }
  22. public uint Pstate
  23. {
  24. get => _nativeContext.GetPstate();
  25. set => _nativeContext.SetPstate(value);
  26. }
  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. public bool Running
  48. {
  49. get => _nativeContext.GetRunning();
  50. private set => _nativeContext.SetRunning(value);
  51. }
  52. private readonly ExceptionCallbackNoArgs _interruptCallback;
  53. private readonly ExceptionCallback _breakCallback;
  54. private readonly ExceptionCallback _supervisorCallback;
  55. private readonly ExceptionCallback _undefinedCallback;
  56. public ExecutionContext(
  57. IJitMemoryAllocator allocator,
  58. ICounter counter,
  59. ExceptionCallbackNoArgs interruptCallback = null,
  60. ExceptionCallback breakCallback = null,
  61. ExceptionCallback supervisorCallback = null,
  62. ExceptionCallback undefinedCallback = null)
  63. {
  64. _nativeContext = new NativeContext(allocator);
  65. _counter = counter;
  66. _interruptCallback = interruptCallback;
  67. _breakCallback = breakCallback;
  68. _supervisorCallback = supervisorCallback;
  69. _undefinedCallback = undefinedCallback;
  70. Running = true;
  71. _nativeContext.SetCounter(MinCountForCheck);
  72. }
  73. public ulong GetX(int index) => _nativeContext.GetX(index);
  74. public void SetX(int index, ulong value) => _nativeContext.SetX(index, value);
  75. public V128 GetV(int index) => _nativeContext.GetV(index);
  76. public void SetV(int index, V128 value) => _nativeContext.SetV(index, value);
  77. public bool GetPstateFlag(PState flag) => _nativeContext.GetPstateFlag(flag);
  78. public void SetPstateFlag(PState flag, bool value) => _nativeContext.SetPstateFlag(flag, value);
  79. public bool GetFPstateFlag(FPState flag) => _nativeContext.GetFPStateFlag(flag);
  80. public void SetFPstateFlag(FPState flag, bool value) => _nativeContext.SetFPStateFlag(flag, value);
  81. internal void CheckInterrupt()
  82. {
  83. if (_interrupted)
  84. {
  85. _interrupted = false;
  86. _interruptCallback?.Invoke(this);
  87. }
  88. _nativeContext.SetCounter(MinCountForCheck);
  89. }
  90. public void RequestInterrupt()
  91. {
  92. _interrupted = true;
  93. }
  94. internal void OnBreak(ulong address, int imm)
  95. {
  96. _breakCallback?.Invoke(this, address, imm);
  97. }
  98. internal void OnSupervisorCall(ulong address, int imm)
  99. {
  100. _supervisorCallback?.Invoke(this, address, imm);
  101. }
  102. internal void OnUndefined(ulong address, int opCode)
  103. {
  104. _undefinedCallback?.Invoke(this, address, opCode);
  105. }
  106. public void StopRunning()
  107. {
  108. Running = false;
  109. _nativeContext.SetCounter(0);
  110. }
  111. public void Dispose()
  112. {
  113. _nativeContext.Dispose();
  114. }
  115. }
  116. }