NES Emulation - DK infinite Loop

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
dragonwarrior
Posts: 8
Joined: Sat Feb 01, 2025 3:30 pm

NES Emulation - DK infinite Loop

Post by dragonwarrior »

In these instructions of the Donkey Kong ROM:

Code: Select all

00:C7E1: 20 ED F4 JSR $F4ED

00:C7E4: 4C E1 C7 JMP $C7E1
There seems to be an infinite loop. This is also the subroutine:

Code: Select all

00:F4ED: A5 18 LDA $18 = #$00

00:F4EF: 29 02 AND #$02

00:F4F1: 85 00 STA $00 = #$00

00:F4F3: A5 19 LDA $19 = #$00

00:F4F5: 29 02 AND #$02

00:F4F7: 45 00 EOR $00 = #$00

00:F4F9: 18 CLC

00:F4FA: F0 01 BEQ $F4FD

00:F4FC: 38 SEC

00:F4FD: 66 18 ROR $18 = #$00

00:F4FF: 66 19 ROR $19 = #$00

00:F501: 66 1A ROR $1A = #$00

00:F503: 66 1B ROR $1B = #$00

00:F505: 66 1C ROR $1C = #$00

00:F507: 66 1D ROR $1D = #$00

00:F509: 66 1E ROR $1E = #$00

00:F50B: 66 1F ROR $1F = #$00

00:F50D: 60 RTS
So I don't see how it can exit the loop, but stepping through the instructions with the FCEUX debugger, it somehow exits the loop.

I found some posts about "infinite" loops in DK like this one: viewtopic.php?t=15561 (the problem here was communication between the PPU and CPU I believe)

and this one: viewtopic.php?t=15934 (this was just a normal loop, not infinite)

but in this case, It's unconditional. I've tried debugging for a while, but couldn't figure out what I did wrong, so I'd appreciate any help.
User avatar
TakuikaNinja
Posts: 195
Joined: Mon Jan 09, 2023 6:42 pm
Location: New Zealand
Contact:

Re: NES Emulation - DK infinite Loop

Post by TakuikaNinja »

Donkey Kong, like most early first party NROM games, run their main logic in the NMI handler. The PPU generates the NMI signal at around the start of vertical blank, as long as they are enabled via bit 7 of the $2000 (PPUCTRL) register.
dragonwarrior
Posts: 8
Joined: Sat Feb 01, 2025 3:30 pm

Re: NES Emulation - DK infinite Loop

Post by dragonwarrior »

Ok, so I read up on NMI and interrupts because I hadn't fully implemented them.

This means that the NMI flag in CTRL is set so when vblank is set at the end of rendering for the STATUS register, NMI occurs right? Then, as you said some game logic happens there?

Can NMI happen if the vblank flag in the STATUS register is set after a write to the CTRL register that sets the NMI flag?

As an aside, do you know if there's a way to check PPU registers in the FCEUX emulator?

Thanks a lot!
Fiskbit
Site Admin
Posts: 1139
Joined: Sat Nov 18, 2017 9:15 pm

Re: NES Emulation - DK infinite Loop

Post by Fiskbit »

An NMI occurs every single time the bitwise AND of bit 7 of PPUCTRL and bit 7 of PPUSTATUS becomes 1. This normally happens when the vblank flag becomes set, but can also happen if you turn NMI off and back on while the vblank flag is still set.

I don't know about FCEUX, but Mesen (which is more accurate and has better developer tools) shows this information both in the debugger and the register viewer.
dragonwarrior
Posts: 8
Joined: Sat Feb 01, 2025 3:30 pm

Re: NES Emulation - DK infinite Loop

Post by dragonwarrior »

Alright thanks a lot, I'll give make some changes and see how it goes.
dragonwarrior
Posts: 8
Joined: Sat Feb 01, 2025 3:30 pm

Re: NES Emulation - DK infinite Loop

Post by dragonwarrior »

That was it! I inplemented interrupts and fixed a couple of things and got to render my first frame.
Post Reply