It is currently Sat Dec 16, 2017 5:56 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sun Nov 20, 2016 10:03 pm 
Offline

Joined: Sun Nov 20, 2016 9:32 pm
Posts: 6
Hello. I have been working on my own NES emulator project, and thus far I've been able to make sense of the NES's behavior thanks to NES DEV's documentation. However, today I have been digging into Super Mario Bros in FCEUX in order to diagnose a bug in my emulator, where SMB freezes the first time the upper left corner of the screen moves into nametable 1. I eventually noticed that in some frames, SMB writes a 1 to the nametable select on scanline 247, but the status bar continues to be rendered from nametable 0 as normal. I've looked all over for an explanation of this, but I can't figure it out. I figure I must be misunderstanding how the PPU uses the nametable select bits, or else I'm just making some kind of silly mistake. Any insights into this problem would be much appreciated.


Top
 Profile  
 
PostPosted: Sun Nov 20, 2016 10:22 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5899
Location: Canada
I don't know what SMB does specifically, but the nametable select bit can also be set by writing to $2006.

More info: Wiki: PPU Scrolling


Top
 Profile  
 
PostPosted: Sun Nov 20, 2016 10:25 pm 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10165
Location: Rio de Janeiro - Brazil
I just checked and the game does indeed write $00 to $2006 twice after the $2000 write.


Top
 Profile  
 
PostPosted: Sun Nov 20, 2016 10:40 pm 
Offline

Joined: Sun Nov 20, 2016 9:32 pm
Posts: 6
Hmmm. Wouldn't v's nametable select bits be set to the expected value when it's reset during rendering? The way I'm doing it now is to directly set v's bit 10 to PPUCTRL's bit 0 on every horizontal reset, and set v's bit 11 to PPUCTRL's bit 1 on every vertical reset. It seems like this doesn't work, but something like this must be happening in order for the game to change nametables during rendering. My best guess is that it has something to do with how v, t, and PPUCTRL interact, but I haven't been able to find the fine details on this.

In any case, it was my assumption that changes to v during vblank are supposed to be overwritten at the start of rendering, but I might be missing something.


Top
 Profile  
 
PostPosted: Sun Nov 20, 2016 10:54 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5899
Location: Canada
The timing for when and what parts of t are copied to v are in that article, but to summarize:

In response to CPU writes:
  • Second write to $2006: immediately copies t to v.
  • Read or write to $2007: increment appropriately.
If rendering is enabled:
  • End of vblank: copies Y bits of t to v from pixels 280 to 304 of the pre-render scanline (i believe it redundantly executes the copy once per pixel in that range?)
  • End of scanline, dot 256: increment Y bits in v
  • End of scanline, dot 257: copy X bits of t into v (also happens on pre-render scanline)
  • Throughout scanline: increment X bits of v as needed to fetch tiles

$2000 and $2005 only write to t, they do not immediately affect v.

There's a lot more detail about all of this in the article.


Last edited by rainwarrior on Mon Nov 21, 2016 5:49 pm, edited 2 times in total.

Top
 Profile  
 
PostPosted: Sun Nov 20, 2016 11:04 pm 
Offline

Joined: Sun Nov 20, 2016 9:32 pm
Posts: 6
rainwarrior wrote:
$2000 and $2005 only write to t, they do not immediately affect v.


Aha. That's the part I was missing; I wrongly assumed that $2000 held onto the nametable select bits to be used later, rather than writing to t immediately. Thanks for the help!

Edit: I do feel a bit sheepish now that I finally spotted the explanation in the PPU scrolling article. I swear I looked for it several times. I guess that's what I get for skimming a bit too lightly. Thank you for being patient with me!


Top
 Profile  
 
PostPosted: Sun Nov 20, 2016 11:32 pm 
Offline
User avatar

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 3192
Location: Mountain View, CA, USA
...and that's exactly why several of us wrote out the "long example", step-by-step, of what the PPU is doing (re: several internal variables/bits) alongside the relevant 6502 instructions. :-) It's very easy to get this wrong/mess it up (both from an emulation perspective and from a coding/game developer perspective).


Top
 Profile  
 
PostPosted: Sun Nov 20, 2016 11:38 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5899
Location: Canada
Sarospa wrote:
I do feel a bit sheepish now that I finally spotted the explanation in the PPU scrolling article. I swear I looked for it several times. I guess that's what I get for skimming a bit too lightly. Thank you for being patient with me!

Stuff that you know most of the details of is often the hardest thing to read clearly. ;)


Top
 Profile  
 
PostPosted: Mon Nov 21, 2016 12:07 am 
Offline

Joined: Sun Nov 20, 2016 9:32 pm
Posts: 6
koitsu wrote:
...and that's exactly why several of us wrote out the "long example", step-by-step, of what the PPU is doing (re: several internal variables/bits) alongside the relevant 6502 instructions. :-) It's very easy to get this wrong/mess it up (both from an emulation perspective and from a coding/game developer perspective).


I'm definitely grateful for all the documentation on NES DEV. I couldn't have gotten this far without it. It really is amazing to see Super Mario Bros running on my own emulator. So thanks for all the work you guys have put into documenting this stuff!


Top
 Profile  
 
PostPosted: Mon Nov 21, 2016 5:04 pm 
Offline
User avatar

Joined: Sun Sep 19, 2004 10:59 pm
Posts: 1393
rainwarrior wrote:
End of vblank: copies t to v from pixels 280 to 304 of the pre render scanline (i believe it redundantly executes the copy once per pixel in that range?)

It should be noted that only the Y bits get copied during those pixels, since the X bits get copied at dot 257.

_________________
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.


Top
 Profile  
 
PostPosted: Mon Nov 21, 2016 5:48 pm 
Offline
User avatar

Joined: Sun Jan 22, 2012 12:03 pm
Posts: 5899
Location: Canada
Thanks, amended.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group