riscv_codec/
opcode.rs

1// the unusual bit groupings are used to match the ISA manual table
2#[allow(clippy::unusual_byte_groupings)]
3// Table 70, page 553 of the Unprivileged ISA Manual
4#[derive(Debug)]
5pub enum Opcode {
6    Load = 0b00_000_11,
7    Auipc = 0b00_101_11,
8    Store = 0b01_000_11,
9    Lui = 0b01_101_11,
10    Op = 0b01_100_11,
11    Op32 = 0b01_110_11,
12    OpImm = 0b00_100_11,
13    OpImm32 = 0b00_110_11,
14    Jalr = 0b11_001_11,
15    Jal = 0b11_011_11,
16    Branch = 0b11_000_11,
17    MiscMem = 0b00_011_11,
18    AMO = 0b01_011_11,
19    OpFp = 0b10_100_11,
20    LoadFp = 0b00_001_11,
21    StoreFp = 0b01_001_11,
22    Madd = 0b10_000_11,
23    Msub = 0b10_001_11,
24    Nmsub = 0b10_010_11,
25    Nmadd = 0b10_011_11,
26    System = 0b11_100_11,
27    Reserved = 0,
28}
29
30#[allow(clippy::unusual_byte_groupings)]
31impl Opcode {
32    pub fn from_int(int: u32) -> Self {
33        if int > 0b11_111_11 {
34            panic!("attempted to convert too large int to opcode")
35        }
36        match int {
37            0b00_000_11 => Self::Load,
38            0b00_101_11 => Self::Auipc,
39            0b01_000_11 => Self::Store,
40            0b01_101_11 => Self::Lui,
41            0b01_100_11 => Self::Op,
42            0b01_110_11 => Self::Op32,
43            0b00_100_11 => Self::OpImm,
44            0b00_110_11 => Self::OpImm32,
45            0b11_001_11 => Self::Jalr,
46            0b11_011_11 => Self::Jal,
47            0b11_000_11 => Self::Branch,
48            0b00_011_11 => Self::MiscMem,
49            0b01_011_11 => Self::AMO,
50            0b10_100_11 => Self::OpFp,
51            0b10_000_11 => Self::Madd,
52            0b10_001_11 => Self::Msub,
53            0b10_010_11 => Self::Nmsub,
54            0b10_011_11 => Self::Nmadd,
55            0b00_001_11 => Self::LoadFp,
56            0b01_001_11 => Self::StoreFp,
57            0b11_100_11 => Self::System,
58            _ => Self::Reserved,
59        }
60    }
61}