flesh out common useful routines for bio access

This commit is contained in:
bunnie 2024-03-01 03:38:22 +08:00
parent e9e373ac0f
commit 633b76eb5d

View file

@ -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<u32>,
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::<u32>();
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;
}
}
}
}