ab_riscv_primitives/instructions/rv32/zce/
zcb.rs1#[cfg(test)]
4mod tests;
5
6use crate::instructions::Instruction;
7use crate::instructions::rv32::c::zca::Rv32ZcaInstruction;
8use crate::instructions::utils::I24;
9use crate::registers::general_purpose::Register;
10use ab_riscv_macros::instruction;
11use core::fmt;
12
13#[instruction(
17 inherit = [Rv32ZcaInstruction, Rv32ZcbOnlyInstruction],
18)]
19#[derive(Debug, Clone, Copy)]
20#[derive_const(PartialEq, Eq)]
21pub enum Rv32ZcbInstruction<Reg> {}
22
23#[instruction]
24const impl<Reg> Instruction for Rv32ZcbInstruction<Reg>
25where
26 Reg: [const] Register<Type = u32>,
27{
28 type Reg = Reg;
29
30 #[inline(always)]
31 #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
32 fn try_decode(instruction: u32) -> Option<Self> {
33 None
34 }
35
36 #[inline(always)]
37 fn alignment() -> u8 {
38 align_of::<u16>() as u8
39 }
40
41 #[inline(always)]
42 fn size(&self) -> u8 {
43 size_of::<u16>() as u8
44 }
45}
46
47#[instruction]
48impl<Reg> fmt::Display for Rv32ZcbInstruction<Reg>
49where
50 Reg: fmt::Display + Copy,
51{
52 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53 match self {}
54 }
55}
56
57#[instruction]
59#[derive(Debug, Clone, Copy)]
60#[derive_const(PartialEq, Eq)]
61#[rustfmt::skip]
62#[doc(hidden)]
63pub enum Rv32ZcbOnlyInstruction<Reg> {
64 CLbu { rd: Reg, rs1: Reg, uimm: u8 },
67 CLh { rd: Reg, rs1: Reg, uimm: u8 },
69 CLhu { rd: Reg, rs1: Reg, uimm: u8 },
71 CSb { rs1: Reg, rs2: Reg, uimm: u8 },
73 CSh { rs1: Reg, rs2: Reg, uimm: u8 },
75
76 CZextB { rd: Reg },
79 #[instruction(if = [Rv32ZbbInstruction])]
81 CSextB { rd: Reg },
82 #[instruction(if = [Rv32ZbbInstruction])]
84 CZextH { rd: Reg },
85 #[instruction(if = [Rv32ZbbInstruction])]
87 CSextH { rd: Reg },
88 CNot { rd: Reg },
90
91 #[instruction(
94 if = [Rv32MInstruction],
95 if = [Rv32ZmmulInstruction]
96 )]
97 CMul { rd: Reg, rs2: Reg },
98}
99
100#[instruction]
101const impl<Reg> Instruction for Rv32ZcbOnlyInstruction<Reg>
102where
103 Reg: [const] Register<Type = u32>,
104{
105 type Reg = Reg;
106
107 #[inline(always)]
108 #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
109 fn try_decode(instruction: u32) -> Option<Self> {
110 #[inline(always)]
112 const fn prime_reg_bits(bits: u8) -> u8 {
113 bits + 8
114 }
115
116 let inst = instruction as u16;
117 let quadrant = inst & 0b11;
118 let funct3 = ((inst >> 13) & 0b111) as u8;
119
120 match quadrant {
121 0b00 if funct3 == 0b100 => {
123 let sub = ((inst >> 10) & 0b111) as u8;
124 let rs1_bits = prime_reg_bits(((inst >> 7) & 0b111) as u8);
125 let rd_rs2_bits = prime_reg_bits(((inst >> 2) & 0b111) as u8);
126
127 match sub {
128 0b000 => {
130 let uimm = ((((inst >> 5) & 1) << 1) | ((inst >> 6) & 1)) as u8;
131 let rs1 = Reg::from_bits(rs1_bits)?;
132 let rd = Reg::from_bits(rd_rs2_bits)?;
133 Some(Self::CLbu { rd, rs1, uimm })
134 }
135 0b001 => {
138 let funct1 = ((inst >> 6) & 1) as u8;
139 let uimm = (((inst >> 5) & 1) as u8) << 1;
140 let rs1 = Reg::from_bits(rs1_bits)?;
141 let rd = Reg::from_bits(rd_rs2_bits)?;
142 if funct1 == 0 {
143 Some(Self::CLhu { rd, rs1, uimm })
144 } else {
145 Some(Self::CLh { rd, rs1, uimm })
146 }
147 }
148 0b010 => {
150 let uimm = ((((inst >> 5) & 1) << 1) | ((inst >> 6) & 1)) as u8;
151 let rs1 = Reg::from_bits(rs1_bits)?;
152 let rs2 = Reg::from_bits(rd_rs2_bits)?;
153 Some(Self::CSb { rs1, rs2, uimm })
154 }
155 0b011 => {
157 if ((inst >> 6) & 1) != 0 {
158 None?;
159 }
160 let uimm = (((inst >> 5) & 1) as u8) << 1;
161 let rs1 = Reg::from_bits(rs1_bits)?;
162 let rs2 = Reg::from_bits(rd_rs2_bits)?;
163 Some(Self::CSh { rs1, rs2, uimm })
164 }
165 _ => None,
166 }
167 }
168
169 0b01 if funct3 == 0b100 => {
177 let funct2_11_10 = ((inst >> 10) & 0b11) as u8;
178 let bit12 = (inst >> 12) & 1;
179
180 if funct2_11_10 != 0b11 || bit12 == 0 {
181 None?;
182 }
183
184 let rd_rs1_bits = prime_reg_bits(((inst >> 7) & 0b111) as u8);
185 let funct2b = ((inst >> 5) & 0b11) as u8;
186 let rs2_sub = ((inst >> 2) & 0b111) as u8;
187
188 match funct2b {
189 0b11 => {
191 let rd = Reg::from_bits(rd_rs1_bits)?;
192 match rs2_sub {
193 0b000 => Some(Self::CZextB { rd }),
194 0b001 => Some(Self::CSextB { rd }),
195 0b010 => Some(Self::CZextH { rd }),
196 0b011 => Some(Self::CSextH { rd }),
197 0b101 => Some(Self::CNot { rd }),
198 _ => None,
200 }
201 }
202 0b10 => {
204 let rd = Reg::from_bits(rd_rs1_bits)?;
205 let rs2 = Reg::from_bits(prime_reg_bits(rs2_sub))?;
206 Some(Self::CMul { rd, rs2 })
207 }
208 _ => None,
210 }
211 }
212 _ => None,
213 }
214 }
215
216 #[inline(always)]
217 fn alignment() -> u8 {
218 align_of::<u16>() as u8
219 }
220
221 #[inline(always)]
222 fn size(&self) -> u8 {
223 size_of::<u16>() as u8
224 }
225}
226
227#[instruction]
228impl<Reg> fmt::Display for Rv32ZcbOnlyInstruction<Reg>
229where
230 Reg: fmt::Display + Copy,
231{
232 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
233 match self {
234 Self::CLbu { rd, rs1, uimm } => write!(f, "c.lbu {rd}, {uimm}({rs1})"),
235 Self::CLh { rd, rs1, uimm } => write!(f, "c.lh {rd}, {uimm}({rs1})"),
236 Self::CLhu { rd, rs1, uimm } => write!(f, "c.lhu {rd}, {uimm}({rs1})"),
237 Self::CSb { rs1, rs2, uimm } => write!(f, "c.sb {rs2}, {uimm}({rs1})"),
238 Self::CSh { rs1, rs2, uimm } => write!(f, "c.sh {rs2}, {uimm}({rs1})"),
239 Self::CZextB { rd } => write!(f, "c.zext.b {rd}"),
240 Self::CSextB { rd } => write!(f, "c.sext.b {rd}"),
241 Self::CZextH { rd } => write!(f, "c.zext.h {rd}"),
242 Self::CSextH { rd } => write!(f, "c.sext.h {rd}"),
243 Self::CNot { rd } => write!(f, "c.not {rd}"),
244 Self::CMul { rd, rs2 } => write!(f, "c.mul {rd}, {rs2}"),
245 }
246 }
247}