MacroInterpreter.cs 16 KB

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