MacroInterpreter.cs 13 KB

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