use core::convert::TryInto;
use gdbstub::arch::Registers;
use gdbstub::internal::LeBytes;
use num_traits::PrimInt;
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct MipsCoreRegs<U> {
pub r: [U; 32],
pub lo: U,
pub hi: U,
pub pc: U,
pub cp0: MipsCp0Regs<U>,
pub fpu: MipsFpuRegs<U>,
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct MipsCp0Regs<U> {
pub status: U,
pub badvaddr: U,
pub cause: U,
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct MipsFpuRegs<U> {
pub r: [U; 32],
pub fcsr: U,
pub fir: U,
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct MipsDspRegs<U> {
pub hi1: U,
pub lo1: U,
pub hi2: U,
pub lo2: U,
pub hi3: U,
pub lo3: U,
pub dspctl: u32,
pub restart: U,
}
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct MipsCoreRegsWithDsp<U> {
pub core: MipsCoreRegs<U>,
pub dsp: MipsDspRegs<U>,
}
impl<U> Registers for MipsCoreRegs<U>
where
U: PrimInt + LeBytes + Default + core::fmt::Debug,
{
type ProgramCounter = U;
fn pc(&self) -> Self::ProgramCounter {
self.pc
}
fn gdb_serialize(&self, mut write_byte: impl FnMut(Option<u8>)) {
macro_rules! write_le_bytes {
($value:expr) => {
let mut buf = [0; 16];
let len = $value.to_le_bytes(&mut buf).unwrap();
let buf = &buf[..len];
for b in buf {
write_byte(Some(*b));
}
};
}
for reg in self.r.iter() {
write_le_bytes!(reg);
}
write_le_bytes!(&self.cp0.status);
write_le_bytes!(&self.lo);
write_le_bytes!(&self.hi);
write_le_bytes!(&self.cp0.badvaddr);
write_le_bytes!(&self.cp0.cause);
write_le_bytes!(&self.pc);
for reg in self.fpu.r.iter() {
write_le_bytes!(reg);
}
write_le_bytes!(&self.fpu.fcsr);
write_le_bytes!(&self.fpu.fir);
}
fn gdb_deserialize(&mut self, bytes: &[u8]) -> Result<(), ()> {
let ptrsize = core::mem::size_of::<U>();
if bytes.len() < ptrsize * 72 {
return Err(());
}
let mut regs = bytes
.chunks_exact(ptrsize)
.map(|c| U::from_le_bytes(c).unwrap());
for reg in self.r.iter_mut() {
*reg = regs.next().ok_or(())?
}
self.cp0.status = regs.next().ok_or(())?;
self.lo = regs.next().ok_or(())?;
self.hi = regs.next().ok_or(())?;
self.cp0.badvaddr = regs.next().ok_or(())?;
self.cp0.cause = regs.next().ok_or(())?;
self.pc = regs.next().ok_or(())?;
for reg in self.fpu.r.iter_mut() {
*reg = regs.next().ok_or(())?
}
self.fpu.fcsr = regs.next().ok_or(())?;
self.fpu.fir = regs.next().ok_or(())?;
Ok(())
}
}
impl<U> Registers for MipsCoreRegsWithDsp<U>
where
U: PrimInt + LeBytes + Default + core::fmt::Debug,
{
type ProgramCounter = U;
fn pc(&self) -> Self::ProgramCounter {
self.core.pc
}
fn gdb_serialize(&self, mut write_byte: impl FnMut(Option<u8>)) {
macro_rules! write_le_bytes {
($value:expr) => {
let mut buf = [0; 16];
let len = $value.to_le_bytes(&mut buf).unwrap();
let buf = &buf[..len];
for b in buf {
write_byte(Some(*b));
}
};
}
self.core.gdb_serialize(&mut write_byte);
write_le_bytes!(&self.dsp.hi1);
write_le_bytes!(&self.dsp.lo1);
write_le_bytes!(&self.dsp.hi2);
write_le_bytes!(&self.dsp.lo2);
write_le_bytes!(&self.dsp.hi3);
write_le_bytes!(&self.dsp.lo3);
for b in &self.dsp.dspctl.to_le_bytes() {
write_byte(Some(*b));
}
write_le_bytes!(&self.dsp.restart);
}
fn gdb_deserialize(&mut self, bytes: &[u8]) -> Result<(), ()> {
self.core.gdb_deserialize(bytes)?;
let ptrsize = core::mem::size_of::<U>();
if bytes.len() < (ptrsize * 79) + 4 {
return Err(());
}
let dspregs_start = ptrsize * 72;
let dspctl_start = ptrsize * 78;
let mut regs = bytes[dspregs_start..dspctl_start]
.chunks_exact(ptrsize)
.map(|c| U::from_le_bytes(c).unwrap());
self.dsp.hi1 = regs.next().ok_or(())?;
self.dsp.lo1 = regs.next().ok_or(())?;
self.dsp.hi2 = regs.next().ok_or(())?;
self.dsp.lo2 = regs.next().ok_or(())?;
self.dsp.hi3 = regs.next().ok_or(())?;
self.dsp.lo3 = regs.next().ok_or(())?;
self.dsp.dspctl =
u32::from_le_bytes(bytes[dspctl_start..dspctl_start + 4].try_into().unwrap());
self.dsp.restart = U::from_le_bytes(
bytes[dspctl_start + 4..]
.chunks_exact(ptrsize)
.next()
.ok_or(())?,
)
.unwrap();
Ok(())
}
}