It is currently Sun Jan 20, 2019 12:16 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: Load Background loop
PostPosted: Sun Dec 30, 2018 10:38 am 
Offline
User avatar

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


Top
 Profile  
 
 Post subject: Re: Load Background loop
PostPosted: Sun Dec 30, 2018 11:37 am 
Offline
User avatar

Joined: Thu Mar 31, 2016 11:15 am
Posts: 464
Easiest way is to code 4 loops, each offset by 256 bytes.


Top
 Profile  
 
 Post subject: Re: Load Background loop
PostPosted: Sun Dec 30, 2018 11:47 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 11094
Location: Rio de Janeiro - Brazil
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.


Top
 Profile  
 
 Post subject: Re: Load Background loop
PostPosted: Sun Dec 30, 2018 12:11 pm 
Offline
User avatar

Joined: Mon Aug 13, 2018 5:10 am
Posts: 25
Location: Notts
Nice! I will go for the 4 loops now and then check the other option in the future. Thanks guys!


Top
 Profile  
 
 Post subject: Re: Load Background loop
PostPosted: Sun Dec 30, 2018 12:18 pm 
Offline
Formerly ~J-@D!~
User avatar

Joined: Sun Mar 12, 2006 12:36 am
Posts: 479
Location: Rive nord de Montréal
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:
    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)))


Top
 Profile  
 
 Post subject: Re: Load Background loop
PostPosted: Mon Dec 31, 2018 10:12 am 
Offline
User avatar

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


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

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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