Replace unicorn bindings with Nuget package (#4378)

* Replace unicorn bindings with Nuget package

* Use nameof for ValueSource args

* Remove redundant code from test projects

* Fix wrong values for EmuStart()

Add notes to address this later again

* Improve formatting

* Fix formatting/alignment issues
This commit is contained in:
TSRBerry 2023-02-09 02:24:32 +01:00 committed by GitHub
parent b3f0978869
commit ec8d4f3af5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
64 changed files with 2276 additions and 3576 deletions

View file

@ -1,62 +1,45 @@
using Ryujinx.Tests.Unicorn.Native;
using Ryujinx.Tests.Unicorn.Native.Const;
using System;
using System;
using UnicornEngine.Const;
namespace Ryujinx.Tests.Unicorn
{
public class UnicornAArch32 : IDisposable
{
internal readonly IntPtr uc;
private bool _isDisposed = false;
internal readonly UnicornEngine.Unicorn uc;
private bool _isDisposed;
public IndexedProperty<int, uint> R
{
get
{
return new IndexedProperty<int, uint>(
(int i) => GetX(i),
(int i, uint value) => SetX(i, value));
}
}
public IndexedProperty<int, uint> R => new(GetX, SetX);
public IndexedProperty<int, SimdValue> Q
{
get
{
return new IndexedProperty<int, SimdValue>(
(int i) => GetQ(i),
(int i, SimdValue value) => SetQ(i, value));
}
}
public IndexedProperty<int, SimdValue> Q => new(GetQ, SetQ);
public uint LR
{
get => GetRegister(Arm.REG_LR);
set => SetRegister(Arm.REG_LR, value);
get => GetRegister(Arm.UC_ARM_REG_LR);
set => SetRegister(Arm.UC_ARM_REG_LR, value);
}
public uint SP
{
get => GetRegister(Arm.REG_SP);
set => SetRegister(Arm.REG_SP, value);
get => GetRegister(Arm.UC_ARM_REG_SP);
set => SetRegister(Arm.UC_ARM_REG_SP, value);
}
public uint PC
{
get => GetRegister(Arm.REG_PC) & 0xfffffffeu;
set => SetRegister(Arm.REG_PC, (value & 0xfffffffeu) | (ThumbFlag ? 1u : 0u));
get => GetRegister(Arm.UC_ARM_REG_PC) & 0xfffffffeu;
set => SetRegister(Arm.UC_ARM_REG_PC, (value & 0xfffffffeu) | (ThumbFlag ? 1u : 0u));
}
public uint CPSR
{
get => GetRegister(Arm.REG_CPSR);
set => SetRegister(Arm.REG_CPSR, value);
get => GetRegister(Arm.UC_ARM_REG_CPSR);
set => SetRegister(Arm.UC_ARM_REG_CPSR, value);
}
public int Fpscr
{
get => (int)GetRegister(Arm.REG_FPSCR) | ((int)GetRegister(Arm.REG_FPSCR_NZCV));
set => SetRegister(Arm.REG_FPSCR, (uint)value);
get => (int)GetRegister(Arm.UC_ARM_REG_FPSCR) | ((int)GetRegister(Arm.UC_ARM_REG_FPSCR_NZCV));
set => SetRegister(Arm.UC_ARM_REG_FPSCR, (uint)value);
}
public bool QFlag
@ -95,16 +78,16 @@ namespace Ryujinx.Tests.Unicorn
set
{
CPSR = (CPSR & ~0x00000020u) | (value ? 0x00000020u : 0u);
SetRegister(Arm.REG_PC, (GetRegister(Arm.REG_PC) & 0xfffffffeu) | (value ? 1u : 0u));
SetRegister(Arm.UC_ARM_REG_PC, (GetRegister(Arm.UC_ARM_REG_PC) & 0xfffffffeu) | (value ? 1u : 0u));
}
}
public UnicornAArch32()
{
Interface.Checked(Interface.uc_open(Arch.ARM, Mode.LITTLE_ENDIAN, out uc));
uc = new UnicornEngine.Unicorn(Common.UC_ARCH_ARM, Common.UC_MODE_LITTLE_ENDIAN);
SetRegister(Arm.REG_C1_C0_2, GetRegister(Arm.REG_C1_C0_2) | 0xf00000);
SetRegister(Arm.REG_FPEXC, 0x40000000);
SetRegister(Arm.UC_ARM_REG_C1_C0_2, GetRegister(Arm.UC_ARM_REG_C1_C0_2) | 0xf00000);
SetRegister(Arm.UC_ARM_REG_FPEXC, 0x40000000);
}
~UnicornAArch32()
@ -122,14 +105,15 @@ namespace Ryujinx.Tests.Unicorn
{
if (!_isDisposed)
{
Interface.Checked(Interface.uc_close(uc));
uc.Close();
_isDisposed = true;
}
}
public void RunForCount(ulong count)
{
Interface.Checked(Interface.uc_emu_start(uc, this.PC, 0xFFFFFFFFFFFFFFFFu, 0, count));
// FIXME: untilAddr should be 0xFFFFFFFFFFFFFFFFu
uc.EmuStart(this.PC, -1, 0, (long)count);
}
public void Step()
@ -137,44 +121,44 @@ namespace Ryujinx.Tests.Unicorn
RunForCount(1);
}
private static Arm[] XRegisters = new Arm[16]
private static int[] XRegisters =
{
Arm.REG_R0,
Arm.REG_R1,
Arm.REG_R2,
Arm.REG_R3,
Arm.REG_R4,
Arm.REG_R5,
Arm.REG_R6,
Arm.REG_R7,
Arm.REG_R8,
Arm.REG_R9,
Arm.REG_R10,
Arm.REG_R11,
Arm.REG_R12,
Arm.REG_R13,
Arm.REG_R14,
Arm.REG_R15,
Arm.UC_ARM_REG_R0,
Arm.UC_ARM_REG_R1,
Arm.UC_ARM_REG_R2,
Arm.UC_ARM_REG_R3,
Arm.UC_ARM_REG_R4,
Arm.UC_ARM_REG_R5,
Arm.UC_ARM_REG_R6,
Arm.UC_ARM_REG_R7,
Arm.UC_ARM_REG_R8,
Arm.UC_ARM_REG_R9,
Arm.UC_ARM_REG_R10,
Arm.UC_ARM_REG_R11,
Arm.UC_ARM_REG_R12,
Arm.UC_ARM_REG_R13,
Arm.UC_ARM_REG_R14,
Arm.UC_ARM_REG_R15,
};
private static Arm[] QRegisters = new Arm[16]
private static int[] QRegisters =
{
Arm.REG_Q0,
Arm.REG_Q1,
Arm.REG_Q2,
Arm.REG_Q3,
Arm.REG_Q4,
Arm.REG_Q5,
Arm.REG_Q6,
Arm.REG_Q7,
Arm.REG_Q8,
Arm.REG_Q9,
Arm.REG_Q10,
Arm.REG_Q11,
Arm.REG_Q12,
Arm.REG_Q13,
Arm.REG_Q14,
Arm.REG_Q15
Arm.UC_ARM_REG_Q0,
Arm.UC_ARM_REG_Q1,
Arm.UC_ARM_REG_Q2,
Arm.UC_ARM_REG_Q3,
Arm.UC_ARM_REG_Q4,
Arm.UC_ARM_REG_Q5,
Arm.UC_ARM_REG_Q6,
Arm.UC_ARM_REG_Q7,
Arm.UC_ARM_REG_Q8,
Arm.UC_ARM_REG_Q9,
Arm.UC_ARM_REG_Q10,
Arm.UC_ARM_REG_Q11,
Arm.UC_ARM_REG_Q12,
Arm.UC_ARM_REG_Q13,
Arm.UC_ARM_REG_Q14,
Arm.UC_ARM_REG_Q15
};
public uint GetX(int index)
@ -205,7 +189,7 @@ namespace Ryujinx.Tests.Unicorn
}
// Getting quadword registers from Unicorn A32 seems to be broken, so we combine its 2 doubleword registers instead.
return GetVector((Arm)((int)Arm.REG_D0 + index * 2));
return GetVector(Arm.UC_ARM_REG_D0 + index * 2);
}
public void SetQ(int index, SimdValue value)
@ -215,96 +199,85 @@ namespace Ryujinx.Tests.Unicorn
throw new ArgumentOutOfRangeException(nameof(index));
}
SetVector((Arm)((int)Arm.REG_D0 + index * 2), value);
SetVector(Arm.UC_ARM_REG_D0 + index * 2, value);
}
public uint GetRegister(Arm register)
public uint GetRegister(int register)
{
byte[] data = new byte[4];
Interface.Checked(Interface.uc_reg_read(uc, (int)register, data));
uc.RegRead(register, data);
return (uint)BitConverter.ToInt32(data, 0);
return BitConverter.ToUInt32(data, 0);
}
public void SetRegister(Arm register, uint value)
public void SetRegister(int register, uint value)
{
byte[] data = BitConverter.GetBytes(value);
Interface.Checked(Interface.uc_reg_write(uc, (int)register, data));
uc.RegWrite(register, data);
}
public SimdValue GetVector(Arm register)
public SimdValue GetVector(int register)
{
byte[] data = new byte[8];
Interface.Checked(Interface.uc_reg_read(uc, (int)register, data));
uc.RegRead(register, data);
ulong lo = BitConverter.ToUInt64(data, 0);
Interface.Checked(Interface.uc_reg_read(uc, (int)register + 1, data));
uc.RegRead(register + 1, data);
ulong hi = BitConverter.ToUInt64(data, 0);
return new SimdValue(lo, hi);
}
private void SetVector(Arm register, SimdValue value)
private void SetVector(int register, SimdValue value)
{
byte[] data = BitConverter.GetBytes(value.GetUInt64(0));
Interface.Checked(Interface.uc_reg_write(uc, (int)register, data));
uc.RegWrite(register, data);
data = BitConverter.GetBytes(value.GetUInt64(1));
Interface.Checked(Interface.uc_reg_write(uc, (int)register + 1, data));
uc.RegWrite(register + 1, data);
}
public byte[] MemoryRead(ulong address, ulong size)
{
byte[] value = new byte[size];
Interface.Checked(Interface.uc_mem_read(uc, address, value, size));
uc.MemRead((long)address, value);
return value;
}
public byte MemoryRead8(ulong address) => MemoryRead(address, 1)[0];
public UInt16 MemoryRead16(ulong address) => (UInt16)BitConverter.ToInt16(MemoryRead(address, 2), 0);
public UInt32 MemoryRead32(ulong address) => (UInt32)BitConverter.ToInt32(MemoryRead(address, 4), 0);
public UInt64 MemoryRead64(ulong address) => (UInt64)BitConverter.ToInt64(MemoryRead(address, 8), 0);
public ushort MemoryRead16(ulong address) => BitConverter.ToUInt16(MemoryRead(address, 2), 0);
public uint MemoryRead32(ulong address) => BitConverter.ToUInt32(MemoryRead(address, 4), 0);
public ulong MemoryRead64(ulong address) => BitConverter.ToUInt64(MemoryRead(address, 8), 0);
public void MemoryWrite(ulong address, byte[] value)
{
Interface.Checked(Interface.uc_mem_write(uc, address, value, (ulong)value.Length));
uc.MemWrite((long)address, value);
}
public void MemoryWrite8(ulong address, byte value) => MemoryWrite(address, new byte[] { value });
public void MemoryWrite16(ulong address, Int16 value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite16(ulong address, UInt16 value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite32(ulong address, Int32 value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite32(ulong address, UInt32 value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite64(ulong address, Int64 value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite64(ulong address, UInt64 value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite8(ulong address, byte value) => MemoryWrite(address, new[] { value });
public void MemoryWrite16(ulong address, short value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite16(ulong address, ushort value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite32(ulong address, int value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite32(ulong address, uint value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite64(ulong address, long value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryWrite64(ulong address, ulong value) => MemoryWrite(address, BitConverter.GetBytes(value));
public void MemoryMap(ulong address, ulong size, MemoryPermission permissions)
{
Interface.Checked(Interface.uc_mem_map(uc, address, size, (uint)permissions));
uc.MemMap((long)address, (long)size, (int)permissions);
}
public void MemoryUnmap(ulong address, ulong size)
{
Interface.Checked(Interface.uc_mem_unmap(uc, address, size));
uc.MemUnmap((long)address, (long)size);
}
public void MemoryProtect(ulong address, ulong size, MemoryPermission permissions)
{
Interface.Checked(Interface.uc_mem_protect(uc, address, size, (uint)permissions));
}
public static bool IsAvailable()
{
try
{
Interface.uc_version(out _, out _);
}
catch (DllNotFoundException) { }
return Interface.IsUnicornAvailable;
uc.MemProtect((long)address, (long)size, (int)permissions);
}
}
}