#![expect(clippy::unusual_byte_groupings, reason = "Test readability")]
use crate::instructions::Instruction;
use crate::instructions::rv32::zk::zbkb::Rv32ZbkbInstruction;
use crate::instructions::test_utils::make_r_type;
use crate::registers::general_purpose::Reg;
#[test]
fn test_pack() {
let inst = make_r_type(0b0110011, 1, 0b100, 2, 3, 0b0000100);
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(
decoded,
Some(Rv32ZbkbInstruction::Pack {
rd: Reg::Ra,
rs1: Reg::Sp,
rs2: Reg::Gp
})
);
}
#[test]
fn test_pack_rs2_zero_returns_none() {
let inst = make_r_type(0b0110011, 1, 0b100, 2, 0, 0b0000100);
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(decoded, None);
}
#[test]
fn test_packh() {
let inst = make_r_type(0b0110011, 1, 0b111, 2, 3, 0b0000100);
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(
decoded,
Some(Rv32ZbkbInstruction::Packh {
rd: Reg::Ra,
rs1: Reg::Sp,
rs2: Reg::Gp
})
);
}
#[test]
fn test_brev8() {
let inst = 0b011010000111_00010_101_00001_0010011u32;
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(
decoded,
Some(Rv32ZbkbInstruction::Brev8 {
rd: Reg::Ra,
rs1: Reg::Sp
})
);
}
#[test]
fn test_zip() {
let inst = 0b0000100_01111_00010_001_00001_0010011u32;
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(
decoded,
Some(Rv32ZbkbInstruction::Zip {
rd: Reg::Ra,
rs1: Reg::Sp
})
);
}
#[test]
fn test_unzip() {
let inst = 0b0000100_01111_00010_101_00001_0010011u32;
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(
decoded,
Some(Rv32ZbkbInstruction::Unzip {
rd: Reg::Ra,
rs1: Reg::Sp
})
);
}
#[test]
fn test_zip_unzip_are_inverses_encoding() {
let zip_inst = 0b0000100_01111_00010_001_00001_0010011u32;
let unzip_inst = 0b0000100_01111_00010_101_00001_0010011u32;
assert_ne!(
Rv32ZbkbInstruction::<Reg<u32>>::try_decode(zip_inst),
Rv32ZbkbInstruction::<Reg<u32>>::try_decode(unzip_inst),
);
}
#[test]
fn test_pack_wrong_funct7_returns_none() {
let inst = make_r_type(0b0110011, 1, 0b100, 2, 3, 0b0000000);
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(decoded, None);
}
#[test]
fn test_packw_opcode_not_decoded_in_rv32() {
let inst = make_r_type(0b0111011, 1, 0b100, 2, 3, 0b0000100);
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(decoded, None);
}
#[test]
fn test_unknown_opcode_returns_none() {
let inst = make_r_type(0b0100011, 1, 0b100, 2, 3, 0b0000100);
let decoded = Rv32ZbkbInstruction::<Reg<u32>>::try_decode(inst);
assert_eq!(decoded, None);
}