InstEmitSystem.cs 6.5 KB

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