CpuTest.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 class CpuTest
  12. {
  13. protected long Position { get; private set; }
  14. private long Size;
  15. private long EntryPoint;
  16. private IntPtr Ram;
  17. private AMemoryAlloc Allocator;
  18. private AMemory Memory;
  19. private AThread Thread;
  20. [SetUp]
  21. public void Setup()
  22. {
  23. Position = 0x0;
  24. Size = 0x1000;
  25. EntryPoint = Position;
  26. Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize);
  27. Allocator = new AMemoryAlloc();
  28. Memory = new AMemory(Ram, Allocator);
  29. Memory.Manager.MapPhys(Position, Size, 2, AMemoryPerm.Read | AMemoryPerm.Write | AMemoryPerm.Execute);
  30. Thread = new AThread(Memory, ThreadPriority.Normal, EntryPoint);
  31. }
  32. [TearDown]
  33. public void Teardown()
  34. {
  35. Thread = null;
  36. Memory = null;
  37. Allocator = null;
  38. Marshal.FreeHGlobal(Ram);
  39. }
  40. protected void Reset()
  41. {
  42. Teardown();
  43. Setup();
  44. }
  45. protected void Opcode(uint Opcode)
  46. {
  47. Thread.Memory.WriteUInt32(Position, Opcode);
  48. Position += 4;
  49. }
  50. protected void SetThreadState(ulong X0 = 0, ulong X1 = 0, ulong X2 = 0,
  51. AVec V0 = default(AVec), AVec V1 = default(AVec), AVec V2 = default(AVec))
  52. {
  53. Thread.ThreadState.X0 = X0;
  54. Thread.ThreadState.X1 = X1;
  55. Thread.ThreadState.X2 = X2;
  56. Thread.ThreadState.V0 = V0;
  57. Thread.ThreadState.V1 = V1;
  58. Thread.ThreadState.V2 = V2;
  59. }
  60. protected void ExecuteOpcodes()
  61. {
  62. using (ManualResetEvent Wait = new ManualResetEvent(false))
  63. {
  64. Thread.ThreadState.Break += (sender, e) => Thread.StopExecution();
  65. Thread.WorkFinished += (sender, e) => Wait.Set();
  66. Thread.Execute();
  67. Wait.WaitOne();
  68. }
  69. }
  70. protected AThreadState GetThreadState()
  71. {
  72. return Thread.ThreadState;
  73. }
  74. protected AThreadState SingleOpcode(uint Opcode,
  75. ulong X0 = 0, ulong X1 = 0, ulong X2 = 0,
  76. AVec V0 = default(AVec), AVec V1 = default(AVec), AVec V2 = default(AVec))
  77. {
  78. this.Opcode(Opcode);
  79. this.Opcode(0xD4200000); // BRK #0
  80. this.Opcode(0xD65F03C0); // RET
  81. SetThreadState(X0, X1, X2, V0, V1, V2);
  82. ExecuteOpcodes();
  83. return GetThreadState();
  84. }
  85. }
  86. }