MacroInterpreter.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. using ChocolArm64.Memory;
  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 NvGpuFifo PFifo;
  41. private INvGpuEngine Engine;
  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 long Pc;
  50. public MacroInterpreter(NvGpuFifo PFifo, INvGpuEngine Engine)
  51. {
  52. this.PFifo = PFifo;
  53. this.Engine = Engine;
  54. Fifo = new Queue<int>();
  55. Gprs = new int[8];
  56. }
  57. public void Execute(AMemory Memory, long Position, int Param)
  58. {
  59. Reset();
  60. Gprs[1] = Param;
  61. Pc = Position;
  62. FetchOpCode(Memory);
  63. while (Step(Memory));
  64. //Due to the delay slot, we still need to execute
  65. //one more instruction before we actually exit.
  66. Step(Memory);
  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(AMemory Memory)
  79. {
  80. long BaseAddr = Pc - 4;
  81. FetchOpCode(Memory);
  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(Memory, Result);
  113. break;
  114. }
  115. //Move and send result.
  116. case AssignmentOperation.MoveAndSend:
  117. {
  118. SetDstGpr(Result);
  119. Send(Memory, 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 paramter.
  130. case AssignmentOperation.MoveAndSetMaddrThenFetchAndSend:
  131. {
  132. SetDstGpr(Result);
  133. SetMethAddr(Result);
  134. Send(Memory, 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(Memory, (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() << 2);
  157. bool NoDelays = (OpCode & 0x20) != 0;
  158. if (NoDelays)
  159. {
  160. FetchOpCode(Memory);
  161. }
  162. return true;
  163. }
  164. }
  165. bool Exit = (OpCode & 0x80) != 0;
  166. return !Exit;
  167. }
  168. private void FetchOpCode(AMemory Memory)
  169. {
  170. OpCode = PipeOp;
  171. PipeOp = Memory.ReadInt32(Pc);
  172. Pc += 4;
  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 we don't have any parameters in the FIFO,
  293. //keep running the PFIFO engine until it writes the parameters.
  294. while (!Fifo.TryDequeue(out Value))
  295. {
  296. if (!PFifo.Step())
  297. {
  298. return 0;
  299. }
  300. }
  301. return Value;
  302. }
  303. private int Read(int Reg)
  304. {
  305. return Engine.Registers[Reg];
  306. }
  307. private void Send(AMemory Memory, int Value)
  308. {
  309. NsGpuPBEntry PBEntry = new NsGpuPBEntry(MethAddr, 0, Value);
  310. Engine.CallMethod(Memory, PBEntry);
  311. MethAddr += MethIncr;
  312. }
  313. }
  314. }