diff options
Diffstat (limited to 'src/cpu.rs')
| -rw-r--r-- | src/cpu.rs | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/cpu.rs b/src/cpu.rs index 8437d10..d2d6d4d 100644 --- a/src/cpu.rs +++ b/src/cpu.rs @@ -3,6 +3,7 @@ //! Implementation of the MOS 6502 CPU. use bitflags::bitflags; +use tracing::{debug, trace}; use crate::{ instruction::{get_operation, AddressingMode, Instruction}, @@ -78,19 +79,28 @@ impl Cpu { /// Fetches the next byte, pointed to by the program counter. fn fetch_byte(&mut self, cycles: &mut u32, memory: &Memory) -> u8 { + debug!("fetching byte from address {:#04X}", self.program_counter); + let data = *memory .data .get(usize::from(self.program_counter)) .expect("program_counter indexed outside of memory"); + debug!( + "fetched byte {data:#02X} from address {:#04X}", + self.program_counter + ); + self.program_counter = self.program_counter.wrapping_add(1); - *cycles -= 1; + Self::expend_cycles(cycles, 1); data } /// Sets the LDA status after executing an LDA operation. fn lda_set_status(&mut self) { + trace!("setting LDA status"); + self.processor_status .set(ProcessorStatus::ZERO, self.accumulator == 0); self.processor_status.set( @@ -101,16 +111,27 @@ impl Cpu { /// Reads the next byte, pointed to by the address. fn read_byte(cycles: &mut u32, address: u8, memory: &Memory) -> u8 { + debug!("reading byte from address {address:#04X}"); + let data = *memory .data .get(usize::from(address)) .expect("address indexed outside of memory"); - *cycles -= 1; + debug!("received byte {data:#02X} from address {address:#02X}"); + + Self::expend_cycles(cycles, 1); data } + /// Expends the specified number of cycles. This method is for internal use to log expended + /// cycles. + fn expend_cycles(cycles: &mut u32, count: u32) { + *cycles -= count; + trace!("expended {count} cycles, {cycles} cycles remaining"); + } + /// Executes instructions on the [`Cpu`] from memory. Runs the specified /// number of cycles. pub fn execute(&mut self, cycles: u32, memory: &Memory) { @@ -119,10 +140,17 @@ impl Cpu { while cycles > 0 { let opcode: u8 = self.fetch_byte(&mut cycles, memory); + trace!("got opcode {opcode:#02X}"); let operation = get_operation(opcode); if let Some(operation) = operation { + trace!( + "running instruction {:?}, addressing mode {:?}", + operation.instruction, + operation.addressing_mode + ); + match (operation.instruction, operation.addressing_mode) { (Instruction::Lda, AddressingMode::Immediate) => { let value = self.fetch_byte(&mut cycles, memory); @@ -140,7 +168,7 @@ impl Cpu { let mut zero_page_address = self.fetch_byte(&mut cycles, memory); zero_page_address += self.index_register_x; - cycles -= 1; + Self::expend_cycles(&mut cycles, 1); self.accumulator = Self::read_byte(&mut cycles, zero_page_address, memory); self.lda_set_status(); |