-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.inc
268 lines (233 loc) · 7.58 KB
/
sound.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
; A very simple GB soundroutine
init_sound:
; Volume/Channel setup
ld a, 08Fh
ldh (NR_52 & 0FFh),a
ld a, 077h
ldh (NR_50 & 0FFh),a
ld a, 0FFh
ldh (NR_51 & 0FFh),a
; don't use noise for now...
ld a, 0
ldh (NR_41 & 0FFh),a
ldh (NR_42 & 0FFh),a
ldh (NR_43 & 0FFh),a
ldh (NR_44 & 0FFh),a
; put a simple square-wave into Wave-RAM
ld hl, 0FF30h
ld c, 08h
ld a, 0FFh
is1: ldi (hl),a
dec c
jr nz, is1
ld c, 08h
xor a
is2: ldi (hl),a
dec c
jr nz, is2
ld hl, VOICE_1
ld de, snd_0
ld (hl),e
inc hl
ld (hl),d
ld hl, VOICE_2
ld de, snd_1
ld (hl),e
inc hl
ld (hl),d
ld hl, VOICE_3
ld de, snd_2
ld (hl),e
inc hl
ld (hl),d
ld a,1
ldh (DUR_1 & 0FFh),a
ldh (DUR_2 & 0FFh),a
ldh (DUR_3 & 0FFh),a
ld a,7
ldh (PLAY_VOICES & 0FFh),a
ret
sound_off:
ld a,0 ; sound off to save battery
ldh (NR_52 & 0FFh),a
ldh (PLAY_VOICES & 0FFh), a
ldh a, (EN_SOUND & 0FFh) ; sound enabled ?
or a
call nz, init_sound
jr exit_irq
; This function is linked into the vblank-IRQ and controls music-playback
timer_irq:
push af
push bc
push de
push hl
ldh a, (PLAY_VOICES & 0FFh) ; all voices done playing ?
or a
jr z, sound_off
ldh a, (EN_SOUND & 0FFh) ; sound enabled ?
or a
jr z, sound_off
ldh a, (DUR_1 & 0FFh)
dec a
ldh (DUR_1 & 0FFh),a
call z, upd_v1
ldh a, (DUR_2 & 0FFh)
dec a
ldh (DUR_2 & 0FFh),a
call z, upd_v2
ldh a, (DUR_3 & 0FFh)
dec a
ldh (DUR_3 & 0FFh),a
call z, upd_v3
exit_irq: pop hl
pop de
pop bc
pop af
reti
upd_v1:
ld hl, PLAY_VOICES
bit 0,(hl)
ret z
ld hl, VOICE_1
ld e,(hl)
inc hl
ld d,(hl)
dec hl
ld a,(de) ; = duration
inc de
ldh (DUR_1 & 0FFh),a
cp 0ffh
jr z, off_v1
ld a,(de) ; = frequency low
inc de
ld b,a
ld a,(de) ; = frequency high
inc de
ld c,a
or b
jr z, mute_v1
ld a, 000h
ldh (NR_10 & 0FFh),a
ld a, 080
ldh (NR_11 & 0FFh),a
ld a, 0F0h
ldh (NR_12 & 0FFh), a
ld a, b
ldh (NR_13 & 0FFh), a
ld a, c
and 7
or 080h
ldh (NR_14 & 0FFh), a
ld (hl),e
inc hl
ld (hl),d
ret
mute_v1:
ld a, 000h
ldh (NR_12 & 0FFh), a
ld (hl),e
inc hl
ld (hl),d
ret
off_v1:
ld hl, PLAY_VOICES
res 0,(hl)
ret
upd_v2:
ld hl, PLAY_VOICES
bit 1,(hl)
ret z
ld hl, VOICE_2
ld e,(hl)
inc hl
ld d,(hl)
dec hl
ld a,(de) ; = duration
inc de
ldh (DUR_2 & 0FFh),a
cp 0ffh
jr z, off_v2
ld a,(de) ; = frequency low
inc de
ld b,a
ld a,(de) ; = frequency high
inc de
ld c,a
or b
jr z, mute_v2
ld a, 080
ldh (NR_21 & 0FFh),a
ld a, 0F0h
ldh (NR_22 & 0FFh), a
ld a, b
ldh (NR_23 & 0FFh), a
ld a, c
and 7
or 080h
ldh (NR_24 & 0FFh), a
ld (hl),e
inc hl
ld (hl),d
ret
mute_v2:
ld a, 000h
ldh (NR_22 & 0FFh), a
ld (hl),e
inc hl
ld (hl),d
ret
off_v2:
ld hl, PLAY_VOICES
res 1,(hl)
ret
upd_v3:
ld hl, PLAY_VOICES
bit 2,(hl)
ret z
ld hl, VOICE_3
ld e,(hl)
inc hl
ld d,(hl)
dec hl
ld a,(de) ; = duration
inc de
ldh (DUR_3 & 0FFh),a
cp 0ffh
jr z, off_v3
ld a,(de) ; = frequency low
inc de
ld b,a
ld a,(de) ; = frequency high
inc de
ld c,a
or b
jr z, mute_v3 ; if freq=0 -> mute
ld a, 080h
ldh (NR_30 & 0FFh),a
ld a, 000h
ldh (NR_31 & 0FFh),a
ld a, 000100000b
ldh (NR_32 & 0FFh),a
ld a, b
ldh (NR_33 & 0FFh), a
ld a, c
and 7
or 080h
ldh (NR_34 & 0FFh), a
ld (hl),e
inc hl
ld (hl),d
ret
mute_v3:
ld a, 000h
ldh (NR_32 & 0FFh), a
ld (hl),e
inc hl
ld (hl),d
ret
off_v3:
ld hl, PLAY_VOICES
res 2,(hl)
ret
; Music, stolen from "Leisure Suit Larry" byte Sierra On-Line
#include "LARRY.INC"