Help me with finding whats wrong in this?

This is an archive of a topic from NESdev BBS, taken in mid-October 2019 before a server upgrade.
View original topic
Help me with finding whats wrong in this?
by on (#425)
Hello again everyone :)

I have been going through the step-debug mode in FCEU Ultra that has shown me that there is an improper thing going on in my emulator. I have dumped the code as I have been running it in an easy to read format... here it is (this is SMB1):

Code:
$8000 | implied6502 | SEI
[Accumulator: 0000] [X: 00] [Y: 00] [SP: FF] [FLAGS: 00000024]

$8001 | implied6502 | CLD
[Accumulator: 0000] [X: 00] [Y: 00] [SP: FF] [FLAGS: 00000024]

$8002 | immediate6502 | LDA $10
[Accumulator: 0010] [X: 00] [Y: 00] [SP: FF] [FLAGS: 00000024]

$8004 | abs6502 | STA $00,$20
[Accumulator: 0010] [X: 00] [Y: 00] [SP: FF] [FLAGS: 00000024]

$8007 | immediate6502 | LDX $FF
[Accumulator: 0010] [X: FF] [Y: 00] [SP: FF] [FLAGS: 000000A4]

$8009 | implied6502 | TXS
[Accumulator: 0010] [X: FF] [Y: 00] [SP: FF] [FLAGS: 000000A4]

$800A | abs6502 | LDA $02,$20
[Accumulator: 2002] [X: FF] [Y: 00] [SP: FF] [FLAGS: 000000A4]

$800D | relative6502 | BPL $FB
[Accumulator: 2002] [X: FF] [Y: 00] [SP: FF] [FLAGS: 000000A4]

$800F | abs6502 | LDA $02,$20
[Accumulator: 2002] [X: FF] [Y: 00] [SP: FF] [FLAGS: 000000A4]

$8012 | relative6502 | BPL $FB
[Accumulator: 2002] [X: FF] [Y: 00] [SP: FF] [FLAGS: 000000A4]

$8014 | immediate6502 | LDY $FE
[Accumulator: 2002] [X: FF] [Y: FE] [SP: FF] [FLAGS: 000000A4]

$8016 | immediate6502 | LDX $05
[Accumulator: 2002] [X: 05] [Y: FE] [SP: FF] [FLAGS: 00000024]


The problem is at address $8012, it should loop back to $800F, as it does in FCEU. At one point, I think it DID do this, and maybe I thought it was a bug. I am assuming that I am improperly calculating the flags at one point, but I was wondering if any of you could help me find out where. If you need any other information, i'd be glad to help.

Thanks! :)

by on (#426)
Is your accumulator 16-bit? It should only be 8-bit. Only 16-bit reg on the 6502 is the PC.

Anyway... the value read from $2002 is going to differ depending on PPU status. The high bit of $2002 is the VBlank flag. Games read $2002 and do a loop to wait for VBlank to occur.

So basically, you need to be emulating the PPU for your code to run like FCEU's is. It's not necessarily a problem with your 6502 emulator.

by on (#9730)
Check to make sure LDA properly clears and sets the N and Z flags.

BPL branches when the N flag is clear, and your trace clearly shows it staying set through an LDA. That would be correct if you're loading a number with bit 7 set, but if you're seeing the code loop in another emulator, then the data doesn't have the high bit set...