aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Tests/Cpu/CpuTest.cs
blob: 7af2d55d0a058d31182f0b38fe8389ce028fa4c7 (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using ChocolArm64;
using ChocolArm64.Memory;
using ChocolArm64.State;
using NUnit.Framework;
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace Ryujinx.Tests.Cpu
{
    [TestFixture]
    public class CpuTest
    {
        protected long Position { get; private set; }
        private long Size;

        private long EntryPoint;

        private IntPtr Ram;
        private AMemoryAlloc Allocator;
        private AMemory Memory;
        private AThread Thread;

        [SetUp]
        public void Setup()
        {
            Position = 0x0;
            Size = 0x1000;

            EntryPoint = Position;

            Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize);
            Allocator = new AMemoryAlloc();
            Memory = new AMemory(Ram, Allocator);
            Memory.Manager.MapPhys(Position, Size, 2, AMemoryPerm.Read | AMemoryPerm.Write | AMemoryPerm.Execute);
            Thread = new AThread(Memory, ThreadPriority.Normal, EntryPoint);
        }

        [TearDown]
        public void Teardown()
        {
            Thread = null;
            Memory = null;
            Allocator = null;
            Marshal.FreeHGlobal(Ram);
        }

        protected void Reset()
        {
            Teardown();
            Setup();
        }

        protected void Opcode(uint Opcode)
        {
            Thread.Memory.WriteUInt32(Position, Opcode);
            Position += 4;
        }

        protected void SetThreadState(ulong X0 = 0, ulong X1 = 0, ulong X2 = 0, ulong X31 = 0,
                                      AVec V0 = default(AVec), AVec V1 = default(AVec), AVec V2 = default(AVec),
                                      bool Overflow = false, bool Carry = false, bool Zero = false, bool Negative = false)
        {
            Thread.ThreadState.X0 = X0;
            Thread.ThreadState.X1 = X1;
            Thread.ThreadState.X2 = X2;
            Thread.ThreadState.X31 = X31;
            Thread.ThreadState.V0 = V0;
            Thread.ThreadState.V1 = V1;
            Thread.ThreadState.V2 = V2;
            Thread.ThreadState.Overflow = Overflow;
            Thread.ThreadState.Carry = Carry;
            Thread.ThreadState.Zero = Zero;
            Thread.ThreadState.Negative = Negative;
        }

        protected void ExecuteOpcodes()
        {
            using (ManualResetEvent Wait = new ManualResetEvent(false))
            {
                Thread.ThreadState.Break += (sender, e) => Thread.StopExecution();
                Thread.WorkFinished += (sender, e) => Wait.Set();

                Thread.Execute();
                Wait.WaitOne();
            }
        }

        protected AThreadState GetThreadState()
        {
            return Thread.ThreadState;
        }

        protected AThreadState SingleOpcode(uint Opcode,
                                            ulong X0 = 0, ulong X1 = 0, ulong X2 = 0, ulong X31 = 0,
                                            AVec V0 = default(AVec), AVec V1 = default(AVec), AVec V2 = default(AVec),
                                            bool Overflow = false, bool Carry = false, bool Zero = false, bool Negative = false)
        {
            this.Opcode(Opcode);
            this.Opcode(0xD4200000); // BRK #0
            this.Opcode(0xD65F03C0); // RET
            SetThreadState(X0, X1, X2, X31, V0, V1, V2, Overflow, Carry, Zero, Negative);
            ExecuteOpcodes();

            return GetThreadState();
        }
    }
}