InstEmitSystem.cs 5.7 KB

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