MacroInterpreter.cs 11 KB

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