Load Background loop

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
iGonzalez
Posts: 25
Joined: Mon Aug 13, 2018 5:10 am
Location: Notts

Load Background loop

Post by iGonzalez » Sun Dec 30, 2018 10:38 am

Hello all!

I'm following the nerdy nights tutorials, but in the background section I can not find how to make the loop to pass for the 960 bytes, I mean, I have this code:

Code: Select all

LoadBackground:
  LDA $2002             
  LDA #$20
  STA $2006            
  LDA #$00
  STA $2006            
  LDX #$00             
@loop:
  LDA background, x    
  STA $2007           
  INX                 
  CPX #$A0             
  BNE @loop 	
so using CPX #$A0 I will go over the first 5 rows, 160 bytes.
The problem that I found is that if I want to go over the 960 bytes.... I don't know how to make the loop to go over 255 bytes (anything bigger than FF).
I'm sure this smells like a proper rookie question, but I can not find an answer, haha

Thanks in advance! :D

User avatar
pubby
Posts: 549
Joined: Thu Mar 31, 2016 11:15 am

Re: Load Background loop

Post by pubby » Sun Dec 30, 2018 11:37 am

Easiest way is to code 4 loops, each offset by 256 bytes.

User avatar
tokumaru
Posts: 11502
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Load Background loop

Post by tokumaru » Sun Dec 30, 2018 11:47 am

4 loops is the quick answer, but eventually you'll want to look at indirect indexed addressing (i.e. LDA ($XX), Y). Instead of using a hardcoded base address, this addressing mode uses a 16-bit value in zero page as the base address, which means you can update the address once you exhaust the range that Y alone can cover. The address may double as a counter if it's aligned correctly, or you can have a separate variable to help with that.

iGonzalez
Posts: 25
Joined: Mon Aug 13, 2018 5:10 am
Location: Notts

Re: Load Background loop

Post by iGonzalez » Sun Dec 30, 2018 12:11 pm

Nice! I will go for the 4 loops now and then check the other option in the future. Thanks guys!

User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 490
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: Load Background loop

Post by Jarhmander » Sun Dec 30, 2018 12:18 pm

Are you familiar with the various indirect addressing mode? That's because you will need to deal with 16-bit quantities, and indexed addressing won't cut it, because it can only be used with 8-bit indexes.

There are several ways to do this, but essentially, you need to put a pointer in zero page, have some sort of counter and iterate until done. The counter will have to be some sort of 16-bit variable, obviously.

Code: Select all

    lda $2002 ; this is just to reset the upper/lower byte latch, btw
    lda #$20
    sta $2006
    lda #$00
    sta $2006 ; hardcoded to load into $2000 in PPU space, per your code
    lda #<background
    sta zptemp+0
    lda #>background
    sta zptemp+1
    ldy #0
    ldx #0
@loop:
    lda (zptemp), y
    sta $2007
    iny
    bne @loop_skip_inx
    inx
    inc zptemp+1
@loop_skip_inx
    cpx #>960
    bne @loop
    cpy #<960
    bne @loop
There are several smallish optimizations possible in this code, but I preferred to let them aside so you can understand the code better. But even then, it's possible you don't understand some parts of this code, especially if you don't understand indirect indexed addressing, how status flags work, etc. Just tell what's not clear and we'll answer your questions. And a warning: I haven't tested that code, but I'm confident it works, although rendering must be disabled when using this code, because it will run long enough to be longer than V-blank and things will break badly if you're still manipuling PPU memory while the PPU is rendering.

On a related note: I'm surprised I didn't find this on the wiki (code that uploads an entire nametable, well, without the attribute table).

P.S.: it's really painful to reply on a mobile device...
((λ (x) (x x)) (λ (x) (x x)))

iGonzalez
Posts: 25
Joined: Mon Aug 13, 2018 5:10 am
Location: Notts

Re: Load Background loop

Post by iGonzalez » Mon Dec 31, 2018 10:12 am

Jarhmander wrote:On a related note: I'm surprised I didn't find this on the wiki (code that uploads an entire nametable, well, without the attribute table).
Yes! I thought that too, haha.

Thanks for your post, I will take time with the code and try to understand it. I read about indirect addressing, but let face it, I don't understand it properly yet, haha, so I need to read more about and spend proper time with it.

Thank you guys!

Post Reply