implementation of code-length & padding for BIO code segments

This commit is contained in:
bunnie 2026-02-20 20:58:40 +08:00
parent 29437c1ae8
commit 60d0126f37
2 changed files with 22 additions and 12 deletions

View file

@ -34,19 +34,22 @@ impl<'a> BioApi<'a> for Bio {
fn init_core( fn init_core(
&mut self, &mut self,
core: BioCore, core: BioCore,
code: &[u8], code: (&[u8], Option<u32>),
offset: usize,
config: CoreConfig, config: CoreConfig,
) -> Result<Option<u32>, BioError> { ) -> Result<Option<u32>, BioError> {
let mut config = CoreInitRkyv { let mut config = CoreInitRkyv {
core, core,
offset, offset: 0,
pad_word: None,
actual_freq: None, actual_freq: None,
config, config,
code: [0u8; 4096], code: [0u8; 4096],
code_len: 0,
result: BioError::Uninit, result: BioError::Uninit,
}; };
config.code[..code.len()].copy_from_slice(code); config.code[..code.0.len()].copy_from_slice(code.0);
config.code_len = code.0.len();
config.pad_word = code.1;
// this should automatically allocate 2 pages because the sizeof() type of CoreInitRkyv is over 4096 // this should automatically allocate 2 pages because the sizeof() type of CoreInitRkyv is over 4096
let mut buf = Buffer::into_buf(config).unwrap(); let mut buf = Buffer::into_buf(config).unwrap();
buf.lend_mut(self.conn, BioOp::InitCore.to_u32().unwrap()) buf.lend_mut(self.conn, BioOp::InitCore.to_u32().unwrap())

View file

@ -219,7 +219,7 @@ impl BioSharedState {
for core in 0..4 { for core in 0..4 {
// crate::println!("ldst trial"); // crate::println!("ldst trial");
self.load_code(mem_init_code(), 0, BioCore::from(core)); self.load_code(mem_init_code(), BioCore::from(core));
self.set_core_run_states([core == 0, core == 1, core == 2, core == 3]); self.set_core_run_states([core == 0, core == 1, core == 2, core == 3]);
for _ in 0..16 { for _ in 0..16 {
let _ = self.bio.r(utra::bio_bdma::SFR_RXF0); let _ = self.bio.r(utra::bio_bdma::SFR_RXF0);
@ -233,7 +233,7 @@ impl BioSharedState {
self.set_core_run_states([false, false, false, false]); self.set_core_run_states([false, false, false, false]);
} }
pub fn load_code(&mut self, prog: &[u8], offset_bytes: usize, core: BioCore) { pub fn load_code(&mut self, prog: (&[u8], Option<u32>), core: BioCore) {
// turn off just the target core // turn off just the target core
let core_num = 1 << (core as usize); let core_num = 1 << (core as usize);
self.bio.wo( self.bio.wo(
@ -241,8 +241,16 @@ impl BioSharedState {
self.bio.r(utra::bio_bdma::SFR_CTRL) & !(core_num | core_num << 4 | core_num << 8), self.bio.r(utra::bio_bdma::SFR_CTRL) & !(core_num | core_num << 4 | core_num << 8),
); );
// crate::println!("load code from {:x}", prog.as_ptr() as usize); // crate::println!("load code from {:x}", prog.as_ptr() as usize);
let offset = offset_bytes / core::mem::size_of::<u32>(); let offset = if let Some(pad_word) = prog.1 {
for (i, chunk) in prog.chunks(4).enumerate() { // insert the pad_word up top. This ensures the page-offsets of the code line up
// with what the linker had specified, effectively replacing the length field with
// a word (usually specified as a NOP instruction)
self.imem_slice[core as usize][0] = pad_word;
1
} else {
0
};
for (i, chunk) in prog.0.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[core as usize][i + offset] = word; self.imem_slice[core as usize][i + offset] = word;
@ -255,7 +263,7 @@ impl BioSharedState {
self.imem_slice[core as usize][i + offset] = ragged_word; self.imem_slice[core as usize][i + offset] = ragged_word;
} }
} }
match self.verify_code(&prog, offset_bytes, core) { match self.verify_code(&prog.0, offset * size_of::<u32>(), core) {
Err(BioError::CodeCheck(offset)) => { Err(BioError::CodeCheck(offset)) => {
crate::println!("Code verification error at {:x}", offset) crate::println!("Code verification error at {:x}", offset)
} }
@ -444,15 +452,14 @@ impl<'a> BioApi<'a> for BioSharedState {
fn init_core( fn init_core(
&mut self, &mut self,
core: BioCore, core: BioCore,
code: &[u8], code: (&[u8], Option<u32>),
offset: usize,
config: CoreConfig, config: CoreConfig,
) -> Result<Option<u32>, BioError> { ) -> Result<Option<u32>, BioError> {
if self.core_config[core as usize].is_some() { if self.core_config[core as usize].is_some() {
return Err(BioError::ResourceInUse); return Err(BioError::ResourceInUse);
} }
self.core_config[core as usize] = Some(config); self.core_config[core as usize] = Some(config);
self.load_code(code, offset, core); self.load_code(code, core);
Ok(self.apply_config(&config, core)) Ok(self.apply_config(&config, core))
} }