diff --git a/libs/xous-bio/src/bio_tests/units.rs b/libs/xous-bio/src/bio_tests/units.rs index 8d8c3157a..086116007 100644 --- a/libs/xous-bio/src/bio_tests/units.rs +++ b/libs/xous-bio/src/bio_tests/units.rs @@ -40,8 +40,8 @@ pub fn hello_multiverse() { let mut bio_ss = BioSharedState::new(); // stop all the machines, so that code can be loaded bio_ss.bio.wo(utra::bio::SFR_CTRL, 0x0); - let mv_test_code = fn_to_slice(multiverse_test, multiverse_test_endcap); - bio_ss.load_code(mv_test_code, 0); + let code = multiverse_code(); + bio_ss.load_code(code, 0); // configure & run the 0th machine // /32 clock @@ -54,59 +54,52 @@ pub fn hello_multiverse() { report_api(0x1310_10FF); } -pub unsafe fn multiverse_test() { - core::arch::asm!( - // reset vectors for each machine - "j 0f", - "nop", - "j 1f", - "nop", - "j 2f", - "nop", - "j 3f", - "nop", - // mach 0 code - "0:", - "li x26, 0xFF", - "add x1, zero, 0x10", - "4:", - "add x1, x1, 0x1", - "mv x21, x1", - "mv x20, zero", - "j 4b", - // mach 1 code - "1:", - "li x26, 0xFF00", - "add x1, zero, 0x20", - "5:", - "add x1, x1, 0x1", - "slli x21, x1, 8", - "mv x20, zero", - "j 5b", - // mach 2 code - "2:", - "li x26, 0xFF0000", - "add x1, zero, 0x20", - "6:", - "add x1, x1, 0x1", - "slli x21, x1, 16", - "mv x20, zero", - "j 6b", - // mach 3 code - "3:", - "li x26, 0xFF0000", - "add x1, zero, 0x20", - "7:", - "add x1, x1, 0x1", - "slli x21, x1, 24", - "mv x20, zero", - "j 7b", - ); -} -// this marks the "end address" of simple_test -pub unsafe fn multiverse_test_endcap() { - core::arch::asm!( - "nop" - ); - -} +bio_code!(multiverse_code, MULTIVERSE_START, MULTIVERSE_END, + "jal x0, 0f", + // reset vectors for each machine + // use `jal x0, target` encoding to force non-C interpretation + // as the reset vectors for each core are aligned to 4-byte boundaries + "jal x0, 0f", + "jal x0, 1f", + "jal x0, 2f", + "jal x0, 3f", + // mach 0 code + "0:", + // x26 sets the GPIO mask + "li x26, 0xFF", + "add x1, zero, 0x10", + "4:", + "add x1, x1, 0x1", + // x21 write clobbers the GPIO bits, ANDed with mask in x26 + "mv x21, x1", + // x20 write causes core to wait until next sync quantum + "mv x20, zero", + "j 4b", + // mach 1 code + "1:", + "li x26, 0xFF00", + "add x1, zero, 0x20", + "5:", + "add x1, x1, 0x1", + "slli x21, x1, 8", + "mv x20, zero", + "j 5b", + // mach 2 code + "2:", + "li x26, 0xFF0000", + "add x1, zero, 0x20", + "6:", + "add x1, x1, 0x1", + "slli x21, x1, 16", + "mv x20, zero", + "j 6b", + // mach 3 code + "3:", + "li x26, 0xFF0000", + "add x1, zero, 0x20", + "7:", + "add x1, x1, 0x1", + "slli x21, x1, 24", + "mv x20, zero", + "j 7b" +); diff --git a/libs/xous-bio/src/lib.rs b/libs/xous-bio/src/lib.rs index 0d3a30501..452a05be5 100644 --- a/libs/xous-bio/src/lib.rs +++ b/libs/xous-bio/src/lib.rs @@ -94,4 +94,44 @@ impl BioSharedState { } } } +} + +#[macro_export] +/// This macro takes three identifiers and assembly code: +/// - name of the function to call to retrieve the assembled code +/// - a unique identifier that serves as label name for the start of the code +/// - a unique identifier that serves as label name for the end of the code +/// - a comma separated list of strings that form the assembly itself +/// +/// *** The comma separated list must *not* end in a comma. *** +/// +/// The macro is unable to derive names of functions or identifiers for labels +/// due to the partially hygienic macro rules of Rust, so you have to come +/// up with a list of unique names by yourself. +macro_rules! bio_code { + ($fn_name:ident, $name_start:ident, $name_end:ident, $($item:expr),*) => { + pub fn $fn_name() -> &'static [u8] { + extern { + static $name_start: *const u8; + static $name_end: *const u8; + } + unsafe { + report_api($name_start as u32); + report_api($name_end as u32); + } + unsafe { core::slice::from_raw_parts($name_start, ($name_end as usize) - ($name_start as usize))} + } + + core::arch::global_asm!( + ".align 4", + concat!(".globl ", stringify!($name_start)), + concat!(stringify!($name_start), ":"), + ".word .", + $($item),* + , ".align 4", + concat!(".globl ", stringify!($name_end)), + concat!(stringify!($name_end), ":"), + ".word .", + ); + }; } \ No newline at end of file