OpCodeTable.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. using Ryujinx.Graphics.Shader.Instructions;
  2. using System;
  3. namespace Ryujinx.Graphics.Shader.Decoders
  4. {
  5. static class OpCodeTable
  6. {
  7. private const int EncodingBits = 14;
  8. private class TableEntry
  9. {
  10. public InstEmitter Emitter { get; }
  11. public Type OpCodeType { get; }
  12. public int XBits { get; }
  13. public TableEntry(InstEmitter emitter, Type opCodeType, int xBits)
  14. {
  15. Emitter = emitter;
  16. OpCodeType = opCodeType;
  17. XBits = xBits;
  18. }
  19. }
  20. private static TableEntry[] _opCodes;
  21. static OpCodeTable()
  22. {
  23. _opCodes = new TableEntry[1 << EncodingBits];
  24. #region Instructions
  25. Set("1110111111011x", InstEmit.Ald, typeof(OpCodeAttribute));
  26. Set("1110111111110x", InstEmit.Ast, typeof(OpCodeAttribute));
  27. Set("11101100xxxxxx", InstEmit.Atoms, typeof(OpCodeAtom));
  28. Set("0100110000000x", InstEmit.Bfe, typeof(OpCodeAluCbuf));
  29. Set("0011100x00000x", InstEmit.Bfe, typeof(OpCodeAluImm));
  30. Set("0101110000000x", InstEmit.Bfe, typeof(OpCodeAluReg));
  31. Set("0100101111110x", InstEmit.Bfi, typeof(OpCodeAluCbuf));
  32. Set("0011011x11110x", InstEmit.Bfi, typeof(OpCodeAluImm));
  33. Set("0101001111110x", InstEmit.Bfi, typeof(OpCodeAluRegCbuf));
  34. Set("0101101111110x", InstEmit.Bfi, typeof(OpCodeAluReg));
  35. Set("111000100100xx", InstEmit.Bra, typeof(OpCodeBranch));
  36. Set("111000110100xx", InstEmit.Brk, typeof(OpCodeBranchPop));
  37. Set("111000100101xx", InstEmit.Brx, typeof(OpCodeBranchIndir));
  38. Set("0101000010100x", InstEmit.Csetp, typeof(OpCodePset));
  39. Set("111000110000xx", InstEmit.Exit, typeof(OpCodeExit));
  40. Set("0100110010101x", InstEmit.F2F, typeof(OpCodeFArithCbuf));
  41. Set("0011100x10101x", InstEmit.F2F, typeof(OpCodeFArithImm));
  42. Set("0101110010101x", InstEmit.F2F, typeof(OpCodeFArithReg));
  43. Set("0100110010110x", InstEmit.F2I, typeof(OpCodeFArithCbuf));
  44. Set("0011100x10110x", InstEmit.F2I, typeof(OpCodeFArithImm));
  45. Set("0101110010110x", InstEmit.F2I, typeof(OpCodeFArithReg));
  46. Set("0100110001011x", InstEmit.Fadd, typeof(OpCodeFArithCbuf));
  47. Set("0011100x01011x", InstEmit.Fadd, typeof(OpCodeFArithImm));
  48. Set("000010xxxxxxxx", InstEmit.Fadd, typeof(OpCodeFArithImm32));
  49. Set("0101110001011x", InstEmit.Fadd, typeof(OpCodeFArithReg));
  50. Set("010010011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithCbuf));
  51. Set("0011001x1xxxxx", InstEmit.Ffma, typeof(OpCodeFArithImm));
  52. Set("000011xxxxxxxx", InstEmit.Ffma32i, typeof(OpCodeFArithImm32));
  53. Set("010100011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithRegCbuf));
  54. Set("010110011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithReg));
  55. Set("0100110000110x", InstEmit.Flo, typeof(OpCodeAluCbuf));
  56. Set("0011100x00110x", InstEmit.Flo, typeof(OpCodeAluImm));
  57. Set("0101110000110x", InstEmit.Flo, typeof(OpCodeAluReg));
  58. Set("0100110001100x", InstEmit.Fmnmx, typeof(OpCodeFArithCbuf));
  59. Set("0011100x01100x", InstEmit.Fmnmx, typeof(OpCodeFArithImm));
  60. Set("0101110001100x", InstEmit.Fmnmx, typeof(OpCodeFArithReg));
  61. Set("0100110001101x", InstEmit.Fmul, typeof(OpCodeFArithCbuf));
  62. Set("0011100x01101x", InstEmit.Fmul, typeof(OpCodeFArithImm));
  63. Set("00011110xxxxxx", InstEmit.Fmul, typeof(OpCodeFArithImm32));
  64. Set("0101110001101x", InstEmit.Fmul, typeof(OpCodeFArithReg));
  65. Set("0100100xxxxxxx", InstEmit.Fset, typeof(OpCodeSetCbuf));
  66. Set("0011000xxxxxxx", InstEmit.Fset, typeof(OpCodeFsetImm));
  67. Set("01011000xxxxxx", InstEmit.Fset, typeof(OpCodeSetReg));
  68. Set("010010111011xx", InstEmit.Fsetp, typeof(OpCodeSetCbuf));
  69. Set("0011011x1011xx", InstEmit.Fsetp, typeof(OpCodeFsetImm));
  70. Set("010110111011xx", InstEmit.Fsetp, typeof(OpCodeSetReg));
  71. Set("0101000011111x", InstEmit.Fswzadd, typeof(OpCodeAluReg));
  72. Set("0111101x1xxxxx", InstEmit.Hadd2, typeof(OpCodeAluCbuf));
  73. Set("0111101x0xxxxx", InstEmit.Hadd2, typeof(OpCodeAluImm2x10));
  74. Set("0010110xxxxxxx", InstEmit.Hadd2, typeof(OpCodeAluImm32));
  75. Set("0101110100010x", InstEmit.Hadd2, typeof(OpCodeAluReg));
  76. Set("01110xxx1xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaCbuf));
  77. Set("01110xxx0xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaImm2x10));
  78. Set("0010100xxxxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaImm32));
  79. Set("0101110100000x", InstEmit.Hfma2, typeof(OpCodeHfmaReg));
  80. Set("01100xxx1xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaRegCbuf));
  81. Set("0111100x1xxxxx", InstEmit.Hmul2, typeof(OpCodeAluCbuf));
  82. Set("0111100x0xxxxx", InstEmit.Hmul2, typeof(OpCodeAluImm2x10));
  83. Set("0010101xxxxxxx", InstEmit.Hmul2, typeof(OpCodeAluImm32));
  84. Set("0101110100001x", InstEmit.Hmul2, typeof(OpCodeAluReg));
  85. Set("0111110x1xxxxx", InstEmit.Hset2, typeof(OpCodeSetCbuf));
  86. Set("0111110x0xxxxx", InstEmit.Hset2, typeof(OpCodeHsetImm2x10));
  87. Set("0101110100011x", InstEmit.Hset2, typeof(OpCodeSetReg));
  88. Set("0111111x1xxxxx", InstEmit.Hsetp2, typeof(OpCodeSetCbuf));
  89. Set("0111111x0xxxxx", InstEmit.Hsetp2, typeof(OpCodeHsetImm2x10));
  90. Set("0101110100100x", InstEmit.Hsetp2, typeof(OpCodeSetReg));
  91. Set("0100110010111x", InstEmit.I2F, typeof(OpCodeAluCbuf));
  92. Set("0011100x10111x", InstEmit.I2F, typeof(OpCodeAluImm));
  93. Set("0101110010111x", InstEmit.I2F, typeof(OpCodeAluReg));
  94. Set("0100110011100x", InstEmit.I2I, typeof(OpCodeAluCbuf));
  95. Set("0011100x11100x", InstEmit.I2I, typeof(OpCodeAluImm));
  96. Set("0101110011100x", InstEmit.I2I, typeof(OpCodeAluReg));
  97. Set("0100110000010x", InstEmit.Iadd, typeof(OpCodeAluCbuf));
  98. Set("0011100x00010x", InstEmit.Iadd, typeof(OpCodeAluImm));
  99. Set("0001110x0xxxxx", InstEmit.Iadd, typeof(OpCodeAluImm32));
  100. Set("0101110000010x", InstEmit.Iadd, typeof(OpCodeAluReg));
  101. Set("010011001100xx", InstEmit.Iadd3, typeof(OpCodeAluCbuf));
  102. Set("0011100x1100xx", InstEmit.Iadd3, typeof(OpCodeAluImm));
  103. Set("010111001100xx", InstEmit.Iadd3, typeof(OpCodeAluReg));
  104. Set("010010100xxxxx", InstEmit.Imad, typeof(OpCodeAluCbuf));
  105. Set("0011010x0xxxxx", InstEmit.Imad, typeof(OpCodeAluImm));
  106. Set("010110100xxxxx", InstEmit.Imad, typeof(OpCodeAluReg));
  107. Set("010100100xxxxx", InstEmit.Imad, typeof(OpCodeAluRegCbuf));
  108. Set("0100110000100x", InstEmit.Imnmx, typeof(OpCodeAluCbuf));
  109. Set("0011100x00100x", InstEmit.Imnmx, typeof(OpCodeAluImm));
  110. Set("0101110000100x", InstEmit.Imnmx, typeof(OpCodeAluReg));
  111. Set("11100000xxxxxx", InstEmit.Ipa, typeof(OpCodeIpa));
  112. Set("1110111111010x", InstEmit.Isberd, typeof(OpCodeAlu));
  113. Set("0100110000011x", InstEmit.Iscadd, typeof(OpCodeAluCbuf));
  114. Set("0011100x00011x", InstEmit.Iscadd, typeof(OpCodeAluImm));
  115. Set("000101xxxxxxxx", InstEmit.Iscadd, typeof(OpCodeAluImm32));
  116. Set("0101110000011x", InstEmit.Iscadd, typeof(OpCodeAluReg));
  117. Set("010010110101xx", InstEmit.Iset, typeof(OpCodeSetCbuf));
  118. Set("001101100101xx", InstEmit.Iset, typeof(OpCodeSetImm));
  119. Set("010110110101xx", InstEmit.Iset, typeof(OpCodeSetReg));
  120. Set("010010110110xx", InstEmit.Isetp, typeof(OpCodeSetCbuf));
  121. Set("0011011x0110xx", InstEmit.Isetp, typeof(OpCodeSetImm));
  122. Set("010110110110xx", InstEmit.Isetp, typeof(OpCodeSetReg));
  123. Set("111000110011xx", InstEmit.Kil, typeof(OpCodeExit));
  124. Set("1110111101000x", InstEmit.Ld, typeof(OpCodeMemory));
  125. Set("1110111110010x", InstEmit.Ldc, typeof(OpCodeLdc));
  126. Set("1110111011010x", InstEmit.Ldg, typeof(OpCodeMemory));
  127. Set("1110111101001x", InstEmit.Lds, typeof(OpCodeMemory));
  128. Set("0100110001000x", InstEmit.Lop, typeof(OpCodeLopCbuf));
  129. Set("0011100001000x", InstEmit.Lop, typeof(OpCodeLopImm));
  130. Set("000001xxxxxxxx", InstEmit.Lop, typeof(OpCodeLopImm32));
  131. Set("0101110001000x", InstEmit.Lop, typeof(OpCodeLopReg));
  132. Set("0010000xxxxxxx", InstEmit.Lop3, typeof(OpCodeLopCbuf));
  133. Set("001111xxxxxxxx", InstEmit.Lop3, typeof(OpCodeLopImm));
  134. Set("0101101111100x", InstEmit.Lop3, typeof(OpCodeLopReg));
  135. Set("0100110010011x", InstEmit.Mov, typeof(OpCodeAluCbuf));
  136. Set("0011100x10011x", InstEmit.Mov, typeof(OpCodeAluImm));
  137. Set("000000010000xx", InstEmit.Mov, typeof(OpCodeAluImm32));
  138. Set("0101110010011x", InstEmit.Mov, typeof(OpCodeAluReg));
  139. Set("0101000010000x", InstEmit.Mufu, typeof(OpCodeFArith));
  140. Set("1111101111100x", InstEmit.Out, typeof(OpCode));
  141. Set("111000101010xx", InstEmit.Pbk, typeof(OpCodePush));
  142. Set("0100110000001x", InstEmit.Popc, typeof(OpCodeAluCbuf));
  143. Set("0011100x00001x", InstEmit.Popc, typeof(OpCodeAluImm));
  144. Set("0101110000001x", InstEmit.Popc, typeof(OpCodeAluReg));
  145. Set("0101000010001x", InstEmit.Pset, typeof(OpCodePset));
  146. Set("0101000010010x", InstEmit.Psetp, typeof(OpCodePset));
  147. Set("0100110011110x", InstEmit.R2p, typeof(OpCodeAluCbuf));
  148. Set("0011100x11110x", InstEmit.R2p, typeof(OpCodeAluImm));
  149. Set("0101110011110x", InstEmit.R2p, typeof(OpCodeAluReg));
  150. Set("1110101111111x", InstEmit.Red, typeof(OpCodeRed));
  151. Set("0100110010010x", InstEmit.Rro, typeof(OpCodeFArithCbuf));
  152. Set("0011100x10010x", InstEmit.Rro, typeof(OpCodeFArithImm));
  153. Set("0101110010010x", InstEmit.Rro, typeof(OpCodeFArithReg));
  154. Set("1111000011001x", InstEmit.S2r, typeof(OpCodeAlu));
  155. Set("0100110010100x", InstEmit.Sel, typeof(OpCodeAluCbuf));
  156. Set("0011100x10100x", InstEmit.Sel, typeof(OpCodeAluImm));
  157. Set("0101110010100x", InstEmit.Sel, typeof(OpCodeAluReg));
  158. Set("1110111100010x", InstEmit.Shfl, typeof(OpCodeShuffle));
  159. Set("0100110001001x", InstEmit.Shl, typeof(OpCodeAluCbuf));
  160. Set("0011100x01001x", InstEmit.Shl, typeof(OpCodeAluImm));
  161. Set("0101110001001x", InstEmit.Shl, typeof(OpCodeAluReg));
  162. Set("0100110000101x", InstEmit.Shr, typeof(OpCodeAluCbuf));
  163. Set("0011100x00101x", InstEmit.Shr, typeof(OpCodeAluImm));
  164. Set("0101110000101x", InstEmit.Shr, typeof(OpCodeAluReg));
  165. Set("111000101001xx", InstEmit.Ssy, typeof(OpCodePush));
  166. Set("1110111101010x", InstEmit.St, typeof(OpCodeMemory));
  167. Set("1110111011011x", InstEmit.Stg, typeof(OpCodeMemory));
  168. Set("1110111101011x", InstEmit.Sts, typeof(OpCodeMemory));
  169. Set("11101011001xxx", InstEmit.Sust, typeof(OpCodeImage));
  170. Set("1111000011111x", InstEmit.Sync, typeof(OpCodeBranchPop));
  171. Set("110000xxxx111x", InstEmit.Tex, typeof(OpCodeTex));
  172. Set("1101111010111x", InstEmit.TexB, typeof(OpCodeTexB));
  173. Set("1101x00xxxxxxx", InstEmit.Texs, typeof(OpCodeTexs));
  174. Set("1101x01xxxxxxx", InstEmit.Texs, typeof(OpCodeTlds));
  175. Set("11011111x0xxxx", InstEmit.Texs, typeof(OpCodeTld4s));
  176. Set("11011100xx111x", InstEmit.Tld, typeof(OpCodeTld));
  177. Set("11011101xx111x", InstEmit.TldB, typeof(OpCodeTld));
  178. Set("110010xxxx111x", InstEmit.Tld4, typeof(OpCodeTld4));
  179. Set("1101111011111x", InstEmit.Tld4, typeof(OpCodeTld4B));
  180. Set("110111100x1110", InstEmit.Txd, typeof(OpCodeTxd));
  181. Set("1101111101001x", InstEmit.Txq, typeof(OpCodeTex));
  182. Set("1101111101010x", InstEmit.TxqB, typeof(OpCodeTex));
  183. Set("01011111xxxxxx", InstEmit.Vmad, typeof(OpCodeVideo));
  184. Set("0101000011011x", InstEmit.Vote, typeof(OpCodeVote));
  185. Set("0100111xxxxxxx", InstEmit.Xmad, typeof(OpCodeAluCbuf));
  186. Set("0011011x00xxxx", InstEmit.Xmad, typeof(OpCodeAluImm));
  187. Set("010100010xxxxx", InstEmit.Xmad, typeof(OpCodeAluRegCbuf));
  188. Set("0101101100xxxx", InstEmit.Xmad, typeof(OpCodeAluReg));
  189. #endregion
  190. }
  191. private static void Set(string encoding, InstEmitter emitter, Type opCodeType)
  192. {
  193. if (encoding.Length != EncodingBits)
  194. {
  195. throw new ArgumentException(nameof(encoding));
  196. }
  197. int bit = encoding.Length - 1;
  198. int value = 0;
  199. int xMask = 0;
  200. int xBits = 0;
  201. int[] xPos = new int[encoding.Length];
  202. for (int index = 0; index < encoding.Length; index++, bit--)
  203. {
  204. char chr = encoding[index];
  205. if (chr == '1')
  206. {
  207. value |= 1 << bit;
  208. }
  209. else if (chr == 'x')
  210. {
  211. xMask |= 1 << bit;
  212. xPos[xBits++] = bit;
  213. }
  214. }
  215. xMask = ~xMask;
  216. TableEntry entry = new TableEntry(emitter, opCodeType, xBits);
  217. for (int index = 0; index < (1 << xBits); index++)
  218. {
  219. value &= xMask;
  220. for (int x = 0; x < xBits; x++)
  221. {
  222. value |= ((index >> x) & 1) << xPos[x];
  223. }
  224. if (_opCodes[value] == null || _opCodes[value].XBits > xBits)
  225. {
  226. _opCodes[value] = entry;
  227. }
  228. }
  229. }
  230. public static (InstEmitter emitter, Type opCodeType) GetEmitter(long opCode)
  231. {
  232. TableEntry entry = _opCodes[(ulong)opCode >> (64 - EncodingBits)];
  233. if (entry != null)
  234. {
  235. return (entry.Emitter, entry.OpCodeType);
  236. }
  237. return (null, null);
  238. }
  239. }
  240. }