A C64 Game - Step 39


And now technically a rather small step, but with huge impact: Music.

For now we're using a demo song from GoatTracker. GoatTracker is a PC tracker tool that lets you compose songs and provides the player source. It's most common for music players to be stored at $1000. Since musicians on the C64 usually need to be programmers as well the music mostly comes with the player code.



At the beginning we initialise the player.

          ;set volume to max
lda #15
sta 53248

;initialise music player (play song #0)
lda #0
jsr MUSIC_PLAYER



The song and player code can be exported to a binary blob, this is simply included in the source:

* = $3000
MUSIC_PLAYER
!binary "gt2music.bin"



Once a frame we call the play music routine to advance the music:

;------------------------------------------------------------
;wait for the raster to reach line $f8
;this is keeping our timing stable
;------------------------------------------------------------
!zone WaitFrame
WaitFrame
;are we on line $F8 already? if so, wait for the next full screen
;prevents mistimings if called too fast
lda $d012
cmp #$F8
beq WaitFrame

;wait for the raster to reach line $f8 (should be closer to the start of this line this way)

.WaitStep2
lda $d012
cmp #$F8
bne .WaitStep2

;play music
jsr MUSIC_PLAYER + 3
rts



That's it. Easy as pie.


Generally you may have to look out as the music player can use other code addresses (usually some zero page bytes) which may interfere with the main game code. This especially applies if you've got interrupt code running.

Have fun!


step39.zip