MacroInterpreter.cs 11 KB

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