Refactor remaining tests for multi-bank RAM op

BDMA memory pages for each core are now on separate pages, with
each core having a private memory space.

Also add code readback tests, debug PC & trap test
This commit is contained in:
bunnie 2024-06-26 15:01:59 +08:00
parent 2bf77883ee
commit d665805523
5 changed files with 236 additions and 142 deletions

View file

@ -3,7 +3,7 @@ use crate::*;
const PIN_SDA: u32 = 2; const PIN_SDA: u32 = 2;
const PIN_SCL: u32 = 3; const PIN_SCL: u32 = 3;
pub fn i2c_test() { pub fn i2c_test() -> usize {
print!("I2C tests\r"); print!("I2C tests\r");
// clear prior test config state // clear prior test config state
@ -17,7 +17,7 @@ pub fn i2c_test() {
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = crate::i2c::i2c_driver(); let code = crate::i2c::i2c_driver();
print!("code length {}\r", code.len()); print!("code length {}\r", code.len());
bio_ss.load_code(code, 0); bio_ss.load_code(code, 0, BioCore::Core0);
// configure & run the 0th machine // configure & run the 0th machine
// 400kHz clock -> 100kHz toggle rate = 0x7D0_0000 @ 800MHz rate FCLK // 400kHz clock -> 100kHz toggle rate = 0x7D0_0000 @ 800MHz rate FCLK
@ -93,14 +93,16 @@ pub fn i2c_test() {
bio_ss.bio.wo(utra::bio_bdma::SFR_IRQMASK_0, 0); bio_ss.bio.wo(utra::bio_bdma::SFR_IRQMASK_0, 0);
if passing { if passing {
print!("===I2C tests PASS===\r"); print!("===I2C tests PASS===\r");
1
} else { } else {
print!("===I2C tests FAIL===\r"); print!("===I2C tests FAIL===\r");
0
} }
} }
/// This just generates some more complex I2C waveforms; we don't have a full I2C hardware /// This just generates some more complex I2C waveforms; we don't have a full I2C hardware
/// unit in the upper level test bench, so we use manual waveform checking. /// unit in the upper level test bench, so we use manual waveform checking.
pub fn complex_i2c_test() { pub fn complex_i2c_test() -> usize {
print!("Complex I2C transactions\r"); print!("Complex I2C transactions\r");
let mut bio_ss = BioSharedState::new(); let mut bio_ss = BioSharedState::new();
let mut i2c = let mut i2c =
@ -124,4 +126,5 @@ pub fn complex_i2c_test() {
Err(_) => print!("read test FAIL\r"), Err(_) => print!("read test FAIL\r"),
} }
print!("===Exit complex I2C transactions===\r"); print!("===Exit complex I2C transactions===\r");
1
} }

View file

@ -3,7 +3,7 @@ use utralib::utra::bio_bdma::{SFR_EVENT_STATUS_SFR_EVENT_STATUS, SFR_FLEVEL_PCLK
use super::report_api; use super::report_api;
use crate::*; use crate::*;
pub fn spi_test() { pub fn spi_test() -> usize {
print!("SPI test\r"); print!("SPI test\r");
// clear prior test config state // clear prior test config state
@ -15,7 +15,12 @@ pub fn spi_test() {
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = spi_driver(); let code = spi_driver();
print!("SPI code length {}\r", code.len()); print!("SPI code length {}\r", code.len());
bio_ss.load_code(code, 0); // this test does same code on all machines, with ID-based dispatch to
// select which machine runs.
bio_ss.load_code(code, 0, BioCore::Core0);
bio_ss.load_code(code, 0, BioCore::Core1);
bio_ss.load_code(code, 0, BioCore::Core2);
bio_ss.load_code(code, 0, BioCore::Core3);
// configure fifo trigger levels // configure fifo trigger levels
bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL0, 4); bio_ss.bio.wfo(utra::bio_bdma::SFR_ELEVEL_FIFO_EVENT_LEVEL0, 4);
@ -47,17 +52,18 @@ pub fn spi_test() {
| bio_ss.bio.ms(utra::bio_bdma::SFR_EXTCLOCK_EXTCLK_GPIO_3, 9), | bio_ss.bio.ms(utra::bio_bdma::SFR_EXTCLOCK_EXTCLK_GPIO_3, 9),
); );
// start cores 2 & 3 // start all cores -- core 0 & 1 should "gutter"
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xCCC); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xFFF);
let mut i = 0; let mut i = 0;
let mut j = 0; let mut j = 0;
const TEST_LEN: usize = 32; const TEST_LEN: usize = 32;
let mut retvals = [0u32; TEST_LEN]; let mut retvals = [0u32; TEST_LEN];
loop { loop {
// print!("dbg pc: {:x}\r", bio_ss.bio.r(utra::bio_bdma::SFR_DBG2));
// fill the FIFO // fill the FIFO
while (((bio_ss.bio.rf(SFR_EVENT_STATUS_SFR_EVENT_STATUS) >> 24) & 0x1) != 0) && (i < TEST_LEN) { while (((bio_ss.bio.rf(SFR_EVENT_STATUS_SFR_EVENT_STATUS) >> 24) & 0x1) != 0) && (i < TEST_LEN) {
// report_api(i as u32 | 0xAA00); // print!(" Tx {:x}\r", i as u32 | 0xAA00);
bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, i as u32 | 0xAA00); bio_ss.bio.wo(utra::bio_bdma::SFR_TXF0, i as u32 | 0xAA00);
i += 1; i += 1;
} }
@ -71,6 +77,8 @@ pub fn spi_test() {
} }
} }
print!("iter "); print!("iter ");
print!("dbg pc 2: {:x}\r", bio_ss.bio.r(utra::bio_bdma::SFR_DBG2));
print!("dbg pc 3: {:x}\r", bio_ss.bio.r(utra::bio_bdma::SFR_DBG3));
while j < TEST_LEN { while j < TEST_LEN {
while bio_ss.bio.rf(SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {} // wait until there's something there while bio_ss.bio.rf(SFR_FLEVEL_PCLK_REGFIFO_LEVEL1) == 0 {} // wait until there's something there
retvals[j] = bio_ss.bio.r(utra::bio_bdma::SFR_RXF1); retvals[j] = bio_ss.bio.r(utra::bio_bdma::SFR_RXF1);
@ -78,13 +86,15 @@ pub fn spi_test() {
print!("{} ", j); print!("{} ", j);
} }
print!("\r"); print!("\r");
print!("dbg pc 2: {:x}\r", bio_ss.bio.r(utra::bio_bdma::SFR_DBG2));
print!("dbg pc 3: {:x}\r", bio_ss.bio.r(utra::bio_bdma::SFR_DBG3));
for (index, &val) in retvals.iter().enumerate() { for (index, &val) in retvals.iter().enumerate() {
print!("SPI rbk {:x}\r", val); print!("SPI rbk {:x}\r", val);
// the XOR to 0xAAAA is just a mask we threw in to test immediate loads // the XOR to 0xAAAA is just a mask we threw in to test immediate loads
assert!(val == ((0xAA00 | index as u32) ^ 0xAAAA)); assert!(val == ((0xAA00 | index as u32) ^ 0xAAAA));
} }
print!("==SPI PASS==\r");
report_api(0x51C0_600D); 1
} }
// This is written to also test some additional features: // This is written to also test some additional features:
@ -103,17 +113,6 @@ bio_code!(
spi_driver, spi_driver,
SPI_DRIVER_START, SPI_DRIVER_START,
SPI_DRIVER_END, SPI_DRIVER_END,
"j 90f",
"nop",
"j 90f",
"nop",
"j 90f",
"nop",
"j 90f",
"nop",
"retmask:",
".word 0x0000AAAA", // just a random mask for testing immediate loads
"90:",
// dispatch based on machine ID // dispatch based on machine ID
"srli x1, x31, 30", "srli x1, x31, 30",
"li x2, 0", "li x2, 0",
@ -124,10 +123,14 @@ bio_code!(
"beq x1, x2, 82f", "beq x1, x2, 82f",
"li x2, 3", "li x2, 3",
"beq x1, x2, 83f", "beq x1, x2, 83f",
"retmask:",
".align 4",
".word 0x0000AAAA", // just a random mask for testing immediate loads
"80:", // machine 0 "80:", // machine 0
"j 80b", "j 80b",
"81:", // machine 1 "81:", // machine 1
"j 81b", "j 81b",
".align 4",
"82:", // machine 2 - tx on bit 8, clock on bit 9, chip select on bit 10 "82:", // machine 2 - tx on bit 8, clock on bit 9, chip select on bit 10
"li x1, 0x700", // setup output mask bits "li x1, 0x700", // setup output mask bits
"mv x26, x1", // mask "mv x26, x1", // mask
@ -297,6 +300,7 @@ bio_code!(
"mv x20, x0", // meet CS min-high time (if necessary) "mv x20, x0", // meet CS min-high time (if necessary)
"beqz x20, 20b", // wait quantum & loop back -- x20 reads back as 0 "beqz x20, 20b", // wait quantum & loop back -- x20 reads back as 0
".align 4",
"83:", // machine 3 - rx on bit 8, clock on bit 9, chip select on bit 10 "83:", // machine 3 - rx on bit 8, clock on bit 9, chip select on bit 10
"li x1, 0x700", // setup mask bits "li x1, 0x700", // setup mask bits
"mv x26, x1", // mask "mv x26, x1", // mask

View file

@ -8,12 +8,12 @@ use crate::*;
// this test requires manual inspection of the outputs // this test requires manual inspection of the outputs
// the GPIO pins should toggle with 0x11, 0x12, 0x13... // the GPIO pins should toggle with 0x11, 0x12, 0x13...
// at the specified quantum rate of the machine. // at the specified quantum rate of the machine.
pub fn hello_world() { pub fn hello_world() -> usize {
print!("hello world test\r"); print!("hello world test\r");
let mut bio_ss = BioSharedState::new(); let mut bio_ss = BioSharedState::new();
let simple_test_code = hello_world_code(); let simple_test_code = hello_world_code();
// copy code to reset vector for 0th machine // copy code to reset vector for 0th machine
bio_ss.load_code(simple_test_code, 0); bio_ss.load_code(simple_test_code, 0, BioCore::Core0);
// configure & run the 0th machine // configure & run the 0th machine
// /32 clock // /32 clock
@ -21,6 +21,7 @@ pub fn hello_world() {
// start the machine // start the machine
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x111); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x111);
print!("===hello world PASS===\r"); print!("===hello world PASS===\r");
1
} }
#[rustfmt::skip] #[rustfmt::skip]
bio_code!(hello_world_code, HELLO_START, HELLO_END, bio_code!(hello_world_code, HELLO_START, HELLO_END,
@ -37,13 +38,19 @@ bio_code!(hello_world_code, HELLO_START, HELLO_END,
// the GPIO pins should toggle with the following pattern: // the GPIO pins should toggle with the following pattern:
// 0x41312111, 0x42322212, 0x43332313, etc. // 0x41312111, 0x42322212, 0x43332313, etc.
// and they should be in sync-lock, no ragged transitions // and they should be in sync-lock, no ragged transitions
pub fn hello_multiverse() { pub fn hello_multiverse() -> usize {
print!("multiverse\r"); print!("multiverse\r");
let mut bio_ss = BioSharedState::new(); let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded // stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = multiverse_code(); let code0 = multiverse_code0();
bio_ss.load_code(code, 0); bio_ss.load_code(code0, 0, BioCore::Core0);
let code1 = multiverse_code1();
bio_ss.load_code(code1, 0, BioCore::Core1);
let code2 = multiverse_code2();
bio_ss.load_code(code2, 0, BioCore::Core2);
let code3 = multiverse_code3();
bio_ss.load_code(code3, 0, BioCore::Core3);
// configure & run the 0th machine // configure & run the 0th machine
// /32 clock // /32 clock
@ -63,31 +70,13 @@ pub fn hello_multiverse() {
// start all the machines, all at once // start all the machines, all at once
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xfff); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xfff);
print!("===multiverse PASS===\r"); print!("===multiverse PASS===\r");
1
} }
#[rustfmt::skip] #[rustfmt::skip]
bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END, bio_code!(multiverse_code0, MULTIVERSE0_START, MULTIVERSE0_END,
// Reset vectors for each core are aligned to 4-byte boundaries // Note that labels can only be numbers from 0-99, and, due to an llvm
// As long as the jump target is <2kiB from reset, this will emit
// a C-instruction, so it needs padding with a NOP. Unfortunately,
// I can't seem to figure out a way to force the assembler to always
// encode as uncompressed, so, you have to be aware of the jump destination
// for the assembler output to line up according to your expectation :(
//
// using 'c.j' syntax for the jump causes the assembler to emit an error,
// but the code still compiles, so...avoiding that for now. might be a bug,
// but I am very not interested in fixing that today.
//
// Also note that labels can only be numbers from 0-99, and, due to an llvm
// bug, labels made exclusively of 0 or 1 should be avoided because they get // bug, labels made exclusively of 0 or 1 should be avoided because they get
// interpreted as binary numbers. dat's some jank in the tank!! // interpreted as binary numbers. dat's some jank in the tank!!
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"j 93f",
"nop",
// mach 0 code // mach 0 code
"90:", "90:",
// x26 sets the GPIO mask // x26 sets the GPIO mask
@ -100,7 +89,10 @@ bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END,
"mv x21, x1", "mv x21, x1",
// x20 write causes core to wait until next sync quantum // x20 write causes core to wait until next sync quantum
"mv x20, zero", "mv x20, zero",
"j 4b", "j 4b"
);
#[rustfmt::skip]
bio_code!(multiverse_code1, MULTIVERSE1_START, MULTIVERSE1_END,
// mach 1 code // mach 1 code
"91:", "91:",
"li x2, 0xFF00", "li x2, 0xFF00",
@ -110,7 +102,10 @@ bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END,
"add x1, x1, 0x1", "add x1, x1, 0x1",
"slli x21, x1, 8", "slli x21, x1, 8",
"mv x20, zero", "mv x20, zero",
"j 5b", "j 5b"
);
#[rustfmt::skip]
bio_code!(multiverse_code2, MULTIVERSE2_START, MULTIVERSE2_END,
// mach 2 code // mach 2 code
"92:", "92:",
"li x2, 0xFF0000", "li x2, 0xFF0000",
@ -120,7 +115,10 @@ bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END,
"add x1, x1, 0x1", "add x1, x1, 0x1",
"slli x21, x1, 16", "slli x21, x1, 16",
"mv x20, zero", "mv x20, zero",
"j 6b", "j 6b"
);
#[rustfmt::skip]
bio_code!(multiverse_code3, MULTIVERSE3_START, MULTIVERSE3_END,
// mach 3 code // mach 3 code
"93:", "93:",
"li x2, 0xFF000000", "li x2, 0xFF000000",
@ -141,7 +139,7 @@ bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END,
// but with a glitch before major transitions. The output could // but with a glitch before major transitions. The output could
// be sync'd locked, but we leave it off for this test so we have // be sync'd locked, but we leave it off for this test so we have
// a demo of how things look when it's off. // a demo of how things look when it's off.
pub fn fifo_basic() { pub fn fifo_basic() -> usize {
print!("FIFO basic\r"); print!("FIFO basic\r");
// clear any prior test config state // clear any prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32); let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
@ -150,8 +148,46 @@ pub fn fifo_basic() {
let mut bio_ss = BioSharedState::new(); let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded // stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = fifo_basic_code(); bio_ss.load_code(fifo_basic0_code(), 0, BioCore::Core0);
bio_ss.load_code(code, 0); bio_ss.load_code(fifo_basic1_code(), 0, BioCore::Core1);
bio_ss.load_code(fifo_basic2_code(), 0, BioCore::Core2);
bio_ss.load_code(fifo_basic3_code(), 0, BioCore::Core3);
// expect no error
match bio_ss.verify_code(&fifo_basic0_code(), 0, BioCore::Core0) {
Err(BioError::CodeCheck(at)) => {
print!("Core 0 rbk fail at {}\r", at);
return 0;
}
_ => (),
}
match bio_ss.verify_code(&fifo_basic1_code(), 0, BioCore::Core1) {
Err(BioError::CodeCheck(at)) => {
print!("Core 1 rbk fail at {}\r", at);
return 0;
}
_ => (),
}
match bio_ss.verify_code(&fifo_basic2_code(), 0, BioCore::Core2) {
Err(BioError::CodeCheck(at)) => {
print!("Core 2 rbk fail at {}\r", at);
return 0;
}
_ => (),
}
match bio_ss.verify_code(&fifo_basic3_code(), 0, BioCore::Core3) {
Err(BioError::CodeCheck(at)) => {
print!("Core 3 rbk fail at {}\r", at);
return 0;
}
_ => (),
}
// expect error
if bio_ss.verify_code(&fifo_basic1_code(), 0, BioCore::Core0).is_ok() {
print!("FAIL: Core 0 passed check with false code\r");
return 0;
}
// configure & run the 0th machine // configure & run the 0th machine
// / 16. clock // / 16. clock
@ -164,17 +200,10 @@ pub fn fifo_basic() {
// start all the machines, all at once // start all the machines, all at once
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xfff); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0xfff);
print!("===FIFO basic PASS===\r"); print!("===FIFO basic PASS===\r");
1
} }
#[rustfmt::skip] #[rustfmt::skip]
bio_code!(fifo_basic_code, FIFO_BASIC_START, FIFO_BASIC_END, bio_code!(fifo_basic0_code, FIFO_BASIC0_START, FIFO_BASIC0_END,
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"j 93f",
"nop",
// mach 0 code // mach 0 code
"90:", "90:",
"li x2, 0xFFFF", "li x2, 0xFFFF",
@ -187,7 +216,10 @@ bio_code!(fifo_basic_code, FIFO_BASIC_START, FIFO_BASIC_END,
"mv x19, x1", "mv x19, x1",
"mv x20, zero", "mv x20, zero",
"mv x1, x19", "mv x1, x19",
"j 11b", "j 11b"
);
#[rustfmt::skip]
bio_code!(fifo_basic1_code, FIFO_BASIC1_START, FIFO_BASIC1_END,
// mach 1 code // mach 1 code
"91:", "91:",
"li x2, 0xFFFF0000", "li x2, 0xFFFF0000",
@ -200,12 +232,18 @@ bio_code!(fifo_basic_code, FIFO_BASIC_START, FIFO_BASIC_END,
"mv x18, x1", "mv x18, x1",
"mv x20, zero", "mv x20, zero",
"mv x1, x18", "mv x1, x18",
"j 21b", "j 21b"
);
#[rustfmt::skip]
bio_code!(fifo_basic2_code, FIFO_BASIC2_START, FIFO_BASIC2_END,
// mach 2 code // mach 2 code
"92:", "92:",
"addi x18, x18, 2", // increment the value in fifo by 2 "addi x18, x18, 2", // increment the value in fifo by 2
"mv x20, zero", "mv x20, zero",
"j 92b", "j 92b"
);
#[rustfmt::skip]
bio_code!(fifo_basic3_code, FIFO_BASIC3_START, FIFO_BASIC3_END,
// mach 3 code // mach 3 code
"93:", "93:",
"li x2, 0x40000", "li x2, 0x40000",
@ -228,7 +266,7 @@ bio_code!(fifo_basic_code, FIFO_BASIC_START, FIFO_BASIC_END,
// //
// GPIO outputs are run without snapping in this case, because there is just one // GPIO outputs are run without snapping in this case, because there is just one
// machine updating outputs and no need to do that. // machine updating outputs and no need to do that.
pub fn host_fifo_tests() { pub fn host_fifo_tests() -> usize {
print!("Host FIFO tests\r"); print!("Host FIFO tests\r");
// clear prior test config state // clear prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32); let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
@ -237,8 +275,8 @@ pub fn host_fifo_tests() {
let mut bio_ss = BioSharedState::new(); let mut bio_ss = BioSharedState::new();
// stop all the machines, so that code can be loaded // stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = fifo_host_bitbang(); bio_ss.load_code(fifo_host0_bitbang(), 0, BioCore::Core0);
bio_ss.load_code(code, 0); bio_ss.load_code(fifo_host1_bitbang(), 0, BioCore::Core1);
// reset all the fifos // reset all the fifos
bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF); bio_ss.bio.wo(utra::bio_bdma::SFR_FIFO_CLR, 0xF);
@ -297,8 +335,9 @@ pub fn host_fifo_tests() {
test_cfg.wo(utra::csrtest::WTEST, 0); test_cfg.wo(utra::csrtest::WTEST, 0);
// stop machine & load code // stop machine & load code
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = fifo_host_bitbang_level_trig(); bio_ss.load_code(fifo_host0_bitbang_level_trig(), 0, BioCore::Core0);
bio_ss.load_code(code, 0); bio_ss.load_code(fifo_host1_bitbang_level_trig(), 0, BioCore::Core1);
bio_ss.load_code(fifo_host2_bitbang_level_trig(), 0, BioCore::Core2);
// clear all events // clear all events
bio_ss.bio.wfo(utra::bio_bdma::SFR_EVENT_CLR_SFR_EVENT_CLR, 0xFFFF_FF); bio_ss.bio.wfo(utra::bio_bdma::SFR_EVENT_CLR_SFR_EVENT_CLR, 0xFFFF_FF);
@ -352,17 +391,17 @@ pub fn host_fifo_tests() {
print!("stop_check {:x}\r", stop_check); print!("stop_check {:x}\r", stop_check);
assert!(stop_check == stop_val); assert!(stop_check == stop_val);
print!("===Host FIFO PASS===\r"); print!("===Host FIFO PASS===\r");
1
} }
#[rustfmt::skip] #[rustfmt::skip]
bio_code!(fifo_host_bitbang, FIFO_HOST_BITBANG_START, FIFO_HOST_BITBANG_END, bio_code!(fifo_host0_bitbang, FIFO_HOST0_BITBANG_START, FIFO_HOST0_BITBANG_END,
"j 90f",
"nop",
"j 91f",
"nop",
"90:", "90:",
"mv x21, x16", "mv x21, x16",
"mv x20, zero", "mv x20, zero",
"j 90b", "j 90b"
);
#[rustfmt::skip]
bio_code!(fifo_host1_bitbang, FIFO_HOST1_BITBANG_START, FIFO_HOST1_BITBANG_END,
"91:", "91:",
"mv x20, zero", "mv x20, zero",
"mv x17, x21", "mv x17, x21",
@ -386,15 +425,9 @@ bio_code!(fifo_host_bitbang, FIFO_HOST_BITBANG_START, FIFO_HOST_BITBANG_END,
// - acks core 0 on event bit 3 // - acks core 0 on event bit 3
#[rustfmt::skip] #[rustfmt::skip]
bio_code!( bio_code!(
fifo_host_bitbang_level_trig, fifo_host0_bitbang_level_trig,
FIFO_HOST_BITBANG_LEVEL_TRIG_START, FIFO_HOST0_BITBANG_LEVEL_TRIG_START,
FIFO_HOST_BITBANG_LEVEL_TRIG_END, FIFO_HOST0_BITBANG_LEVEL_TRIG_END,
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"90:", // machine 0 "90:", // machine 0
"li x2, 0xfeedface", "li x2, 0xfeedface",
"mv x21, x2", // init gpio to the "i'm here" sentinel "mv x21, x2", // init gpio to the "i'm here" sentinel
@ -409,7 +442,13 @@ bio_code!(
"mv x28, x4", // set the bit that machine 2 is listening to "mv x28, x4", // set the bit that machine 2 is listening to
"mv x3, x30", // wait for ack that gpio was sampled "mv x3, x30", // wait for ack that gpio was sampled
"mv x29, x3", // clear the trigger "mv x29, x3", // clear the trigger
"j 20b", "j 20b"
);
#[rustfmt::skip]
bio_code!(
fifo_host1_bitbang_level_trig,
FIFO_HOST1_BITBANG_LEVEL_TRIG_START,
FIFO_HOST1_BITBANG_LEVEL_TRIG_END,
"91:", // machine 1 "91:", // machine 1
"li x1, 0x1", // event bit 0 "li x1, 0x1", // event bit 0
"mv x27, x1", // set trigger mask "mv x27, x1", // set trigger mask
@ -418,7 +457,13 @@ bio_code!(
"and x2, x2, x1", // mask event result "and x2, x2, x1", // mask event result
"mv x29, x2", // clear the event trigger "mv x29, x2", // clear the event trigger
"mv x17, x21", // gpio in -> fifo1 "mv x17, x21", // gpio in -> fifo1
"j 21b", "j 21b"
);
#[rustfmt::skip]
bio_code!(
fifo_host2_bitbang_level_trig,
FIFO_HOST2_BITBANG_LEVEL_TRIG_START,
FIFO_HOST2_BITBANG_LEVEL_TRIG_END,
"92:", // machine 2 "92:", // machine 2
"li x2, 0x2", // event bit 1 "li x2, 0x2", // event bit 1
"mv x27, x2", // set trigger mask "mv x27, x2", // set trigger mask
@ -442,7 +487,7 @@ struct FifoLevelTestConfig {
// This can be done without any code running on the machines; the host can // This can be done without any code running on the machines; the host can
// set and observe all fifo levels and triggers directly. // set and observe all fifo levels and triggers directly.
pub fn fifo_level_tests() { pub fn fifo_level_tests() -> usize {
print!("FIFO level comprehensive\r"); print!("FIFO level comprehensive\r");
// clear prior test config state // clear prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32); let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
@ -711,10 +756,11 @@ pub fn fifo_level_tests() {
bio_ss.bio.wo(irq_mask_reg, 0); bio_ss.bio.wo(irq_mask_reg, 0);
} }
} }
print!("===FIFO level comprehensive PASS===\r") print!("===FIFO level comprehensive PASS===\r");
1
} }
pub fn aclk_tests() { pub fn aclk_tests() -> usize {
print!("ACLK test\r"); print!("ACLK test\r");
// clear any prior test config state // clear any prior test config state
let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32); let mut test_cfg = CSR::new(utra::csrtest::HW_CSRTEST_BASE as *mut u32);
@ -724,7 +770,7 @@ pub fn aclk_tests() {
// stop all the machines, so that code can be loaded // stop all the machines, so that code can be loaded
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = aclk_code(); let code = aclk_code();
bio_ss.load_code(code, 0); bio_ss.load_code(code, 0, BioCore::Core1);
// configure & run the 0th machine // configure & run the 0th machine
bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0xA_0000); bio_ss.bio.wo(utra::bio_bdma::SFR_QDIV1, 0xA_0000);
@ -751,23 +797,11 @@ pub fn aclk_tests() {
assert!(results[6] - results[5] == 10); // related to the clock divider assert!(results[6] - results[5] == 10); // related to the clock divider
print!("===ACLK test PASS===\r"); print!("===ACLK test PASS===\r");
1
} }
#[rustfmt::skip] #[rustfmt::skip]
bio_code!(aclk_code, ACLK_START, ACLK_END, bio_code!(aclk_code, ACLK_START, ACLK_END,
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"j 93f",
"nop",
// mach 0 code
"90:",
"j 90b",
// mach 1 code
"91:",
"mv x17, x31", "mv x17, x31",
"mv x17, x31", "mv x17, x31",
"mv x17, x31", "mv x17, x31",
@ -777,13 +811,5 @@ bio_code!(aclk_code, ACLK_START, ACLK_END,
"mv x20, x0", "mv x20, x0",
"mv x17, x31", "mv x17, x31",
"mv x20, x0", "mv x20, x0",
"mv x17, x31", "mv x17, x31"
"40:",
"j 40b",
// mach 2 code
"92:",
"j 92b",
// mach 3 code
"93:",
"j 93b"
); );

View file

@ -37,7 +37,7 @@ impl<'a> BioI2C<'a> {
bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0); bio_ss.bio.wo(utra::bio_bdma::SFR_CTRL, 0x0);
let code = i2c_driver(); let code = i2c_driver();
print!("I2C code length: {} bytes\r", code.len()); print!("I2C code length: {} bytes\r", code.len());
bio_ss.load_code(code, 0); bio_ss.load_code(code, 0, BioCore::Core0);
if hs { if hs {
// 1600kHz clock -> 400kHz toggle rate = 0x1F4_0000 @ 800MHz rate FCLK // 1600kHz clock -> 400kHz toggle rate = 0x1F4_0000 @ 800MHz rate FCLK
@ -196,14 +196,6 @@ bio_code!(
i2c_driver, i2c_driver,
I2C_DRIVER_START, I2C_DRIVER_START,
I2C_DRIVER_END, I2C_DRIVER_END,
"j 90f",
"nop",
"j 91f",
"nop",
"j 92f",
"nop",
"j 93f",
"nop",
"90:", // machine 0 code "90:", // machine 0 code
"mv x1, x16", // x1 <- initiation command "mv x1, x16", // x1 <- initiation command
"andi x2, x1, 1", // x2 <- r/w bit. write if 0. "andi x2, x1, 1", // x2 <- r/w bit. write if 0.
@ -339,12 +331,5 @@ bio_code!(
// REPORT // REPORT
"slli x8, x8, 16", // shift NACK count into place "slli x8, x8, 16", // shift NACK count into place
"add x18, x7, x8", // x18 <- send ACK + NACK "add x18, x7, x8", // x18 <- send ACK + NACK
"j 90b", // wait for next command "j 90b" // wait for next command
// gutter unused machines
"91:",
"j 91b",
"92:",
"j 92b",
"93:",
"j 93b"
); );

View file

@ -31,6 +31,8 @@
//! - x31 r/- [31:30] -> core ID; [29:0] -> cpu clocks since reset //! - x31 r/- [31:30] -> core ID; [29:0] -> cpu clocks since reset
#![cfg_attr(feature = "baremetal", no_std)] #![cfg_attr(feature = "baremetal", no_std)]
use core::mem::size_of;
use utralib::generated::*; use utralib::generated::*;
#[cfg(feature = "tests")] #[cfg(feature = "tests")]
@ -38,6 +40,15 @@ pub mod bio_tests;
pub mod i2c; pub mod i2c;
#[repr(usize)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum BioCore {
Core0 = 0,
Core1 = 1,
Core2 = 2,
Core3 = 3,
}
#[derive(Debug)] #[derive(Debug)]
pub enum BioError { pub enum BioError {
/// specified state machine is not valid /// specified state machine is not valid
@ -46,6 +57,8 @@ pub enum BioError {
Oom, Oom,
/// no more machines available /// no more machines available
NoFreeMachines, NoFreeMachines,
/// Loaded code did not match, first error at argument
CodeCheck(usize),
} }
pub fn get_id() -> u32 { pub fn get_id() -> u32 {
@ -71,19 +84,35 @@ pub fn lfsr_next_u32(state: u32) -> u32 {
(state << 1) + bit (state << 1) + bit
} }
pub const BIO_PRIVATE_MEM_LEN: usize = 4096;
pub struct BioSharedState { pub struct BioSharedState {
pub bio: CSR<u32>, pub bio: CSR<u32>,
pub imem_slice: &'static mut [u32], pub imem_slice: [&'static mut [u32]; 4],
} }
impl BioSharedState { impl BioSharedState {
#[cfg(feature = "baremetal")] #[cfg(feature = "baremetal")]
pub fn new() -> Self { pub fn new() -> Self {
// map the instruction memory // map the instruction memory
let imem_slice = unsafe { let imem_slice = unsafe {
[
core::slice::from_raw_parts_mut( core::slice::from_raw_parts_mut(
utralib::generated::HW_BIO_RAM_MEM as *mut u32, utralib::generated::HW_BIO_IMEM0_MEM as *mut u32,
utralib::generated::HW_BIO_RAM_MEM_LEN, HW_BIO_IMEM0_MEM_LEN / size_of::<u32>(),
) ),
core::slice::from_raw_parts_mut(
utralib::generated::HW_BIO_IMEM1_MEM as *mut u32,
HW_BIO_IMEM1_MEM_LEN / size_of::<u32>(),
),
core::slice::from_raw_parts_mut(
utralib::generated::HW_BIO_IMEM2_MEM as *mut u32,
HW_BIO_IMEM2_MEM_LEN / size_of::<u32>(),
),
core::slice::from_raw_parts_mut(
utralib::generated::HW_BIO_IMEM3_MEM as *mut u32,
HW_BIO_IMEM3_MEM_LEN / size_of::<u32>(),
),
]
}; };
BioSharedState { bio: CSR::new(utra::bio_bdma::HW_BIO_BDMA_BASE as *mut u32), imem_slice } BioSharedState { bio: CSR::new(utra::bio_bdma::HW_BIO_BDMA_BASE as *mut u32), imem_slice }
@ -99,36 +128,83 @@ impl BioSharedState {
) )
.unwrap(); .unwrap();
let imem: xous::MemoryRange = xous::syscall::map_memory( let imem0 = xous::syscall::map_memory(
xous::MemoryAddress::new(utra::bio_bdma::HW_BIO_RAM_MEM), xous::MemoryAddress::new(utralib::HW_BIO_IMEM0_MEM),
None, None,
utra::bio_bdma::HW_BIO_RAM_MEM_LEN, utralib::HW_BIO_IMEM0_MEM_LEN,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
let imem1 = xous::syscall::map_memory(
xous::MemoryAddress::new(utralib::HW_BIO_IMEM1_MEM),
None,
utralib::HW_BIO_IMEM1_MEM_LEN,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
let imem2 = xous::syscall::map_memory(
xous::MemoryAddress::new(utralib::HW_BIO_IMEM2_MEM),
None,
utralib::HW_BIO_IMEM2_MEM_LEN,
xous::MemoryFlags::R | xous::MemoryFlags::W,
)
.unwrap();
let imem3 = xous::syscall::map_memory(
xous::MemoryAddress::new(utralib::HW_BIO_IMEM3_MEM),
None,
utralib::HW_BIO_IMEM3_MEM_LEN,
xous::MemoryFlags::R | xous::MemoryFlags::W, xous::MemoryFlags::R | xous::MemoryFlags::W,
) )
.unwrap(); .unwrap();
BioSharedState { BioSharedState {
bio: CSR::new(csr.as_mut_ptr() as *mut u32), bio: CSR::new(csr.as_mut_ptr() as *mut u32),
imem_slice: unsafe { imem.as_slice_mut() }, imem_slice: unsafe {
[imem0.as_slice_mut(), imem1.as_slice_mut(), imem2.as_slice_mut(), imem3.as_slice_mut()]
},
} }
} }
pub fn load_code(&mut self, prog: &[u8], offset_bytes: usize) { pub fn load_code(&mut self, prog: &[u8], offset_bytes: usize, core: BioCore) {
let offset = offset_bytes / core::mem::size_of::<u32>(); let offset = offset_bytes / core::mem::size_of::<u32>();
for (i, chunk) in prog.chunks(4).enumerate() { for (i, chunk) in prog.chunks(4).enumerate() {
if chunk.len() == 4 { if chunk.len() == 4 {
let word: u32 = u32::from_le_bytes(chunk.try_into().unwrap()); let word: u32 = u32::from_le_bytes(chunk.try_into().unwrap());
self.imem_slice[i + offset] = word; self.imem_slice[core as usize][i + offset] = word;
} else { } else {
// copy the last word as a "ragged word" // copy the last word as a "ragged word"
let mut ragged_word = 0; let mut ragged_word = 0;
for (j, &b) in chunk.iter().enumerate() { for (j, &b) in chunk.iter().enumerate() {
ragged_word |= (b as u32) << (4 - chunk.len() + j); ragged_word |= (b as u32) << (4 - chunk.len() + j);
} }
self.imem_slice[i + offset] = ragged_word; self.imem_slice[core as usize][i + offset] = ragged_word;
} }
} }
} }
pub fn verify_code(&mut self, prog: &[u8], offset_bytes: usize, core: BioCore) -> Result<(), BioError> {
let offset = offset_bytes / core::mem::size_of::<u32>();
for (i, chunk) in prog.chunks(4).enumerate() {
if chunk.len() == 4 {
let word: u32 = u32::from_le_bytes(chunk.try_into().unwrap());
let rbk = self.imem_slice[core as usize][i + offset];
if rbk != word {
print!("{:?} expected {:x} got {:x} at {}\r", core, word, rbk, i + offset);
return Err(BioError::CodeCheck(i + offset));
}
} else {
// copy the last word as a "ragged word"
let mut ragged_word = 0;
for (j, &b) in chunk.iter().enumerate() {
ragged_word |= (b as u32) << (4 - chunk.len() + j);
}
if self.imem_slice[core as usize][i + offset] != ragged_word {
return Err(BioError::CodeCheck(i + offset));
};
}
}
Ok(())
}
} }
#[macro_export] #[macro_export]