From edd2b60665e94d827eb57aa3f4a65fefd7685db3 Mon Sep 17 00:00:00 2001 From: John Breaux Date: Sat, 25 Mar 2023 18:32:45 -0500 Subject: [PATCH] cpu.rs: Refactor dump(&self) to do fewer allocations --- src/cpu.rs | 48 +++++++++++++++++++++++++++--------------- src/cpu/disassemble.rs | 1 - 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/cpu.rs b/src/cpu.rs index 8e66b64..d3a79c8 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -340,28 +340,42 @@ impl CPU { } pub fn dump(&self) { - let dumpstyle = owo_colors::Style::new().bright_black(); - let mut dump = format!( - "PC: {:04x}, SP: {:04x}, I: {:04x}\n", - self.pc, self.sp, self.i + //let dumpstyle = owo_colors::Style::new().bright_black(); + std::println!( + "PC: {:04x}, SP: {:04x}, I: {:04x}\n{}DLY: {}, SND: {}, CYC: {:6}", + self.pc, + self.sp, + self.i, + self + .v + .into_iter() + .enumerate() + .map(|(i, gpr)| { + format!( + "v{i:X}: {gpr:02x} {}", + match i % 4 { + 3 => "\n", + _ => "", + } + ) + }) + .collect::(), + self.delay, + self.sound, + self.cycle, ); - for (i, gpr) in self.v.into_iter().enumerate() { - dump += &format!( - "V{i:x}: {:02x} {}", - gpr, - match i % 4 { - 3 => "\n", - _ => "", - } - ) - } - dump += &format!("DLY: {}, SND: {}", self.delay, self.sound); - - std::println!("{}", dump.style(dumpstyle)); } } impl Default for CPU { + /// Constructs a new CPU with sane defaults and debug mode ON + /// + /// | value | default | description + /// |--------|---------|------------ + /// | screen |`0x0f00` | Location of screen memory. + /// | font |`0x0050` | Location of font memory. + /// | pc |`0x0200` | Start location. Generally 0x200 or 0x600. + /// | sp |`0x0efe` | Initial top of stack. fn default() -> Self { CPU { screen: 0xf00, diff --git a/src/cpu/disassemble.rs b/src/cpu/disassemble.rs index c2397f4..9a442df 100644 --- a/src/cpu/disassemble.rs +++ b/src/cpu/disassemble.rs @@ -1,6 +1,5 @@ //! A disassembler for Chip-8 opcodes - use super::{Adr, Nib, Reg}; use owo_colors::{OwoColorize, Style}; type Ins = Nib;