A32InstInterpretHelper.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using ChocolArm64.Decoder;
  2. using ChocolArm64.State;
  3. using System;
  4. namespace ChocolArm64.Instruction32
  5. {
  6. static class A32InstInterpretHelper
  7. {
  8. public static bool IsConditionTrue(AThreadState State, ACond Cond)
  9. {
  10. switch (Cond)
  11. {
  12. case ACond.Eq: return State.Zero;
  13. case ACond.Ne: return !State.Zero;
  14. case ACond.Ge_Un: return State.Carry;
  15. case ACond.Lt_Un: return !State.Carry;
  16. case ACond.Mi: return State.Negative;
  17. case ACond.Pl: return !State.Negative;
  18. case ACond.Vs: return State.Overflow;
  19. case ACond.Vc: return !State.Overflow;
  20. case ACond.Gt_Un: return State.Carry && !State.Zero;
  21. case ACond.Le_Un: return !State.Carry && State.Zero;
  22. case ACond.Ge: return State.Negative == State.Overflow;
  23. case ACond.Lt: return State.Negative != State.Overflow;
  24. case ACond.Gt: return State.Negative == State.Overflow && !State.Zero;
  25. case ACond.Le: return State.Negative != State.Overflow && State.Zero;
  26. }
  27. return true;
  28. }
  29. public unsafe static uint GetReg(AThreadState State, int Reg)
  30. {
  31. if ((uint)Reg > 15)
  32. {
  33. throw new ArgumentOutOfRangeException(nameof(Reg));
  34. }
  35. fixed (uint* Ptr = &State.R0)
  36. {
  37. return *(Ptr + Reg);
  38. }
  39. }
  40. public unsafe static void SetReg(AThreadState State, int Reg, uint Value)
  41. {
  42. if ((uint)Reg > 15)
  43. {
  44. throw new ArgumentOutOfRangeException(nameof(Reg));
  45. }
  46. fixed (uint* Ptr = &State.R0)
  47. {
  48. *(Ptr + Reg) = Value;
  49. }
  50. }
  51. public static uint GetPc(AThreadState State)
  52. {
  53. //Due to the old fetch-decode-execute pipeline of old ARM CPUs,
  54. //the PC is 4 or 8 bytes (2 instructions) ahead of the current instruction.
  55. return State.R15 + (State.Thumb ? 2U : 4U);
  56. }
  57. }
  58. }