bugzmanov / nes_ebook

A mini book on writing NES emulator using rust lang

Home Page:https://bugzmanov.github.io/nes_ebook/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chapter 3.4 conversion to i8 for Branch

dariandzirko opened this issue · comments

In cpu.rs

fn branch(&mut self, condition: bool) {
        if condition {
            let jump: i8 = self.mem_read(self.program_counter) as i8;
            let jump_addr = self
                .program_counter
                .wrapping_add(1)
                .wrapping_add(jump as u16);

If I change jump to u16 I get an extremely incorrect result immediately resulting in a BRK command run. The negative flag isn't set and it is immediately changed back to u16 later in the wrapping add. Any idea about this? Is there something in the documentation I am missing where this should a signed and then unsigned add?

signed is necessary here, the jump offset can be a negative value, see here
https://wiki.cdot.senecacollege.ca/wiki/6502_Jumps,_Branches,_and_Procedures