#[cfg(test)]
mod tests;
use crate::{ExecutableInstruction, ExecutionError, RegisterFile};
use ab_riscv_macros::instruction_execution;
use ab_riscv_primitives::prelude::*;
use core::ops::ControlFlow;
#[instruction_execution]
impl<Reg, Regs, ExtState, Memory, PC, InstructionHandler, CustomError>
ExecutableInstruction<Regs, ExtState, Memory, PC, InstructionHandler, CustomError>
for Rv64ZbkbInstruction<Reg>
where
Reg: Register<Type = u64>,
Regs: RegisterFile<Reg>,
{
#[inline(always)]
fn execute(
self,
regs: &mut Regs,
_ext_state: &mut ExtState,
_memory: &mut Memory,
_program_counter: &mut PC,
_system_instruction_handler: &mut InstructionHandler,
) -> Result<ControlFlow<()>, ExecutionError<Reg::Type, CustomError>> {
match self {
Self::Pack { rd, rs1, rs2 } => {
let lo = regs.read(rs1) & 0x0000_0000_FFFF_FFFFu64;
let hi = (regs.read(rs2) & 0x0000_0000_FFFF_FFFFu64) << 32;
regs.write(rd, lo | hi);
}
Self::Packh { rd, rs1, rs2 } => {
let lo = regs.read(rs1) & 0xFF;
let hi = (regs.read(rs2) & 0xFF) << 8;
regs.write(rd, lo | hi);
}
Self::Packw { rd, rs1, rs2 } => {
let lo = regs.read(rs1) & 0xFFFF;
let hi = (regs.read(rs2) & 0xFFFF) << 16;
let word = (lo | hi) as u32;
let value = i64::from(word.cast_signed()).cast_unsigned();
regs.write(rd, value);
}
Self::Brev8 { rd, rs1 } => {
let src = regs.read(rs1);
let mut bytes = src.to_le_bytes();
for byte in &mut bytes {
*byte = byte.reverse_bits();
}
regs.write(rd, u64::from_le_bytes(bytes));
}
}
Ok(ControlFlow::Continue(()))
}
}