Quantcast
Channel: nesdev.org
Viewing all articles
Browse latest Browse all 785

Help with 2-byte indexing lookup table

$
0
0
Hello,

I am going through some code from the "Nerdy Nights" tutorial. In it there is a piece of code that I am having trouble understanding, which uses a 2 byte lookup table for indexing.

Here is part of the lookup table and the code;

Code:

note_table:    .word $07F1, $0780, $0713    .word $06AD, $064D, $05F3, $059D, $054D, $0500, $04B8, $0475, $0435, $03F8, $03BF, $0389    lda #$0C              ;the 13th entry in the table (A2)    asl a                   ;multiply by 2 because we are indexing into a table of words    tay    lda note_table, y     ;read the low byte of the period    sta $4002               ;write to SQ1_LO    lda note_table+1, y  ;read the high byte of the period    sta $4003               ;write to SQ1_HI
So, we need to access two bytes from the lookup table to store into these registers ($4002 & $4003). to play a musical note.

In the code above, why can't we just load the y register with the value #$18 (24 decimal), rather than;
lda #$0C ;load a with 12
asl a ;shift left to multiple by 2, now A contains 24 dec (#$18)
tay
lda note_table, y ;read the low byte of the period, y = 24 and loads the value $03 from the note table above
sta $4002 ;write to SQ1_LO, write $03 to $4002
lda note_table+1, y ;read the high byte of the period
sta $4003 ;write to SQ1_HI, write second byte $f8 (from note table) to $4003

Thanks for any help anyone might be able to offer.
TonyAme

Statistics: Posted by trony — Mon Mar 25, 2024 11:29 pm — Replies 1 — Views 34



Viewing all articles
Browse latest Browse all 785

Latest Images

Trending Articles



Latest Images