MacroInterpreter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using Ryujinx.Common.Logging;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Graphics.Gpu
  5. {
  6. class MacroInterpreter
  7. {
  8. private enum AssignmentOperation
  9. {
  10. IgnoreAndFetch = 0,
  11. Move = 1,
  12. MoveAndSetMaddr = 2,
  13. FetchAndSend = 3,
  14. MoveAndSend = 4,
  15. FetchAndSetMaddr = 5,
  16. MoveAndSetMaddrThenFetchAndSend = 6,
  17. MoveAndSetMaddrThenSendHigh = 7
  18. }
  19. private enum AluOperation
  20. {
  21. AluReg = 0,
  22. AddImmediate = 1,
  23. BitfieldReplace = 2,
  24. BitfieldExtractLslImm = 3,
  25. BitfieldExtractLslReg = 4,
  26. ReadImmediate = 5
  27. }
  28. private enum AluRegOperation
  29. {
  30. Add = 0,
  31. AddWithCarry = 1,
  32. Subtract = 2,
  33. SubtractWithBorrow = 3,
  34. BitwiseExclusiveOr = 8,
  35. BitwiseOr = 9,
  36. BitwiseAnd = 10,
  37. BitwiseAndNot = 11,
  38. BitwiseNotAnd = 12
  39. }
  40. private GpuContext _context;
  41. private NvGpuFifo _pFifo;
  42. public Queue<int> Fifo { get; private set; }
  43. private int[] _gprs;
  44. private int _methAddr;
  45. private int _methIncr;
  46. private bool _carry;
  47. private int _opCode;
  48. private int _pipeOp;
  49. private int _pc;
  50. public MacroInterpreter(GpuContext context, NvGpuFifo pFifo)
  51. {
  52. _context = context;
  53. _pFifo = pFifo;
  54. Fifo = new Queue<int>();
  55. _gprs = new int[8];
  56. }
  57. public void Execute(int[] mme, int position, int param)
  58. {
  59. Reset();
  60. _gprs[1] = param;
  61. _pc = position;
  62. FetchOpCode(mme);
  63. while (Step(mme));
  64. // Due to the delay slot, we still need to execute
  65. // one more instruction before we actually exit.
  66. Step(mme);
  67. }
  68. private void Reset()
  69. {
  70. for (int index = 0; index < _gprs.Length; index++)
  71. {
  72. _gprs[index] = 0;
  73. }
  74. _methAddr = 0;
  75. _methIncr = 0;
  76. _carry = false;
  77. }
  78. private bool Step(int[] mme)
  79. {
  80. int baseAddr = _pc - 1;
  81. FetchOpCode(mme);
  82. if ((_opCode & 7) < 7)
  83. {
  84. // Operation produces a value.
  85. AssignmentOperation asgOp = (AssignmentOperation)((_opCode >> 4) & 7);
  86. int result = GetAluResult();
  87. switch (asgOp)
  88. {
  89. // Fetch parameter and ignore result.
  90. case AssignmentOperation.IgnoreAndFetch:
  91. {
  92. SetDstGpr(FetchParam());
  93. break;
  94. }
  95. // Move result.
  96. case AssignmentOperation.Move:
  97. {
  98. SetDstGpr(result);
  99. break;
  100. }
  101. // Move result and use as Method Address.
  102. case AssignmentOperation.MoveAndSetMaddr:
  103. {
  104. SetDstGpr(result);
  105. SetMethAddr(result);
  106. break;
  107. }
  108. // Fetch parameter and send result.
  109. case AssignmentOperation.FetchAndSend:
  110. {
  111. SetDstGpr(FetchParam());
  112. Send(result);
  113. break;
  114. }
  115. // Move and send result.
  116. case AssignmentOperation.MoveAndSend:
  117. {
  118. SetDstGpr(result);
  119. Send(result);
  120. break;
  121. }
  122. // Fetch parameter and use result as Method Address.
  123. case AssignmentOperation.FetchAndSetMaddr:
  124. {
  125. SetDstGpr(FetchParam());
  126. SetMethAddr(result);
  127. break;
  128. }
  129. // Move result and use as Method Address, then fetch and send parameter.
  130. case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend:
  131. {
  132. SetDstGpr(result);
  133. SetMethAddr(result);
  134. Send(FetchParam());
  135. break;
  136. }
  137. // Move result and use as Method Address, then send bits 17:12 of result.
  138. case AssignmentOperation.MoveAndSetMaddrThenSendHigh:
  139. {
  140. SetDstGpr(result);
  141. SetMethAddr(result);
  142. Send((result >> 12) & 0x3f);
  143. break;
  144. }
  145. }
  146. }
  147. else
  148. {
  149. // Branch.
  150. bool onNotZero = ((_opCode >> 4) & 1) != 0;
  151. bool taken = onNotZero
  152. ? GetGprA() != 0
  153. : GetGprA() == 0;
  154. if (taken)
  155. {
  156. _pc = baseAddr + GetImm();
  157. bool noDelays = (_opCode & 0x20) != 0;
  158. if (noDelays)
  159. {
  160. FetchOpCode(mme);
  161. }
  162. return true;
  163. }
  164. }
  165. bool exit = (_opCode & 0x80) != 0;
  166. return !exit;
  167. }
  168. private void FetchOpCode(int[] mme)
  169. {
  170. _opCode = _pipeOp;
  171. _pipeOp = mme[_pc++];
  172. }
  173. private int GetAluResult()
  174. {
  175. AluOperation op = (AluOperation)(_opCode & 7);
  176. switch (op)
  177. {
  178. case AluOperation.AluReg:
  179. {
  180. AluRegOperation aluOp = (AluRegOperation)((_opCode >> 17) & 0x1f);
  181. return GetAluResult(aluOp, GetGprA(), GetGprB());
  182. }
  183. case AluOperation.AddImmediate:
  184. {
  185. return GetGprA() + GetImm();
  186. }
  187. case AluOperation.BitfieldReplace:
  188. case AluOperation.BitfieldExtractLslImm:
  189. case AluOperation.BitfieldExtractLslReg:
  190. {
  191. int bfSrcBit = (_opCode >> 17) & 0x1f;
  192. int bfSize = (_opCode >> 22) & 0x1f;
  193. int bfDstBit = (_opCode >> 27) & 0x1f;
  194. int bfMask = (1 << bfSize) - 1;
  195. int dst = GetGprA();
  196. int src = GetGprB();
  197. switch (op)
  198. {
  199. case AluOperation.BitfieldReplace:
  200. {
  201. src = (int)((uint)src >> bfSrcBit) & bfMask;
  202. dst &= ~(bfMask << bfDstBit);
  203. dst |= src << bfDstBit;
  204. return dst;
  205. }
  206. case AluOperation.BitfieldExtractLslImm:
  207. {
  208. src = (int)((uint)src >> dst) & bfMask;
  209. return src << bfDstBit;
  210. }
  211. case AluOperation.BitfieldExtractLslReg:
  212. {
  213. src = (int)((uint)src >> bfSrcBit) & bfMask;
  214. return src << dst;
  215. }
  216. }
  217. break;
  218. }
  219. case AluOperation.ReadImmediate:
  220. {
  221. return Read(GetGprA() + GetImm());
  222. }
  223. }
  224. throw new ArgumentException(nameof(_opCode));
  225. }
  226. private int GetAluResult(AluRegOperation aluOp, int a, int b)
  227. {
  228. switch (aluOp)
  229. {
  230. case AluRegOperation.Add:
  231. {
  232. ulong result = (ulong)a + (ulong)b;
  233. _carry = result > 0xffffffff;
  234. return (int)result;
  235. }
  236. case AluRegOperation.AddWithCarry:
  237. {
  238. ulong result = (ulong)a + (ulong)b + (_carry ? 1UL : 0UL);
  239. _carry = result > 0xffffffff;
  240. return (int)result;
  241. }
  242. case AluRegOperation.Subtract:
  243. {
  244. ulong result = (ulong)a - (ulong)b;
  245. _carry = result < 0x100000000;
  246. return (int)result;
  247. }
  248. case AluRegOperation.SubtractWithBorrow:
  249. {
  250. ulong result = (ulong)a - (ulong)b - (_carry ? 0UL : 1UL);
  251. _carry = result < 0x100000000;
  252. return (int)result;
  253. }
  254. case AluRegOperation.BitwiseExclusiveOr: return a ^ b;
  255. case AluRegOperation.BitwiseOr: return a | b;
  256. case AluRegOperation.BitwiseAnd: return a & b;
  257. case AluRegOperation.BitwiseAndNot: return a & ~b;
  258. case AluRegOperation.BitwiseNotAnd: return ~(a & b);
  259. }
  260. throw new ArgumentOutOfRangeException(nameof(aluOp));
  261. }
  262. private int GetImm()
  263. {
  264. // Note: The immediate is signed, the sign-extension is intended here.
  265. return _opCode >> 14;
  266. }
  267. private void SetMethAddr(int value)
  268. {
  269. _methAddr = (value >> 0) & 0xfff;
  270. _methIncr = (value >> 12) & 0x3f;
  271. }
  272. private void SetDstGpr(int value)
  273. {
  274. _gprs[(_opCode >> 8) & 7] = value;
  275. }
  276. private int GetGprA()
  277. {
  278. return GetGprValue((_opCode >> 11) & 7);
  279. }
  280. private int GetGprB()
  281. {
  282. return GetGprValue((_opCode >> 14) & 7);
  283. }
  284. private int GetGprValue(int index)
  285. {
  286. return index != 0 ? _gprs[index] : 0;
  287. }
  288. private int FetchParam()
  289. {
  290. int value;
  291. if (!Fifo.TryDequeue(out value))
  292. {
  293. Logger.PrintWarning(LogClass.Gpu, "Macro attempted to fetch an inexistent argument.");
  294. return 0;
  295. }
  296. return value;
  297. }
  298. private int Read(int reg)
  299. {
  300. return _context.State.Read(reg);
  301. }
  302. private void Send(int value)
  303. {
  304. MethodParams meth = new MethodParams(_methAddr, value);
  305. _context.State.CallMethod(meth);
  306. _methAddr += _methIncr;
  307. }
  308. }
  309. }