Error in nestest log, or with my logic?

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
Fjotten
Posts: 7
Joined: Sun Oct 25, 2015 4:16 pm

Error in nestest log, or with my logic?

Post by Fjotten »

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: Select all

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?
Rahsennor
Posts: 479
Joined: Thu Aug 20, 2015 3:09 am

Re: Error in nestest log, or with my logic?

Post by Rahsennor »

The N and V flags are set according the memory value, prior to the AND operation. Reference.
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Error in nestest log, or with my logic?

Post by rainwarrior »

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.
Fjotten
Posts: 7
Joined: Sun Oct 25, 2015 4:16 pm

Re: Error in nestest log, or with my logic?

Post by Fjotten »

Thank you very much. That fixed it. I have been following this resource and it did not mention that at all.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Error in nestest log, or with my logic?

Post by thefox »

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
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Error in nestest log, or with my logic?

Post by Zepper »

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.
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Error in nestest log, or with my logic?

Post by thefox »

I don't think there were any big errors that I found, more like mistakes/typos, like this one for BRK:

Code: Select all

3  $0100,S  W  push PCH on stack (with B flag set), decrement S
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
Zepper
Formerly Fx3
Posts: 3262
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Re: Error in nestest log, or with my logic?

Post by Zepper »

...and what's "wrong" with this?
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Error in nestest log, or with my logic?

Post by thefox »

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.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Error in nestest log, or with my logic?

Post by thefox »

Another error I happened to notice:

Code: Select all

IRQ and BRK both set the I flag, whereas the NMI does not affect its state.
(NMI does set the I flag.)
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
Post Reply