summaryrefslogtreecommitdiff
path: root/src/debug.rs
blob: d19dbec04df69db792120d872f41f1513c460774 (plain) (blame)
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
use bedrock_core::*;

use std::time::Instant;

macro_rules! yellow { () => { eprint!("\x1b[33m") };}
macro_rules! normal { () => { eprint!("\x1b[0m" ) };}


pub struct DebugState {
    pub enabled: bool,
    last_cycle: usize,
    last_mark: Instant,
}

impl DebugState {
    pub fn new(enabled: bool) -> Self {
        Self {
            enabled,
            last_cycle: 0,
            last_mark: Instant::now(),
        }
    }

    pub fn print(&self, string: &str) {
        if self.enabled {
            println!("{}", string);
        }
    }

    pub fn debug_summary(&mut self, core: &BedrockCore) {
        eprintln!("\n PC: 0x{:04x}   Cycles: {} (+{} in {:.2?})",
            core.mem.pc, core.cycle,
            core.cycle.saturating_sub(self.last_cycle),
            self.last_mark.elapsed(),
        );
        eprint!("WST: ");
        debug_stack(&core.wst, 0x10);
        eprint!("RST: ");
        debug_stack(&core.rst, 0x10);

        self.last_cycle = core.cycle;
        self.last_mark = Instant::now();
    }
}


fn debug_stack(stack: &Stack, len: usize) {
    for i in 0..len {
        if i == stack.sp as usize { yellow!(); }
        eprint!("{:02x} ", stack.mem[i]);
    }
    normal!();
    eprintln!();
}