; Example of using playroutine 2 (gamemusic)

                processor 6502

mt_musicdata    = $1000                         ;Defines for the gamemusic
mt_zpbase       = $fc                           ;player

                org $0800

                include ..\..\gmusic.s          ;Include the gamemusic player

                org mt_musicdata

                incbin example2.bin             ;Include the raw music data

mt_sfxtbllo:    dc.b <arpeggio2                 ;Then our sound FX table
                dc.b <arpeggio1                 ;(where this or the sound
                dc.b <gunshot                   ;effects are located, doesn't
                dc.b <explosion                 ;matter)

mt_sfxtblhi:    dc.b >arpeggio2
                dc.b >arpeggio1
                dc.b >gunshot
                dc.b >explosion

gunshot:        include sfx_gun.s               ;The order in memory doesn't
explosion:      include sfx_expl.s              ;matter, only the order in the
arpeggio1:      include sfx_arp1.s              ;sound FX table
arpeggio2:      include sfx_arp2.s

                org $2000

getin           = $ffe4

start:          jsr relocatemusic
                jsr initraster
                lda #22
                sta $d018
                lda #$01
                jsr playtune                    ;Initialize subtune 1
                ldx #$00                        ;(subtune 0 is silence)
                lda #$20
clearscreen:    sta $0400,x
                sta $0500,x
                sta $0600,x
                sta $0700,x
                inx
                bne clearscreen
textloop:       lda text,x
                jsr convertascii
                sta $0400,x
                lda #$01
                sta $d800,x
                inx
                cpx #4*40
                bcc textloop
idle:           lda currraster
                ldx #52
                jsr printhex
                lda maxraster
                ldx #55
                jsr printhex
                jsr getin
                cmp #"A"
                bcc idle
                cmp #"D"+1
                bcs idle
playsound:      sec
                sbc #"A"                        ;Sound effect number 0-3
                ldx #$02                        ;Play on channel 3
                jsr playsfx
                jmp idle

printhex:       pha
                lsr
                lsr
                lsr
                lsr
                tay
                lda hexnybble,y
                jsr convertascii
                sta $0400,x
                pla
                and #$0f
                tay
                lda hexnybble,y
                jsr convertascii
                sta $0401,x
                rts

convertascii:   cmp #$60
                bcc ca_ok
                sbc #$60
ca_ok:          rts

initraster:     sei
                lda #<raster
                sta $0314
                lda #>raster
                sta $0315
                lda #50                         ;Set low bits of raster
                sta $d012                       ;position
                lda $d011
                and #$7f                        ;Set high bit of raster
                sta $d011                       ;position (0)
                lda #$7f                        ;Set timer interrupt off
                sta $dc0d
                lda #$01                        ;Set raster interrupt on
                sta $d01a
                lda $dc0d                       ;Acknowledge timer interrupt
                cli
                rts

raster:         nop
                nop
                nop
                nop
                nop
                nop
                nop
                lda $d012
                sta raster_cmp+1
                inc $d020
                jsr music                       ;Play music and measure
                lda $d012                       ;rastertime it took to
                dec $d020                       ;execute
                sec
raster_cmp:     sbc #$00
                sta currraster
                cmp maxraster
                bcc raster_skip
                sta maxraster
raster_skip:    dec $d019
                jmp $ea31

text:           dc.b "Example 2: Game-musicroutine            "
                dc.b "Rastertime:   /                         "
                dc.b "                                        "
                dc.b "Press A-D for sound effects.            "

hexnybble:      dc.b "0123456789ABCDEF"

maxraster:      dc.b 0
currraster:     dc.b 0
