bugzmanov / nes_ebook

A mini book on writing NES emulator using rust lang
https://bugzmanov.github.io/nes_ebook/index.html
385 stars 68 forks source link

Hint for using wrapping_add() ? #17

Closed leopnt closed 2 years ago

leopnt commented 2 years ago

At the end of 3.1, maybe you could give a hint to use wrapping_add(). Because rust doesn't allow overflow in debug mode:

fn inx(&mut self) {                                                         
    self.rx = self.rx.wrapping_add(1) as u8;                                
}

instead of:

fn inx(&mut self) {                                                         
    self.rx += 1;                                
}

to solve:

#[test]
fn test_inx_overflow() {
    let mut cpu = CPU::new();
    cpu.register_x = 0xff;
    cpu.interpret(vec![0xe8, 0xe8, 0x00]);

    assert_eq!(cpu.register_x, 1)
}
leopnt commented 2 years ago

Just seen you provided source code to each chapter, my bad