Arithmetic.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Ryujinx.HLE.Exceptions;
  2. using Ryujinx.HLE.HOS.Tamper.Operations;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.HLE.HOS.Tamper.CodeEmitters
  6. {
  7. /// <summary>
  8. /// Code type 9 allows performing arithmetic on registers.
  9. /// </summary>
  10. class Arithmetic
  11. {
  12. private const int OperationWidthIndex = 1;
  13. private const int OperationTypeIndex = 2;
  14. private const int DestinationRegisterIndex = 3;
  15. private const int LeftHandSideRegisterIndex = 4;
  16. private const int UseImmediateAsRhsIndex = 5;
  17. private const int RightHandSideRegisterIndex = 6;
  18. private const int RightHandSideImmediateIndex = 8;
  19. private const int RightHandSideImmediate8 = 8;
  20. private const int RightHandSideImmediate16 = 16;
  21. private const byte Add = 0; // lhs + rhs
  22. private const byte Sub = 1; // lhs - rhs
  23. private const byte Mul = 2; // lhs * rhs
  24. private const byte Lsh = 3; // lhs << rhs
  25. private const byte Rsh = 4; // lhs >> rhs
  26. private const byte And = 5; // lhs & rhs
  27. private const byte Or = 6; // lhs | rhs
  28. private const byte Not = 7; // ~lhs (discards right-hand operand)
  29. private const byte Xor = 8; // lhs ^ rhs
  30. private const byte Mov = 9; // lhs (discards right-hand operand)
  31. public static void Emit(byte[] instruction, CompilationContext context)
  32. {
  33. // 9TCRS0s0
  34. // T: Width of arithmetic operation(1, 2, 4, or 8 bytes).
  35. // C: Arithmetic operation to apply, see below.
  36. // R: Register to store result in.
  37. // S: Register to use as left - hand operand.
  38. // s: Register to use as right - hand operand.
  39. // 9TCRS100 VVVVVVVV (VVVVVVVV)
  40. // T: Width of arithmetic operation(1, 2, 4, or 8 bytes).
  41. // C: Arithmetic operation to apply, see below.
  42. // R: Register to store result in.
  43. // S: Register to use as left - hand operand.
  44. // V: Value to use as right - hand operand.
  45. byte operationWidth = instruction[OperationWidthIndex];
  46. byte operation = instruction[OperationTypeIndex];
  47. Register destinationRegister = context.GetRegister(instruction[DestinationRegisterIndex]);
  48. Register leftHandSideRegister = context.GetRegister(instruction[LeftHandSideRegisterIndex]);
  49. byte rightHandSideIsImmediate = instruction[UseImmediateAsRhsIndex];
  50. IOperand rightHandSideOperand;
  51. switch (rightHandSideIsImmediate)
  52. {
  53. case 0:
  54. // Use a register as right-hand side.
  55. rightHandSideOperand = context.GetRegister(instruction[RightHandSideRegisterIndex]);
  56. break;
  57. case 1:
  58. // Use an immediate as right-hand side.
  59. int immediateSize = operationWidth <= 4 ? RightHandSideImmediate8 : RightHandSideImmediate16;
  60. ulong immediate = InstructionHelper.GetImmediate(instruction, RightHandSideImmediateIndex, immediateSize);
  61. rightHandSideOperand = new Value<ulong>(immediate);
  62. break;
  63. default:
  64. throw new TamperCompilationException($"Invalid right-hand side switch {rightHandSideIsImmediate} in Atmosphere cheat");
  65. }
  66. void Emit(Type operationType, IOperand rhs = null)
  67. {
  68. List<IOperand> operandList = new List<IOperand>();
  69. operandList.Add(destinationRegister);
  70. operandList.Add(leftHandSideRegister);
  71. if (rhs != null)
  72. {
  73. operandList.Add(rhs);
  74. }
  75. InstructionHelper.Emit(operationType, operationWidth, context, operandList.ToArray());
  76. }
  77. switch (operation)
  78. {
  79. case Add: Emit(typeof(OpAdd<>), rightHandSideOperand); break;
  80. case Sub: Emit(typeof(OpSub<>), rightHandSideOperand); break;
  81. case Mul: Emit(typeof(OpMul<>), rightHandSideOperand); break;
  82. case Lsh: Emit(typeof(OpLsh<>), rightHandSideOperand); break;
  83. case Rsh: Emit(typeof(OpRsh<>), rightHandSideOperand); break;
  84. case And: Emit(typeof(OpAnd<>), rightHandSideOperand); break;
  85. case Or: Emit(typeof(OpOr<> ), rightHandSideOperand); break;
  86. case Not: Emit(typeof(OpNot<>) ); break;
  87. case Xor: Emit(typeof(OpXor<>), rightHandSideOperand); break;
  88. case Mov: Emit(typeof(OpMov<>) ); break;
  89. default:
  90. throw new TamperCompilationException($"Invalid arithmetic operation {operation} in Atmosphere cheat");
  91. }
  92. }
  93. }
  94. }