cpu.rs: Refactor dump(&self) to do fewer allocations

This commit is contained in:
John 2023-03-25 18:32:45 -05:00
parent 47fa41fd01
commit edd2b60665
2 changed files with 31 additions and 18 deletions

View File

@ -340,28 +340,42 @@ impl CPU {
} }
pub fn dump(&self) { pub fn dump(&self) {
let dumpstyle = owo_colors::Style::new().bright_black(); //let dumpstyle = owo_colors::Style::new().bright_black();
let mut dump = format!( std::println!(
"PC: {:04x}, SP: {:04x}, I: {:04x}\n", "PC: {:04x}, SP: {:04x}, I: {:04x}\n{}DLY: {}, SND: {}, CYC: {:6}",
self.pc, self.sp, self.i self.pc,
); self.sp,
for (i, gpr) in self.v.into_iter().enumerate() { self.i,
dump += &format!( self
"V{i:x}: {:02x} {}", .v
gpr, .into_iter()
.enumerate()
.map(|(i, gpr)| {
format!(
"v{i:X}: {gpr:02x} {}",
match i % 4 { match i % 4 {
3 => "\n", 3 => "\n",
_ => "", _ => "",
} }
) )
} })
dump += &format!("DLY: {}, SND: {}", self.delay, self.sound); .collect::<String>(),
self.delay,
std::println!("{}", dump.style(dumpstyle)); self.sound,
self.cycle,
);
} }
} }
impl Default for CPU { 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 { fn default() -> Self {
CPU { CPU {
screen: 0xf00, screen: 0xf00,

View File

@ -1,6 +1,5 @@
//! A disassembler for Chip-8 opcodes //! A disassembler for Chip-8 opcodes
use super::{Adr, Nib, Reg}; use super::{Adr, Nib, Reg};
use owo_colors::{OwoColorize, Style}; use owo_colors::{OwoColorize, Style};
type Ins = Nib; type Ins = Nib;