1; Example of using playroutine 2 (gamemusic)
2;
3; playeraddress+0 = init music (A = tunenumber starting from 0)
4; playeraddress+3 = play music
5; playeraddress+6 = play sound effect (A = sound effect address lowbyte,
6;                                      Y = sound effect address highbyte,
7;                                      X = channel index 0, 7 or 14)
8;
9; Remember that this player uses 2 zeropage locations, so save those
10; locations before calling the player, if you need all of the ZP.
11; The player doesn't depend on their contents between calls at all!
12
13
14
15                processor 6502
16
17                org $0800
18
19getin           = $ffe4
20
21start:          jsr initraster
22                lda #22
23                sta $d018
24                lda #$00
25                jsr $1000                       ;Initialize subtune 0
26                ldx #$00
27                lda #$20
28clearscreen:    sta $0400,x
29                sta $0500,x
30                sta $0600,x
31                sta $0700,x
32                inx
33                bne clearscreen
34textloop:       lda text,x
35                jsr convertascii
36                sta $0400,x
37                lda #$01
38                sta $d800,x
39                inx
40                cpx #4*40
41                bcc textloop
42idle:           lda currraster
43                ldx #52
44                jsr printhex
45                lda maxraster
46                ldx #55
47                jsr printhex
48                jsr getin
49                cmp #"A"
50                bcc idle
51                cmp #"D"+1
52                bcs idle
53playsound:      sec
54                sbc #"A"                        ;Sound effect number 0-3
55                tax
56                lda sfxtbllo,x                  ;Address in A,Y
57                ldy sfxtblhi,x
58                ldx #$0e                        ;Channel index in X
59                jsr $1006                       ;(0, 7 or 14)
60                jmp idle
61
62printhex:       pha
63                lsr
64                lsr
65                lsr
66                lsr
67                tay
68                lda hexnybble,y
69                jsr convertascii
70                sta $0400,x
71                pla
72                and #$0f
73                tay
74                lda hexnybble,y
75                jsr convertascii
76                sta $0401,x
77                rts
78
79convertascii:   cmp #$60
80                bcc ca_ok
81                sbc #$60
82ca_ok:          rts
83
84initraster:     sei
85                lda #<raster
86                sta $0314
87                lda #>raster
88                sta $0315
89                lda #50                         ;Set low bits of raster
90                sta $d012                       ;position
91                lda $d011
92                and #$7f                        ;Set high bit of raster
93                sta $d011                       ;position (0)
94                lda #$7f                        ;Set timer interrupt off
95                sta $dc0d
96                lda #$01                        ;Set raster interrupt on
97                sta $d01a
98                lda $dc0d                       ;Acknowledge timer interrupt
99                cli
100irwait:         lda maxraster
101                beq irwait
102                lda #$00
103                sta maxraster
104                rts
105
106raster:         nop
107                nop
108                nop
109                nop
110                nop
111                lda $d012
112                sta raster_cmp+1
113                inc $d020
114                jsr $1003                       ;Play music and measure
115                lda $d012                       ;rastertime it took to
116                dec $d020                       ;execute
117                sec
118raster_cmp:     sbc #$00
119                sta currraster
120                cmp maxraster
121                bcc raster_skip
122                sta maxraster
123raster_skip:    dec $d019
124                jmp $ea31
125
126text:           dc.b "Example 2: Game-musicroutine            "
127                dc.b "Rastertime:   /                         "
128                dc.b "                                        "
129                dc.b "Press A-D for sound effects.            "
130
131hexnybble:      dc.b "0123456789ABCDEF"
132
133maxraster:      dc.b 0
134currraster:     dc.b 0
135
136sfxtbllo:       dc.b <arpeggio2                ;Our sound FX table
137                dc.b <arpeggio1
138                dc.b <gunshot
139                dc.b <explosion
140
141sfxtblhi:       dc.b >arpeggio2
142                dc.b >arpeggio1
143                dc.b >gunshot
144                dc.b >explosion
145
146arpeggio2:      include sfx_arp2.s            ;The order in memory determines
147arpeggio1:      include sfx_arp1.s            ;priority when trying to override
148gunshot:        include sfx_gun.s             ;a currently playing effect
149explosion:      include sfx_expl.s            ;arpeggio2 = lowest pri.
150                                              ;explosion = highest pri.
151
152                org $1000
153
154                incbin example2.bin           ;Playroutine & music data
155