ExecutionContext.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Diagnostics;
  3. namespace ARMeilleure.State
  4. {
  5. public class ExecutionContext : IExecutionContext
  6. {
  7. private const int MinCountForCheck = 40000;
  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 bool IsAarch32 { get; set; }
  29. internal ExecutionMode ExecutionMode
  30. {
  31. get
  32. {
  33. if (IsAarch32)
  34. {
  35. return GetPstateFlag(PState.TFlag)
  36. ? ExecutionMode.Aarch32Thumb
  37. : ExecutionMode.Aarch32Arm;
  38. }
  39. else
  40. {
  41. return ExecutionMode.Aarch64;
  42. }
  43. }
  44. }
  45. public bool Running { get; set; }
  46. public event EventHandler<EventArgs> Interrupt;
  47. public event EventHandler<InstExceptionEventArgs> Break;
  48. public event EventHandler<InstExceptionEventArgs> SupervisorCall;
  49. public event EventHandler<InstUndefinedEventArgs> Undefined;
  50. static ExecutionContext()
  51. {
  52. _hostTickFreq = 1.0 / Stopwatch.Frequency;
  53. _tickCounter = new Stopwatch();
  54. _tickCounter.Start();
  55. }
  56. public ExecutionContext()
  57. {
  58. _nativeContext = new NativeContext();
  59. Running = true;
  60. _nativeContext.SetCounter(MinCountForCheck);
  61. }
  62. public ulong GetX(int index) => _nativeContext.GetX(index);
  63. public void SetX(int index, ulong value) => _nativeContext.SetX(index, value);
  64. public V128 GetV(int index) => _nativeContext.GetV(index);
  65. public void SetV(int index, V128 value) => _nativeContext.SetV(index, value);
  66. public bool GetPstateFlag(PState flag) => _nativeContext.GetPstateFlag(flag);
  67. public void SetPstateFlag(PState flag, bool value) => _nativeContext.SetPstateFlag(flag, value);
  68. internal void CheckInterrupt()
  69. {
  70. if (_interrupted)
  71. {
  72. _interrupted = false;
  73. Interrupt?.Invoke(this, EventArgs.Empty);
  74. }
  75. _nativeContext.SetCounter(MinCountForCheck);
  76. }
  77. public void RequestInterrupt()
  78. {
  79. _interrupted = true;
  80. }
  81. internal void OnBreak(ulong address, int imm)
  82. {
  83. Break?.Invoke(this, new InstExceptionEventArgs(address, imm));
  84. }
  85. internal void OnSupervisorCall(ulong address, int imm)
  86. {
  87. SupervisorCall?.Invoke(this, new InstExceptionEventArgs(address, imm));
  88. }
  89. internal void OnUndefined(ulong address, int opCode)
  90. {
  91. Undefined?.Invoke(this, new InstUndefinedEventArgs(address, opCode));
  92. }
  93. public void Dispose()
  94. {
  95. _nativeContext.Dispose();
  96. }
  97. }
  98. }