aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.HLE/HOS/Tamper/CodeEmitters/LegacyArithmetic.cs
blob: 3f222864fbc53c3cef961750d77a6490a98d67bb (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
using Ryujinx.HLE.Exceptions;
using Ryujinx.HLE.HOS.Tamper.Operations;
using System;

namespace Ryujinx.HLE.HOS.Tamper.CodeEmitters
{
    /// <summary>
    /// Code type 7 allows performing arithmetic on registers. However, it has been deprecated by Code
    /// type 9, and is only kept for backwards compatibility.
    /// </summary>
    class LegacyArithmetic
    {
        const int OperationWidthIndex = 1;
        const int DestinationRegisterIndex = 3;
        const int OperationTypeIndex = 4;
        const int ValueImmediateIndex = 8;

        const int ValueImmediateSize = 8;

        private const byte Add = 0; // reg += rhs
        private const byte Sub = 1; // reg -= rhs
        private const byte Mul = 2; // reg *= rhs
        private const byte Lsh = 3; // reg <<= rhs
        private const byte Rsh = 4; // reg >>= rhs

        public static void Emit(byte[] instruction, CompilationContext context)
        {
            // 7T0RC000 VVVVVVVV
            // T: Width of arithmetic operation(1, 2, 4, or 8 bytes).
            // R: Register to apply arithmetic to.
            // C: Arithmetic operation to apply, see below.
            // V: Value to use for arithmetic operation.

            byte operationWidth = instruction[OperationWidthIndex];
            Register register = context.GetRegister(instruction[DestinationRegisterIndex]);
            byte operation = instruction[OperationTypeIndex];
            ulong immediate = InstructionHelper.GetImmediate(instruction, ValueImmediateIndex, ValueImmediateSize);
            Value<ulong> rightHandSideValue = new(immediate);

            void Emit(Type operationType)
            {
                InstructionHelper.Emit(operationType, operationWidth, context, register, register, rightHandSideValue);
            }

            switch (operation)
            {
                case Add:
                    Emit(typeof(OpAdd<>));
                    break;
                case Sub:
                    Emit(typeof(OpSub<>));
                    break;
                case Mul:
                    Emit(typeof(OpMul<>));
                    break;
                case Lsh:
                    Emit(typeof(OpLsh<>));
                    break;
                case Rsh:
                    Emit(typeof(OpRsh<>));
                    break;
                default:
                    throw new TamperCompilationException($"Invalid arithmetic operation {operation} in Atmosphere cheat");
            }
        }
    }
}