This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Loading background from .word addresses pointers

Loading background from .word addresses pointers
by on (#158734)
I have a db(s) of my backgrounds (rooms) like this:

rooms_db:
.word Room0, Room1

where Room0 is simply like:

Room0:
.db $27,$27,$27,$27,$27,$27,$27,$27,$27,$27, etc.

Room1:
.db $27,$27,$27,$27,$27,$27,$27,$27,$27,$27, etc.

I cannot make work my load background routine from those pointers in rooms_db, load code is:

LoadBackground:

LDA #$00 ; Turn off the PPU in the Main Code First
STA $2001

LDA $2002 ; read PPU status to reset the high/low latch
LDA #$20
STA $2006 ; write the high byte of $2000 address
LDA #$00
STA $2006 ; write the low byte of $2000 address

LDX #$00

LDA #LOW(rooms_db)
STA pointerLo ; put the low byte of the address of background into pointer
LDA #HIGH(rooms_db)
STA pointerHi ; put the high byte of the address into pointer
LDX #$00 ; start at pointer + 0
LDY #$00
OutsideLoop:

InsideLoop:
LDA [pointerLo], y ; copy one background byte from address in pointer plus Y
STA $2007 ; this runs 256 * 4 times

INY ; inside loop counter
CPY #$00
BNE InsideLoop ; run the inside loop 256 times before continuing down

INC pointerHi ; low byte went 0 to 256, so high byte needs to be changed now

INX
CPX #$04
BNE OutsideLoop ; run the outside loop 256 times before continuing down

RTS

With that I was expecting to load Room0, just as if I explicitly hard coded it instead of "rooms_db"
Re: Loading background from .word addresses pointers
by on (#158737)
I think you are looking for something like...
Code:
lda room
asl ; A = room * 2 (each entry in rooms_db is 2 bytes)
tax
lda rooms_db+0, X ; low byte
sta pointer+0
lda rooms_db+1, X ; high byte
sta pointer+1
Re: Loading background from .word addresses pointers
by on (#158767)
Thanks Rainwarrior, I just implemented this at it work great!