A32InstInterpretHelper.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using ChocolArm64.Decoders;
  2. using ChocolArm64.State;
  3. using System;
  4. namespace ChocolArm64.Instructions32
  5. {
  6. static class A32InstInterpretHelper
  7. {
  8. public static bool IsConditionTrue(CpuThreadState state, Cond cond)
  9. {
  10. switch (cond)
  11. {
  12. case Cond.Eq: return state.Zero;
  13. case Cond.Ne: return !state.Zero;
  14. case Cond.GeUn: return state.Carry;
  15. case Cond.LtUn: return !state.Carry;
  16. case Cond.Mi: return state.Negative;
  17. case Cond.Pl: return !state.Negative;
  18. case Cond.Vs: return state.Overflow;
  19. case Cond.Vc: return !state.Overflow;
  20. case Cond.GtUn: return state.Carry && !state.Zero;
  21. case Cond.LeUn: return !state.Carry && state.Zero;
  22. case Cond.Ge: return state.Negative == state.Overflow;
  23. case Cond.Lt: return state.Negative != state.Overflow;
  24. case Cond.Gt: return state.Negative == state.Overflow && !state.Zero;
  25. case Cond.Le: return state.Negative != state.Overflow && state.Zero;
  26. }
  27. return true;
  28. }
  29. public unsafe static uint GetReg(CpuThreadState 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(CpuThreadState 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(CpuThreadState 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. }