CpuTest.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. using ChocolArm64;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using NUnit.Framework;
  5. using Ryujinx.Tests.Unicorn;
  6. using System;
  7. using System.Runtime.InteropServices;
  8. using System.Runtime.Intrinsics;
  9. using System.Runtime.Intrinsics.X86;
  10. using System.Threading;
  11. namespace Ryujinx.Tests.Cpu
  12. {
  13. [TestFixture]
  14. public class CpuTest
  15. {
  16. protected long Position { get; private set; }
  17. private long Size;
  18. private long EntryPoint;
  19. private IntPtr RamPointer;
  20. private AMemory Memory;
  21. private AThread Thread;
  22. private static bool UnicornAvailable;
  23. private UnicornAArch64 UnicornEmu;
  24. static CpuTest()
  25. {
  26. UnicornAvailable = UnicornAArch64.IsAvailable();
  27. if (!UnicornAvailable)
  28. {
  29. Console.WriteLine("WARNING: Could not find Unicorn.");
  30. }
  31. }
  32. [SetUp]
  33. public void Setup()
  34. {
  35. Position = 0x1000;
  36. Size = 0x1000;
  37. EntryPoint = Position;
  38. ATranslator Translator = new ATranslator();
  39. RamPointer = Marshal.AllocHGlobal(new IntPtr(Size));
  40. Memory = new AMemory(RamPointer);
  41. Memory.Map(Position, 0, Size);
  42. Thread = new AThread(Translator, Memory, EntryPoint);
  43. if (UnicornAvailable)
  44. {
  45. UnicornEmu = new UnicornAArch64();
  46. UnicornEmu.MemoryMap((ulong)Position, (ulong)Size, MemoryPermission.READ | MemoryPermission.EXEC);
  47. UnicornEmu.PC = (ulong)EntryPoint;
  48. }
  49. }
  50. [TearDown]
  51. public void Teardown()
  52. {
  53. Marshal.FreeHGlobal(RamPointer);
  54. Memory = null;
  55. Thread = null;
  56. UnicornEmu = null;
  57. }
  58. protected void Reset()
  59. {
  60. Teardown();
  61. Setup();
  62. }
  63. protected void Opcode(uint Opcode)
  64. {
  65. Thread.Memory.WriteUInt32(Position, Opcode);
  66. if (UnicornAvailable)
  67. {
  68. UnicornEmu.MemoryWrite32((ulong)Position, Opcode);
  69. }
  70. Position += 4;
  71. }
  72. protected void SetThreadState(ulong X0 = 0, ulong X1 = 0, ulong X2 = 0, ulong X3 = 0, ulong X31 = 0,
  73. Vector128<float> V0 = default(Vector128<float>),
  74. Vector128<float> V1 = default(Vector128<float>),
  75. Vector128<float> V2 = default(Vector128<float>),
  76. Vector128<float> V3 = default(Vector128<float>),
  77. bool Overflow = false, bool Carry = false, bool Zero = false, bool Negative = false,
  78. int Fpcr = 0x0, int Fpsr = 0x0)
  79. {
  80. Thread.ThreadState.X0 = X0;
  81. Thread.ThreadState.X1 = X1;
  82. Thread.ThreadState.X2 = X2;
  83. Thread.ThreadState.X3 = X3;
  84. Thread.ThreadState.X31 = X31;
  85. Thread.ThreadState.V0 = V0;
  86. Thread.ThreadState.V1 = V1;
  87. Thread.ThreadState.V2 = V2;
  88. Thread.ThreadState.V3 = V3;
  89. Thread.ThreadState.Overflow = Overflow;
  90. Thread.ThreadState.Carry = Carry;
  91. Thread.ThreadState.Zero = Zero;
  92. Thread.ThreadState.Negative = Negative;
  93. Thread.ThreadState.Fpcr = Fpcr;
  94. Thread.ThreadState.Fpsr = Fpsr;
  95. if (UnicornAvailable)
  96. {
  97. UnicornEmu.X[0] = X0;
  98. UnicornEmu.X[1] = X1;
  99. UnicornEmu.X[2] = X2;
  100. UnicornEmu.X[3] = X3;
  101. UnicornEmu.SP = X31;
  102. UnicornEmu.Q[0] = V0;
  103. UnicornEmu.Q[1] = V1;
  104. UnicornEmu.Q[2] = V2;
  105. UnicornEmu.Q[3] = V3;
  106. UnicornEmu.OverflowFlag = Overflow;
  107. UnicornEmu.CarryFlag = Carry;
  108. UnicornEmu.ZeroFlag = Zero;
  109. UnicornEmu.NegativeFlag = Negative;
  110. UnicornEmu.Fpcr = Fpcr;
  111. UnicornEmu.Fpsr = Fpsr;
  112. }
  113. }
  114. protected void ExecuteOpcodes()
  115. {
  116. using (ManualResetEvent Wait = new ManualResetEvent(false))
  117. {
  118. Thread.ThreadState.Break += (sender, e) => Thread.StopExecution();
  119. Thread.WorkFinished += (sender, e) => Wait.Set();
  120. Thread.Execute();
  121. Wait.WaitOne();
  122. }
  123. if (UnicornAvailable)
  124. {
  125. UnicornEmu.RunForCount((ulong)(Position - EntryPoint - 8) / 4);
  126. }
  127. }
  128. protected AThreadState GetThreadState() => Thread.ThreadState;
  129. protected AThreadState SingleOpcode(uint Opcode,
  130. ulong X0 = 0, ulong X1 = 0, ulong X2 = 0, ulong X3 = 0, ulong X31 = 0,
  131. Vector128<float> V0 = default(Vector128<float>),
  132. Vector128<float> V1 = default(Vector128<float>),
  133. Vector128<float> V2 = default(Vector128<float>),
  134. Vector128<float> V3 = default(Vector128<float>),
  135. bool Overflow = false, bool Carry = false, bool Zero = false, bool Negative = false,
  136. int Fpcr = 0x0, int Fpsr = 0x0)
  137. {
  138. this.Opcode(Opcode);
  139. this.Opcode(0xD4200000); // BRK #0
  140. this.Opcode(0xD65F03C0); // RET
  141. SetThreadState(X0, X1, X2, X3, X31, V0, V1, V2, V3, Overflow, Carry, Zero, Negative, Fpcr, Fpsr);
  142. ExecuteOpcodes();
  143. return GetThreadState();
  144. }
  145. [Flags]
  146. protected enum FPSR
  147. {
  148. None = 0,
  149. /// <summary>Invalid Operation cumulative floating-point exception bit.</summary>
  150. IOC = 1 << 0,
  151. /// <summary>Divide by Zero cumulative floating-point exception bit.</summary>
  152. DZC = 1 << 1,
  153. /// <summary>Overflow cumulative floating-point exception bit.</summary>
  154. OFC = 1 << 2,
  155. /// <summary>Underflow cumulative floating-point exception bit.</summary>
  156. UFC = 1 << 3,
  157. /// <summary>Inexact cumulative floating-point exception bit.</summary>
  158. IXC = 1 << 4,
  159. /// <summary>Input Denormal cumulative floating-point exception bit.</summary>
  160. IDC = 1 << 7,
  161. /// <summary>Cumulative saturation bit.</summary>
  162. QC = 1 << 27
  163. }
  164. protected enum FpSkips { None, IfNaN_S, IfNaN_D };
  165. protected enum FpUseTolerance { None, OneUlps_S, OneUlps_D };
  166. protected void CompareAgainstUnicorn(
  167. FPSR FpsrMask = FPSR.None,
  168. FpSkips FpSkips = FpSkips.None,
  169. FpUseTolerance FpUseTolerance = FpUseTolerance.None)
  170. {
  171. if (!UnicornAvailable)
  172. {
  173. return;
  174. }
  175. if (FpSkips == FpSkips.IfNaN_S && float.IsNaN(VectorExtractSingle(UnicornEmu.Q[0], (byte)0)))
  176. {
  177. Assert.Ignore("NaN test.");
  178. }
  179. if (FpSkips == FpSkips.IfNaN_D && double.IsNaN(VectorExtractDouble(UnicornEmu.Q[0], (byte)0)))
  180. {
  181. Assert.Ignore("NaN test.");
  182. }
  183. Assert.That(Thread.ThreadState.X0, Is.EqualTo(UnicornEmu.X[0]));
  184. Assert.That(Thread.ThreadState.X1, Is.EqualTo(UnicornEmu.X[1]));
  185. Assert.That(Thread.ThreadState.X2, Is.EqualTo(UnicornEmu.X[2]));
  186. Assert.That(Thread.ThreadState.X3, Is.EqualTo(UnicornEmu.X[3]));
  187. Assert.That(Thread.ThreadState.X4, Is.EqualTo(UnicornEmu.X[4]));
  188. Assert.That(Thread.ThreadState.X5, Is.EqualTo(UnicornEmu.X[5]));
  189. Assert.That(Thread.ThreadState.X6, Is.EqualTo(UnicornEmu.X[6]));
  190. Assert.That(Thread.ThreadState.X7, Is.EqualTo(UnicornEmu.X[7]));
  191. Assert.That(Thread.ThreadState.X8, Is.EqualTo(UnicornEmu.X[8]));
  192. Assert.That(Thread.ThreadState.X9, Is.EqualTo(UnicornEmu.X[9]));
  193. Assert.That(Thread.ThreadState.X10, Is.EqualTo(UnicornEmu.X[10]));
  194. Assert.That(Thread.ThreadState.X11, Is.EqualTo(UnicornEmu.X[11]));
  195. Assert.That(Thread.ThreadState.X12, Is.EqualTo(UnicornEmu.X[12]));
  196. Assert.That(Thread.ThreadState.X13, Is.EqualTo(UnicornEmu.X[13]));
  197. Assert.That(Thread.ThreadState.X14, Is.EqualTo(UnicornEmu.X[14]));
  198. Assert.That(Thread.ThreadState.X15, Is.EqualTo(UnicornEmu.X[15]));
  199. Assert.That(Thread.ThreadState.X16, Is.EqualTo(UnicornEmu.X[16]));
  200. Assert.That(Thread.ThreadState.X17, Is.EqualTo(UnicornEmu.X[17]));
  201. Assert.That(Thread.ThreadState.X18, Is.EqualTo(UnicornEmu.X[18]));
  202. Assert.That(Thread.ThreadState.X19, Is.EqualTo(UnicornEmu.X[19]));
  203. Assert.That(Thread.ThreadState.X20, Is.EqualTo(UnicornEmu.X[20]));
  204. Assert.That(Thread.ThreadState.X21, Is.EqualTo(UnicornEmu.X[21]));
  205. Assert.That(Thread.ThreadState.X22, Is.EqualTo(UnicornEmu.X[22]));
  206. Assert.That(Thread.ThreadState.X23, Is.EqualTo(UnicornEmu.X[23]));
  207. Assert.That(Thread.ThreadState.X24, Is.EqualTo(UnicornEmu.X[24]));
  208. Assert.That(Thread.ThreadState.X25, Is.EqualTo(UnicornEmu.X[25]));
  209. Assert.That(Thread.ThreadState.X26, Is.EqualTo(UnicornEmu.X[26]));
  210. Assert.That(Thread.ThreadState.X27, Is.EqualTo(UnicornEmu.X[27]));
  211. Assert.That(Thread.ThreadState.X28, Is.EqualTo(UnicornEmu.X[28]));
  212. Assert.That(Thread.ThreadState.X29, Is.EqualTo(UnicornEmu.X[29]));
  213. Assert.That(Thread.ThreadState.X30, Is.EqualTo(UnicornEmu.X[30]));
  214. Assert.That(Thread.ThreadState.X31, Is.EqualTo(UnicornEmu.SP));
  215. if (FpUseTolerance == FpUseTolerance.None)
  216. {
  217. Assert.That(Thread.ThreadState.V0, Is.EqualTo(UnicornEmu.Q[0]));
  218. }
  219. else
  220. {
  221. if (!Is.EqualTo(UnicornEmu.Q[0]).ApplyTo(Thread.ThreadState.V0).IsSuccess)
  222. {
  223. if (FpUseTolerance == FpUseTolerance.OneUlps_S)
  224. {
  225. if (float.IsNormal (VectorExtractSingle(UnicornEmu.Q[0], (byte)0)) ||
  226. float.IsSubnormal(VectorExtractSingle(UnicornEmu.Q[0], (byte)0)))
  227. {
  228. Assert.That (VectorExtractSingle(Thread.ThreadState.V0, (byte)0),
  229. Is.EqualTo(VectorExtractSingle(UnicornEmu.Q[0], (byte)0)).Within(1).Ulps);
  230. Assert.That (VectorExtractSingle(Thread.ThreadState.V0, (byte)1),
  231. Is.EqualTo(VectorExtractSingle(UnicornEmu.Q[0], (byte)1)).Within(1).Ulps);
  232. Assert.That (VectorExtractSingle(Thread.ThreadState.V0, (byte)2),
  233. Is.EqualTo(VectorExtractSingle(UnicornEmu.Q[0], (byte)2)).Within(1).Ulps);
  234. Assert.That (VectorExtractSingle(Thread.ThreadState.V0, (byte)3),
  235. Is.EqualTo(VectorExtractSingle(UnicornEmu.Q[0], (byte)3)).Within(1).Ulps);
  236. }
  237. else
  238. {
  239. Assert.That(Thread.ThreadState.V0, Is.EqualTo(UnicornEmu.Q[0]));
  240. }
  241. }
  242. if (FpUseTolerance == FpUseTolerance.OneUlps_D)
  243. {
  244. if (double.IsNormal (VectorExtractDouble(UnicornEmu.Q[0], (byte)0)) ||
  245. double.IsSubnormal(VectorExtractDouble(UnicornEmu.Q[0], (byte)0)))
  246. {
  247. Assert.That (VectorExtractDouble(Thread.ThreadState.V0, (byte)0),
  248. Is.EqualTo(VectorExtractDouble(UnicornEmu.Q[0], (byte)0)).Within(1).Ulps);
  249. Assert.That (VectorExtractDouble(Thread.ThreadState.V0, (byte)1),
  250. Is.EqualTo(VectorExtractDouble(UnicornEmu.Q[0], (byte)1)).Within(1).Ulps);
  251. }
  252. else
  253. {
  254. Assert.That(Thread.ThreadState.V0, Is.EqualTo(UnicornEmu.Q[0]));
  255. }
  256. }
  257. }
  258. }
  259. Assert.That(Thread.ThreadState.V1, Is.EqualTo(UnicornEmu.Q[1]));
  260. Assert.That(Thread.ThreadState.V2, Is.EqualTo(UnicornEmu.Q[2]));
  261. Assert.That(Thread.ThreadState.V3, Is.EqualTo(UnicornEmu.Q[3]));
  262. Assert.That(Thread.ThreadState.V4, Is.EqualTo(UnicornEmu.Q[4]));
  263. Assert.That(Thread.ThreadState.V5, Is.EqualTo(UnicornEmu.Q[5]));
  264. Assert.That(Thread.ThreadState.V6, Is.EqualTo(UnicornEmu.Q[6]));
  265. Assert.That(Thread.ThreadState.V7, Is.EqualTo(UnicornEmu.Q[7]));
  266. Assert.That(Thread.ThreadState.V8, Is.EqualTo(UnicornEmu.Q[8]));
  267. Assert.That(Thread.ThreadState.V9, Is.EqualTo(UnicornEmu.Q[9]));
  268. Assert.That(Thread.ThreadState.V10, Is.EqualTo(UnicornEmu.Q[10]));
  269. Assert.That(Thread.ThreadState.V11, Is.EqualTo(UnicornEmu.Q[11]));
  270. Assert.That(Thread.ThreadState.V12, Is.EqualTo(UnicornEmu.Q[12]));
  271. Assert.That(Thread.ThreadState.V13, Is.EqualTo(UnicornEmu.Q[13]));
  272. Assert.That(Thread.ThreadState.V14, Is.EqualTo(UnicornEmu.Q[14]));
  273. Assert.That(Thread.ThreadState.V15, Is.EqualTo(UnicornEmu.Q[15]));
  274. Assert.That(Thread.ThreadState.V16, Is.EqualTo(UnicornEmu.Q[16]));
  275. Assert.That(Thread.ThreadState.V17, Is.EqualTo(UnicornEmu.Q[17]));
  276. Assert.That(Thread.ThreadState.V18, Is.EqualTo(UnicornEmu.Q[18]));
  277. Assert.That(Thread.ThreadState.V19, Is.EqualTo(UnicornEmu.Q[19]));
  278. Assert.That(Thread.ThreadState.V20, Is.EqualTo(UnicornEmu.Q[20]));
  279. Assert.That(Thread.ThreadState.V21, Is.EqualTo(UnicornEmu.Q[21]));
  280. Assert.That(Thread.ThreadState.V22, Is.EqualTo(UnicornEmu.Q[22]));
  281. Assert.That(Thread.ThreadState.V23, Is.EqualTo(UnicornEmu.Q[23]));
  282. Assert.That(Thread.ThreadState.V24, Is.EqualTo(UnicornEmu.Q[24]));
  283. Assert.That(Thread.ThreadState.V25, Is.EqualTo(UnicornEmu.Q[25]));
  284. Assert.That(Thread.ThreadState.V26, Is.EqualTo(UnicornEmu.Q[26]));
  285. Assert.That(Thread.ThreadState.V27, Is.EqualTo(UnicornEmu.Q[27]));
  286. Assert.That(Thread.ThreadState.V28, Is.EqualTo(UnicornEmu.Q[28]));
  287. Assert.That(Thread.ThreadState.V29, Is.EqualTo(UnicornEmu.Q[29]));
  288. Assert.That(Thread.ThreadState.V30, Is.EqualTo(UnicornEmu.Q[30]));
  289. Assert.That(Thread.ThreadState.V31, Is.EqualTo(UnicornEmu.Q[31]));
  290. Assert.That(Thread.ThreadState.V31, Is.EqualTo(UnicornEmu.Q[31]));
  291. Assert.That(Thread.ThreadState.Fpcr, Is.EqualTo(UnicornEmu.Fpcr));
  292. Assert.That(Thread.ThreadState.Fpsr & (int)FpsrMask, Is.EqualTo(UnicornEmu.Fpsr & (int)FpsrMask));
  293. Assert.That(Thread.ThreadState.Overflow, Is.EqualTo(UnicornEmu.OverflowFlag));
  294. Assert.That(Thread.ThreadState.Carry, Is.EqualTo(UnicornEmu.CarryFlag));
  295. Assert.That(Thread.ThreadState.Zero, Is.EqualTo(UnicornEmu.ZeroFlag));
  296. Assert.That(Thread.ThreadState.Negative, Is.EqualTo(UnicornEmu.NegativeFlag));
  297. }
  298. protected static Vector128<float> MakeVectorE0(double E0)
  299. {
  300. if (!Sse2.IsSupported)
  301. {
  302. throw new PlatformNotSupportedException();
  303. }
  304. return Sse.StaticCast<long, float>(Sse2.SetVector128(0, BitConverter.DoubleToInt64Bits(E0)));
  305. }
  306. protected static Vector128<float> MakeVectorE0E1(double E0, double E1)
  307. {
  308. if (!Sse2.IsSupported)
  309. {
  310. throw new PlatformNotSupportedException();
  311. }
  312. return Sse.StaticCast<long, float>(
  313. Sse2.SetVector128(BitConverter.DoubleToInt64Bits(E1), BitConverter.DoubleToInt64Bits(E0)));
  314. }
  315. protected static Vector128<float> MakeVectorE1(double E1)
  316. {
  317. if (!Sse2.IsSupported)
  318. {
  319. throw new PlatformNotSupportedException();
  320. }
  321. return Sse.StaticCast<long, float>(Sse2.SetVector128(BitConverter.DoubleToInt64Bits(E1), 0));
  322. }
  323. protected static float VectorExtractSingle(Vector128<float> Vector, byte Index)
  324. {
  325. if (!Sse41.IsSupported)
  326. {
  327. throw new PlatformNotSupportedException();
  328. }
  329. int Value = Sse41.Extract(Sse.StaticCast<float, int>(Vector), Index);
  330. return BitConverter.Int32BitsToSingle(Value);
  331. }
  332. protected static double VectorExtractDouble(Vector128<float> Vector, byte Index)
  333. {
  334. if (!Sse41.IsSupported)
  335. {
  336. throw new PlatformNotSupportedException();
  337. }
  338. long Value = Sse41.Extract(Sse.StaticCast<float, long>(Vector), Index);
  339. return BitConverter.Int64BitsToDouble(Value);
  340. }
  341. protected static Vector128<float> MakeVectorE0(ulong E0)
  342. {
  343. if (!Sse2.IsSupported)
  344. {
  345. throw new PlatformNotSupportedException();
  346. }
  347. return Sse.StaticCast<ulong, float>(Sse2.SetVector128(0, E0));
  348. }
  349. protected static Vector128<float> MakeVectorE0E1(ulong E0, ulong E1)
  350. {
  351. if (!Sse2.IsSupported)
  352. {
  353. throw new PlatformNotSupportedException();
  354. }
  355. return Sse.StaticCast<ulong, float>(Sse2.SetVector128(E1, E0));
  356. }
  357. protected static Vector128<float> MakeVectorE1(ulong E1)
  358. {
  359. if (!Sse2.IsSupported)
  360. {
  361. throw new PlatformNotSupportedException();
  362. }
  363. return Sse.StaticCast<ulong, float>(Sse2.SetVector128(E1, 0));
  364. }
  365. protected static ulong GetVectorE0(Vector128<float> Vector)
  366. {
  367. if (!Sse41.IsSupported)
  368. {
  369. throw new PlatformNotSupportedException();
  370. }
  371. return Sse41.Extract(Sse.StaticCast<float, ulong>(Vector), (byte)0);
  372. }
  373. protected static ulong GetVectorE1(Vector128<float> Vector)
  374. {
  375. if (!Sse41.IsSupported)
  376. {
  377. throw new PlatformNotSupportedException();
  378. }
  379. return Sse41.Extract(Sse.StaticCast<float, ulong>(Vector), (byte)1);
  380. }
  381. protected static uint GenNormal_S()
  382. {
  383. uint Rnd;
  384. do Rnd = TestContext.CurrentContext.Random.NextUInt();
  385. while ((Rnd & 0x7F800000u) == 0u ||
  386. (Rnd & 0x7F800000u) == 0x7F800000u);
  387. return Rnd;
  388. }
  389. protected static uint GenSubNormal_S()
  390. {
  391. uint Rnd;
  392. do Rnd = TestContext.CurrentContext.Random.NextUInt();
  393. while ((Rnd & 0x007FFFFFu) == 0u);
  394. return Rnd & 0x807FFFFFu;
  395. }
  396. protected static ulong GenNormal_D()
  397. {
  398. ulong Rnd;
  399. do Rnd = TestContext.CurrentContext.Random.NextULong();
  400. while ((Rnd & 0x7FF0000000000000ul) == 0ul ||
  401. (Rnd & 0x7FF0000000000000ul) == 0x7FF0000000000000ul);
  402. return Rnd;
  403. }
  404. protected static ulong GenSubNormal_D()
  405. {
  406. ulong Rnd;
  407. do Rnd = TestContext.CurrentContext.Random.NextULong();
  408. while ((Rnd & 0x000FFFFFFFFFFFFFul) == 0ul);
  409. return Rnd & 0x800FFFFFFFFFFFFFul;
  410. }
  411. }
  412. }