drogon wrote:
I know nothing about the NES, however...
Could it be that this code is/was part of a subroutine, so the calling code can just pass in the note to play and have the subroutine do the rest?
But then the calling code could pass in the doubled value too...
Maybe the calling code just increments a single byte sequence counter to play a tune?
-Gordon
Pretty sure this is the case where they took a function that was designed to be called with a parameter as Gordon said.
At the extreme end of this you could just write the values out yourself without a look up table at all if you just needed to play that one note:
Code:
lda #$04
sta $4002
lda #$35
sta $4003
However that generally wouldn't be terribly useful if you're reading a set of notes from a music file, in which case you'd need to call the function to change the note as the music is playing.
Consider:
Code:
music_data:
.byte $01 $02 $03 (and so on)
note_table:
.word $07F1, $0780, $0713
.word $06AD, $064D, $05F3, $059D, $054D, $0500, $04B8, $0475, $0435, $03F8, $03BF, $0389
play_music:
lda #0
tax
rm_loop:
lda music_data, x
jsr play_note
inx
bcc rm_loop ; Let's pretend we have 256 notes for this example.
play_note:
asl a
tay
lda note_table, y
sta $4002
lda note_table+1, y
sta $4003
ret
Note I haven't actually tried this code, I just wrote it off the top of my head; I don't have anything setup for doing NES development. Hopefully it at least illustrates the idea.
Now, there is no reason you can't store your music with the values already doubled if you want to. And perhaps if you need to squeeze out some clock cycles for a game loop that wouldn't be a bad idea. After all the music in your game is not going to get read by anything but your game.
Edit:
I was thinking about this a bit further.
The NES/Famicom featured an expansion port on the bottom of the consoles back when they were released the 80s. (I don't think any of the newer slimmed down versions had it though). Given the reverse engineering I've seen on the NES, I would wager it was used primary for developers who needed to debug/test things, and I would not be at all shocked if somewhere, someplace, buried deep in the Nintendo dev kits of the day, there was a MIDI connector so you could use a MIDI keyboard for composing game music and hear it actually playing on the NES itself.
The MIDI standard defines 128 notes, fitting nicely in 7 bits. Which might explain where the original code may of come from. (Just food for thought)