MacroInterpreter.cs 13 KB

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