ad9959: refactor pad()

This commit is contained in:
Robert Jördens 2021-05-26 16:06:55 +00:00
parent 2368a4f6ed
commit 4f9113cb45
1 changed files with 18 additions and 15 deletions

View File

@ -596,6 +596,22 @@ impl ProfileSerializer {
self.index += value.len() + 1;
}
fn pad(&mut self) {
// Pad the buffer to 32-bit (4 byte) alignment by adding dummy writes to CSR and LSRR.
match self.index & 3 {
3 => {
// For a level of 3, we have to pad with 5 bytes to align things.
self.add_write(Register::CSR, &[(self.mode as u8) << 1]);
self.add_write(Register::LSRR, &[0, 0]);
}
2 => self.add_write(Register::CSR, &[(self.mode as u8) << 1]),
1 => self.add_write(Register::LSRR, &[0, 0]),
0 => {}
_ => unreachable!(),
}
}
/// Get the serialized profile as a slice of 32-bit words.
///
/// # Note
@ -604,21 +620,8 @@ impl ProfileSerializer {
///
/// # Returns
/// A slice of `u32` words representing the serialized profile.
pub fn finalize<'a>(&'a mut self) -> &[u32] {
// Pad the buffer to 32-bit alignment by adding dummy writes to CSR and LSRR.
let padding = 4 - (self.index % 4);
match padding {
1 => {
// For a pad size of 1, we have to pad with 5 bytes to align things.
self.add_write(Register::CSR, &[(self.mode as u8) << 1]);
self.add_write(Register::LSRR, &[0, 0]);
}
2 => self.add_write(Register::CSR, &[(self.mode as u8) << 1]),
3 => self.add_write(Register::LSRR, &[0, 0]),
4 => {}
_ => unreachable!(),
}
pub fn finalize<'a>(&'a mut self) -> &'a [u32] {
self.pad();
unsafe {
core::slice::from_raw_parts::<'a, u32>(
&self.data as *const _ as *const u32,