Error in nestest log, or with my logic?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Error in nestest log, or with my logic?
by on (#157743)
I have started working on my own NES emulator, and after finishing most, if not all, of my CPU core I have started testing it with the nestest but I found a weird error at the following instructions.

Code:
C8B9  18        CLC                             A:00 X:00 Y:00 P:2F SP:FB
C8BA  24 01     BIT $01 = FF                    A:00 X:00 Y:00 P:2E SP:FB
C8BC  A9 55     LDA #$55                        A:00 X:00 Y:00 P:EE SP:FB


Upon further inspection I see that my emulator have the correct values: A = 0, $01 = 0xFF and P = 0x2E. According to the nestest log P = 0xEE after executing BIT, but how is that possible when A = 0 and $01 = 0xFF? 0x00 & 0xFF = 0x00, which means that both the N and V flags should be false at this point. Following the same logic, I would assume that P = 0x2E after executing BIT.

Am I missing something, or is there some error with the nestest log?
Re: Error in nestest log, or with my logic?
by on (#157747)
The N and V flags are set according the memory value, prior to the AND operation. Reference.
Re: Error in nestest log, or with my logic?
by on (#157749)
With BIT only the Z flag is from the AND result. N and V are from the operand's fetched value, not the result of the AND.

Edit: Rahsennor already covered it. Sorry for duplicate answer.
Re: Error in nestest log, or with my logic?
by on (#157820)
Thank you very much. That fixed it. I have been following this resource and it did not mention that at all.
Re: Error in nestest log, or with my logic?
by on (#157821)
It's good to use multiple references.

This one is a decent high-level overview: http://www.obelisk.me.uk/6502/reference.html
This one details the cycle-by-cycle behavior (there are a few errors though, IIRC): http://nesdev.com/6502_cpu.txt
Re: Error in nestest log, or with my logic?
by on (#157822)
thefox wrote:
This one details the cycle-by-cycle behavior (there are a few errors though, IIRC): http://nesdev.com/6502_cpu.txt


Do you mind to mention some of these errors? I used such doc to write my 6502 emulator.
Re: Error in nestest log, or with my logic?
by on (#157838)
I don't think there were any big errors that I found, more like mistakes/typos, like this one for BRK:

Code:
3  $0100,S  W  push PCH on stack (with B flag set), decrement S
Re: Error in nestest log, or with my logic?
by on (#157839)
...and what's "wrong" with this?
Re: Error in nestest log, or with my logic?
by on (#157840)
PCH is the hibyte of PC, it doesn't have a "B flag". The note about B flag was supposed to be on another line.
Re: Error in nestest log, or with my logic?
by on (#157976)
Another error I happened to notice:

Code:
IRQ and BRK both set the I flag, whereas the NMI does not affect its state.

(NMI does set the I flag.)