lc3_ensemble/sim/
debug.rs1use std::fmt::Write;
6
7use crate::ast::Reg;
8
9use super::Simulator;
10
11#[derive(PartialEq, Eq, Hash)]
13pub enum Breakpoint {
14 PC(u16),
16
17 Reg {
19 reg: Reg,
21 value: Comparator
23 },
24 Mem {
26 addr: u16,
28 value: Comparator
30 },
31}
32
33impl Breakpoint where Breakpoint: Send + Sync { }
34
35impl Breakpoint {
36 pub fn check(&self, sim: &Simulator) -> bool {
38 match self {
39 Breakpoint::PC(expected) => expected == &sim.pc,
40 Breakpoint::Reg { reg, value: cmp } => cmp.check(sim.reg_file[*reg].get()),
41 Breakpoint::Mem { addr, value: cmp } => cmp.check(sim.mem[*addr].get()), }
43 }
44
45 fn fmt_bp(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46 match self {
47 Self::PC(expected) => {
48 write!(f, "PC == x{expected:04X}")?;
49 },
50 Self::Reg { reg, value } => {
51 write!(f, "{reg} ")?;
52 value.fmt_cmp(f)?;
53 },
54 Self::Mem { addr, value } => {
55 write!(f, "mem[x{addr:04X}] ")?;
56 value.fmt_cmp(f)?;
57 },
58 }
59 Ok(())
60 }
61}
62impl std::fmt::Debug for Breakpoint {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 f.write_str("Breakpoint(")?;
65 self.fmt_bp(f)?;
66 f.write_char(')')
67 }
68}
69#[derive(PartialEq, Eq, Hash, Debug)]
71pub enum Comparator {
72 Never,
74 Lt(u16),
76 Eq(u16),
78 Le(u16),
80 Gt(u16),
82 Ne(u16),
84 Ge(u16),
86 Always
88}
89impl Comparator {
90 pub fn check(&self, operand: u16) -> bool {
92 match *self {
93 Comparator::Never => false,
94 Comparator::Lt(r) => operand < r,
95 Comparator::Eq(r) => operand == r,
96 Comparator::Le(r) => operand <= r,
97 Comparator::Gt(r) => operand > r,
98 Comparator::Ne(r) => operand != r,
99 Comparator::Ge(r) => operand >= r,
100 Comparator::Always => true,
101 }
102 }
103
104 fn fmt_cmp(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105 match self {
106 Comparator::Never => f.write_str("never"),
107 Comparator::Lt(r) => write!(f, "< {r}"),
108 Comparator::Eq(r) => write!(f, "== {r}"),
109 Comparator::Le(r) => write!(f, "<= {r}"),
110 Comparator::Gt(r) => write!(f, "> {r}"),
111 Comparator::Ne(r) => write!(f, "!= {r}"),
112 Comparator::Ge(r) => write!(f, ">= {r}"),
113 Comparator::Always => f.write_str("always"),
114 }
115 }
116}