add functional multiply-accumulate test

note that multiplies must go from register to register,
they can't directly pull out of FIFO regs
This commit is contained in:
bunnie 2024-09-26 18:17:31 +08:00
parent 7b8f4d91ec
commit 81f0523ee3

View file

@ -139,3 +139,84 @@ bio_code!(stack_test2_code, STACK_TEST2_START, STACK_TEST2_END,
"add sp, sp, 8",
"ret"
);
#[cfg(feature = "bio-mul")]
/// Safety: Only safe if the target BIO has the multiply extension
pub unsafe fn mac_test() -> usize {
print!("MAC test\r");
// clear prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
test_cfg.wo(utra::csrtest::WTEST, 0);
let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
bio_ss.load_code(mac_code(), 0, BioCore::Core0);
// These actually "don't matter" because there are no synchronization instructions in the code
// Everything runs at "full tilt"
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV0, 0x0);
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x111);
// a test vector
let a: [i32; 7] = [1, -1, 3047, 0, 12, -92101, 12432];
let b = 7;
let check = mac(&a, b);
// pass values to fifo; fifo is 8-deep, so this should fit without checking depth
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, a.len() as u32);
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, b as u32);
for v in a.iter() {
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, *v as u32);
}
// wait for the computation to return
while bio_ss.bio.rf(utra::bio_bdma::SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {}
let result = bio_ss.bio.r(utra::bio_bdma::SFR_RXF1) as i32;
print!("Got {}\r", result);
if result != check {
print!("Computed {}, should be {}\r", result, check);
print!("===MAC test FAIL===\r");
0
} else {
print!("===MAC test PASS===\r");
1
}
}
/// the recursive function implemented below
#[cfg(feature = "bio-mul")]
fn mac(a: &[i32], b: i32) -> i32 {
let mut r: i32 = 0;
for &v in a.iter() {
r += v * b;
}
r
}
#[rustfmt::skip]
#[cfg(feature = "bio-mul")]
bio_code!(mac_code, MAC_START, MAC_END,
// first arg into x16 is the number of elements to MAC
// second arg is the coefficient
// remaining args are the vector
// compute mac = a0 * b + a1 * b + a2 * b ...
// return value is in x17
"20:",
"mv a0, x16", // number of elements
"mv a1, x16", // coefficient
"mv a2, x0", // initialize return value
"jal ra, 30f",
"mv x17, a2", // return the value
"j 20b", // go back for more
"30:",
"bne x0, a0, 31f", // test if end
"ret",
"31:",
"addi a0, a0, -1", // decrement arg counter
"mv t1, x16", // fetch vector value: note, we can't multiply directly from a FIFO because while the multi-cycle multiply runs, the FIFO keeps draining
// "mul t0, a1, t1", // multiply
// mul x5, x11, x6, same as above. hand-assembled because Rust refuses to emit `mul` instructions for global_asm!
".word 0x026582b3",
"add a2, t0, a2", // accumulate
"j 30b" // loop
);