summaryrefslogtreecommitdiff
path: root/src/debug.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/debug.rs')
-rw-r--r--src/debug.rs54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/debug.rs b/src/debug.rs
new file mode 100644
index 0000000..d19dbec
--- /dev/null
+++ b/src/debug.rs
@@ -0,0 +1,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!();
+}