Reason for multiple writes to PPUADDR after buffer transfer

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

Post Reply
hackfresh
Posts: 100
Joined: Sun May 03, 2015 8:19 pm

Reason for multiple writes to PPUADDR after buffer transfer

Post by hackfresh » Tue Jan 24, 2017 5:10 pm

Not exactly sure what the purpose of this is after reading through various docs. Maybe someone else can help. This is from an MMC3 game after transferring the buffer data to the PPU

Code: Select all

	
        LDA #$00					        ; SET BUFFER LENGTH/TYPE = EMPTY
	STA $037D						;
                                                                ; DUMMY WRITES?
        LDA #$3F						; SET PPU ADDRESS TO $3F00  = PAL RAM
	STA $2006						;
	LDA #$00						;
	STA $2006						;
	STA $2006						; SET PPU ADDRESS TO $0000  = PATTERN TABLE
	STA $2006						;


User avatar
rainwarrior
Posts: 8006
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by rainwarrior » Tue Jan 24, 2017 5:14 pm

The PPU address is used during rendering to fetch tiles from the nametables. This shares the same bus and register as the one you use to write data to the PPU. Thus, after you're done messing around with it for writing data, it needs to be set back up in the correct position for reading before rendering happens.

This article might help:
http://wiki.nesdev.com/w/index.php/PPU_scrolling

A write to $2000, $2005, $2005 before the end of vblank should normally be sufficient, though. Usually it's easier to set scroll through $2005 than $2006. Some programmers do seem to write extra zeroes to $2006 as an unnecessary measure of protection? (Maybe from bad experiences while learning.)

The oft-maligned Nerdy Nights tutorial does two redundant $2006 writes in its examples, which if you're talking about a homebrew game, they might have learned to do it from that example.

hackfresh
Posts: 100
Joined: Sun May 03, 2015 8:19 pm

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by hackfresh » Tue Jan 24, 2017 5:34 pm

I understand why it is/might be necessary do the second write of all zeroes to PPUADDR. The write of $3F00 seems totally unneeded unless I'm misunderstanding something.

This is actually code from a commercial game.

User avatar
rainwarrior
Posts: 8006
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by rainwarrior » Tue Jan 24, 2017 5:37 pm

You didn't say what game the code was from, so I couldn't tell if you were talking about contiguous instructions or not.

If there's nothing in between the $3F00 and $0000 then yes, it's completely redundant and pointless, though it's not particularly surprising to hear about vestigial useless code in a commercial game. That happens all the time.

hackfresh
Posts: 100
Joined: Sun May 03, 2015 8:19 pm

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by hackfresh » Tue Jan 24, 2017 6:43 pm

Yup my bad. Thanks for the answer. That makes sense.

tepples
Posts: 22288
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by tepples » Tue Jan 24, 2017 7:04 pm

The game where I first saw this was among the first NES games to have a top status bar. I bet back in 1985, not even Nintendo's programmers were sure exactly how the VRAM address worked.

User avatar
Dwedit
Posts: 4412
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by Dwedit » Tue Jan 24, 2017 7:47 pm

Devil World scrolled vertically and horizontal with two splits, and that was before 1985. But they didn't know how to do both coarse and fine vertical scrolling then.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

User avatar
rainwarrior
Posts: 8006
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by rainwarrior » Tue Jan 24, 2017 7:52 pm

hackfresh wrote:Yup my bad.
I don't think you did anything wrong by hiding the name of the game you're talking about, but it's not normally taboo here to mention games that you're debugging/disassembling. You're not allowed to post pirated ROMs, etc. but we talk about emulating specific games all the time.

I mean, if you have a good reason for it, I don't want to pry. Tepples apparently knows what game it is but has respected your intent to keep it unnamed.

User avatar
Bregalad
Posts: 8008
Joined: Fri Nov 12, 2004 2:49 pm
Location: Chexbres, VD, Switzerland

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by Bregalad » Wed Jan 25, 2017 12:05 am

Actually it's pretty standard for commercial games to write $3f00 and then $0000 to $2006. So many games does this I don't think it's relevant to mention a specific game.

Setting the adress to $3f00 ensures that color #0 is used for the backdrop. Setting it to $0000 however I don't see the point, perhaps to set the scroll to (0, 0).

hackfresh
Posts: 100
Joined: Sun May 03, 2015 8:19 pm

Re: Reason for multiple writes to PPUADDR after buffer trans

Post by hackfresh » Wed Jan 25, 2017 12:43 am

The game in question is Tecmo Super Bowl. I have it basically 95+% commented and reverse engineered at this point. That was one of the few things I wasn't quite sure what the purpose was especially with both writes back to back.

Post Reply