Skip to main content

ab_riscv_primitives/instructions/rv32/zk/
zbkx.rs

1//! RV32 Zbkx extension
2
3#[cfg(test)]
4mod tests;
5
6use crate::hart::HartConfig;
7use crate::instructions::{ImplementedExtension, Instruction, IsaExtension};
8use crate::registers::general_purpose::Register;
9use ab_riscv_macros::instruction;
10use core::fmt;
11
12/// RISC-V RV32 Zbkx instruction (Crossbar permutations)
13#[instruction]
14#[derive(Debug, Clone, Copy)]
15#[derive_const(PartialEq, Eq)]
16#[rustfmt::skip]
17pub enum Rv32ZbkxInstruction<Hart>
18where
19    Hart: HartConfig,
20{
21    Xperm4 { rd: Hart::Reg, rs1: Hart::Reg, rs2: Hart::Reg },
22    Xperm8 { rd: Hart::Reg, rs1: Hart::Reg, rs2: Hart::Reg },
23}
24
25#[instruction]
26const impl<Reg, Hart> Instruction for Rv32ZbkxInstruction<Hart>
27where
28    Reg: [const] Register<Type = u32>,
29    Hart: [const] HartConfig<Reg = Reg>,
30{
31    const OWN_ISA_EXTENSIONS: &'static [IsaExtension] = &[IsaExtension::new("zbkx", 1, 0)];
32
33    const ALIGNMENT: u8 = align_of::<u32>() as u8;
34
35    type Hart = Hart;
36
37    #[inline(always)]
38    #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
39    fn try_decode(instruction: u32) -> Option<Self> {
40        let opcode = (instruction & 0b111_1111) as u8;
41        let rd_bits = ((instruction >> 7) & 0x1f) as u8;
42        let funct3 = ((instruction >> 12) & 0b111) as u8;
43        let rs1_bits = ((instruction >> 15) & 0x1f) as u8;
44        let rs2_bits = ((instruction >> 20) & 0x1f) as u8;
45        let funct7 = ((instruction >> 25) & 0b111_1111) as u8;
46
47        match opcode {
48            0b011_0011 => {
49                let rd = Reg::from_bits(rd_bits)?;
50                let rs1 = Reg::from_bits(rs1_bits)?;
51                let rs2 = Reg::from_bits(rs2_bits)?;
52                match (funct3, funct7) {
53                    (0b010, 0b001_0100) => Some(Self::Xperm4 { rd, rs1, rs2 }),
54                    (0b100, 0b001_0100) => Some(Self::Xperm8 { rd, rs1, rs2 }),
55                    _ => None,
56                }
57            }
58            _ => None,
59        }
60    }
61
62    #[inline(always)]
63    fn size(&self) -> u8 {
64        size_of::<u32>() as u8
65    }
66}
67
68#[instruction]
69impl<Reg, Hart> fmt::Display for Rv32ZbkxInstruction<Hart>
70where
71    Reg: fmt::Display,
72    Hart: HartConfig<Reg = Reg>,
73{
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match self {
76            Self::Xperm4 { rd, rs1, rs2 } => write!(f, "xperm4 {rd}, {rs1}, {rs2}"),
77            Self::Xperm8 { rd, rs1, rs2 } => write!(f, "xperm8 {rd}, {rs1}, {rs2}"),
78        }
79    }
80}