Background is not aligned with the top of the screen

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
rodroddy
Posts: 2
Joined: Sat Jan 28, 2017 12:25 am

Background is not aligned with the top of the screen

Post by rodroddy »

An image, to illustrate what I am talking about:

Image

I am also not sure what the solid blue rectangle is doing at the top. Perhaps it is related, but I have only been doing NES programming for about three days so have no clue. The hollow blue square is a sprite that is being drawn as expected, the yellow filled square and the rest of the green grid are the background. As far as I understand my code, the green grid should be aligned with the top of the screen, instead of that empty black space that is occupying its position. As you can see it is running on Nestopia. I also ran it on nesDS (a NES emulator for the Nintendo DS) and it gave the same problem, except the grid was not pushed down as far.

My background drawing code:

Code: Select all

        lda $2002             ; read PPU status to reset the high/low latch
        lda #$20
        sta $2006             ; write the high byte of $2000 address
        sta pattern
        ldy #$00
        sty $2006             ; write the low byte of $2000 address
        lda #<background                                          
        sta base
        lda #>background
        sta base+1                                              
        ldx #$04
*       lda (base),y          ; load data from address (background + the value in x)
        sta $2007             ; write to PPU
        iny
        bne -                 ; Branch to LoadBackgroundLoop if compare was Not Equal to zero
        dex
        beq +
        inc base+1
        inc pattern
        lda pattern
        sta $2006
        sty $2006
        jmp -
I am using the Ophis assembler. I am not sure how common its syntax is, so *'s are anonymous labels, - references the nearest previous label, + the next nearest label. Example programs that I have assembled and run (though with a different assembler) have been able to display backgrounds aligned correctly. Is there anything obviously wrong with my code? Should I include some other code? Has anybody come across this before?
User avatar
rainwarrior
Posts: 8734
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Background is not aligned with the top of the screen

Post by rainwarrior »

Scrolling position on the NES usually takes a few attempts to understand.

I am not sure exactly what your problem is from the information you've given, but perhaps this article from our wiki will help:
http://wiki.nesdev.com/w/index.php/PPU_scrolling
rodroddy
Posts: 2
Joined: Sat Jan 28, 2017 12:25 am

Re: Background is not aligned with the top of the screen

Post by rodroddy »

rainwarrior wrote:Scrolling position on the NES usually takes a few attempts to understand.

I am not sure exactly what your problem is from the information you've given, but perhaps this article from our wiki will help:
http://wiki.nesdev.com/w/index.php/PPU_scrolling
It did help! I was turning off scrolling before drawing the background and I guess you cannot do that. Thank you very much.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Background is not aligned with the top of the screen

Post by tokumaru »

rodroddy wrote:I was turning off scrolling
Scrolling is not something you turn on and off. On the NES, "the scroll" is nothing more than the point at which rendering starts, i.e. the part of the name tables that will show up at the top left corner of the screen. Scrolling animations are created by changing that point over time (which the NES doesn't do automatically, the programmer has to do it step by step) and updating the name tables so new content is displayed as the screen moves.

That being said, the scroll has another important role on the NES. To simplify the design of the hardware, the PPU uses the same register (i.e. small amount of internal memory) to control the scroll and the VRAM address. So every time you use $2006 to set the VRAM address and write to (or read from) VRAM, you're messing up the scroll. For this reason, after every screen update, you're required to reset the scroll to the point you want, using PPU registers $2000 (the lower 2 bits select a name table) and $2005 (2 consecutive writes select the horizontal and vertical scroll, respectively). If you don't do that, rendering will start from a messed up location related to the state you left the PPU address register in after writing to VRAM.
Post Reply