UnicornAArch32.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. using System;
  2. using UnicornEngine.Const;
  3. namespace Ryujinx.Tests.Unicorn
  4. {
  5. public class UnicornAArch32 : IDisposable
  6. {
  7. internal readonly UnicornEngine.Unicorn uc;
  8. private bool _isDisposed;
  9. public IndexedProperty<int, uint> R => new(GetX, SetX);
  10. public IndexedProperty<int, SimdValue> Q => new(GetQ, SetQ);
  11. public uint LR
  12. {
  13. get => GetRegister(Arm.UC_ARM_REG_LR);
  14. set => SetRegister(Arm.UC_ARM_REG_LR, value);
  15. }
  16. public uint SP
  17. {
  18. get => GetRegister(Arm.UC_ARM_REG_SP);
  19. set => SetRegister(Arm.UC_ARM_REG_SP, value);
  20. }
  21. public uint PC
  22. {
  23. get => GetRegister(Arm.UC_ARM_REG_PC) & 0xfffffffeu;
  24. set => SetRegister(Arm.UC_ARM_REG_PC, (value & 0xfffffffeu) | (ThumbFlag ? 1u : 0u));
  25. }
  26. public uint CPSR
  27. {
  28. get => GetRegister(Arm.UC_ARM_REG_CPSR);
  29. set => SetRegister(Arm.UC_ARM_REG_CPSR, value);
  30. }
  31. public int Fpscr
  32. {
  33. get => (int)GetRegister(Arm.UC_ARM_REG_FPSCR) | ((int)GetRegister(Arm.UC_ARM_REG_FPSCR_NZCV));
  34. set => SetRegister(Arm.UC_ARM_REG_FPSCR, (uint)value);
  35. }
  36. public bool QFlag
  37. {
  38. get => (CPSR & 0x8000000u) != 0;
  39. set => CPSR = (CPSR & ~0x8000000u) | (value ? 0x8000000u : 0u);
  40. }
  41. public bool OverflowFlag
  42. {
  43. get => (CPSR & 0x10000000u) != 0;
  44. set => CPSR = (CPSR & ~0x10000000u) | (value ? 0x10000000u : 0u);
  45. }
  46. public bool CarryFlag
  47. {
  48. get => (CPSR & 0x20000000u) != 0;
  49. set => CPSR = (CPSR & ~0x20000000u) | (value ? 0x20000000u : 0u);
  50. }
  51. public bool ZeroFlag
  52. {
  53. get => (CPSR & 0x40000000u) != 0;
  54. set => CPSR = (CPSR & ~0x40000000u) | (value ? 0x40000000u : 0u);
  55. }
  56. public bool NegativeFlag
  57. {
  58. get => (CPSR & 0x80000000u) != 0;
  59. set => CPSR = (CPSR & ~0x80000000u) | (value ? 0x80000000u : 0u);
  60. }
  61. public bool ThumbFlag
  62. {
  63. get => (CPSR & 0x00000020u) != 0;
  64. set
  65. {
  66. CPSR = (CPSR & ~0x00000020u) | (value ? 0x00000020u : 0u);
  67. SetRegister(Arm.UC_ARM_REG_PC, (GetRegister(Arm.UC_ARM_REG_PC) & 0xfffffffeu) | (value ? 1u : 0u));
  68. }
  69. }
  70. public UnicornAArch32()
  71. {
  72. uc = new UnicornEngine.Unicorn(Common.UC_ARCH_ARM, Common.UC_MODE_LITTLE_ENDIAN);
  73. SetRegister(Arm.UC_ARM_REG_C1_C0_2, GetRegister(Arm.UC_ARM_REG_C1_C0_2) | 0xf00000);
  74. SetRegister(Arm.UC_ARM_REG_FPEXC, 0x40000000);
  75. }
  76. ~UnicornAArch32()
  77. {
  78. Dispose(false);
  79. }
  80. public void Dispose()
  81. {
  82. Dispose(true);
  83. GC.SuppressFinalize(this);
  84. }
  85. protected virtual void Dispose(bool disposing)
  86. {
  87. if (!_isDisposed)
  88. {
  89. uc.Close();
  90. _isDisposed = true;
  91. }
  92. }
  93. public void RunForCount(ulong count)
  94. {
  95. // FIXME: untilAddr should be 0xFFFFFFFFFFFFFFFFu
  96. uc.EmuStart(this.PC, -1, 0, (long)count);
  97. }
  98. public void Step()
  99. {
  100. RunForCount(1);
  101. }
  102. private static int[] XRegisters =
  103. {
  104. Arm.UC_ARM_REG_R0,
  105. Arm.UC_ARM_REG_R1,
  106. Arm.UC_ARM_REG_R2,
  107. Arm.UC_ARM_REG_R3,
  108. Arm.UC_ARM_REG_R4,
  109. Arm.UC_ARM_REG_R5,
  110. Arm.UC_ARM_REG_R6,
  111. Arm.UC_ARM_REG_R7,
  112. Arm.UC_ARM_REG_R8,
  113. Arm.UC_ARM_REG_R9,
  114. Arm.UC_ARM_REG_R10,
  115. Arm.UC_ARM_REG_R11,
  116. Arm.UC_ARM_REG_R12,
  117. Arm.UC_ARM_REG_R13,
  118. Arm.UC_ARM_REG_R14,
  119. Arm.UC_ARM_REG_R15,
  120. };
  121. private static int[] QRegisters =
  122. {
  123. Arm.UC_ARM_REG_Q0,
  124. Arm.UC_ARM_REG_Q1,
  125. Arm.UC_ARM_REG_Q2,
  126. Arm.UC_ARM_REG_Q3,
  127. Arm.UC_ARM_REG_Q4,
  128. Arm.UC_ARM_REG_Q5,
  129. Arm.UC_ARM_REG_Q6,
  130. Arm.UC_ARM_REG_Q7,
  131. Arm.UC_ARM_REG_Q8,
  132. Arm.UC_ARM_REG_Q9,
  133. Arm.UC_ARM_REG_Q10,
  134. Arm.UC_ARM_REG_Q11,
  135. Arm.UC_ARM_REG_Q12,
  136. Arm.UC_ARM_REG_Q13,
  137. Arm.UC_ARM_REG_Q14,
  138. Arm.UC_ARM_REG_Q15
  139. };
  140. public uint GetX(int index)
  141. {
  142. if ((uint)index > 15)
  143. {
  144. throw new ArgumentOutOfRangeException(nameof(index));
  145. }
  146. return GetRegister(XRegisters[index]);
  147. }
  148. public void SetX(int index, uint value)
  149. {
  150. if ((uint)index > 15)
  151. {
  152. throw new ArgumentOutOfRangeException(nameof(index));
  153. }
  154. SetRegister(XRegisters[index], value);
  155. }
  156. public SimdValue GetQ(int index)
  157. {
  158. if ((uint)index > 15)
  159. {
  160. throw new ArgumentOutOfRangeException(nameof(index));
  161. }
  162. // Getting quadword registers from Unicorn A32 seems to be broken, so we combine its 2 doubleword registers instead.
  163. return GetVector(Arm.UC_ARM_REG_D0 + index * 2);
  164. }
  165. public void SetQ(int index, SimdValue value)
  166. {
  167. if ((uint)index > 15)
  168. {
  169. throw new ArgumentOutOfRangeException(nameof(index));
  170. }
  171. SetVector(Arm.UC_ARM_REG_D0 + index * 2, value);
  172. }
  173. public uint GetRegister(int register)
  174. {
  175. byte[] data = new byte[4];
  176. uc.RegRead(register, data);
  177. return BitConverter.ToUInt32(data, 0);
  178. }
  179. public void SetRegister(int register, uint value)
  180. {
  181. byte[] data = BitConverter.GetBytes(value);
  182. uc.RegWrite(register, data);
  183. }
  184. public SimdValue GetVector(int register)
  185. {
  186. byte[] data = new byte[8];
  187. uc.RegRead(register, data);
  188. ulong lo = BitConverter.ToUInt64(data, 0);
  189. uc.RegRead(register + 1, data);
  190. ulong hi = BitConverter.ToUInt64(data, 0);
  191. return new SimdValue(lo, hi);
  192. }
  193. private void SetVector(int register, SimdValue value)
  194. {
  195. byte[] data = BitConverter.GetBytes(value.GetUInt64(0));
  196. uc.RegWrite(register, data);
  197. data = BitConverter.GetBytes(value.GetUInt64(1));
  198. uc.RegWrite(register + 1, data);
  199. }
  200. public byte[] MemoryRead(ulong address, ulong size)
  201. {
  202. byte[] value = new byte[size];
  203. uc.MemRead((long)address, value);
  204. return value;
  205. }
  206. public byte MemoryRead8(ulong address) => MemoryRead(address, 1)[0];
  207. public ushort MemoryRead16(ulong address) => BitConverter.ToUInt16(MemoryRead(address, 2), 0);
  208. public uint MemoryRead32(ulong address) => BitConverter.ToUInt32(MemoryRead(address, 4), 0);
  209. public ulong MemoryRead64(ulong address) => BitConverter.ToUInt64(MemoryRead(address, 8), 0);
  210. public void MemoryWrite(ulong address, byte[] value)
  211. {
  212. uc.MemWrite((long)address, value);
  213. }
  214. public void MemoryWrite8(ulong address, byte value) => MemoryWrite(address, new[] { value });
  215. public void MemoryWrite16(ulong address, short value) => MemoryWrite(address, BitConverter.GetBytes(value));
  216. public void MemoryWrite16(ulong address, ushort value) => MemoryWrite(address, BitConverter.GetBytes(value));
  217. public void MemoryWrite32(ulong address, int value) => MemoryWrite(address, BitConverter.GetBytes(value));
  218. public void MemoryWrite32(ulong address, uint value) => MemoryWrite(address, BitConverter.GetBytes(value));
  219. public void MemoryWrite64(ulong address, long value) => MemoryWrite(address, BitConverter.GetBytes(value));
  220. public void MemoryWrite64(ulong address, ulong value) => MemoryWrite(address, BitConverter.GetBytes(value));
  221. public void MemoryMap(ulong address, ulong size, MemoryPermission permissions)
  222. {
  223. uc.MemMap((long)address, (long)size, (int)permissions);
  224. }
  225. public void MemoryUnmap(ulong address, ulong size)
  226. {
  227. uc.MemUnmap((long)address, (long)size);
  228. }
  229. public void MemoryProtect(ulong address, ulong size, MemoryPermission permissions)
  230. {
  231. uc.MemProtect((long)address, (long)size, (int)permissions);
  232. }
  233. }
  234. }