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

Wrong status flags in LDA example?

romatthe opened this issue · comments

Hi, I was just reading through your NES EMU book, but I was confused pretty quickly.

In Chapter 3.1, you start by implementing LDA as the very first instruction.
At the end of the instruction implementation, you adjust the status flags:

if result == 0 {
    self.status = self.status | 0b0000_0001;
} else {
    self.status = self.status & 0b1111_1110;
}

This seems to be modifying the Carry flag. However, the 6502 instruction reference you linked clearly indicates that it is the Zero and Negative flag that should be updated.

. flag effect
C Carry Flag Not affected
Z Zero Flag Set if A = 0
I Interrupt Disable Not affected
D Decimal Mode Flag Not affected
B Break Command Not affected
V Overflow Flag Not affected
N Negative Flag Set if bit 7 of A is set

Considering that the status register layout is

NVxx DIZC

should the code not be as follows?

if result == 0 {
    self.status = self.status | 0b0000_0010;
} else {
    self.status = self.status & 0b1111_1101;
}

Hey! Thank you for noticing this.

You're right - I've messed it up. At chapter 3.3 I've switched to using https://crates.io/crates/bitflags, and the bug was left unnoticed (https://github.com/bugzmanov/nes_ebook/blob/master/code/ch3.3/src/cpu.rs#L206-L211)

I will update the chapter later today or feel free to open pull request if you'd like to )

cc @romatthe