Page 1 of 2

Scrolling

Posted: Sun Nov 26, 2006 3:54 pm
by CartCollector
When I scroll the background, it moves 32 pixels up from what it's supposed to be at. It only moves up once the scrolling starts. It does not move up continually, and if the scrolling stops, it still stays up. I've tested it on recent versions of Nintendulator, Nestopia, FCE Ultra, and RockNES, and this occurs on all of them. Does anyone know what causes this?

Posted: Sun Nov 26, 2006 4:17 pm
by Quietust
What method are you using to scroll the screen? It's possible you're using the wrong method.

Posted: Sun Nov 26, 2006 4:49 pm
by CartCollector
I'm using this:

Code: Select all

lda #$00        ; Reset VRAM
sta $2006
sta $2006
	
inc Value      
ldx Value

stx $2005
sta $2005	;No vertical movement

Posted: Sun Nov 26, 2006 5:44 pm
by Celius
Nothing is wrong with the code you have presented. This code would work on each one of those emulators; the problem exists somewhere else. Have you checked the value of "Value" after it does this scroll jump?

Posted: Sun Nov 26, 2006 6:02 pm
by Disch
My first thought was that his scrolling code is right, but the code he has before he starts scrolling is wrong (ie, what he thinks is wrong is actually right, and what he is basing it on to be right is actually wrong)

What code are you using before you start scrolling (before the screen shifts up 32 pixels)

Posted: Mon Nov 27, 2006 10:00 am
by Bregalad
Most probably you set the scroll during the frame and not during VBlank. The twice write to $2006 set the vertical scroll to zero, but during the frame, it will shake the whole screen down. Then the $2005 writes setup the proper horizontal scrolling, and the vertical one is ignored because not refreshed until anything is written to $2006.

Posted: Mon Nov 27, 2006 8:50 pm
by CartCollector
Okay, I'm doing this after each NMI:

Code: Select all

inc Ticks
ldx Ticks
cpx #$08	;once every 8 frames
bne InfiniteLoop

inc Value

lda #$00
sta Ticks
sta $2006
sta $2006
	      
ldx Value

stx $2005
sta $2005	;No vertical movement
	
jmp InfiniteLoop
I'm using NESASM, if that helps anyone.

Posted: Tue Nov 28, 2006 10:04 am
by Bregalad
Well, it's recommanded you write to $2005 DURING the NMI code so that you're sure it is during VBlank.

Posted: Tue Nov 28, 2006 11:50 am
by Disch
This is your scrolling code which looks like it would be working fine.

Which supports my earlier idea, that what you think the screen is "supposed to be" is actually wrong, and what you think is wrong is actually right.

What code are you using to draw before you start scrolling? You said that's when it shows up "right" -- so that's probably where the problem is.

Or just upload the whole ROM or source somewhere and link us to it. It's really hard to remotely debug something with only small code snippits.

Posted: Tue Nov 28, 2006 5:38 pm
by CartCollector
Okay, I'll upload the ROM. But where can I get free file hosting without spam?

As for "right" being "wrong," I made the nametable using NSA, and it shows up there the same way it shows up when the screen is not scrolled. But whenever I scroll the screen, it suddenly jumps.

And for the "during the frame" hypothesis, I do all of this code right after the NMI is triggered. There is nothing else going on.

Posted: Wed Nov 29, 2006 11:01 am
by Disch
CartCollector wrote:Okay, I'll upload the ROM. But where can I get free file hosting without spam?
Geocities/Angelfire/etc

just don't give them your real e-mail address and you won't get spam. Who cares if you have ads on the site if you're only using it for file storage.

Posted: Wed Nov 29, 2006 9:24 pm
by CartCollector
Here's the full source code. GeoCities wouldn't let me upload any .nes, .chr, or .nam files, because they had "invalid filenames." You could use any 8k .chr and 1k .nam with that source, assemble it through NESASM, and get the same results I'm getting.

Posted: Wed Nov 29, 2006 9:31 pm
by Quietust
Ah, I see what's going on.
Your VRAM address got left at $3F10 after writing to the palette, which is effectively moving the viewport upward. You must initialize the VRAM address before you enable rendering, and it also doesn't hurt to set it on every frame.

In short, Disch was right.

Posted: Wed Nov 29, 2006 9:43 pm
by Disch
You could .zip things up, you know ;P

Anyway -- having looked it over:

- in your 'ClrMem' routine you're performing STA's without ever having set A to anything (no prior LDA command). So you're effectively writing garbage instead of $00

- You're having NMIs occur, but you never RTI, you just jump back to your infinite loop, which will cause your stack to overflow like mad (run your ROM in FCEUXD and look at RAM at $0100-01FF in the memory viewer / hex editor). This will still work for what you're doing now -- but it's bad practice and will probably cause problems later when you're doing more things with the stack.

Always exit your NMI with an RTI.

- (Probalby the cause to the problem you're talking about): You're never setting the scroll to any initial value... so the screen will be starting from a "garbage starting point". So for the first 8 frames, the scroll is screwed. I believe you left the PPU address at $3F20 when you turned the screen on -- so that's like starting with a Y scroll of 11. It isn't until 8 frames in where you reset that scroll to zero -- which is why your screen is shifting up by ~11 pixels when the screen starts scrolling

So I was right before -- your scrolling is correct -- and what you're thinking is "right" is actually a little off.

To remedy this -- set the scroll before turning the screen on. You can do this by writing zero to $2006 twice.

- Also -- to avoid one frame of flicker -- you might want to wait until VBlank before turning the screen on. If you turn the frame on in the middle of rendering time, the screen will start rendering halfway down the frame (will look weird).


EDIT -- I'm too slow! Q is too quick ;D and whoops -- I thought you were starting at $3F20 but Q is right about $3F10 -- which would only be a Y scroll of 3?

Posted: Wed Nov 29, 2006 11:20 pm
by Quietust
Disch wrote:$3F10 -- which would only be a Y scroll of 3?
Remember, $3F10 is $310 relative to that nametable. If he's using horizontal mirroring (which he probably is, otherwise he'd be seeing junk for the first 8 frames), that'll result in an effective Y-scroll of -40 (plus horizontally scrolled halfway).