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