OpCodeTable.cs 11 KB

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