How did the score bar in Super Mario Bros. not scroll?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

How did the score bar in Super Mario Bros. not scroll?

Post by DementedPurple »

So yeah, How would the scorebar not scroll with the camera? I know that It's in the nametable, because you wouldn't be able to fit that many sprites in a row. So then how does it not scroll? It can't move with the camera because while it can scroll smoothly, the nametable can only fit in one 8x8 box, so if they did this, it would slack behind a back before jumping back into place. I'm confused.
ReaperSMS
Posts: 174
Joined: Sun Sep 19, 2004 11:07 pm

Re: How did the score bar in Super Mario Bros. not scroll?

Post by ReaperSMS »

Sprite 0 is the bottom of the coin in the status bar. In NMI, it leaves the scroll set to (0,0), waits for sprite0 hit, waits a precise amount of time to get to hblank, and then sets the scroll value for the rest of the playfield.
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: How did the score bar in Super Mario Bros. not scroll?

Post by dougeff »

The PPU draws the screen line by line from the top, using the scroll registers to help it decide which pixels to draw.

The top of the screen, the scroll is set to 0,0. It waits until just the scoreboard is drawn on the screen, then changes the x scroll. From there on down, the PPU is getting pixels from further to the right.

Changing the x scroll mid-screen is easy, and can be done many times.
nesdoug.com -- blog/tutorial on programming for the NES
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How did the score bar in Super Mario Bros. not scroll?

Post by DementedPurple »

What's sprite zero hit? Did they just have to time the code in allingment with the TV?
User avatar
gauauu
Posts: 779
Joined: Sat Jan 09, 2016 9:21 pm
Location: Central Illinois, USA
Contact:

Re: How did the score bar in Super Mario Bros. not scroll?

Post by gauauu »

SMB 1's split scroll is the standard example for how NES split scrolling works. If you google, you'll find lots of answers, which might be preferable to asking it bit-by-bit here. Here's a long and detailed explanation.
User avatar
DRW
Posts: 2225
Joined: Sat Sep 07, 2013 2:59 pm

Re: How did the score bar in Super Mario Bros. not scroll?

Post by DRW »

DementedPurple wrote:What's sprite zero hit?
Instead of asking this question, you could have googled the term:
http://lmgtfy.com/?q=nes+sprite+zero+hit

The first link leads you to the definition of these words and explains what this does on the NES.
My game "City Trouble":
Gameplay video: https://youtu.be/Eee0yurkIW4
Download (ROM, manual, artworks): http://www.denny-r-walter.de/city.html
Drag
Posts: 1615
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: How did the score bar in Super Mario Bros. not scroll?

Post by Drag »

The NES can render 64 sprite tiles in one frame, named sprite 0 to sprite 63. The PPU can also detect when any non-transparent pixels of sprite 0 overlap any non-background-color pixels of the background. The moment a pixel is drawn on screen which contains both a sprite 0 pixel and a bg pixel overlapping, the PPU sets a flag in a register. Games will often wait in a loop, checking the register over and over, and once the register finally shows that flag being set, the game will change the scrolling registers. The effect is that the top of the screen is scrolled one way (in SMB's case, fixed at [0,0]), and the bottom of the screen is scrolled a different way (scrolled with the "camera"), with the "split" occuring at the location of sprite 0 on the screen.

As hinted, this is called a "screen split", and using sprite 0 like this is called the "sprite 0 hit", but you usually just need to say "sprite 0" and we'll know what you mean.
qfwfq
Posts: 20
Joined: Sun Aug 28, 2016 9:01 pm
Location: Seattle
Contact:

Re: How did the score bar in Super Mario Bros. not scroll?

Post by qfwfq »

gauauu wrote:SMB 1's split scroll is the standard example for how NES split scrolling works. If you google, you'll find lots of answers, which might be preferable to asking it bit-by-bit here. Here's a long and detailed explanation.
That StackOverflow answer is a gem. Thanks for sharing.
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How did the score bar in Super Mario Bros. not scroll?

Post by DementedPurple »

How would that work? Wouldn't writing to the PPU outside of vblank cause corruption?
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How did the score bar in Super Mario Bros. not scroll?

Post by tokumaru »

DementedPurple wrote:How would that work? Wouldn't writing to the PPU outside of vblank cause corruption?
Well, changing the scroll mid-screen can be seen as a form of corruption, since you are tearing the screen after all, but it's a predictable corruption that can be used to your advantage. If you don't do it at the correct times and places, you may get visual glitches, though.

If you understand how the PPU works and the consequences of accessing each of its registers, you can get away with some PPU manipulations outside of vblank, but since these techniques can be difficult to pull off, even for experienced coders, it's simpler for beginners to assume most of the PPU is off limits during rendering.

Some PPU parameters can be safely changed at any time though, such as the sprite height, pattern table addresses, color emphasis, grayscale mode and background and sprite masking in the leftmost 8 pixels, without screwing up the PPU's rendering process. If you time such changes correctly (usually you'd want them to happen during hblank), you can pull off some interesting effects without any fear of messing up the display.
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How did the score bar in Super Mario Bros. not scroll?

Post by DementedPurple »

Would it work if I just used a sprite-zero-hit? Speaking of sprite-zero-hit, it doesn't mention anywhere in the wiki how I would access it. It doesn't say anything about any memory addresses.
User avatar
dustmop
Posts: 136
Joined: Wed Oct 16, 2013 7:55 am

Re: How did the score bar in Super Mario Bros. not scroll?

Post by dustmop »

My 2016 compo entry used sprite 0 to implement its hud, and the source is on github. Check out https://github.com/dustmop/filthy-kitch ... isplay.asm

The function HudSplitAssign sets the position of sprite 0, and HudSplitWait waits for the hit to happen. You should try playing the game in FCEUX and seeing how the nametable viewer looks.
Last edited by dustmop on Wed May 03, 2017 11:35 am, edited 1 time in total.
User avatar
dougeff
Posts: 3079
Joined: Fri May 08, 2015 7:17 pm

Re: How did the score bar in Super Mario Bros. not scroll?

Post by dougeff »

Would it work if I...
Would what work?

If the PPU detects sprite #0 over the BG, it sets a bit in a memory address.

The program will have to look for the bit, and decide what to do.

The program also has to set the location of the sprite, which determines when the bit is set...and how far down you want to time an event.
nesdoug.com -- blog/tutorial on programming for the NES
DementedPurple
Posts: 318
Joined: Mon Jan 30, 2017 5:20 pm
Location: Colorado USA

Re: How did the score bar in Super Mario Bros. not scroll?

Post by DementedPurple »

dougeff wrote: it sets a bit in a memory address.
That's really vague, the NES has a lot of memory addresses. Does it depend on what sprite I'm using?
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: How did the score bar in Super Mario Bros. not scroll?

Post by tokumaru »

DementedPurple wrote:Would it work if I just used a sprite-zero-hit?
The sprite 0 hit by itself doesn't do anything, its purpose is to let the program know when a specific portion of the image is being drawn, so you can create effects that affect the correct part of the image.
Speaking of sprite-zero-hit, it doesn't mention anywhere in the wiki how I would access it. It doesn't say anything about any memory addresses.
The sprite 0 hit flag is part of the PPU status register, as documented in the wiki. You have to position the very first sprite (i.e. OAM entry 0) in a way that one of its solid pixels overlaps (or underlaps, depending on the state of the priority bit) a solid background pixel. When the PPU renders that pixel, the sprite 0 hit flag will be set, and the game code can detect that by polling the PPU status register ($2002) repeatedly.
Post Reply