LoadRegisterWithMemory.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using Ryujinx.HLE.Exceptions;
  2. namespace Ryujinx.HLE.HOS.Tamper.CodeEmitters
  3. {
  4. /// <summary>
  5. /// Code type 5 allows loading a value from memory into a register, either using a fixed address or by
  6. /// dereferencing the destination register.
  7. /// </summary>
  8. class LoadRegisterWithMemory
  9. {
  10. private const int OperationWidthIndex = 1;
  11. private const int MemoryRegionIndex = 2;
  12. private const int DestinationRegisterIndex = 3;
  13. private const int UseDestinationAsSourceIndex = 4;
  14. private const int OffsetImmediateIndex = 6;
  15. private const int OffsetImmediateSize = 10;
  16. public static void Emit(byte[] instruction, CompilationContext context)
  17. {
  18. // 5TMR00AA AAAAAAAA
  19. // T: Width of memory read (1, 2, 4, or 8 bytes).
  20. // M: Memory region to write to (0 = Main NSO, 1 = Heap).
  21. // R: Register to load value into.
  22. // A: Immediate offset to use from memory region base.
  23. // 5TMR10AA AAAAAAAA
  24. // T: Width of memory read(1, 2, 4, or 8 bytes).
  25. // M: Ignored.
  26. // R: Register to use as base address and to load value into.
  27. // A: Immediate offset to use from register R.
  28. byte operationWidth = instruction[OperationWidthIndex];
  29. MemoryRegion memoryRegion = (MemoryRegion)instruction[MemoryRegionIndex];
  30. Register destinationRegister = context.GetRegister(instruction[DestinationRegisterIndex]);
  31. byte useDestinationAsSourceIndex = instruction[UseDestinationAsSourceIndex];
  32. ulong address = InstructionHelper.GetImmediate(instruction, OffsetImmediateIndex, OffsetImmediateSize);
  33. Pointer sourceMemory;
  34. switch (useDestinationAsSourceIndex)
  35. {
  36. case 0:
  37. // Don't use the source register as an additional address offset.
  38. sourceMemory = MemoryHelper.EmitPointer(memoryRegion, address, context);
  39. break;
  40. case 1:
  41. // Use the source register as the base address.
  42. sourceMemory = MemoryHelper.EmitPointer(destinationRegister, address, context);
  43. break;
  44. default:
  45. throw new TamperCompilationException($"Invalid source mode {useDestinationAsSourceIndex} in Atmosphere cheat");
  46. }
  47. InstructionHelper.EmitMov(operationWidth, context, destinationRegister, sourceMemory);
  48. }
  49. }
  50. }