2018-02-23 07:29:20 -05:00
|
|
|
using ChocolArm64;
|
2018-02-15 19:04:38 -05:00
|
|
|
using ChocolArm64.Memory;
|
|
|
|
using ChocolArm64.State;
|
|
|
|
using NUnit.Framework;
|
|
|
|
using System;
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Threading;
|
|
|
|
|
|
|
|
namespace Ryujinx.Tests.Cpu
|
|
|
|
{
|
|
|
|
[TestFixture]
|
2018-02-23 07:29:20 -05:00
|
|
|
public class CpuTest
|
2018-02-15 19:04:38 -05:00
|
|
|
{
|
2018-02-23 07:29:20 -05:00
|
|
|
protected long Position { get; private set; }
|
|
|
|
private long Size;
|
|
|
|
|
|
|
|
private long EntryPoint;
|
|
|
|
|
|
|
|
private IntPtr Ram;
|
|
|
|
private AMemoryAlloc Allocator;
|
|
|
|
private AMemory Memory;
|
|
|
|
private AThread Thread;
|
2018-02-15 19:04:38 -05:00
|
|
|
|
|
|
|
[SetUp]
|
|
|
|
public void Setup()
|
|
|
|
{
|
2018-02-23 07:29:20 -05:00
|
|
|
Position = 0x0;
|
|
|
|
Size = 0x1000;
|
|
|
|
|
|
|
|
EntryPoint = Position;
|
|
|
|
|
2018-02-15 19:04:38 -05:00
|
|
|
Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize);
|
|
|
|
Allocator = new AMemoryAlloc();
|
|
|
|
Memory = new AMemory(Ram, Allocator);
|
2018-02-23 07:29:20 -05:00
|
|
|
Memory.Manager.MapPhys(Position, Size, 2, AMemoryPerm.Read | AMemoryPerm.Write | AMemoryPerm.Execute);
|
|
|
|
Thread = new AThread(Memory, ThreadPriority.Normal, EntryPoint);
|
2018-02-15 19:04:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
[TearDown]
|
|
|
|
public void Teardown()
|
|
|
|
{
|
2018-02-23 07:29:20 -05:00
|
|
|
Thread = null;
|
|
|
|
Memory = null;
|
|
|
|
Allocator = null;
|
2018-02-15 19:04:38 -05:00
|
|
|
Marshal.FreeHGlobal(Ram);
|
|
|
|
}
|
|
|
|
|
2018-02-23 07:29:20 -05:00
|
|
|
protected void Reset()
|
2018-02-15 19:04:38 -05:00
|
|
|
{
|
2018-02-23 07:29:20 -05:00
|
|
|
Teardown();
|
|
|
|
Setup();
|
2018-02-15 19:04:38 -05:00
|
|
|
}
|
|
|
|
|
2018-02-23 07:29:20 -05:00
|
|
|
protected void Opcode(uint Opcode)
|
2018-02-15 19:04:38 -05:00
|
|
|
{
|
2018-02-23 07:29:20 -05:00
|
|
|
Thread.Memory.WriteUInt32(Position, Opcode);
|
|
|
|
Position += 4;
|
|
|
|
}
|
2018-02-15 19:04:38 -05:00
|
|
|
|
2018-02-24 20:50:58 -05:00
|
|
|
protected void SetThreadState(ulong X0 = 0, ulong X1 = 0, ulong X2 = 0, ulong X31 = 0,
|
2018-02-23 09:53:32 -05:00
|
|
|
AVec V0 = default(AVec), AVec V1 = default(AVec), AVec V2 = default(AVec),
|
|
|
|
bool Overflow = false, bool Carry = false, bool Zero = false, bool Negative = false)
|
2018-02-23 07:29:20 -05:00
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
Thread.ThreadState.X0 = X0;
|
|
|
|
Thread.ThreadState.X1 = X1;
|
|
|
|
Thread.ThreadState.X2 = X2;
|
2018-02-24 20:50:58 -05:00
|
|
|
Thread.ThreadState.X31 = X31;
|
2018-02-18 14:28:07 -05:00
|
|
|
Thread.ThreadState.V0 = V0;
|
|
|
|
Thread.ThreadState.V1 = V1;
|
|
|
|
Thread.ThreadState.V2 = V2;
|
2018-02-23 09:53:32 -05:00
|
|
|
Thread.ThreadState.Overflow = Overflow;
|
|
|
|
Thread.ThreadState.Carry = Carry;
|
|
|
|
Thread.ThreadState.Zero = Zero;
|
|
|
|
Thread.ThreadState.Negative = Negative;
|
2018-02-23 07:29:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
2018-02-18 14:28:07 -05:00
|
|
|
return Thread.ThreadState;
|
2018-02-15 19:04:38 -05:00
|
|
|
}
|
|
|
|
|
2018-02-23 07:29:20 -05:00
|
|
|
protected AThreadState SingleOpcode(uint Opcode,
|
2018-02-24 20:50:58 -05:00
|
|
|
ulong X0 = 0, ulong X1 = 0, ulong X2 = 0, ulong X31 = 0,
|
2018-02-23 09:53:32 -05:00
|
|
|
AVec V0 = default(AVec), AVec V1 = default(AVec), AVec V2 = default(AVec),
|
|
|
|
bool Overflow = false, bool Carry = false, bool Zero = false, bool Negative = false)
|
2018-02-15 19:04:38 -05:00
|
|
|
{
|
2018-02-23 07:29:20 -05:00
|
|
|
this.Opcode(Opcode);
|
|
|
|
this.Opcode(0xD4200000); // BRK #0
|
|
|
|
this.Opcode(0xD65F03C0); // RET
|
2018-02-24 20:50:58 -05:00
|
|
|
SetThreadState(X0, X1, X2, X31, V0, V1, V2, Overflow, Carry, Zero, Negative);
|
2018-02-23 07:29:20 -05:00
|
|
|
ExecuteOpcodes();
|
|
|
|
|
|
|
|
return GetThreadState();
|
2018-02-15 19:04:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|