1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//! Utilities to debug simulation.
//! 
//! The key type here is [`Breakpoint`], which can be appended to the [`Simulator`]'s
//! breakpoint field to cause the simulator to break.
use std::fmt::Write;

use crate::ast::Reg;

use super::Simulator;

macro_rules! fmt_cmp {
    ($f:expr, $cmp:expr) => { fmt_cmp!($f, $cmp, "{}") };
    ($f:expr, $cmp:expr, $lit:literal) => {
        match $cmp.flag {
            0b000 => write!($f, "never"),
            0b100 => write!($f, concat!("< ", $lit), $cmp.value),
            0b010 => write!($f, concat!("== ", $lit), $cmp.value),
            0b110 => write!($f, concat!("<= ", $lit), $cmp.value),
            0b001 => write!($f, concat!("> ", $lit), $cmp.value),
            0b101 => write!($f, concat!("!= ", $lit), $cmp.value),
            0b011 => write!($f, concat!(">= ", $lit), $cmp.value),
            0b111 => write!($f, "always"),
            _ => unreachable!("comparator flag should have been less than 8")
        }
    }
}

/// Common breakpoints.
pub enum Breakpoint {
    /// Break when the PC is equal to the given value.
    PC(Comparator),

    /// Break when the provided register is set to a given value.
    Reg {
        /// Register to check.
        reg: Reg,
        /// Predicate to break against.
        value: Comparator
    },
    /// Break when the provided memory address is written to with a given value.
    Mem {
        /// Address to check.
        addr: u16,
        /// Predicate to break against.
        value: Comparator
    },

    /// Breaks based on an arbitrarily defined function.
    /// 
    /// This can be constructed with the [`Breakpoint::generic`] function.
    Generic(BreakpointFn),

    /// Both conditions have to apply for the break to be applied.
    And([Box<Breakpoint>; 2]),
    /// One of these conditions have to apply for the break to be applied.
    Or([Box<Breakpoint>; 2]),
}

impl Breakpoint where Breakpoint: Send + Sync { /* assert Breakpoint is send/sync */ }
type BreakpointFn = Box<dyn Fn(&Simulator) -> bool + Send + Sync + 'static>;

impl Breakpoint {
    /// Creates a breakpoint out of a function.
    pub fn generic(f: impl Fn(&Simulator) -> bool + Send + Sync + 'static) -> Breakpoint {
        Breakpoint::Generic(Box::new(f))
    }

    /// Checks if a break should occur.
    pub fn check(&self, sim: &Simulator) -> bool {
        match self {
            Breakpoint::PC(cmp) => cmp.check(sim.pc),
            Breakpoint::Reg { reg, value: cmp } => cmp.check(sim.reg_file[*reg].get()),
            Breakpoint::Mem { addr, value: cmp } => cmp.check(sim.mem.data[*addr as usize].get()), // this is not using mem's get because we don't want to trigger an IO read
            Breakpoint::Generic(pred) => (pred)(sim),
            Breakpoint::And([l, r]) => l.check(sim) && r.check(sim),
            Breakpoint::Or([l, r]) => l.check(sim) || r.check(sim),
        }
    }

    fn fmt_bp(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Self::PC(cmp) => {
                f.write_str("PC ")?;
                fmt_cmp!(f, cmp, "x{:04X}")?;
            },
            Self::Reg { reg, value } => {
                write!(f, "{reg} ")?;
                fmt_cmp!(f, value)?;
            },
            Self::Mem { addr, value } => {
                write!(f, "mem[x{addr:04X}] ")?;
                fmt_cmp!(f, value)?;
            },
            Self::Generic(_) => f.debug_struct("Generic").finish_non_exhaustive()?,
            Self::And([l, r]) => {
                f.write_char('(')?;
                l.fmt_bp(f)?;
                f.write_str(") && (")?;
                r.fmt_bp(f)?;
                f.write_char(')')?;
            },
            Self::Or([l, r]) => {
                f.write_char('(')?;
                l.fmt_bp(f)?;
                f.write_str(") || (")?;
                r.fmt_bp(f)?;
                f.write_char(')')?;
            },
        }
        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(')')
    }
}
impl std::ops::BitAnd for Breakpoint {
    type Output = Breakpoint;

    fn bitand(self, rhs: Self) -> Self::Output {
        Breakpoint::And([Box::new(self), Box::new(rhs)])
    }
}
impl std::ops::BitOr for Breakpoint {
    type Output = Breakpoint;

    fn bitor(self, rhs: Self) -> Self::Output {
        Breakpoint::Or([Box::new(self), Box::new(rhs)])
    }
}
impl PartialEq for Breakpoint {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::PC(l0), Self::PC(r0)) => l0 == r0,
            (Self::Reg { reg: l_reg, value: l_value }, Self::Reg { reg: r_reg, value: r_value }) => l_reg == r_reg && l_value == r_value,
            (Self::Mem { addr: l_addr, value: l_value }, Self::Mem { addr: r_addr, value: r_value }) => l_addr == r_addr && l_value == r_value,
            (Self::Generic(_), Self::Generic(_)) => false, /* can't really figure this one out */
            (Self::And(l0), Self::And(r0)) => l0 == r0,
            (Self::Or(l0), Self::Or(r0)) => l0 == r0,
            _ => false,
        }
    }
}

/// Predicate checking whether the current value is equal to the value.
#[derive(PartialEq, Eq)]
pub struct Comparator {
    flag: u8,
    /// The value we're checking against.
    pub value: u16
}
impl Comparator {
    /// Never breaks.
    pub fn never() -> Self {
        Self { flag: 0b000, value: 0 }
    }
    /// Break if the desired value is less than the provided value.
    pub fn lt(value: u16) -> Self {
        Self { flag: 0b100, value }
    }
    /// Break if the desired value is equal to the provided value.
    pub fn eq(value: u16) -> Self {
        Self { flag: 0b010, value }
    }
    /// Break if the desired value is less than or equal to the provided value.
    pub fn le(value: u16) -> Self {
        Self { flag: 0b110, value }
    }
    /// Break if the desired value is greater than the provided value.
    pub fn gt(value: u16) -> Self {
        Self { flag: 0b001, value }
    }
    /// Break if the desired value is not equal to the provided value.
    pub fn ne(value: u16) -> Self {
        Self { flag: 0b101, value }
    }
    /// Break if the desired value is greater than or equal to the provided value.
    pub fn ge(value: u16) -> Self {
        Self { flag: 0b011, value }
    }
    /// Always breaks.
    pub fn always() -> Self {
        Self { flag: 0b111, value: 0 }
    }

    /// Checks if the operand passes the comparator.
    pub fn check(&self, operand: u16) -> bool {
        let cmp_flags = match operand.cmp(&self.value) {
            std::cmp::Ordering::Less    => 0b100,
            std::cmp::Ordering::Equal   => 0b010,
            std::cmp::Ordering::Greater => 0b001,
        };

        (cmp_flags & self.flag) != 0
    }
}

#[cfg(test)]
mod test {
    use crate::ast::reg_consts::R7;
    use crate::sim::debug::{Breakpoint, Comparator};

    #[test]
    fn print() {
        println!("{:?}", Breakpoint::Reg { reg: R7, value: Comparator::lt(14) } & Breakpoint::Reg { reg: R7, value: Comparator::ge(10) });
        println!("{:?}", Breakpoint::Mem { addr: 0x4000, value: Comparator::lt(14) } | Breakpoint::Mem { addr: 0x5000, value: Comparator::ge(10) } & Breakpoint::PC(Comparator::eq(0x3000)));
    }
}