Skip to main content

ab_riscv_primitives/instructions/rv64/
zabha.rs

1//! RV64 Zabha extension
2
3#[cfg(test)]
4mod tests;
5
6use crate::instructions::Instruction;
7use crate::instructions::rv64::a::zaamo::Rv64ZaamoInstruction;
8use crate::registers::general_purpose::Register;
9use ab_riscv_macros::instruction;
10use core::fmt;
11
12/// RISC-V RV64 Zabha instruction (Byte and Halfword Atomic Memory Operations)
13#[instruction(inherit = [Rv64ZaamoInstruction])]
14#[derive(Debug, Clone, Copy)]
15#[derive_const(PartialEq, Eq)]
16#[rustfmt::skip]
17pub enum Rv64ZabhaInstruction<Reg> {
18    AmoswapB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
19    AmoswapH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
20    AmoaddB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
21    AmoaddH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
22    AmoxorB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
23    AmoxorH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
24    AmoandB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
25    AmoandH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
26    AmoorB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
27    AmoorH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
28    AmominB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
29    AmominH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
30    AmomaxB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
31    AmomaxH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
32    AmominuB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
33    AmominuH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
34    AmomaxuB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
35    AmomaxuH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
36    /// Compare-and-swap byte. Only present when `Zacas` is also implemented.
37    #[instruction(if = [Rv64ZacasInstruction])]
38    AmocasB { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
39    /// Compare-and-swap halfword. Only present when `Zacas` is also implemented.
40    #[instruction(if = [Rv64ZacasInstruction])]
41    AmocasH { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
42}
43
44#[instruction]
45const impl<Reg> Instruction for Rv64ZabhaInstruction<Reg>
46where
47    Reg: [const] Register<Type = u64>,
48{
49    const ALIGNMENT: u8 = align_of::<u32>() as u8;
50
51    type Reg = Reg;
52
53    #[inline(always)]
54    #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
55    fn try_decode(instruction: u32) -> Option<Self> {
56        let opcode = (instruction & 0b111_1111) as u8;
57        let rd_bits = ((instruction >> 7) & 0x1f) as u8;
58        let funct3 = ((instruction >> 12) & 0b111) as u8;
59        let rs1_bits = ((instruction >> 15) & 0x1f) as u8;
60        let rs2_bits = ((instruction >> 20) & 0x1f) as u8;
61        let funct7 = ((instruction >> 25) & 0b111_1111) as u8;
62
63        match (opcode, funct3) {
64            // AMO, byte- or halfword-sized
65            (0b010_1111, 0b000 | 0b001) => {
66                let rd = Reg::from_bits(rd_bits)?;
67                let rs1 = Reg::from_bits(rs1_bits)?;
68                let rs2 = Reg::from_bits(rs2_bits)?;
69                let funct5 = funct7 >> 2;
70                let aq = (funct7 & 0b10) != 0;
71                let rl = (funct7 & 0b01) != 0;
72
73                if funct3 == 0b000 {
74                    match funct5 {
75                        0b00001 => Some(Self::AmoswapB {
76                            rd,
77                            rs1,
78                            rs2,
79                            aq,
80                            rl,
81                        }),
82                        0b00000 => Some(Self::AmoaddB {
83                            rd,
84                            rs1,
85                            rs2,
86                            aq,
87                            rl,
88                        }),
89                        0b00100 => Some(Self::AmoxorB {
90                            rd,
91                            rs1,
92                            rs2,
93                            aq,
94                            rl,
95                        }),
96                        0b01100 => Some(Self::AmoandB {
97                            rd,
98                            rs1,
99                            rs2,
100                            aq,
101                            rl,
102                        }),
103                        0b01000 => Some(Self::AmoorB {
104                            rd,
105                            rs1,
106                            rs2,
107                            aq,
108                            rl,
109                        }),
110                        0b10000 => Some(Self::AmominB {
111                            rd,
112                            rs1,
113                            rs2,
114                            aq,
115                            rl,
116                        }),
117                        0b10100 => Some(Self::AmomaxB {
118                            rd,
119                            rs1,
120                            rs2,
121                            aq,
122                            rl,
123                        }),
124                        0b11000 => Some(Self::AmominuB {
125                            rd,
126                            rs1,
127                            rs2,
128                            aq,
129                            rl,
130                        }),
131                        0b11100 => Some(Self::AmomaxuB {
132                            rd,
133                            rs1,
134                            rs2,
135                            aq,
136                            rl,
137                        }),
138                        0b00101 => Some(Self::AmocasB {
139                            rd,
140                            rs1,
141                            rs2,
142                            aq,
143                            rl,
144                        }),
145                        _ => None,
146                    }
147                } else {
148                    match funct5 {
149                        0b00001 => Some(Self::AmoswapH {
150                            rd,
151                            rs1,
152                            rs2,
153                            aq,
154                            rl,
155                        }),
156                        0b00000 => Some(Self::AmoaddH {
157                            rd,
158                            rs1,
159                            rs2,
160                            aq,
161                            rl,
162                        }),
163                        0b00100 => Some(Self::AmoxorH {
164                            rd,
165                            rs1,
166                            rs2,
167                            aq,
168                            rl,
169                        }),
170                        0b01100 => Some(Self::AmoandH {
171                            rd,
172                            rs1,
173                            rs2,
174                            aq,
175                            rl,
176                        }),
177                        0b01000 => Some(Self::AmoorH {
178                            rd,
179                            rs1,
180                            rs2,
181                            aq,
182                            rl,
183                        }),
184                        0b10000 => Some(Self::AmominH {
185                            rd,
186                            rs1,
187                            rs2,
188                            aq,
189                            rl,
190                        }),
191                        0b10100 => Some(Self::AmomaxH {
192                            rd,
193                            rs1,
194                            rs2,
195                            aq,
196                            rl,
197                        }),
198                        0b11000 => Some(Self::AmominuH {
199                            rd,
200                            rs1,
201                            rs2,
202                            aq,
203                            rl,
204                        }),
205                        0b11100 => Some(Self::AmomaxuH {
206                            rd,
207                            rs1,
208                            rs2,
209                            aq,
210                            rl,
211                        }),
212                        0b00101 => Some(Self::AmocasH {
213                            rd,
214                            rs1,
215                            rs2,
216                            aq,
217                            rl,
218                        }),
219                        _ => None,
220                    }
221                }
222            }
223            _ => None,
224        }
225    }
226
227    #[inline(always)]
228    fn size(&self) -> u8 {
229        size_of::<u32>() as u8
230    }
231}
232
233/// Format `aq`/`rl` suffix for display
234#[inline(always)]
235fn aq_rl_suffix(aq: &bool, rl: &bool) -> &'static str {
236    match (*aq, *rl) {
237        (false, false) => "",
238        (true, false) => ".aq",
239        (false, true) => ".rl",
240        (true, true) => ".aqrl",
241    }
242}
243
244#[instruction]
245impl<Reg> fmt::Display for Rv64ZabhaInstruction<Reg>
246where
247    Reg: fmt::Display + Copy,
248{
249    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250        #[rustfmt::skip]
251        match self {
252            Self::AmoswapB { rd, rs1, rs2, aq, rl } => write!(f, "amoswap.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
253            Self::AmoswapH { rd, rs1, rs2, aq, rl } => write!(f, "amoswap.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
254            Self::AmoaddB { rd, rs1, rs2, aq, rl } => write!(f, "amoadd.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
255            Self::AmoaddH { rd, rs1, rs2, aq, rl } => write!(f, "amoadd.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
256            Self::AmoxorB { rd, rs1, rs2, aq, rl } => write!(f, "amoxor.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
257            Self::AmoxorH { rd, rs1, rs2, aq, rl } => write!(f, "amoxor.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
258            Self::AmoandB { rd, rs1, rs2, aq, rl } => write!(f, "amoand.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
259            Self::AmoandH { rd, rs1, rs2, aq, rl } => write!(f, "amoand.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
260            Self::AmoorB { rd, rs1, rs2, aq, rl } => write!(f, "amoor.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
261            Self::AmoorH { rd, rs1, rs2, aq, rl } => write!(f, "amoor.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
262            Self::AmominB { rd, rs1, rs2, aq, rl } => write!(f, "amomin.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
263            Self::AmominH { rd, rs1, rs2, aq, rl } => write!(f, "amomin.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
264            Self::AmomaxB { rd, rs1, rs2, aq, rl } => write!(f, "amomax.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
265            Self::AmomaxH { rd, rs1, rs2, aq, rl } => write!(f, "amomax.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
266            Self::AmominuB { rd, rs1, rs2, aq, rl } => write!(f, "amominu.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
267            Self::AmominuH { rd, rs1, rs2, aq, rl } => write!(f, "amominu.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
268            Self::AmomaxuB { rd, rs1, rs2, aq, rl } => write!(f, "amomaxu.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
269            Self::AmomaxuH { rd, rs1, rs2, aq, rl } => write!(f, "amomaxu.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
270            Self::AmocasB { rd, rs1, rs2, aq, rl } => write!(f, "amocas.b{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
271            Self::AmocasH { rd, rs1, rs2, aq, rl } => write!(f, "amocas.h{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
272        }
273    }
274}