Skip to main content

ab_riscv_interpreter/rv32/b/
zba.rs

1//! RV32 Zba extension
2
3#[cfg(test)]
4mod tests;
5
6use crate::{
7    ExecutableInstruction, ExecutableInstructionCsr, ExecutableInstructionOperands, ExecutionError,
8    ExecutionResult, FetchInstructionResult, InstructionFetcher, OpaqueThreadedExecutionResult,
9    RegisterFile, Rs1Rs2OperandValues, Rs1Rs2Operands, ThreadedExecutableInstruction,
10    ThreadedExecutionResult,
11};
12use ab_riscv_macros::instruction_execution;
13use ab_riscv_primitives::prelude::*;
14
15#[instruction_execution]
16const impl<Reg, Hart> ExecutableInstructionOperands for Rv32ZbaInstruction<Hart>
17where
18    Reg: Register<Type = u32>,
19    Hart: HartConfig<Reg = Reg>,
20{
21}
22
23#[instruction_execution]
24const impl<Reg, Hart, Env> ExecutableInstructionCsr<Env> for Rv32ZbaInstruction<Hart>
25where
26    Reg: Register<Type = u32>,
27    Hart: HartConfig<Reg = Reg>,
28{
29}
30
31#[instruction_execution]
32const impl<Reg, Hart, Regs, Env, Memory, PC> ExecutableInstruction<Regs, Env, Memory, PC>
33    for Rv32ZbaInstruction<Hart>
34where
35    Reg: [const] Register<Type = u32>,
36    Hart: HartConfig<Reg = Reg>,
37    Regs: [const] RegisterFile<Reg>,
38{
39    #[inline(always)]
40    #[cfg_attr(feature = "no-panic", no_panic_const::no_panic(const))]
41    fn execute(
42        self,
43        Rs1Rs2OperandValues {
44            rs1_value,
45            rs2_value,
46        }: Rs1Rs2OperandValues<Reg::Type>,
47        _regs: &mut Regs,
48        _env: &mut Env,
49        _memory: &mut Memory,
50        _program_counter: &PC,
51    ) -> ExecutionResult<Reg> {
52        match self {
53            Self::Sh1add { rd, rs1: _, rs2: _ } => {
54                let value = (rs1_value << 1).wrapping_add(rs2_value);
55                ExecutionResult::Continue { rd, value }
56            }
57            Self::Sh2add { rd, rs1: _, rs2: _ } => {
58                let value = (rs1_value << 2).wrapping_add(rs2_value);
59                ExecutionResult::Continue { rd, value }
60            }
61            Self::Sh3add { rd, rs1: _, rs2: _ } => {
62                let value = (rs1_value << 3).wrapping_add(rs2_value);
63                ExecutionResult::Continue { rd, value }
64            }
65        }
66    }
67}