InstEmitSystem.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using ARMeilleure.Decoders;
  2. using ARMeilleure.IntermediateRepresentation;
  3. using ARMeilleure.State;
  4. using ARMeilleure.Translation;
  5. using System;
  6. using System.Reflection;
  7. using static ARMeilleure.Instructions.InstEmitHelper;
  8. using static ARMeilleure.IntermediateRepresentation.Operand.Factory;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static partial class InstEmit
  12. {
  13. private const int DczSizeLog2 = 4;
  14. public static void Hint(ArmEmitterContext context)
  15. {
  16. // Execute as no-op.
  17. }
  18. public static void Isb(ArmEmitterContext context)
  19. {
  20. // Execute as no-op.
  21. }
  22. public static void Mrs(ArmEmitterContext context)
  23. {
  24. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  25. MethodInfo info;
  26. switch (GetPackedId(op))
  27. {
  28. case 0b11_011_0000_0000_001: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCtrEl0)); break;
  29. case 0b11_011_0000_0000_111: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetDczidEl0)); break;
  30. case 0b11_011_0100_0010_000: EmitGetNzcv(context); return;
  31. case 0b11_011_0100_0100_000: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFpcr)); break;
  32. case 0b11_011_0100_0100_001: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFpsr)); break;
  33. case 0b11_011_1101_0000_010: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidrEl0)); break;
  34. case 0b11_011_1101_0000_011: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidr)); break;
  35. case 0b11_011_1110_0000_000: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCntfrqEl0)); break;
  36. case 0b11_011_1110_0000_001: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCntpctEl0)); break;
  37. case 0b11_011_1110_0000_010: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCntvctEl0)); break;
  38. default: throw new NotImplementedException($"Unknown MRS 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  39. }
  40. SetIntOrZR(context, op.Rt, context.Call(info));
  41. }
  42. public static void Msr(ArmEmitterContext context)
  43. {
  44. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  45. MethodInfo info;
  46. switch (GetPackedId(op))
  47. {
  48. case 0b11_011_0100_0010_000: EmitSetNzcv(context); return;
  49. case 0b11_011_0100_0100_000: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpcr)); break;
  50. case 0b11_011_0100_0100_001: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpsr)); break;
  51. case 0b11_011_1101_0000_010: info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetTpidrEl0)); break;
  52. default: throw new NotImplementedException($"Unknown MSR 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  53. }
  54. context.Call(info, GetIntOrZR(context, op.Rt));
  55. }
  56. public static void Nop(ArmEmitterContext context)
  57. {
  58. // Do nothing.
  59. }
  60. public static void Sys(ArmEmitterContext context)
  61. {
  62. // This instruction is used to do some operations on the CPU like cache invalidation,
  63. // address translation and the like.
  64. // We treat it as no-op here since we don't have any cache being emulated anyway.
  65. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  66. switch (GetPackedId(op))
  67. {
  68. case 0b11_011_0111_0100_001:
  69. {
  70. // DC ZVA
  71. Operand t = GetIntOrZR(context, op.Rt);
  72. for (long offset = 0; offset < (4 << DczSizeLog2); offset += 8)
  73. {
  74. Operand address = context.Add(t, Const(offset));
  75. InstEmitMemoryHelper.EmitStore(context, address, RegisterConsts.ZeroIndex, 3);
  76. }
  77. break;
  78. }
  79. // No-op
  80. case 0b11_011_0111_1110_001: //DC CIVAC
  81. break;
  82. }
  83. }
  84. private static int GetPackedId(OpCodeSystem op)
  85. {
  86. int id;
  87. id = op.Op2 << 0;
  88. id |= op.CRm << 3;
  89. id |= op.CRn << 7;
  90. id |= op.Op1 << 11;
  91. id |= op.Op0 << 14;
  92. return id;
  93. }
  94. private static void EmitGetNzcv(ArmEmitterContext context)
  95. {
  96. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  97. Operand vSh = context.ShiftLeft(GetFlag(PState.VFlag), Const((int)PState.VFlag));
  98. Operand cSh = context.ShiftLeft(GetFlag(PState.CFlag), Const((int)PState.CFlag));
  99. Operand zSh = context.ShiftLeft(GetFlag(PState.ZFlag), Const((int)PState.ZFlag));
  100. Operand nSh = context.ShiftLeft(GetFlag(PState.NFlag), Const((int)PState.NFlag));
  101. Operand nzcvSh = context.BitwiseOr(context.BitwiseOr(nSh, zSh), context.BitwiseOr(cSh, vSh));
  102. SetIntOrZR(context, op.Rt, nzcvSh);
  103. }
  104. private static void EmitSetNzcv(ArmEmitterContext context)
  105. {
  106. OpCodeSystem op = (OpCodeSystem)context.CurrOp;
  107. Operand t = GetIntOrZR(context, op.Rt);
  108. t = context.ConvertI64ToI32(t);
  109. Operand v = context.ShiftRightUI(t, Const((int)PState.VFlag));
  110. v = context.BitwiseAnd (v, Const(1));
  111. Operand c = context.ShiftRightUI(t, Const((int)PState.CFlag));
  112. c = context.BitwiseAnd (c, Const(1));
  113. Operand z = context.ShiftRightUI(t, Const((int)PState.ZFlag));
  114. z = context.BitwiseAnd (z, Const(1));
  115. Operand n = context.ShiftRightUI(t, Const((int)PState.NFlag));
  116. n = context.BitwiseAnd (n, Const(1));
  117. SetFlag(context, PState.VFlag, v);
  118. SetFlag(context, PState.CFlag, c);
  119. SetFlag(context, PState.ZFlag, z);
  120. SetFlag(context, PState.NFlag, n);
  121. }
  122. }
  123. }