pub mod rv32_zbkb_helpers;
#[cfg(test)]
mod tests;
use crate::{ExecutableInstruction, ExecutionError, InterpreterState};
use ab_riscv_macros::instruction_execution;
use ab_riscv_primitives::prelude::*;
use core::ops::ControlFlow;
#[instruction_execution]
impl<Reg, ExtState, Memory, PC, InstructionHandler, CustomError>
ExecutableInstruction<
InterpreterState<Reg, ExtState, Memory, PC, InstructionHandler, CustomError>,
CustomError,
> for Rv32ZbkbInstruction<Reg>
where
Reg: Register<Type = u32>,
[(); Reg::N]:,
{
#[inline(always)]
fn execute(
self,
state: &mut InterpreterState<Reg, ExtState, Memory, PC, InstructionHandler, CustomError>,
) -> Result<ControlFlow<()>, ExecutionError<Reg::Type, CustomError>> {
match self {
Self::Pack { rd, rs1, rs2 } => {
let lo = state.regs.read(rs1) & 0x0000_FFFFu32;
let hi = (state.regs.read(rs2) & 0x0000_FFFFu32) << 16;
state.regs.write(rd, lo | hi);
}
Self::Packh { rd, rs1, rs2 } => {
let lo = state.regs.read(rs1) & 0xFF;
let hi = (state.regs.read(rs2) & 0xFF) << 8;
state.regs.write(rd, lo | hi);
}
Self::Brev8 { rd, rs1 } => {
let src = state.regs.read(rs1);
let bytes = src.to_le_bytes().map(u8::reverse_bits);
state.regs.write(rd, u32::from_le_bytes(bytes));
}
Self::Zip { rd, rs1 } => {
let src = state.regs.read(rs1);
state.regs.write(rd, rv32_zbkb_helpers::zip(src));
}
Self::Unzip { rd, rs1 } => {
let src = state.regs.read(rs1);
state.regs.write(rd, rv32_zbkb_helpers::unzip(src));
}
}
Ok(ControlFlow::Continue(()))
}
}