InstEmitSystem32.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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.OperandHelper;
  9. namespace ARMeilleure.Instructions
  10. {
  11. static partial class InstEmit32
  12. {
  13. public static void Mcr(ArmEmitterContext context)
  14. {
  15. OpCode32System op = (OpCode32System)context.CurrOp;
  16. if (op.Coproc != 15)
  17. {
  18. InstEmit.Und(context);
  19. return;
  20. }
  21. if (op.Opc1 != 0)
  22. {
  23. throw new NotImplementedException($"Unknown MRC Opc1 0x{op.Opc1:X16} at 0x{op.Address:X16}.");
  24. }
  25. MethodInfo info;
  26. switch (op.CRn)
  27. {
  28. case 13: // Process and Thread Info.
  29. if (op.CRm != 0)
  30. {
  31. throw new NotImplementedException($"Unknown MRC CRm 0x{op.CRm:X16} at 0x{op.Address:X16}.");
  32. }
  33. switch (op.Opc2)
  34. {
  35. case 2:
  36. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetTpidrEl032)); break;
  37. default:
  38. throw new NotImplementedException($"Unknown MRC Opc2 0x{op.Opc2:X16} at 0x{op.Address:X16}.");
  39. }
  40. break;
  41. case 7:
  42. switch (op.CRm) // Cache and Memory barrier.
  43. {
  44. case 10:
  45. switch (op.Opc2)
  46. {
  47. case 5: // Data Memory Barrier Register.
  48. return; // No-op.
  49. default:
  50. throw new NotImplementedException($"Unknown MRC Opc2 0x{op.Opc2:X16} at 0x{op.Address:X16}.");
  51. }
  52. default:
  53. throw new NotImplementedException($"Unknown MRC CRm 0x{op.CRm:X16} at 0x{op.Address:X16}.");
  54. }
  55. default:
  56. throw new NotImplementedException($"Unknown MRC 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  57. }
  58. context.Call(info, GetIntA32(context, op.Rt));
  59. }
  60. public static void Mrc(ArmEmitterContext context)
  61. {
  62. OpCode32System op = (OpCode32System)context.CurrOp;
  63. if (op.Coproc != 15)
  64. {
  65. InstEmit.Und(context);
  66. return;
  67. }
  68. if (op.Opc1 != 0)
  69. {
  70. throw new NotImplementedException($"Unknown MRC Opc1 0x{op.Opc1:X16} at 0x{op.Address:X16}.");
  71. }
  72. MethodInfo info;
  73. switch (op.CRn)
  74. {
  75. case 13: // Process and Thread Info.
  76. if (op.CRm != 0)
  77. {
  78. throw new NotImplementedException($"Unknown MRC CRm 0x{op.CRm:X16} at 0x{op.Address:X16}.");
  79. }
  80. switch (op.Opc2)
  81. {
  82. case 2:
  83. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidrEl032)); break;
  84. case 3:
  85. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetTpidr32)); break;
  86. default:
  87. throw new NotImplementedException($"Unknown MRC Opc2 0x{op.Opc2:X16} at 0x{op.Address:X16}.");
  88. }
  89. break;
  90. default:
  91. throw new NotImplementedException($"Unknown MRC 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  92. }
  93. if (op.Rt == RegisterAlias.Aarch32Pc)
  94. {
  95. // Special behavior: copy NZCV flags into APSR.
  96. EmitSetNzcv(context, context.Call(info));
  97. return;
  98. }
  99. else
  100. {
  101. SetIntA32(context, op.Rt, context.Call(info));
  102. }
  103. }
  104. public static void Mrrc(ArmEmitterContext context)
  105. {
  106. OpCode32System op = (OpCode32System)context.CurrOp;
  107. if (op.Coproc != 15)
  108. {
  109. InstEmit.Und(context);
  110. return;
  111. }
  112. int opc = op.MrrcOp;
  113. MethodInfo info;
  114. switch (op.CRm)
  115. {
  116. case 14: // Timer.
  117. switch (opc)
  118. {
  119. case 0:
  120. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetCntpctEl0)); break;
  121. default:
  122. throw new NotImplementedException($"Unknown MRRC Opc1 0x{opc:X16} at 0x{op.Address:X16}.");
  123. }
  124. break;
  125. default:
  126. throw new NotImplementedException($"Unknown MRRC 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  127. }
  128. Operand result = context.Call(info);
  129. SetIntA32(context, op.Rt, context.ConvertI64ToI32(result));
  130. SetIntA32(context, op.CRn, context.ConvertI64ToI32(context.ShiftRightUI(result, Const(32))));
  131. }
  132. public static void Nop(ArmEmitterContext context) { }
  133. public static void Vmrs(ArmEmitterContext context)
  134. {
  135. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  136. if (op.Rt == RegisterAlias.Aarch32Pc && op.Sreg == 0b0001)
  137. {
  138. // Special behavior: copy NZCV flags into APSR.
  139. SetFlag(context, PState.VFlag, GetFpFlag(FPState.VFlag));
  140. SetFlag(context, PState.CFlag, GetFpFlag(FPState.CFlag));
  141. SetFlag(context, PState.ZFlag, GetFpFlag(FPState.ZFlag));
  142. SetFlag(context, PState.NFlag, GetFpFlag(FPState.NFlag));
  143. return;
  144. }
  145. MethodInfo info;
  146. switch (op.Sreg)
  147. {
  148. case 0b0000: // FPSID
  149. throw new NotImplementedException("Supervisor Only");
  150. case 0b0001: // FPSCR
  151. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.GetFpscr)); break;
  152. case 0b0101: // MVFR2
  153. throw new NotImplementedException("MVFR2");
  154. case 0b0110: // MVFR1
  155. throw new NotImplementedException("MVFR1");
  156. case 0b0111: // MVFR0
  157. throw new NotImplementedException("MVFR0");
  158. case 0b1000: // FPEXC
  159. throw new NotImplementedException("Supervisor Only");
  160. default:
  161. throw new NotImplementedException($"Unknown VMRS 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  162. }
  163. SetIntA32(context, op.Rt, context.Call(info));
  164. }
  165. public static void Vmsr(ArmEmitterContext context)
  166. {
  167. OpCode32SimdSpecial op = (OpCode32SimdSpecial)context.CurrOp;
  168. MethodInfo info;
  169. switch (op.Sreg)
  170. {
  171. case 0b0000: // FPSID
  172. throw new NotImplementedException("Supervisor Only");
  173. case 0b0001: // FPSCR
  174. info = typeof(NativeInterface).GetMethod(nameof(NativeInterface.SetFpscr)); break;
  175. case 0b0101: // MVFR2
  176. throw new NotImplementedException("MVFR2");
  177. case 0b0110: // MVFR1
  178. throw new NotImplementedException("MVFR1");
  179. case 0b0111: // MVFR0
  180. throw new NotImplementedException("MVFR0");
  181. case 0b1000: // FPEXC
  182. throw new NotImplementedException("Supervisor Only");
  183. default:
  184. throw new NotImplementedException($"Unknown VMSR 0x{op.RawOpCode:X8} at 0x{op.Address:X16}.");
  185. }
  186. context.Call(info, GetIntA32(context, op.Rt));
  187. }
  188. private static void EmitSetNzcv(ArmEmitterContext context, Operand t)
  189. {
  190. Operand v = context.ShiftRightUI(t, Const((int)PState.VFlag));
  191. v = context.BitwiseAnd(v, Const(1));
  192. Operand c = context.ShiftRightUI(t, Const((int)PState.CFlag));
  193. c = context.BitwiseAnd(c, Const(1));
  194. Operand z = context.ShiftRightUI(t, Const((int)PState.ZFlag));
  195. z = context.BitwiseAnd(z, Const(1));
  196. Operand n = context.ShiftRightUI(t, Const((int)PState.NFlag));
  197. n = context.BitwiseAnd(n, Const(1));
  198. SetFlag(context, PState.VFlag, v);
  199. SetFlag(context, PState.CFlag, c);
  200. SetFlag(context, PState.ZFlag, z);
  201. SetFlag(context, PState.NFlag, n);
  202. }
  203. }
  204. }