use crate::isa::reg::RegIdx;
#[derive(Debug)]
pub struct Gpr {
regs: [u64; 32],
}
impl Default for Gpr {
fn default() -> Self {
Self::new()
}
}
impl Gpr {
pub const fn new() -> Self {
Self { regs: [0; 32] }
}
pub const fn read(&self, idx: RegIdx) -> u64 {
if idx.is_zero() { 0 } else { self.regs[idx.as_usize()] }
}
pub const fn write(&mut self, idx: RegIdx, val: u64) {
if !idx.is_zero() {
self.regs[idx.as_usize()] = val;
}
}
}
impl std::fmt::Display for Gpr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (i, [even, odd]) in self.regs.as_chunks::<2>().0.iter().enumerate() {
let first = i * 2;
writeln!(f, "x{first:<2}={even:#018x} x{:<2}={odd:#018x}", first + 1)?;
}
Ok(())
}
}