aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Tamper/MemoryHelper.cs
blob: b9e649cfcbd541a47360e4713f7259c227703ecf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using Ryujinx.HLE.Exceptions;
using Ryujinx.HLE.HOS.Tamper.Operations;

namespace Ryujinx.HLE.HOS.Tamper
{
    class MemoryHelper
    {
        public static ulong GetAddressShift(MemoryRegion source, CompilationContext context)
        {
            return source switch
            {
                // Memory address is relative to the code start.
                MemoryRegion.NSO => context.ExeAddress,
                // Memory address is relative to the heap.
                MemoryRegion.Heap => context.HeapAddress,
                // Memory address is relative to the alias region.
                MemoryRegion.Alias => context.AliasAddress,
                // Memory address is relative to the asrl region, which matches the code region.
                MemoryRegion.Asrl => context.AslrAddress,
                _ => throw new TamperCompilationException($"Invalid memory source {source} in Atmosphere cheat"),
            };
        }

        private static void EmitAdd(Value<ulong> finalValue, IOperand firstOperand, IOperand secondOperand, CompilationContext context)
        {
            context.CurrentOperations.Add(new OpAdd<ulong>(finalValue, firstOperand, secondOperand));
        }

        public static Pointer EmitPointer(ulong addressImmediate, CompilationContext context)
        {
            Value<ulong> addressImmediateValue = new(addressImmediate);

            return new Pointer(addressImmediateValue, context.Process);
        }

        public static Pointer EmitPointer(Register addressRegister, CompilationContext context)
        {
            return new Pointer(addressRegister, context.Process);
        }

        public static Pointer EmitPointer(Register addressRegister, ulong offsetImmediate, CompilationContext context)
        {
            Value<ulong> offsetImmediateValue = new(offsetImmediate);
            Value<ulong> finalAddressValue = new(0);
            EmitAdd(finalAddressValue, addressRegister, offsetImmediateValue, context);

            return new Pointer(finalAddressValue, context.Process);
        }

        public static Pointer EmitPointer(Register addressRegister, Register offsetRegister, CompilationContext context)
        {
            Value<ulong> finalAddressValue = new(0);
            EmitAdd(finalAddressValue, addressRegister, offsetRegister, context);

            return new Pointer(finalAddressValue, context.Process);
        }

        public static Pointer EmitPointer(Register addressRegister, Register offsetRegister, ulong offsetImmediate, CompilationContext context)
        {
            Value<ulong> offsetImmediateValue = new(offsetImmediate);
            Value<ulong> finalOffsetValue = new(0);
            EmitAdd(finalOffsetValue, offsetRegister, offsetImmediateValue, context);
            Value<ulong> finalAddressValue = new(0);
            EmitAdd(finalAddressValue, addressRegister, finalOffsetValue, context);

            return new Pointer(finalAddressValue, context.Process);
        }

        public static Pointer EmitPointer(MemoryRegion memoryRegion, ulong offsetImmediate, CompilationContext context)
        {
            offsetImmediate += GetAddressShift(memoryRegion, context);

            return EmitPointer(offsetImmediate, context);
        }

        public static Pointer EmitPointer(MemoryRegion memoryRegion, Register offsetRegister, CompilationContext context)
        {
            ulong offsetImmediate = GetAddressShift(memoryRegion, context);

            return EmitPointer(offsetRegister, offsetImmediate, context);
        }

        public static Pointer EmitPointer(MemoryRegion memoryRegion, Register offsetRegister, ulong offsetImmediate, CompilationContext context)
        {
            offsetImmediate += GetAddressShift(memoryRegion, context);

            return EmitPointer(offsetRegister, offsetImmediate, context);
        }
    }
}