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, PartialEq, Eq)]
20pub enum Rv32ZcbInstruction<Reg> {}
21
22#[instruction]
23impl<Reg> const Instruction for Rv32ZcbInstruction<Reg>
24where
25 Reg: [const] Register<Type = u32>,
26{
27 type Reg = Reg;
28
29 #[inline(always)]
30 fn try_decode(instruction: u32) -> Option<Self> {
31 None
32 }
33
34 #[inline(always)]
35 fn alignment() -> u8 {
36 align_of::<u16>() as u8
37 }
38
39 #[inline(always)]
40 fn size(&self) -> u8 {
41 size_of::<u16>() as u8
42 }
43}
44
45#[instruction]
46impl<Reg> fmt::Display for Rv32ZcbInstruction<Reg>
47where
48 Reg: fmt::Display + Copy,
49{
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 match self {}
52 }
53}
54
55#[instruction]
57#[derive(Debug, Clone, Copy, PartialEq, Eq)]
58#[rustfmt::skip]
59#[doc(hidden)]
60pub enum Rv32ZcbOnlyInstruction<Reg> {
61 CLbu { rd: Reg, rs1: Reg, uimm: u8 },
64 CLh { rd: Reg, rs1: Reg, uimm: u8 },
66 CLhu { rd: Reg, rs1: Reg, uimm: u8 },
68 CSb { rs1: Reg, rs2: Reg, uimm: u8 },
70 CSh { rs1: Reg, rs2: Reg, uimm: u8 },
72
73 CZextB { rd: Reg },
76 #[instruction(if = [Rv32ZbbInstruction])]
78 CSextB { rd: Reg },
79 #[instruction(if = [Rv32ZbbInstruction])]
81 CZextH { rd: Reg },
82 #[instruction(if = [Rv32ZbbInstruction])]
84 CSextH { rd: Reg },
85 CNot { rd: Reg },
87
88 #[instruction(
91 if = [Rv32MInstruction],
92 if = [Rv32ZmmulInstruction]
93 )]
94 CMul { rd: Reg, rs2: Reg },
95}
96
97#[instruction]
98impl<Reg> const Instruction for Rv32ZcbOnlyInstruction<Reg>
99where
100 Reg: [const] Register<Type = u32>,
101{
102 type Reg = Reg;
103
104 #[inline(always)]
105 fn try_decode(instruction: u32) -> Option<Self> {
106 #[inline(always)]
108 const fn prime_reg_bits(bits: u8) -> u8 {
109 bits + 8
110 }
111
112 let inst = instruction as u16;
113 let quadrant = inst & 0b11;
114 let funct3 = ((inst >> 13) & 0b111) as u8;
115
116 match quadrant {
117 0b00 if funct3 == 0b100 => {
119 let sub = ((inst >> 10) & 0b111) as u8;
120 let rs1_bits = prime_reg_bits(((inst >> 7) & 0b111) as u8);
121 let rd_rs2_bits = prime_reg_bits(((inst >> 2) & 0b111) as u8);
122
123 match sub {
124 0b000 => {
126 let uimm = ((((inst >> 5) & 1) << 1) | ((inst >> 6) & 1)) as u8;
127 let rs1 = Reg::from_bits(rs1_bits)?;
128 let rd = Reg::from_bits(rd_rs2_bits)?;
129 Some(Self::CLbu { rd, rs1, uimm })
130 }
131 0b001 => {
134 let funct1 = ((inst >> 6) & 1) as u8;
135 let uimm = (((inst >> 5) & 1) as u8) << 1;
136 let rs1 = Reg::from_bits(rs1_bits)?;
137 let rd = Reg::from_bits(rd_rs2_bits)?;
138 if funct1 == 0 {
139 Some(Self::CLhu { rd, rs1, uimm })
140 } else {
141 Some(Self::CLh { rd, rs1, uimm })
142 }
143 }
144 0b010 => {
146 let uimm = ((((inst >> 5) & 1) << 1) | ((inst >> 6) & 1)) as u8;
147 let rs1 = Reg::from_bits(rs1_bits)?;
148 let rs2 = Reg::from_bits(rd_rs2_bits)?;
149 Some(Self::CSb { rs1, rs2, uimm })
150 }
151 0b011 => {
153 if ((inst >> 6) & 1) != 0 {
154 None?;
155 }
156 let uimm = (((inst >> 5) & 1) as u8) << 1;
157 let rs1 = Reg::from_bits(rs1_bits)?;
158 let rs2 = Reg::from_bits(rd_rs2_bits)?;
159 Some(Self::CSh { rs1, rs2, uimm })
160 }
161 _ => None,
162 }
163 }
164
165 0b01 if funct3 == 0b100 => {
173 let funct2_11_10 = ((inst >> 10) & 0b11) as u8;
174 let bit12 = (inst >> 12) & 1;
175
176 if funct2_11_10 != 0b11 || bit12 == 0 {
177 None?;
178 }
179
180 let rd_rs1_bits = prime_reg_bits(((inst >> 7) & 0b111) as u8);
181 let funct2b = ((inst >> 5) & 0b11) as u8;
182 let rs2_sub = ((inst >> 2) & 0b111) as u8;
183
184 match funct2b {
185 0b11 => {
187 let rd = Reg::from_bits(rd_rs1_bits)?;
188 match rs2_sub {
189 0b000 => Some(Self::CZextB { rd }),
190 0b001 => Some(Self::CSextB { rd }),
191 0b010 => Some(Self::CZextH { rd }),
192 0b011 => Some(Self::CSextH { rd }),
193 0b101 => Some(Self::CNot { rd }),
194 _ => None,
196 }
197 }
198 0b10 => {
200 let rd = Reg::from_bits(rd_rs1_bits)?;
201 let rs2 = Reg::from_bits(prime_reg_bits(rs2_sub))?;
202 Some(Self::CMul { rd, rs2 })
203 }
204 _ => None,
206 }
207 }
208 _ => None,
209 }
210 }
211
212 #[inline(always)]
213 fn alignment() -> u8 {
214 align_of::<u16>() as u8
215 }
216
217 #[inline(always)]
218 fn size(&self) -> u8 {
219 size_of::<u16>() as u8
220 }
221}
222
223#[instruction]
224impl<Reg> fmt::Display for Rv32ZcbOnlyInstruction<Reg>
225where
226 Reg: fmt::Display + Copy,
227{
228 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
229 match self {
230 Self::CLbu { rd, rs1, uimm } => write!(f, "c.lbu {rd}, {uimm}({rs1})"),
231 Self::CLh { rd, rs1, uimm } => write!(f, "c.lh {rd}, {uimm}({rs1})"),
232 Self::CLhu { rd, rs1, uimm } => write!(f, "c.lhu {rd}, {uimm}({rs1})"),
233 Self::CSb { rs1, rs2, uimm } => write!(f, "c.sb {rs2}, {uimm}({rs1})"),
234 Self::CSh { rs1, rs2, uimm } => write!(f, "c.sh {rs2}, {uimm}({rs1})"),
235 Self::CZextB { rd } => write!(f, "c.zext.b {rd}"),
236 Self::CSextB { rd } => write!(f, "c.sext.b {rd}"),
237 Self::CZextH { rd } => write!(f, "c.zext.h {rd}"),
238 Self::CSextH { rd } => write!(f, "c.sext.h {rd}"),
239 Self::CNot { rd } => write!(f, "c.not {rd}"),
240 Self::CMul { rd, rs2 } => write!(f, "c.mul {rd}, {rs2}"),
241 }
242 }
243}