Hi everyone,
I'm a web developer and NES gamer who suddendly started wondering how those amazing games were coded. I've been studing Nerdy Nights tutorials and I have a question about one line of code I hope someone can ask. It's the tutorial about scrolling, example 4. Code is as follows
It took me two hours to figure it out but I've still a question. The line I don't understand is this one near the bottom:
LDA [sourceLow], y
The question is:
Why sourceHigh is not used (only sourceLow is being used) to fetch background tiles in DrawColumnLoop ?
Maybe I don't understand the code but previously you add #LOW(columnData) to sourceLow and #HIGH(columnData) to sourceHigh to get the address but then I see that only sourceLow is being used.
Any reply would be really appreciated. Thanks to all!
I'm a web developer and NES gamer who suddendly started wondering how those amazing games were coded. I've been studing Nerdy Nights tutorials and I have a question about one line of code I hope someone can ask. It's the tutorial about scrolling, example 4. Code is as follows
Code:
DrawNewColumn:
LDA scroll ; calculate new column address using scroll register
LSR A
LSR A
LSR A ; shift right 3 times = divide by 8
STA columnLow ; $00 to $1F, screen is 32 tiles wide
LDA nametable ; calculate new column address using current nametable
EOR #$01 ; invert low bit, A = $00 or $01
ASL A ; shift up, A = $00 or $02
ASL A ; $00 or $04
CLC
ADC #$20 ; add high byte of nametable base address ($2000)
STA columnHigh ; now address = $20 or $24 for nametable 0 or 1
LDA columnNumber ; column number * 32 = column data offset
ASL A
ASL A
ASL A
ASL A
ASL A
STA sourceLow
LDA columnNumber
AND #%11111000
LSR A
LSR A
LSR A
STA sourceHigh
LDA sourceLow ; column data start + offset = address to load column data from
CLC
ADC #LOW(columnData)
STA sourceLow
LDA sourceHigh
ADC #HIGH(columnData)
STA sourceHigh
DrawColumn:
LDA #%00000100 ; set to increment +32 mode
STA $2000
LDA $2002 ; read PPU status to reset the high/low latch
LDA columnHigh
STA $2006 ; write the high byte of column address
LDA columnLow
STA $2006 ; write the low byte of column address
LDX #$1E ; copy 30 bytes
LDY #$00
DrawColumnLoop:
LDA [sourceLow], y
STA $2007
INY
DEX
BNE DrawColumnLoop
RTS
LDA scroll ; calculate new column address using scroll register
LSR A
LSR A
LSR A ; shift right 3 times = divide by 8
STA columnLow ; $00 to $1F, screen is 32 tiles wide
LDA nametable ; calculate new column address using current nametable
EOR #$01 ; invert low bit, A = $00 or $01
ASL A ; shift up, A = $00 or $02
ASL A ; $00 or $04
CLC
ADC #$20 ; add high byte of nametable base address ($2000)
STA columnHigh ; now address = $20 or $24 for nametable 0 or 1
LDA columnNumber ; column number * 32 = column data offset
ASL A
ASL A
ASL A
ASL A
ASL A
STA sourceLow
LDA columnNumber
AND #%11111000
LSR A
LSR A
LSR A
STA sourceHigh
LDA sourceLow ; column data start + offset = address to load column data from
CLC
ADC #LOW(columnData)
STA sourceLow
LDA sourceHigh
ADC #HIGH(columnData)
STA sourceHigh
DrawColumn:
LDA #%00000100 ; set to increment +32 mode
STA $2000
LDA $2002 ; read PPU status to reset the high/low latch
LDA columnHigh
STA $2006 ; write the high byte of column address
LDA columnLow
STA $2006 ; write the low byte of column address
LDX #$1E ; copy 30 bytes
LDY #$00
DrawColumnLoop:
LDA [sourceLow], y
STA $2007
INY
DEX
BNE DrawColumnLoop
RTS
It took me two hours to figure it out but I've still a question. The line I don't understand is this one near the bottom:
LDA [sourceLow], y
The question is:
Why sourceHigh is not used (only sourceLow is being used) to fetch background tiles in DrawColumnLoop ?
Maybe I don't understand the code but previously you add #LOW(columnData) to sourceLow and #HIGH(columnData) to sourceHigh to get the address but then I see that only sourceLow is being used.
Any reply would be really appreciated. Thanks to all!