OpCodeTable.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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("0100110000000x", InstEmit.Bfe, typeof(OpCodeAluCbuf));
  28. Set("0011100x00000x", InstEmit.Bfe, typeof(OpCodeAluImm));
  29. Set("0101110000000x", InstEmit.Bfe, typeof(OpCodeAluReg));
  30. Set("111000100100xx", InstEmit.Bra, typeof(OpCodeBranch));
  31. Set("0101000010100x", InstEmit.Csetp, typeof(OpCodePsetp));
  32. Set("111000110000xx", InstEmit.Exit, typeof(OpCodeExit));
  33. Set("0100110010101x", InstEmit.F2F, typeof(OpCodeFArithCbuf));
  34. Set("0011100x10101x", InstEmit.F2F, typeof(OpCodeFArithImm));
  35. Set("0101110010101x", InstEmit.F2F, typeof(OpCodeFArithReg));
  36. Set("0100110010110x", InstEmit.F2I, typeof(OpCodeFArithCbuf));
  37. Set("0011100x10110x", InstEmit.F2I, typeof(OpCodeFArithImm));
  38. Set("0101110010110x", InstEmit.F2I, typeof(OpCodeFArithReg));
  39. Set("0100110001011x", InstEmit.Fadd, typeof(OpCodeFArithCbuf));
  40. Set("0011100x01011x", InstEmit.Fadd, typeof(OpCodeFArithImm));
  41. Set("000010xxxxxxxx", InstEmit.Fadd, typeof(OpCodeFArithImm32));
  42. Set("0101110001011x", InstEmit.Fadd, typeof(OpCodeFArithReg));
  43. Set("010010011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithCbuf));
  44. Set("0011001x1xxxxx", InstEmit.Ffma, typeof(OpCodeFArithImm));
  45. Set("010100011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithRegCbuf));
  46. Set("010110011xxxxx", InstEmit.Ffma, typeof(OpCodeFArithReg));
  47. Set("0100110001100x", InstEmit.Fmnmx, typeof(OpCodeFArithCbuf));
  48. Set("0011100x01100x", InstEmit.Fmnmx, typeof(OpCodeFArithImm));
  49. Set("0101110001100x", InstEmit.Fmnmx, typeof(OpCodeFArithReg));
  50. Set("0100110001101x", InstEmit.Fmul, typeof(OpCodeFArithCbuf));
  51. Set("0011100x01101x", InstEmit.Fmul, typeof(OpCodeFArithImm));
  52. Set("00011110xxxxxx", InstEmit.Fmul, typeof(OpCodeFArithImm32));
  53. Set("0101110001101x", InstEmit.Fmul, typeof(OpCodeFArithReg));
  54. Set("0100100xxxxxxx", InstEmit.Fset, typeof(OpCodeSetCbuf));
  55. Set("0011000xxxxxxx", InstEmit.Fset, typeof(OpCodeFsetImm));
  56. Set("01011000xxxxxx", InstEmit.Fset, typeof(OpCodeSetReg));
  57. Set("010010111011xx", InstEmit.Fsetp, typeof(OpCodeSetCbuf));
  58. Set("0011011x1011xx", InstEmit.Fsetp, typeof(OpCodeFsetImm));
  59. Set("010110111011xx", InstEmit.Fsetp, typeof(OpCodeSetReg));
  60. Set("0111101x1xxxxx", InstEmit.Hadd2, typeof(OpCodeAluCbuf));
  61. Set("0111101x0xxxxx", InstEmit.Hadd2, typeof(OpCodeAluImm2x10));
  62. Set("0010110xxxxxxx", InstEmit.Hadd2, typeof(OpCodeAluImm32));
  63. Set("0101110100010x", InstEmit.Hadd2, typeof(OpCodeAluReg));
  64. Set("01110xxx1xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaCbuf));
  65. Set("01110xxx0xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaImm2x10));
  66. Set("0010100xxxxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaImm32));
  67. Set("0101110100000x", InstEmit.Hfma2, typeof(OpCodeHfmaReg));
  68. Set("01100xxx1xxxxx", InstEmit.Hfma2, typeof(OpCodeHfmaRegCbuf));
  69. Set("0111100x1xxxxx", InstEmit.Hmul2, typeof(OpCodeAluCbuf));
  70. Set("0111100x0xxxxx", InstEmit.Hmul2, typeof(OpCodeAluImm2x10));
  71. Set("0010101xxxxxxx", InstEmit.Hmul2, typeof(OpCodeAluImm32));
  72. Set("0101110100001x", InstEmit.Hmul2, typeof(OpCodeAluReg));
  73. Set("0111111x1xxxxx", InstEmit.Hsetp2, typeof(OpCodeSetCbuf));
  74. Set("0111111x0xxxxx", InstEmit.Hsetp2, typeof(OpCodeHsetImm2x10));
  75. Set("0101110100100x", InstEmit.Hsetp2, typeof(OpCodeSetReg));
  76. Set("0100110010111x", InstEmit.I2F, typeof(OpCodeAluCbuf));
  77. Set("0011100x10111x", InstEmit.I2F, typeof(OpCodeAluImm));
  78. Set("0101110010111x", InstEmit.I2F, typeof(OpCodeAluReg));
  79. Set("0100110011100x", InstEmit.I2I, typeof(OpCodeAluCbuf));
  80. Set("0011100x11100x", InstEmit.I2I, typeof(OpCodeAluImm));
  81. Set("0101110011100x", InstEmit.I2I, typeof(OpCodeAluReg));
  82. Set("0100110000010x", InstEmit.Iadd, typeof(OpCodeAluCbuf));
  83. Set("0011100000010x", InstEmit.Iadd, typeof(OpCodeAluImm));
  84. Set("0001110x0xxxxx", InstEmit.Iadd, typeof(OpCodeAluImm32));
  85. Set("0101110000010x", InstEmit.Iadd, typeof(OpCodeAluReg));
  86. Set("010011001100xx", InstEmit.Iadd3, typeof(OpCodeAluCbuf));
  87. Set("001110001100xx", InstEmit.Iadd3, typeof(OpCodeAluImm));
  88. Set("010111001100xx", InstEmit.Iadd3, typeof(OpCodeAluReg));
  89. Set("0100110000100x", InstEmit.Imnmx, typeof(OpCodeAluCbuf));
  90. Set("0011100x00100x", InstEmit.Imnmx, typeof(OpCodeAluImm));
  91. Set("0101110000100x", InstEmit.Imnmx, typeof(OpCodeAluReg));
  92. Set("11100000xxxxxx", InstEmit.Ipa, typeof(OpCodeIpa));
  93. Set("1110111111010x", InstEmit.Isberd, typeof(OpCodeAlu));
  94. Set("0100110000011x", InstEmit.Iscadd, typeof(OpCodeAluCbuf));
  95. Set("0011100x00011x", InstEmit.Iscadd, typeof(OpCodeAluImm));
  96. Set("000101xxxxxxxx", InstEmit.Iscadd, typeof(OpCodeAluImm32));
  97. Set("0101110000011x", InstEmit.Iscadd, typeof(OpCodeAluReg));
  98. Set("010010110101xx", InstEmit.Iset, typeof(OpCodeSetCbuf));
  99. Set("001101100101xx", InstEmit.Iset, typeof(OpCodeSetImm));
  100. Set("010110110101xx", InstEmit.Iset, typeof(OpCodeSetReg));
  101. Set("010010110110xx", InstEmit.Isetp, typeof(OpCodeSetCbuf));
  102. Set("0011011x0110xx", InstEmit.Isetp, typeof(OpCodeSetImm));
  103. Set("010110110110xx", InstEmit.Isetp, typeof(OpCodeSetReg));
  104. Set("111000110011xx", InstEmit.Kil, typeof(OpCodeExit));
  105. Set("1110111101000x", InstEmit.Ld, typeof(OpCodeMemory));
  106. Set("1110111110010x", InstEmit.Ldc, typeof(OpCodeLdc));
  107. Set("1110111011010x", InstEmit.Ldg, typeof(OpCodeMemory));
  108. Set("0100110001000x", InstEmit.Lop, typeof(OpCodeLopCbuf));
  109. Set("0011100001000x", InstEmit.Lop, typeof(OpCodeLopImm));
  110. Set("000001xxxxxxxx", InstEmit.Lop, typeof(OpCodeLopImm32));
  111. Set("0101110001000x", InstEmit.Lop, typeof(OpCodeLopReg));
  112. Set("0010000xxxxxxx", InstEmit.Lop3, typeof(OpCodeLopCbuf));
  113. Set("001111xxxxxxxx", InstEmit.Lop3, typeof(OpCodeLopImm));
  114. Set("0101101111100x", InstEmit.Lop3, typeof(OpCodeLopReg));
  115. Set("0100110010011x", InstEmit.Mov, typeof(OpCodeAluCbuf));
  116. Set("0011100x10011x", InstEmit.Mov, typeof(OpCodeAluImm));
  117. Set("000000010000xx", InstEmit.Mov, typeof(OpCodeAluImm32));
  118. Set("0101110010011x", InstEmit.Mov, typeof(OpCodeAluReg));
  119. Set("0101000010000x", InstEmit.Mufu, typeof(OpCodeFArith));
  120. Set("1111101111100x", InstEmit.Out, typeof(OpCode));
  121. Set("0101000010010x", InstEmit.Psetp, typeof(OpCodePsetp));
  122. Set("0100110010010x", InstEmit.Rro, typeof(OpCodeFArithCbuf));
  123. Set("0011100x10010x", InstEmit.Rro, typeof(OpCodeFArithImm));
  124. Set("0101110010010x", InstEmit.Rro, typeof(OpCodeFArithReg));
  125. Set("1111000011001x", InstEmit.S2r, typeof(OpCodeAlu));
  126. Set("0100110010100x", InstEmit.Sel, typeof(OpCodeAluCbuf));
  127. Set("0011100x10100x", InstEmit.Sel, typeof(OpCodeAluImm));
  128. Set("0101110010100x", InstEmit.Sel, typeof(OpCodeAluReg));
  129. Set("0100110001001x", InstEmit.Shl, typeof(OpCodeAluCbuf));
  130. Set("0011100x01001x", InstEmit.Shl, typeof(OpCodeAluImm));
  131. Set("0101110001001x", InstEmit.Shl, typeof(OpCodeAluReg));
  132. Set("0100110000101x", InstEmit.Shr, typeof(OpCodeAluCbuf));
  133. Set("0011100x00101x", InstEmit.Shr, typeof(OpCodeAluImm));
  134. Set("0101110000101x", InstEmit.Shr, typeof(OpCodeAluReg));
  135. Set("111000101001xx", InstEmit.Ssy, typeof(OpCodeSsy));
  136. Set("1110111101010x", InstEmit.St, typeof(OpCodeMemory));
  137. Set("1110111011011x", InstEmit.Stg, typeof(OpCodeMemory));
  138. Set("11101011001xxx", InstEmit.Sust, typeof(OpCodeImage));
  139. Set("1111000011111x", InstEmit.Sync, typeof(OpCodeSync));
  140. Set("110000xxxx111x", InstEmit.Tex, typeof(OpCodeTex));
  141. Set("1101111010111x", InstEmit.TexB, typeof(OpCodeTexB));
  142. Set("1101x00xxxxxxx", InstEmit.Texs, typeof(OpCodeTexs));
  143. Set("1101x01xxxxxxx", InstEmit.Texs, typeof(OpCodeTlds));
  144. Set("1101x11100xxxx", InstEmit.Texs, typeof(OpCodeTld4s));
  145. Set("11011100xx111x", InstEmit.Tld, typeof(OpCodeTld));
  146. Set("11011101xx111x", InstEmit.TldB, typeof(OpCodeTld));
  147. Set("110010xxxx111x", InstEmit.Tld4, typeof(OpCodeTld4));
  148. Set("1101111101001x", InstEmit.Txq, typeof(OpCodeTex));
  149. Set("1101111101010x", InstEmit.TxqB, typeof(OpCodeTex));
  150. Set("01011111xxxxxx", InstEmit.Vmad, typeof(OpCodeVideo));
  151. Set("0100111xxxxxxx", InstEmit.Xmad, typeof(OpCodeAluCbuf));
  152. Set("0011011x00xxxx", InstEmit.Xmad, typeof(OpCodeAluImm));
  153. Set("010100010xxxxx", InstEmit.Xmad, typeof(OpCodeAluRegCbuf));
  154. Set("0101101100xxxx", InstEmit.Xmad, typeof(OpCodeAluReg));
  155. #endregion
  156. }
  157. private static void Set(string encoding, InstEmitter emitter, Type opCodeType)
  158. {
  159. if (encoding.Length != EncodingBits)
  160. {
  161. throw new ArgumentException(nameof(encoding));
  162. }
  163. int bit = encoding.Length - 1;
  164. int value = 0;
  165. int xMask = 0;
  166. int xBits = 0;
  167. int[] xPos = new int[encoding.Length];
  168. for (int index = 0; index < encoding.Length; index++, bit--)
  169. {
  170. char chr = encoding[index];
  171. if (chr == '1')
  172. {
  173. value |= 1 << bit;
  174. }
  175. else if (chr == 'x')
  176. {
  177. xMask |= 1 << bit;
  178. xPos[xBits++] = bit;
  179. }
  180. }
  181. xMask = ~xMask;
  182. TableEntry entry = new TableEntry(emitter, opCodeType, xBits);
  183. for (int index = 0; index < (1 << xBits); index++)
  184. {
  185. value &= xMask;
  186. for (int x = 0; x < xBits; x++)
  187. {
  188. value |= ((index >> x) & 1) << xPos[x];
  189. }
  190. if (_opCodes[value] == null || _opCodes[value].XBits > xBits)
  191. {
  192. _opCodes[value] = entry;
  193. }
  194. }
  195. }
  196. public static (InstEmitter emitter, Type opCodeType) GetEmitter(long opCode)
  197. {
  198. TableEntry entry = _opCodes[(ulong)opCode >> (64 - EncodingBits)];
  199. if (entry != null)
  200. {
  201. return (entry.Emitter, entry.OpCodeType);
  202. }
  203. return (null, null);
  204. }
  205. }
  206. }