@@ -118,6 +118,16 @@ static inline uint32_t decode_r4type_rs3(const uint32_t insn)
118118}
119119#endif
120120
121+ #if RV32_HAS (EXT_RVV )
122+ /* decode RVV vm field
123+ * vm = inst[25]
124+ */
125+ static inline uint32_t decode_rvv_vm (const uint32_t insn )
126+ {
127+ return (insn >> 25 ) & 0x1 ;
128+ }
129+ #endif
130+
121131#if RV32_HAS (EXT_C )
122132enum {
123133 /* clang-format off */
@@ -1792,6 +1802,183 @@ static inline bool op_cfsw(rv_insn_t *ir, const uint32_t insn)
17921802#define op_cflwsp OP_UNIMP
17931803#endif /* RV32_HAS(EXT_C) && RV32_HAS(EXT_F) */
17941804
1805+ static inline bool op_ivv (rv_insn_t * ir , const uint32_t insn ) {
1806+ #define MASK 0xfc00707f
1807+ #define MATCH_VADD_VI 0x3057
1808+ #define MATCH_VAND_VI 0x24003057
1809+ #define MATCH_VMADC_VI 0x46003057
1810+ #define MATCH_VMSEQ_VI 0x60003057
1811+ #define MATCH_VMSGT_VI 0x7c003057
1812+ #define MATCH_VMSGTU_VI 0x78003057
1813+ #define MATCH_VMSLE_VI 0x74003057
1814+ #define MATCH_VMSLEU_VI 0x70003057
1815+ #define MATCH_VMSNE_VI 0x64003057
1816+ #define MATCH_VOR_VI 0x28003057
1817+ #define MATCH_VRGATHER_VI 0x30003057
1818+ #define MATCH_VRSUB_VI 0xc003057
1819+ #define MATCH_VSADD_VI 0x84003057
1820+ #define MATCH_VSADDU_VI 0x80003057
1821+ #define MATCH_VSLIDEDOWN_VI 0x3c003057
1822+ #define MATCH_VSLIDEUP_VI 0x38003057
1823+ #define MATCH_VSLL_VI 0x94003057
1824+ #define MATCH_VSRA_VI 0xa4003057
1825+ #define MATCH_VSRL_VI 0xa0003057
1826+ #define MATCH_VSSRA_VI 0xac003057
1827+ #define MATCH_VSSRL_VI 0xa8003057
1828+ #define MATCH_VXOR_VI 0x2c003057
1829+
1830+ ir -> rs1 = decode_rs1 (insn );
1831+ ir -> rs2 = decode_rs2 (insn );
1832+ ir -> vm = decode_rvv_vm (insn );
1833+ switch (insn & MASK ) {
1834+ case MATCH_VADD_VI :
1835+ ir -> opcode = rv_insn_vadd_vi ;
1836+ break ;
1837+ case MATCH_VAND_VI :
1838+ ir -> opcode = rv_insn_vand_vi ;
1839+ break ;
1840+ case MATCH_VMADC_VI :
1841+ ir -> opcode = rv_insn_vmadc_vi ;
1842+ break ;
1843+ case MATCH_VMSEQ_VI :
1844+ ir -> opcode = rv_insn_vmseq_vi ;
1845+ break ;
1846+ case MATCH_VMSGT_VI :
1847+ ir -> opcode = rv_insn_vmsgt_vi ;
1848+ break ;
1849+ case MATCH_VMSGTU_VI :
1850+ ir -> opcode = rv_insn_vmsgtu_vi ;
1851+ break ;
1852+ case MATCH_VMSLE_VI :
1853+ ir -> opcode = rv_insn_vmsle_vi ;
1854+ break ;
1855+ case MATCH_VMSLEU_VI :
1856+ ir -> opcode = rv_insn_vmsleu_vi ;
1857+ break ;
1858+ case MATCH_VMSNE_VI :
1859+ ir -> opcode = rv_insn_vmsne_vi ;
1860+ break ;
1861+ case MATCH_VOR_VI :
1862+ ir -> opcode = rv_insn_vor_vi ;
1863+ break ;
1864+ case MATCH_VRGATHER_VI :
1865+ ir -> opcode = rv_insn_vrgather_vi ;
1866+ break ;
1867+ case MATCH_VRSUB_VI :
1868+ ir -> opcode = rv_insn_vrsub_vi ;
1869+ break ;
1870+ case MATCH_VSADD_VI :
1871+ ir -> opcode = rv_insn_vsadd_vi ;
1872+ break ;
1873+ case MATCH_VSADDU_VI :
1874+ ir -> opcode = rv_insn_vsaddu_vi ;
1875+ break ;
1876+ case MATCH_VSLIDEDOWN_VI :
1877+ ir -> opcode = rv_insn_vslidedown_vi ;
1878+ break ;
1879+ case MATCH_VSLIDEUP_VI :
1880+ ir -> opcode = rv_insn_vslideup_vi ;
1881+ break ;
1882+ case MATCH_VSLL_VI :
1883+ ir -> opcode = rv_insn_vsll_vi ;
1884+ break ;
1885+ case MATCH_VSRA_VI :
1886+ ir -> opcode = rv_insn_vsra_vi ;
1887+ break ;
1888+ case MATCH_VSRL_VI :
1889+ ir -> opcode = rv_insn_vsrl_vi ;
1890+ break ;
1891+ case MATCH_VSSRA_VI :
1892+ ir -> opcode = rv_insn_vssra_vi ;
1893+ break ;
1894+ case MATCH_VSSRL_VI :
1895+ ir -> opcode = rv_insn_vssrl_vi ;
1896+ break ;
1897+ case MATCH_VXOR_VI :
1898+ ir -> opcode = rv_insn_vxor_vi ;
1899+ break ;
1900+ default :
1901+ return false;
1902+ }
1903+ }
1904+
1905+ static inline bool op_fvv (rv_insn_t * ir , const uint32_t insn ) {}
1906+ static inline bool op_mvv (rv_insn_t * ir , const uint32_t insn ) {}
1907+ static inline bool op_ivi (rv_insn_t * ir , const uint32_t insn ) {}
1908+ static inline bool op_ivx (rv_insn_t * ir , const uint32_t insn ) {}
1909+ static inline bool op_fvf (rv_insn_t * ir , const uint32_t insn ) {}
1910+ static inline bool op_mvx (rv_insn_t * ir , const uint32_t insn ) {}
1911+
1912+ /* OP: RVV
1913+ * opcode is 0x57 for VALU and VCFG
1914+ *
1915+ * VALU format:
1916+ * 31 26 25 24 20 19 15 14 12 11 7 6 0
1917+ * funct6 | vm | vs2 | vs1 | 0 0 0 (funct3) | vd |1010111| OP-V (OPIVV)
1918+ * funct6 | vm | vs2 | vs1 | 0 0 1 (funct3) | vd/rd |1010111| OP-V (OPFVV)
1919+ * funct6 | vm | vs2 | vs1 | 0 1 0 (funct3) | vd/rd |1010111| OP-V (OPMVV)
1920+ * funct6 | vm | vs2 | imm[4:0] | 0 1 1 (funct3) | vd |1010111| OP-V (OPIVI)
1921+ * funct6 | vm | vs2 | rs1 | 1 0 0 (funct3) | vd |1010111| OP-V (OPIVX)
1922+ * funct6 | vm | vs2 | rs1 | 1 0 1 (funct3) | vd |1010111| OP-V (OPFVF)
1923+ * funct6 | vm | vs2 | rs1 | 1 1 0 (funct3) | vd/rd |1010111| OP-V (OPMVX)
1924+ * 6 1 5 5 3 5 7
1925+ *
1926+ * Where 'vm' is the bit indicates whether masking is enabled
1927+ * see https://github.com/riscv/riscv-v-spec/blob/master/v-spec.adoc#531-mask-encoding
1928+ *
1929+ * VMEM format:
1930+ *
1931+ * 31 29 28 27 26 25 24 20 19 15 14 12 11 7 6 0
1932+ * nf | mew| mop | vm | lumop | rs1 | width | vd |0000111| VL* unit-stride
1933+ * nf | mew| mop | vm | rs2 | rs1 | width | vd |0000111| VLS* strided
1934+ * nf | mew| mop | vm | vs2 | rs1 | width | vd |0000111| VLX* indexed
1935+ * 3 1 2 1 5 5 3 5 7
1936+ *
1937+ * VCFG format:
1938+ *
1939+ * 31 30 25 24 20 19 15 14 12 11 7 6 0
1940+ * 0 | zimm[10:0] | rs1 | 1 1 1 | rd |1010111| vsetvli
1941+ * 1 | 1| zimm[ 9:0] | uimm[4:0]| 1 1 1 | rd |1010111| vsetivli
1942+ * 1 | 000000 | rs2 | rs1 | 1 1 1 | rd |1010111| vsetvl
1943+ * 1 6 5 5 3 5 7
1944+ *
1945+ * reference:
1946+ * https://github.com/riscv/riscv-isa-manual/blob/main/src/images/wavedrom/valu-format.edn
1947+ * https://github.com/riscv/riscv-isa-manual/blob/main/src/images/wavedrom/v-inst-table.edn
1948+ * https://observablehq.com/@drom/risc-v-v
1949+ *
1950+ * funct3
1951+ * | 0 | 0 | 0 | OPIVV | vector-vector | N/A
1952+ * | 0 | 0 | 1 | OPFVV | vector-vector | N/A
1953+ * | 0 | 1 | 0 | OPMVV | vector-vector | N/A
1954+ * | 0 | 1 | 1 | OPIVI | vector-immediate | `imm[4:0]`
1955+ * | 1 | 0 | 0 | OPIVX | vector-scalar | GPR `x` register `rs1`
1956+ * | 1 | 0 | 1 | OPFVF | vector-scalar | FP `f` register `rs1`
1957+ * | 1 | 1 | 0 | OPMVX | vector-scalar | GPR `x` register `rs1`
1958+ */
1959+ static inline bool op_v (rv_insn_t * ir , const uint32_t insn )
1960+ {
1961+ uint32_t funct3_mask = 0x7000 ;
1962+ switch (insn & funct3_mask ) {
1963+ case 0 :
1964+ return op_ivv (ir , insn );
1965+ case 1 :
1966+ return op_fvv (ir , insn );
1967+ case 2 :
1968+ return op_mvv (ir , insn );
1969+ case 3 :
1970+ return op_ivi (ir , insn );
1971+ case 4 :
1972+ return op_ivx (ir , insn );
1973+ case 5 :
1974+ return op_fvf (ir , insn );
1975+ case 6 :
1976+ return op_mvx (ir , insn );
1977+ default :
1978+ return false;
1979+ }
1980+ }
1981+
17951982/* handler for all unimplemented opcodes */
17961983static inline bool op_unimp (rv_insn_t * ir UNUSED , uint32_t insn UNUSED )
17971984{
@@ -1806,13 +1993,22 @@ bool rv_decode(rv_insn_t *ir, uint32_t insn)
18061993{
18071994 assert (ir );
18081995
1996+ #if RV32_HAS (EXT_RVV )
1997+ #define MASK_OPCODE_RVV 0x57
1998+ #define MASK_OPCODE_RVV_VMEM 0x57
1999+ if (insn & MASK_OPCODE_RVV ) {
2000+ return op_v (ir , insn );
2001+ }
2002+ #endif
2003+
18092004#define OP_UNIMP op_unimp
18102005#define OP (insn ) op_##insn
18112006
18122007 /* RV32 base opcode map */
18132008 /* clang-format off */
18142009 static const decode_t rv_jump_table [] = {
1815- // 000 001 010 011 100 101 110 111
2010+ // insn[4:2]
2011+ // 000 001 010 011 100 101 110 111 // insn[6:5]
18162012 OP (load ), OP (load_fp ), OP (unimp ), OP (misc_mem ), OP (op_imm ), OP (auipc ), OP (unimp ), OP (unimp ), // 00
18172013 OP (store ), OP (store_fp ), OP (unimp ), OP (amo ), OP (op ), OP (lui ), OP (unimp ), OP (unimp ), // 01
18182014 OP (madd ), OP (msub ), OP (nmsub ), OP (nmadd ), OP (op_fp ), OP (unimp ), OP (unimp ), OP (unimp ), // 10
0 commit comments