It is currently Mon Oct 16, 2017 11:07 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Wed Apr 26, 2017 8:15 am 
Offline

Joined: Wed Apr 26, 2017 8:02 am
Posts: 4
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

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


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!


Top
 Profile  
 
PostPosted: Wed Apr 26, 2017 8:24 am 
Offline
User avatar

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 10046
Location: Rio de Janeiro - Brazil
viktor77 wrote:
Why sourceHigh is not used (only sourceLow is being used) to fetch background tiles in DrawColumnLoop ?

It's implicit. You only need to tell the CPU where the lower byte of the pointer is, and it knows that the next byte contains the rest of the pointer. Both bytes are being used but since pointers are always contiguous the instruction only needs to indicate where the pointer starts.


Top
 Profile  
 
PostPosted: Wed Apr 26, 2017 8:28 am 
Offline

Joined: Wed Apr 26, 2017 8:02 am
Posts: 4
Thank you tokumaru for such a quick and accurate answer! Now I can keep on, thanks again!


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

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 5 guests


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