use std::fmt::Write;
use crate::ast::Reg;
use super::Simulator;
#[derive(PartialEq, Eq, Hash)]
pub enum Breakpoint {
PC(u16),
Reg {
reg: Reg,
value: Comparator
},
Mem {
addr: u16,
value: Comparator
},
}
impl Breakpoint where Breakpoint: Send + Sync { }
impl Breakpoint {
pub fn check(&self, sim: &Simulator) -> bool {
match self {
Breakpoint::PC(expected) => expected == &sim.pc,
Breakpoint::Reg { reg, value: cmp } => cmp.check(sim.reg_file[*reg].get()),
Breakpoint::Mem { addr, value: cmp } => cmp.check(sim.mem[*addr].get()), }
}
fn fmt_bp(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::PC(expected) => {
write!(f, "PC == x{expected:04X}")?;
},
Self::Reg { reg, value } => {
write!(f, "{reg} ")?;
value.fmt_cmp(f)?;
},
Self::Mem { addr, value } => {
write!(f, "mem[x{addr:04X}] ")?;
value.fmt_cmp(f)?;
},
}
Ok(())
}
}
impl std::fmt::Debug for Breakpoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("Breakpoint(")?;
self.fmt_bp(f)?;
f.write_char(')')
}
}
#[derive(PartialEq, Eq, Hash, Debug)]
pub enum Comparator {
Never,
Lt(u16),
Eq(u16),
Le(u16),
Gt(u16),
Ne(u16),
Ge(u16),
Always
}
impl Comparator {
pub fn check(&self, operand: u16) -> bool {
match *self {
Comparator::Never => false,
Comparator::Lt(r) => operand < r,
Comparator::Eq(r) => operand == r,
Comparator::Le(r) => operand <= r,
Comparator::Gt(r) => operand > r,
Comparator::Ne(r) => operand != r,
Comparator::Ge(r) => operand >= r,
Comparator::Always => true,
}
}
fn fmt_cmp(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Comparator::Never => f.write_str("never"),
Comparator::Lt(r) => write!(f, "< {r}"),
Comparator::Eq(r) => write!(f, "== {r}"),
Comparator::Le(r) => write!(f, "<= {r}"),
Comparator::Gt(r) => write!(f, "> {r}"),
Comparator::Ne(r) => write!(f, "!= {r}"),
Comparator::Ge(r) => write!(f, ">= {r}"),
Comparator::Always => f.write_str("always"),
}
}
}