MemoryHelper.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using Ryujinx.HLE.Exceptions;
  2. using Ryujinx.HLE.HOS.Tamper.Operations;
  3. namespace Ryujinx.HLE.HOS.Tamper
  4. {
  5. class MemoryHelper
  6. {
  7. public static ulong GetAddressShift(MemoryRegion source, CompilationContext context)
  8. {
  9. switch (source)
  10. {
  11. case MemoryRegion.NSO:
  12. // Memory address is relative to the code start.
  13. return context.ExeAddress;
  14. case MemoryRegion.Heap:
  15. // Memory address is relative to the heap.
  16. return context.HeapAddress;
  17. default:
  18. throw new TamperCompilationException($"Invalid memory source {source} in Atmosphere cheat");
  19. }
  20. }
  21. private static void EmitAdd(Value<ulong> finalValue, IOperand firstOperand, IOperand secondOperand, CompilationContext context)
  22. {
  23. context.CurrentOperations.Add(new OpAdd<ulong>(finalValue, firstOperand, secondOperand));
  24. }
  25. public static Pointer EmitPointer(ulong addressImmediate, CompilationContext context)
  26. {
  27. Value<ulong> addressImmediateValue = new Value<ulong>(addressImmediate);
  28. return new Pointer(addressImmediateValue, context.Process);
  29. }
  30. public static Pointer EmitPointer(Register addressRegister, CompilationContext context)
  31. {
  32. return new Pointer(addressRegister, context.Process);
  33. }
  34. public static Pointer EmitPointer(Register addressRegister, ulong offsetImmediate, CompilationContext context)
  35. {
  36. Value<ulong> offsetImmediateValue = new Value<ulong>(offsetImmediate);
  37. Value<ulong> finalAddressValue = new Value<ulong>(0);
  38. EmitAdd(finalAddressValue, addressRegister, offsetImmediateValue, context);
  39. return new Pointer(finalAddressValue, context.Process);
  40. }
  41. public static Pointer EmitPointer(Register addressRegister, Register offsetRegister, CompilationContext context)
  42. {
  43. Value<ulong> finalAddressValue = new Value<ulong>(0);
  44. EmitAdd(finalAddressValue, addressRegister, offsetRegister, context);
  45. return new Pointer(finalAddressValue, context.Process);
  46. }
  47. public static Pointer EmitPointer(Register addressRegister, Register offsetRegister, ulong offsetImmediate, CompilationContext context)
  48. {
  49. Value<ulong> offsetImmediateValue = new Value<ulong>(offsetImmediate);
  50. Value<ulong> finalOffsetValue = new Value<ulong>(0);
  51. EmitAdd(finalOffsetValue, offsetRegister, offsetImmediateValue, context);
  52. Value<ulong> finalAddressValue = new Value<ulong>(0);
  53. EmitAdd(finalAddressValue, addressRegister, finalOffsetValue, context);
  54. return new Pointer(finalAddressValue, context.Process);
  55. }
  56. public static Pointer EmitPointer(MemoryRegion memoryRegion, ulong offsetImmediate, CompilationContext context)
  57. {
  58. offsetImmediate += GetAddressShift(memoryRegion, context);
  59. return EmitPointer(offsetImmediate, context);
  60. }
  61. public static Pointer EmitPointer(MemoryRegion memoryRegion, Register offsetRegister, CompilationContext context)
  62. {
  63. ulong offsetImmediate = GetAddressShift(memoryRegion, context);
  64. return EmitPointer(offsetRegister, offsetImmediate, context);
  65. }
  66. public static Pointer EmitPointer(MemoryRegion memoryRegion, Register offsetRegister, ulong offsetImmediate, CompilationContext context)
  67. {
  68. offsetImmediate += GetAddressShift(memoryRegion, context);
  69. return EmitPointer(offsetRegister, offsetImmediate, context);
  70. }
  71. }
  72. }