A loop that writes to $2006 more than twice?

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Post Reply
User avatar
SusiKette
Posts: 103
Joined: Fri Mar 16, 2018 1:52 pm
Location: Finland

A loop that writes to $2006 more than twice?

Post by SusiKette » Fri Jul 05, 2019 12:53 am

I saw this loop in Rockman 3's reset code (not sure if Mega Man 3 has it, but it probably will). As far as I know only the first two writes to $2006 after reading $2002 have any significant results. The EOR in the loop is also a bit odd. It would write #$00 every other loop and #$10 on the others, but it still doesn't have any significant meaning what the loop is supposed to achieve.

Code: Select all

 LDA $2002
 LDA #$10
 TAY
 
Loop:
 STA $2006
 STA $2006
 EOR #$10
 DEY
 BNE Loop
I'm not sure if the second write to $2006 resets the latch similar to reading $2002, but according to FCEUX it does since the PPU address changes between $0000 and $1010.

EDIT: I should also add that there are no writes to $2007 anywhere near this loop
Last edited by SusiKette on Fri Jul 05, 2019 1:45 am, edited 1 time in total.

lidnariq
Posts: 8690
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: A loop that writes to $2006 more than twice?

Post by lidnariq » Fri Jul 05, 2019 1:20 am

SusiKette wrote:As far as I know only the first two writes to $2006
If rendering is disabled, only the last two writes will have any significant results. (nesdevwiki:PPU scrolling)
after reading $2005
$2005 is a write-only register. They might have meant to type $2002...
it still doesn't have any significant meaning what the loop is supposed to achieve.
It doesn't obviously do anything useful.

Even if rendering were enabled, the main loop there is only 15 cycles long, or 45 pixels: far too fast to be anything interesting graphically.

User avatar
SusiKette
Posts: 103
Joined: Fri Mar 16, 2018 1:52 pm
Location: Finland

Re: A loop that writes to $2006 more than twice?

Post by SusiKette » Fri Jul 05, 2019 1:48 am

It was supposed to be $2002. That was my mistake. Still there is no reason I know of to write useless code.

User avatar
tokumaru
Posts: 11437
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: A loop that writes to $2006 more than twice?

Post by tokumaru » Fri Jul 05, 2019 4:14 am

We have a far better understanding of the intricacies of the PPU today than developers back in the day had. It's not unusual to find useless PPU accesses in commercial games.

User avatar
Quietust
Posts: 1487
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: A loop that writes to $2006 more than twice?

Post by Quietust » Fri Jul 05, 2019 5:12 pm

This logic is actually quite common in games which used the MMC3. Its actual function is to manually clock the IRQ counter 8 times (by toggling the VRAM address between $0000 and $1010), though we don't actually know why this was done - perhaps it was for compatibility with older chip revisions, to make sure the counter was initialized properly.

Interestingly, there are also quite a few games (including Super Mario Bros. 3) which used a broken version of that code, performing EOR #$00 instead of EOR #$10 - given that these games seemed to work just fine, this would be consistent with the idea of the code being used for compatibility with older (perhaps pre-production) chips.
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.

Post Reply