Skip to content

Commit

Permalink
half-w4ritten nesttest trace
Browse files Browse the repository at this point in the history
  • Loading branch information
imagine-hussain committed Apr 24, 2024
1 parent 218e551 commit bc28808
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
43 changes: 42 additions & 1 deletion src/lib/cpu/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
opcodes::OpCode,
Bus, RcCell, Reset,
};
use std::{cell::RefCell, rc::Rc};
use std::{cell::RefCell, fmt::format, rc::Rc};

Check warning on line 10 in src/lib/cpu/cpu.rs

View workflow job for this annotation

GitHub Actions / clippy

unused import: `fmt::format`

warning: unused import: `fmt::format` --> src/lib/cpu/cpu.rs:10:26 | 10 | use std::{cell::RefCell, fmt::format, rc::Rc}; | ^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default

/// Emulator for the `6502` CPU.
///
Expand Down Expand Up @@ -249,6 +249,47 @@ impl Cpu {
program_counter: self.program_counter,
}
}

/// Trace current cpu state in nestest.log format
/// Example output line:
/// ```
/// C000 4C F5 C5 JMP $C5F5 A:00 X:00 Y:00 P:24 SP:FD PPU: 0, 21 CYC:7
/// ```
///
/// Columns:
/// - `program_counter`: `C000`
/// - `CPU opcode`: `4C F5 C5` - Variable len - Recall that opcodes are 1-3 bytes. In the case
/// of shorter opcodes, we keep the columns spacing consistent and left-align the text
/// - `CPU_opcode in ASM` -
/// - $80 + X = real address = mem value at real address ???
/// @ 80 = 0100 = 00
/// - first num is mem referenc ethat we get if we apply offsert to the request
/// address based on addressing mode
/// - then a u16 target fecthed from [0x80..0x81]
/// - the content of that address cell
/// - rest of the cpu registers: A, X, Y, P, SP
/// - CPU and PPU clock cycles
pub fn nestest_trace(&mut self) -> String {
// Allocing = cringe?
let mut trace = String::with_capacity(92);
let pad_till_col = |s: &mut String, col: usize| {
let amount_to_pad = col - s.len();
s.extend(std::iter::repeat(' ').take(amount_to_pad));
};

trace.extend(format!("{:04X}", self.program_counter).chars());
pad_till_col(&mut trace, 6);
// Program Counter
let opcode: OpCode = self.read(self.program_counter).into();

Check warning on line 284 in src/lib/cpu/cpu.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `opcode`

warning: unused variable: `opcode` --> src/lib/cpu/cpu.rs:284:13 | 284 | let opcode: OpCode = self.read(self.program_counter).into(); | ^^^^^^ help: if this is intentional, prefix it with an underscore: `_opcode` | = note: `#[warn(unused_variables)]` on by default

// todo!("put in the raw opcode");
pad_till_col(&mut trace, 16);
// let x = opcode.decompile();

// C000 4C F5 C5 JMP $C5F5 A:00 X:00 Y:00 P:24 SP:FD PPU: 0, 21 CYC:7
todo!("write the trace");
}
}

#[derive(Debug, Clone, Copy)]
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// TODO: hmmmm - mayve declare elsewhere
pub mod tests;

use std::{cell::RefCell, rc::Rc};

use env_logger::Env;
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit bc28808

Please sign in to comment.