Skip to main content

ab_riscv_primitives/instructions/
zawrs.rs

1//! Zawrs extension
2
3#[cfg(test)]
4mod tests;
5
6use crate::instructions::Instruction;
7use crate::registers::general_purpose::Register;
8use ab_riscv_macros::instruction;
9use core::fmt;
10
11/// RISC-V Zawrs instruction (Wait-on-Reservation-Set)
12#[instruction]
13#[derive(Debug, Clone, Copy)]
14#[derive_const(PartialEq, Eq)]
15pub enum ZawrsInstruction<Reg> {
16    /// Wait-on-Reservation-Set, no timeout
17    WrsNto,
18    /// Wait-on-Reservation-Set, short timeout
19    WrsSto,
20}
21
22#[instruction]
23const impl<Reg> Instruction for ZawrsInstruction<Reg>
24where
25    Reg: [const] Register,
26{
27    type Reg = Reg;
28
29    #[inline(always)]
30    #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
31    fn try_decode(instruction: u32) -> Option<Self> {
32        let opcode = (instruction & 0b111_1111) as u8;
33        let rd_bits = ((instruction >> 7) & 0x1f) as u8;
34        let funct3 = ((instruction >> 12) & 0b111) as u8;
35        let rs1_bits = ((instruction >> 15) & 0x1f) as u8;
36        let imm = (instruction >> 20) & 0xfff;
37
38        match (opcode, funct3, rd_bits, rs1_bits, imm) {
39            (0b111_0011, 0b000, 0, 0, 0x00d) => Some(Self::WrsNto),
40            (0b111_0011, 0b000, 0, 0, 0x01d) => Some(Self::WrsSto),
41            _ => None,
42        }
43    }
44
45    #[inline(always)]
46    fn alignment() -> u8 {
47        align_of::<u32>() as u8
48    }
49
50    #[inline(always)]
51    fn size(&self) -> u8 {
52        size_of::<u32>() as u8
53    }
54}
55
56#[instruction]
57impl<Reg> fmt::Display for ZawrsInstruction<Reg>
58where
59    Reg: fmt::Display,
60{
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match self {
63            Self::WrsNto => write!(f, "wrs.nto"),
64            Self::WrsSto => write!(f, "wrs.sto"),
65        }
66    }
67}