aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Tests/Cpu/CpuTest.cs
blob: 0f1e641340ada00b43d242a5a13148169b684d53 (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
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 partial class CpuTest
    {
        IntPtr Ram;
        AMemoryAlloc Allocator;
        AMemory Memory;

        [SetUp]
        public void Setup()
        {
            Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize);
            Allocator = new AMemoryAlloc();
            Memory = new AMemory(Ram, Allocator);
            Memory.Manager.MapPhys(0x1000, 0x1000, 2, AMemoryPerm.Read | AMemoryPerm.Write | AMemoryPerm.Execute);
        }

        [TearDown]
        public void Teardown()
        {
            Marshal.FreeHGlobal(Ram);
        }

        private void Execute(AThread Thread)
        {
            AutoResetEvent Wait = new AutoResetEvent(false);
            Thread.ThreadState.Break += (sender, e) => Thread.StopExecution();
            Thread.WorkFinished += (sender, e) => Wait.Set();

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

        private AThreadState SingleOpcode(uint Opcode, 
                                          ulong X0 = 0, ulong X1 = 0, ulong X2 = 0, 
                                          AVec V0 = new AVec(), AVec V1 = new AVec(), AVec V2 = new AVec())
        {
            Memory.WriteUInt32(0x1000, Opcode);
            Memory.WriteUInt32(0x1004, 0xD4200000); // BRK #0
            Memory.WriteUInt32(0x1008, 0xD65F03C0); // RET

            AThread Thread = new AThread(Memory, ThreadPriority.Normal, 0x1000);
            Thread.ThreadState.X0 = X0;
            Thread.ThreadState.X1 = X1;
            Thread.ThreadState.X2 = X2;
            Thread.ThreadState.V0 = V0;
            Thread.ThreadState.V1 = V1;
            Thread.ThreadState.V2 = V2;
            Execute(Thread);
            return Thread.ThreadState;
        }

        [Test]
        public void SanityCheck()
        {
            uint Opcode = 0xD503201F; // NOP
            Assert.AreEqual(SingleOpcode(Opcode, X0: 0).X0, 0);
            Assert.AreEqual(SingleOpcode(Opcode, X0: 1).X0, 1);
            Assert.AreEqual(SingleOpcode(Opcode, X0: 2).X0, 2);
            Assert.AreEqual(SingleOpcode(Opcode, X0: 42).X0, 42);
        }
    }
}