MacroInterpreter.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. using Ryujinx.Common.Logging;
  2. using Ryujinx.Graphics.Gpu.State;
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Ryujinx.Graphics.Gpu
  6. {
  7. /// <summary>
  8. /// Macro code interpreter.
  9. /// </summary>
  10. class MacroInterpreter
  11. {
  12. private enum AssignmentOperation
  13. {
  14. IgnoreAndFetch = 0,
  15. Move = 1,
  16. MoveAndSetMaddr = 2,
  17. FetchAndSend = 3,
  18. MoveAndSend = 4,
  19. FetchAndSetMaddr = 5,
  20. MoveAndSetMaddrThenFetchAndSend = 6,
  21. MoveAndSetMaddrThenSendHigh = 7
  22. }
  23. private enum AluOperation
  24. {
  25. AluReg = 0,
  26. AddImmediate = 1,
  27. BitfieldReplace = 2,
  28. BitfieldExtractLslImm = 3,
  29. BitfieldExtractLslReg = 4,
  30. ReadImmediate = 5
  31. }
  32. private enum AluRegOperation
  33. {
  34. Add = 0,
  35. AddWithCarry = 1,
  36. Subtract = 2,
  37. SubtractWithBorrow = 3,
  38. BitwiseExclusiveOr = 8,
  39. BitwiseOr = 9,
  40. BitwiseAnd = 10,
  41. BitwiseAndNot = 11,
  42. BitwiseNotAnd = 12
  43. }
  44. public Queue<int> Fifo { get; private set; }
  45. private int[] _gprs;
  46. private int _methAddr;
  47. private int _methIncr;
  48. private bool _carry;
  49. private int _opCode;
  50. private int _pipeOp;
  51. private int _pc;
  52. /// <summary>
  53. /// Creates a new instance of the macro code interpreter.
  54. /// </summary>
  55. public MacroInterpreter()
  56. {
  57. Fifo = new Queue<int>();
  58. _gprs = new int[8];
  59. }
  60. /// <summary>
  61. /// Executes a macro program until it exits.
  62. /// </summary>
  63. /// <param name="mme">Code of the program to execute</param>
  64. /// <param name="position">Start position to execute</param>
  65. /// <param name="param">Optional argument passed to the program, 0 if not used</param>
  66. /// <param name="state">Current GPU state</param>
  67. public void Execute(int[] mme, int position, int param, GpuState state)
  68. {
  69. Reset();
  70. _gprs[1] = param;
  71. _pc = position;
  72. FetchOpCode(mme);
  73. while (Step(mme, state));
  74. // Due to the delay slot, we still need to execute
  75. // one more instruction before we actually exit.
  76. Step(mme, state);
  77. }
  78. /// <summary>
  79. /// Resets the internal interpreter state.
  80. /// Call each time you run a new program.
  81. /// </summary>
  82. private void Reset()
  83. {
  84. for (int index = 0; index < _gprs.Length; index++)
  85. {
  86. _gprs[index] = 0;
  87. }
  88. _methAddr = 0;
  89. _methIncr = 0;
  90. _carry = false;
  91. }
  92. /// <summary>
  93. /// Executes a single instruction of the program.
  94. /// </summary>
  95. /// <param name="mme">Program code to execute</param>
  96. /// <param name="state">Current GPU state</param>
  97. /// <returns>True to continue execution, false if the program exited</returns>
  98. private bool Step(int[] mme, GpuState state)
  99. {
  100. int baseAddr = _pc - 1;
  101. FetchOpCode(mme);
  102. if ((_opCode & 7) < 7)
  103. {
  104. // Operation produces a value.
  105. AssignmentOperation asgOp = (AssignmentOperation)((_opCode >> 4) & 7);
  106. int result = GetAluResult(state);
  107. switch (asgOp)
  108. {
  109. // Fetch parameter and ignore result.
  110. case AssignmentOperation.IgnoreAndFetch:
  111. {
  112. SetDstGpr(FetchParam());
  113. break;
  114. }
  115. // Move result.
  116. case AssignmentOperation.Move:
  117. {
  118. SetDstGpr(result);
  119. break;
  120. }
  121. // Move result and use as Method Address.
  122. case AssignmentOperation.MoveAndSetMaddr:
  123. {
  124. SetDstGpr(result);
  125. SetMethAddr(result);
  126. break;
  127. }
  128. // Fetch parameter and send result.
  129. case AssignmentOperation.FetchAndSend:
  130. {
  131. SetDstGpr(FetchParam());
  132. Send(state, result);
  133. break;
  134. }
  135. // Move and send result.
  136. case AssignmentOperation.MoveAndSend:
  137. {
  138. SetDstGpr(result);
  139. Send(state, result);
  140. break;
  141. }
  142. // Fetch parameter and use result as Method Address.
  143. case AssignmentOperation.FetchAndSetMaddr:
  144. {
  145. SetDstGpr(FetchParam());
  146. SetMethAddr(result);
  147. break;
  148. }
  149. // Move result and use as Method Address, then fetch and send parameter.
  150. case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend:
  151. {
  152. SetDstGpr(result);
  153. SetMethAddr(result);
  154. Send(state, FetchParam());
  155. break;
  156. }
  157. // Move result and use as Method Address, then send bits 17:12 of result.
  158. case AssignmentOperation.MoveAndSetMaddrThenSendHigh:
  159. {
  160. SetDstGpr(result);
  161. SetMethAddr(result);
  162. Send(state, (result >> 12) & 0x3f);
  163. break;
  164. }
  165. }
  166. }
  167. else
  168. {
  169. // Branch.
  170. bool onNotZero = ((_opCode >> 4) & 1) != 0;
  171. bool taken = onNotZero
  172. ? GetGprA() != 0
  173. : GetGprA() == 0;
  174. if (taken)
  175. {
  176. _pc = baseAddr + GetImm();
  177. bool noDelays = (_opCode & 0x20) != 0;
  178. if (noDelays)
  179. {
  180. FetchOpCode(mme);
  181. }
  182. return true;
  183. }
  184. }
  185. bool exit = (_opCode & 0x80) != 0;
  186. return !exit;
  187. }
  188. /// <summary>
  189. /// Fetches a single operation code from the program code.
  190. /// </summary>
  191. /// <param name="mme">Program code</param>
  192. private void FetchOpCode(int[] mme)
  193. {
  194. _opCode = _pipeOp;
  195. _pipeOp = mme[_pc++];
  196. }
  197. /// <summary>
  198. /// Gets the result of the current Arithmetic and Logic unit operation.
  199. /// </summary>
  200. /// <param name="state">Current GPU state</param>
  201. /// <returns>Operation result</returns>
  202. private int GetAluResult(GpuState state)
  203. {
  204. AluOperation op = (AluOperation)(_opCode & 7);
  205. switch (op)
  206. {
  207. case AluOperation.AluReg:
  208. {
  209. AluRegOperation aluOp = (AluRegOperation)((_opCode >> 17) & 0x1f);
  210. return GetAluResult(aluOp, GetGprA(), GetGprB());
  211. }
  212. case AluOperation.AddImmediate:
  213. {
  214. return GetGprA() + GetImm();
  215. }
  216. case AluOperation.BitfieldReplace:
  217. case AluOperation.BitfieldExtractLslImm:
  218. case AluOperation.BitfieldExtractLslReg:
  219. {
  220. int bfSrcBit = (_opCode >> 17) & 0x1f;
  221. int bfSize = (_opCode >> 22) & 0x1f;
  222. int bfDstBit = (_opCode >> 27) & 0x1f;
  223. int bfMask = (1 << bfSize) - 1;
  224. int dst = GetGprA();
  225. int src = GetGprB();
  226. switch (op)
  227. {
  228. case AluOperation.BitfieldReplace:
  229. {
  230. src = (int)((uint)src >> bfSrcBit) & bfMask;
  231. dst &= ~(bfMask << bfDstBit);
  232. dst |= src << bfDstBit;
  233. return dst;
  234. }
  235. case AluOperation.BitfieldExtractLslImm:
  236. {
  237. src = (int)((uint)src >> dst) & bfMask;
  238. return src << bfDstBit;
  239. }
  240. case AluOperation.BitfieldExtractLslReg:
  241. {
  242. src = (int)((uint)src >> bfSrcBit) & bfMask;
  243. return src << dst;
  244. }
  245. }
  246. break;
  247. }
  248. case AluOperation.ReadImmediate:
  249. {
  250. return Read(state, GetGprA() + GetImm());
  251. }
  252. }
  253. throw new ArgumentException(nameof(_opCode));
  254. }
  255. /// <summary>
  256. /// Gets the result of an Arithmetic and Logic operation using registers.
  257. /// </summary>
  258. /// <param name="aluOp">Arithmetic and Logic unit operation with registers</param>
  259. /// <param name="a">First operand value</param>
  260. /// <param name="b">Second operand value</param>
  261. /// <returns>Operation result</returns>
  262. private int GetAluResult(AluRegOperation aluOp, int a, int b)
  263. {
  264. switch (aluOp)
  265. {
  266. case AluRegOperation.Add:
  267. {
  268. ulong result = (ulong)a + (ulong)b;
  269. _carry = result > 0xffffffff;
  270. return (int)result;
  271. }
  272. case AluRegOperation.AddWithCarry:
  273. {
  274. ulong result = (ulong)a + (ulong)b + (_carry ? 1UL : 0UL);
  275. _carry = result > 0xffffffff;
  276. return (int)result;
  277. }
  278. case AluRegOperation.Subtract:
  279. {
  280. ulong result = (ulong)a - (ulong)b;
  281. _carry = result < 0x100000000;
  282. return (int)result;
  283. }
  284. case AluRegOperation.SubtractWithBorrow:
  285. {
  286. ulong result = (ulong)a - (ulong)b - (_carry ? 0UL : 1UL);
  287. _carry = result < 0x100000000;
  288. return (int)result;
  289. }
  290. case AluRegOperation.BitwiseExclusiveOr: return a ^ b;
  291. case AluRegOperation.BitwiseOr: return a | b;
  292. case AluRegOperation.BitwiseAnd: return a & b;
  293. case AluRegOperation.BitwiseAndNot: return a & ~b;
  294. case AluRegOperation.BitwiseNotAnd: return ~(a & b);
  295. }
  296. throw new ArgumentOutOfRangeException(nameof(aluOp));
  297. }
  298. /// <summary>
  299. /// Extracts a 32-bits signed integer constant from the current operation code.
  300. /// </summary>
  301. /// <returns>The 32-bits immediate value encoded at the current operation code</returns>
  302. private int GetImm()
  303. {
  304. // Note: The immediate is signed, the sign-extension is intended here.
  305. return _opCode >> 14;
  306. }
  307. /// <summary>
  308. /// Sets the current method address, for method calls.
  309. /// </summary>
  310. /// <param name="value">Packed address and increment value</param>
  311. private void SetMethAddr(int value)
  312. {
  313. _methAddr = (value >> 0) & 0xfff;
  314. _methIncr = (value >> 12) & 0x3f;
  315. }
  316. /// <summary>
  317. /// Sets the destination register value.
  318. /// </summary>
  319. /// <param name="value">Value to set (usually the operation result)</param>
  320. private void SetDstGpr(int value)
  321. {
  322. _gprs[(_opCode >> 8) & 7] = value;
  323. }
  324. /// <summary>
  325. /// Gets first operand value from the respective register.
  326. /// </summary>
  327. /// <returns>Operand value</returns>
  328. private int GetGprA()
  329. {
  330. return GetGprValue((_opCode >> 11) & 7);
  331. }
  332. /// <summary>
  333. /// Gets second operand value from the respective register.
  334. /// </summary>
  335. /// <returns>Operand value</returns>
  336. private int GetGprB()
  337. {
  338. return GetGprValue((_opCode >> 14) & 7);
  339. }
  340. /// <summary>
  341. /// Gets the value from a register, or 0 if the R0 register is specified.
  342. /// </summary>
  343. /// <param name="index">Index of the register</param>
  344. /// <returns>Register value</returns>
  345. private int GetGprValue(int index)
  346. {
  347. return index != 0 ? _gprs[index] : 0;
  348. }
  349. /// <summary>
  350. /// Fetches a call argument from the call argument FIFO.
  351. /// </summary>
  352. /// <returns>The call argument, or 0 if the FIFO is empty</returns>
  353. private int FetchParam()
  354. {
  355. if (!Fifo.TryDequeue(out int value))
  356. {
  357. Logger.PrintWarning(LogClass.Gpu, "Macro attempted to fetch an inexistent argument.");
  358. return 0;
  359. }
  360. return value;
  361. }
  362. /// <summary>
  363. /// Reads data from a GPU register.
  364. /// </summary>
  365. /// <param name="state">Current GPU state</param>
  366. /// <param name="reg">Register offset to read</param>
  367. /// <returns>GPU register value</returns>
  368. private int Read(GpuState state, int reg)
  369. {
  370. return state.Read(reg);
  371. }
  372. /// <summary>
  373. /// Performs a GPU method call.
  374. /// </summary>
  375. /// <param name="state">Current GPU state</param>
  376. /// <param name="value">Call argument</param>
  377. private void Send(GpuState state, int value)
  378. {
  379. MethodParams meth = new MethodParams(_methAddr, value);
  380. state.CallMethod(meth);
  381. _methAddr += _methIncr;
  382. }
  383. }
  384. }