ExecutionContext.cs 3.9 KB

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