1#[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#[instruction]
13#[derive(Debug, Clone, Copy)]
14#[derive_const(PartialEq, Eq)]
15#[rustfmt::skip]
16pub enum Rv64ZaamoInstruction<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 AmoswapD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
28 AmoaddD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
29 AmoxorD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
30 AmoandD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
31 AmoorD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
32 AmominD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
33 AmomaxD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
34 AmominuD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
35 AmomaxuD { rd: Reg, rs1: Reg, rs2: Reg, aq: bool, rl: bool },
36}
37
38#[instruction]
39const impl<Reg> Instruction for Rv64ZaamoInstruction<Reg>
40where
41 Reg: [const] Register<Type = u64>,
42{
43 const ALIGNMENT: u8 = align_of::<u32>() as u8;
44
45 type Reg = Reg;
46
47 #[inline(always)]
48 #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
49 fn try_decode(instruction: u32) -> Option<Self> {
50 let opcode = (instruction & 0b111_1111) as u8;
51 let rd_bits = ((instruction >> 7) & 0x1f) as u8;
52 let funct3 = ((instruction >> 12) & 0b111) as u8;
53 let rs1_bits = ((instruction >> 15) & 0x1f) as u8;
54 let rs2_bits = ((instruction >> 20) & 0x1f) as u8;
55 let funct7 = ((instruction >> 25) & 0b111_1111) as u8;
56
57 match (opcode, funct3) {
58 (0b010_1111, 0b010 | 0b011) => {
60 let rd = Reg::from_bits(rd_bits)?;
61 let rs1 = Reg::from_bits(rs1_bits)?;
62 let rs2 = Reg::from_bits(rs2_bits)?;
63 let funct5 = funct7 >> 2;
64 let aq = (funct7 & 0b10) != 0;
65 let rl = (funct7 & 0b01) != 0;
66
67 if funct3 == 0b010 {
68 match funct5 {
69 0b00001 => Some(Self::Amoswap {
70 rd,
71 rs1,
72 rs2,
73 aq,
74 rl,
75 }),
76 0b00000 => Some(Self::Amoadd {
77 rd,
78 rs1,
79 rs2,
80 aq,
81 rl,
82 }),
83 0b00100 => Some(Self::Amoxor {
84 rd,
85 rs1,
86 rs2,
87 aq,
88 rl,
89 }),
90 0b01100 => Some(Self::Amoand {
91 rd,
92 rs1,
93 rs2,
94 aq,
95 rl,
96 }),
97 0b01000 => Some(Self::Amoor {
98 rd,
99 rs1,
100 rs2,
101 aq,
102 rl,
103 }),
104 0b10000 => Some(Self::Amomin {
105 rd,
106 rs1,
107 rs2,
108 aq,
109 rl,
110 }),
111 0b10100 => Some(Self::Amomax {
112 rd,
113 rs1,
114 rs2,
115 aq,
116 rl,
117 }),
118 0b11000 => Some(Self::Amominu {
119 rd,
120 rs1,
121 rs2,
122 aq,
123 rl,
124 }),
125 0b11100 => Some(Self::Amomaxu {
126 rd,
127 rs1,
128 rs2,
129 aq,
130 rl,
131 }),
132 _ => None,
133 }
134 } else {
135 match funct5 {
136 0b00001 => Some(Self::AmoswapD {
137 rd,
138 rs1,
139 rs2,
140 aq,
141 rl,
142 }),
143 0b00000 => Some(Self::AmoaddD {
144 rd,
145 rs1,
146 rs2,
147 aq,
148 rl,
149 }),
150 0b00100 => Some(Self::AmoxorD {
151 rd,
152 rs1,
153 rs2,
154 aq,
155 rl,
156 }),
157 0b01100 => Some(Self::AmoandD {
158 rd,
159 rs1,
160 rs2,
161 aq,
162 rl,
163 }),
164 0b01000 => Some(Self::AmoorD {
165 rd,
166 rs1,
167 rs2,
168 aq,
169 rl,
170 }),
171 0b10000 => Some(Self::AmominD {
172 rd,
173 rs1,
174 rs2,
175 aq,
176 rl,
177 }),
178 0b10100 => Some(Self::AmomaxD {
179 rd,
180 rs1,
181 rs2,
182 aq,
183 rl,
184 }),
185 0b11000 => Some(Self::AmominuD {
186 rd,
187 rs1,
188 rs2,
189 aq,
190 rl,
191 }),
192 0b11100 => Some(Self::AmomaxuD {
193 rd,
194 rs1,
195 rs2,
196 aq,
197 rl,
198 }),
199 _ => None,
200 }
201 }
202 }
203 _ => None,
204 }
205 }
206
207 #[inline(always)]
208 fn size(&self) -> u8 {
209 size_of::<u32>() as u8
210 }
211}
212
213#[inline(always)]
215fn aq_rl_suffix(aq: &bool, rl: &bool) -> &'static str {
216 match (*aq, *rl) {
217 (false, false) => "",
218 (true, false) => ".aq",
219 (false, true) => ".rl",
220 (true, true) => ".aqrl",
221 }
222}
223
224#[instruction]
225impl<Reg> fmt::Display for Rv64ZaamoInstruction<Reg>
226where
227 Reg: fmt::Display,
228{
229 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
230 #[rustfmt::skip]
231 match self {
232 Self::Amoswap { rd, rs1, rs2, aq, rl } => write!(f, "amoswap.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
233 Self::Amoadd { rd, rs1, rs2, aq, rl } => write!(f, "amoadd.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
234 Self::Amoxor { rd, rs1, rs2, aq, rl } => write!(f, "amoxor.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
235 Self::Amoand { rd, rs1, rs2, aq, rl } => write!(f, "amoand.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
236 Self::Amoor { rd, rs1, rs2, aq, rl } => write!(f, "amoor.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
237 Self::Amomin { rd, rs1, rs2, aq, rl } => write!(f, "amomin.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
238 Self::Amomax { rd, rs1, rs2, aq, rl } => write!(f, "amomax.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
239 Self::Amominu { rd, rs1, rs2, aq, rl } => write!(f, "amominu.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
240 Self::Amomaxu { rd, rs1, rs2, aq, rl } => write!(f, "amomaxu.w{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
241
242 Self::AmoswapD { rd, rs1, rs2, aq, rl } => write!(f, "amoswap.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
243 Self::AmoaddD { rd, rs1, rs2, aq, rl } => write!(f, "amoadd.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
244 Self::AmoxorD { rd, rs1, rs2, aq, rl } => write!(f, "amoxor.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
245 Self::AmoandD { rd, rs1, rs2, aq, rl } => write!(f, "amoand.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
246 Self::AmoorD { rd, rs1, rs2, aq, rl } => write!(f, "amoor.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
247 Self::AmominD { rd, rs1, rs2, aq, rl } => write!(f, "amomin.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
248 Self::AmomaxD { rd, rs1, rs2, aq, rl } => write!(f, "amomax.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
249 Self::AmominuD { rd, rs1, rs2, aq, rl } => write!(f, "amominu.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
250 Self::AmomaxuD { rd, rs1, rs2, aq, rl } => write!(f, "amomaxu.d{} {rd}, {rs2}, ({rs1})", aq_rl_suffix(aq, rl)),
251 }
252 }
253}