MacroInterpreter.cs 15 KB

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