CpuTest.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using ChocolArm64;
  2. using ChocolArm64.Memory;
  3. using ChocolArm64.State;
  4. using NUnit.Framework;
  5. using System;
  6. using System.Runtime.InteropServices;
  7. using System.Threading;
  8. namespace Ryujinx.Tests.Cpu
  9. {
  10. [TestFixture]
  11. public partial class CpuTest
  12. {
  13. IntPtr Ram;
  14. AMemoryAlloc Allocator;
  15. AMemory Memory;
  16. [SetUp]
  17. public void Setup()
  18. {
  19. Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize);
  20. Allocator = new AMemoryAlloc();
  21. Memory = new AMemory(Ram, Allocator);
  22. Memory.Manager.MapPhys(0x1000, 0x1000, 2, AMemoryPerm.Read | AMemoryPerm.Write | AMemoryPerm.Execute);
  23. }
  24. [TearDown]
  25. public void Teardown()
  26. {
  27. Marshal.FreeHGlobal(Ram);
  28. }
  29. private void Execute(AThread Thread)
  30. {
  31. AutoResetEvent Wait = new AutoResetEvent(false);
  32. Thread.Registers.Break += (sender, e) => Thread.StopExecution();
  33. Thread.WorkFinished += (sender, e) => Wait.Set();
  34. Wait.Reset();
  35. Thread.Execute();
  36. Wait.WaitOne();
  37. }
  38. private ARegisters SingleOpcode(uint Opcode,
  39. ulong X0 = 0, ulong X1 = 0, ulong X2 = 0,
  40. AVec V0 = new AVec(), AVec V1 = new AVec(), AVec V2 = new AVec())
  41. {
  42. Memory.WriteUInt32(0x1000, Opcode);
  43. Memory.WriteUInt32(0x1004, 0xD4200000); // BRK #0
  44. Memory.WriteUInt32(0x1008, 0xD65F03C0); // RET
  45. AThread Thread = new AThread(Memory, ThreadPriority.Normal, 0x1000);
  46. Thread.Registers.X0 = X0;
  47. Thread.Registers.X1 = X1;
  48. Thread.Registers.X2 = X2;
  49. Thread.Registers.V0 = V0;
  50. Thread.Registers.V1 = V1;
  51. Thread.Registers.V2 = V2;
  52. Execute(Thread);
  53. return Thread.Registers;
  54. }
  55. [Test]
  56. public void SanityCheck()
  57. {
  58. uint Opcode = 0xD503201F; // NOP
  59. Assert.AreEqual(SingleOpcode(Opcode, X0: 0).X0, 0);
  60. Assert.AreEqual(SingleOpcode(Opcode, X0: 1).X0, 1);
  61. Assert.AreEqual(SingleOpcode(Opcode, X0: 2).X0, 2);
  62. Assert.AreEqual(SingleOpcode(Opcode, X0: 42).X0, 42);
  63. }
  64. }
  65. }