From 633b76eb5d44831fcb93db57c8ac7dcca7856a6b Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 1 Mar 2024 03:38:22 +0800 Subject: [PATCH] flesh out common useful routines for bio access --- libs/xous-bio/src/lib.rs | 43 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) diff --git a/libs/xous-bio/src/lib.rs b/libs/xous-bio/src/lib.rs index cb899d278..0d3a30501 100644 --- a/libs/xous-bio/src/lib.rs +++ b/libs/xous-bio/src/lib.rs @@ -30,23 +30,41 @@ pub fn lfsr_next(state: u16) -> u16 { ((state << 1) + bit) & 0x1_FF } +type BioRustFn = unsafe fn(); + +pub fn fn_to_slice(target_fn: BioRustFn, endcap_fn: BioRustFn) -> &'static [u8] { + let start_ptr = target_fn as *const u8; + unsafe { + core::slice::from_raw_parts(start_ptr, + (endcap_fn as *const u8) as usize - start_ptr as usize + ) + } +} + pub struct BioSharedState { pub bio: CSR, + pub imem_slice: &'static mut [u32], } impl BioSharedState { #[cfg(feature = "baremetal")] pub fn new() -> Self { + // map the instruction memory + let imem_slice = unsafe { + core::slice::from_raw_parts_mut( + utralib::generated::HW_BIO_RAM_MEM as *mut u32, + utralib::generated::HW_BIO_RAM_MEM_LEN + ) + }; + BioSharedState { bio: CSR::new(utra::bio::HW_BIO_BASE as *mut u32), + imem_slice, } } #[cfg(not(feature = "baremetal"))] pub fn new() -> Self { - // Note: this requires a memory region window to be manually specified in create-image - // so that the loader maps the pages for the PIO block. This is because the PIO block is - // an IP block that is created *outside* of the normal LiteX ecosystem. Specifically look in - // xtask/src/builder.rs for a "--extra-svd" argument that refers to precursors/pio.svd. + // TODO let csr = xous::syscall::map_memory( xous::MemoryAddress::new(utra::bio::HW_BIO_BASE), None, @@ -59,4 +77,21 @@ impl BioSharedState { bio: CSR::new(csr.as_mut_ptr() as *mut u32), } } + + pub fn load_code(&mut self, prog: &[u8], offset_bytes: usize) { + let offset = offset_bytes / core::mem::size_of::(); + for (i, chunk) in prog.chunks(4).enumerate() { + if chunk.len() == 4 { + let word: u32 = u32::from_le_bytes(chunk.try_into().unwrap()); + self.imem_slice[i + offset] = word; + } 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); + } + self.imem_slice[i + offset] = ragged_word; + } + } + } } \ No newline at end of file