Skip to main content

ab_riscv_primitives/instructions/rv32/a/
zaamo.rs

1//! RV32 Zaamo extension
2
3#[cfg(test)]
4mod tests;
5
6use crate::instructions::Instruction;
7use crate::registers::general_purpose::Register;
8use ab_riscv_macros::instruction;
9use core::fmt;
10
11/// RISC-V RV32 Zaamo instruction (Atomic memory operations)
12#[instruction]
13#[derive(Debug, Clone, Copy)]
14#[derive_const(PartialEq, Eq)]
15#[rustfmt::skip]
16pub enum Rv32ZaamoInstruction<Reg> {
17    Amoswap { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
18    Amoadd { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
19    Amoxor { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
20    Amoand { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
21    Amoor { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
22    Amomin { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
23    Amomax { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
24    Amominu { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
25    Amomaxu { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
26}
27
28#[instruction]
29const impl<Reg> Instruction for Rv32ZaamoInstruction<Reg>
30where
31    Reg: [const] Register<Type = u32>,
32{
33    const ALIGNMENT: u8 = align_of::<u32>() as u8;
34
35    type Reg = Reg;
36
37    #[inline(always)]
38    #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
39    fn try_decode(instruction: u32) -> Option<Self> {
40        let opcode = (instruction & 0b111_1111) as u8;
41        let rd_bits = ((instruction >> 7) & 0x1f) as u8;
42        let funct3 = ((instruction >> 12) & 0b111) as u8;
43        let rs1_bits = ((instruction >> 15) & 0x1f) as u8;
44        let rs2_bits = ((instruction >> 20) & 0x1f) as u8;
45        let funct7 = ((instruction >> 25) & 0b111_1111) as u8;
46
47        match (opcode, funct3) {
48            // AMO, word-sized (only size that exists in RV32)
49            (0b010_1111, 0b010) => {
50                let rd = Reg::from_bits(rd_bits)?;
51                let rs1 = Reg::from_bits(rs1_bits)?;
52                let rs2 = Reg::from_bits(rs2_bits)?;
53                let funct5 = funct7 >> 2;
54                let aq = (funct7 & 0b10) != 0;
55                let rl = (funct7 & 0b01) != 0;
56
57                match funct5 {
58                    0b00001 => Some(Self::Amoswap {
59                        rd,
60                        rs1,
61                        rs2,
62                        aq,
63                        rl,
64                    }),
65                    0b00000 => Some(Self::Amoadd {
66                        rd,
67                        rs1,
68                        rs2,
69                        aq,
70                        rl,
71                    }),
72                    0b00100 => Some(Self::Amoxor {
73                        rd,
74                        rs1,
75                        rs2,
76                        aq,
77                        rl,
78                    }),
79                    0b01100 => Some(Self::Amoand {
80                        rd,
81                        rs1,
82                        rs2,
83                        aq,
84                        rl,
85                    }),
86                    0b01000 => Some(Self::Amoor {
87                        rd,
88                        rs1,
89                        rs2,
90                        aq,
91                        rl,
92                    }),
93                    0b10000 => Some(Self::Amomin {
94                        rd,
95                        rs1,
96                        rs2,
97                        aq,
98                        rl,
99                    }),
100                    0b10100 => Some(Self::Amomax {
101                        rd,
102                        rs1,
103                        rs2,
104                        aq,
105                        rl,
106                    }),
107                    0b11000 => Some(Self::Amominu {
108                        rd,
109                        rs1,
110                        rs2,
111                        aq,
112                        rl,
113                    }),
114                    0b11100 => Some(Self::Amomaxu {
115                        rd,
116                        rs1,
117                        rs2,
118                        aq,
119                        rl,
120                    }),
121                    _ => None,
122                }
123            }
124            _ => None,
125        }
126    }
127
128    #[inline(always)]
129    fn size(&self) -> u8 {
130        size_of::<u32>() as u8
131    }
132}
133
134/// Format `aq`/`rl` suffix for display
135#[inline(always)]
136fn aq_rl_suffix(aq: &bool, rl: &bool) -> &'static str {
137    match (*aq, *rl) {
138        (false, false) => "",
139        (true, false) => ".aq",
140        (false, true) => ".rl",
141        (true, true) => ".aqrl",
142    }
143}
144
145#[instruction]
146impl<Reg> fmt::Display for Rv32ZaamoInstruction<Reg>
147where
148    Reg: fmt::Display,
149{
150    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
151        #[rustfmt::skip]
152        match self {
153            Self::Amoswap { rd, rs1, rs2, aq, rl } => write!(f, "amoswap.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
154            Self::Amoadd { rd, rs1, rs2, aq, rl } => write!(f, "amoadd.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
155            Self::Amoxor { rd, rs1, rs2, aq, rl } => write!(f, "amoxor.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
156            Self::Amoand { rd, rs1, rs2, aq, rl } => write!(f, "amoand.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
157            Self::Amoor { rd, rs1, rs2, aq, rl } => write!(f, "amoor.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
158            Self::Amomin { rd, rs1, rs2, aq, rl } => write!(f, "amomin.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
159            Self::Amomax { rd, rs1, rs2, aq, rl } => write!(f, "amomax.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
160            Self::Amominu { rd, rs1, rs2, aq, rl } => write!(f, "amominu.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
161            Self::Amomaxu { rd, rs1, rs2, aq, rl } => write!(f, "amomaxu.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
162        }
163    }
164}