Skip to main content

ab_riscv_primitives/instructions/rv32/zce/
zcb.rs

1//! RV32 Zcb extension
2
3#[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/// RISC-V RV32 Zcb compressed instruction set.
14///
15/// All register operands are prime-field (x8–x15) registers.
16#[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 that contains isolated Zcb instructions without inheriting Zca for testing purposes
58#[instruction]
59#[derive(Debug, Clone, Copy)]
60#[derive_const(PartialEq, Eq)]
61#[rustfmt::skip]
62#[doc(hidden)]
63pub enum Rv32ZcbOnlyInstruction<Reg> {
64    // Q00 loads / stores
65    /// C.LBU  rd' = zero_extend(mem8\[rs1' + uimm])  uimm ∈ {0,1,2,3}
66    CLbu { rd: Reg, rs1: Reg, uimm: u8 },
67    /// C.LH   rd' = sign_extend(mem16\[rs1' + uimm])  uimm ∈ {0,2}
68    CLh { rd: Reg, rs1: Reg, uimm: u8 },
69    /// C.LHU  rd' = zero_extend(mem16\[rs1' + uimm])  uimm ∈ {0,2}
70    CLhu { rd: Reg, rs1: Reg, uimm: u8 },
71    /// C.SB   mem8\[rs1' + uimm] = rs2'  uimm ∈ {0,1,2,3}
72    CSb { rs1: Reg, rs2: Reg, uimm: u8 },
73    /// C.SH   mem16\[rs1' + uimm] = rs2'  uimm ∈ {0,2}
74    CSh { rs1: Reg, rs2: Reg, uimm: u8 },
75
76    // Q01 unary bit-manipulation
77    /// C.ZEXT.B  rd' = rd' & 0xff
78    CZextB { rd: Reg },
79    /// C.SEXT.B  rd' = sext(rd'\[7:0])  (requires Zbb)
80    #[instruction(if = [Rv32ZbbInstruction])]
81    CSextB { rd: Reg },
82    /// C.ZEXT.H  rd' = rd' & 0xffff  (requires Zbb)
83    #[instruction(if = [Rv32ZbbInstruction])]
84    CZextH { rd: Reg },
85    /// C.SEXT.H  rd' = sext(rd'\[15:0])  (requires Zbb)
86    #[instruction(if = [Rv32ZbbInstruction])]
87    CSextH { rd: Reg },
88    /// C.NOT  rd' = ~rd'
89    CNot { rd: Reg },
90
91    // Q01 binary
92    /// C.MUL  rd' = (rd' * rs2')\[31:0]  (requires M or Zmmul)
93    #[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        /// Map a 3-bit "prime" register field to an absolute register number
111        #[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            // Q00 funct3=100: C.LBU / C.LHU / C.LH / C.SB / C.SH
122            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                    // C.LBU  uimm[1]=inst[5], uimm[0]=inst[6]
129                    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                    // C.LHU (funct1=inst[6]=0) / C.LH (funct1=inst[6]=1)
136                    // uimm[1]=inst[5], uimm[0]=0 (halfword aligned)
137                    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                    // C.SB  uimm[1]=inst[5], uimm[0]=inst[6]
149                    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                    // C.SH  funct1=inst[6]=0, uimm[1]=inst[5]
156                    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            // Q01 funct3=100, funct2[11:10]=11, bit12=1: unary ops and C.MUL
170            //
171            // Encoding layout (per ratified Zcb spec):
172            //   funct2b = inst[6:5]
173            //   0b11 => unary ops, sub-op = inst[4:2]
174            //   0b10 => C.MUL, rs2' = inst[4:2]
175            //   0b00, 0b01 => reserved
176            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                    // Unary ops: funct2b=0b11, sub-op in inst[4:2]
190                    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                            // 0b100, 0b110, 0b111 reserved
199                            _ => None,
200                        }
201                    }
202                    // C.MUL: funct2b=0b10, rs2' = inst[4:2]
203                    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                    // funct2b=0b00 and funct2b=0b01 are reserved in Zcb
209                    _ => 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}