-
Notifications
You must be signed in to change notification settings - Fork 0
/
libSound.asm
605 lines (490 loc) · 14.5 KB
/
libSound.asm
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
;===============================================================================
; Constants
MusicBaseAddress = $B000
MusicFrameAddress = $B003
TriangleStart = 17
TriangleEnd = 16
SawtoothStart = 33
SawtoothEnd = 32
PulseStart = 65
PulseEnd = 64
NoiseStart = 129
NoiseEnd = 128
Attack_2ms = 0
Attack_8ms = 16 ; 1<<4
Attack_16ms = 32 ; 2<<4
Attack_24ms = 48 ; 3<<4
Attack_38ms = 64 ; 4<<4
Attack_56ms = 80 ; 5<<4
Attack_68ms = 96 ; 6<<4
Attack_80ms = 112 ; 7<<4
Attack_100ms = 128 ; 8<<4
Attack_250ms = 144 ; 9<<4
Attack_500ms = 160 ; 10<<4
Attack_800ms = 176 ; 11<<4
Attack_1s = 192 ; 12<<4
Attack_3s = 208 ; 13<<4
Attack_5s = 224 ; 14<<4
Attack_8s = 240 ; 15<<4
Decay_6ms = 0
Decay_24ms = 1
Decay_48ms = 2
Decay_72ms = 3
Decay_114ms = 4
Decay_168ms = 5
Decay_204ms = 6
Decay_240ms = 7
Decay_300ms = 8
Decay_750ms = 9
Decay_1_5s = 10
Decay_2_4s = 11
Decay_3s = 12
Decay_9s = 13
Decay_15s = 14
Decay_24s = 15
Sustain_Vol0 = 0
Sustain_Vol1 = 16 ; 1<<4
Sustain_Vol2 = 32 ; 2<<4
Sustain_Vol3 = 48 ; 3<<4
Sustain_Vol4 = 64 ; 4<<4
Sustain_Vol5 = 80 ; 5<<4
Sustain_Vol6 = 96 ; 6<<4
Sustain_Vol7 = 112 ; 7<<4
Sustain_Vol8 = 128 ; 8<<4
Sustain_Vol9 = 144 ; 9<<4
Sustain_Vol10 = 160 ; 10<<4
Sustain_Vol11 = 176 ; 11<<4
Sustain_Vol12 = 192 ; 12<<4
Sustain_Vol13 = 208 ; 13<<4
Sustain_Vol14 = 224 ; 14<<4
Sustain_Vol15 = 240 ; 15<<4
Release_6ms = 0
Release_24ms = 1
Release_48ms = 2
Release_72ms = 3
Release_114ms = 4
Release_168ms = 5
Release_204ms = 6
Release_240ms = 7
Release_300ms = 8
Release_750ms = 9
Release_1_5s = 10
Release_2_4s = 11
Release_3s = 12
Release_9s = 13
Release_15s = 14
Release_24s = 15
CmdWave = 0
CmdAttackDecay = 1
CmdSustainRelease = 2
CmdFrequencyHigh = 3
CmdFrequencyLow = 4
CmdPulseWidthHigh = 5
CmdPulseWidthLow = 6
CmdDelay = 7
CmdEnd = 8
;===============================================================================
; Variables
soundVoiceActive byte 0, 0, 0
soundVoiceCmdPtrHigh byte 0, 0, 0
soundVoiceCmdPtrLow byte 0, 0, 0
soundVoiceCmdIndex byte 0, 0, 0
soundVoiceDelay byte 0, 0, 0
;===============================================================================
; Pre-made sound effect comand buffers
soundFiring byte CmdAttackDecay, Attack_2ms+Decay_6ms
byte CmdSustainRelease, Sustain_Vol10+Release_6ms
byte CmdWave, SawtoothStart
byte CmdFrequencyHigh, 18
byte CmdFrequencyLow, 209
byte CmdDelay, 5
byte CmdWave, SawtoothEnd
byte CmdAttackDecay, Attack_2ms+Decay_6ms
byte CmdSustainRelease, Sustain_Vol10+Release_6ms
byte CmdWave, SawtoothStart
byte CmdFrequencyHigh, 22
byte CmdFrequencyLow, 96
byte CmdDelay, 5
byte CmdWave, SawtoothEnd
byte CmdEnd
soundFiringHigh byte >soundFiring
soundFiringLow byte <soundFiring
; --------------------------------------------------------
soundExplosion byte CmdWave, NoiseEnd
byte CmdAttackDecay, Attack_38ms+Decay_114ms
byte CmdSustainRelease, Sustain_Vol10+Release_300ms
byte CmdFrequencyHigh, 21
byte CmdFrequencyLow, 31
byte CmdWave, NoiseStart
byte CmdDelay, 20
byte CmdWave, NoiseEnd
byte CmdEnd
soundExplosionHigh byte >soundExplosion
soundExplosionLow byte <soundExplosion
; --------------------------------------------------------
soundPickup byte CmdAttackDecay, Attack_2ms+Decay_6ms
byte CmdSustainRelease, Sustain_Vol10+Release_6ms
byte CmdFrequencyHigh, 84
byte CmdFrequencyLow, 125
byte CmdWave, SawtoothStart
byte CmdDelay, 10
byte CmdWave, SawtoothEnd
byte CmdAttackDecay, Attack_2ms+Decay_6ms
byte CmdSustainRelease, Sustain_Vol10+Release_300ms
byte CmdFrequencyHigh, 100
byte CmdFrequencyLow, 121
byte CmdWave, SawtoothStart
byte CmdDelay, 10
byte CmdWave, SawtoothEnd
byte CmdEnd
soundPickupHigh byte >soundPickup
soundPickupLow byte <soundPickup
; --------------------------------------------------------
soundEnding byte CmdPulseWidthHigh, 8
byte CmdPulseWidthLow, 128
byte CmdFrequencyHigh, 56
byte CmdFrequencyLow, 99
byte CmdAttackDecay, Attack_2ms+Decay_6ms
byte CmdSustainRelease, Sustain_Vol10+Release_6ms
byte CmdWave, PulseStart
byte CmdDelay, 5
byte CmdWave, PulseEnd
byte CmdDelay, 1
byte CmdAttackDecay, Attack_2ms+Decay_6ms
byte CmdSustainRelease, Sustain_Vol10+Release_750ms
byte CmdWave, PulseStart
byte CmdDelay, 30
byte CmdWave, PulseEnd
byte CmdEnd
soundEndingHigh byte >soundEnding
soundEndingLow byte <soundEnding
;===============================================================================
; Macros/Subroutines
libSoundInit
; Clear all the SID registers
ldx #0
lda #0
@loop1 sta FRELO1,X
inx
cpx #$19
bcc @loop1
ldx #0
lda #0
@loop2 sta FRELO2,X
inx
cpx #$19
bcc @loop2
ldx #0
lda #0
@loop3 sta FRELO3,X
inx
cpx #$19
bcc @loop3
; Volume & Filter Select
lda #15
sta SIGVOL
rts
;===============================================================================
libMusicInit
lda #0
tax
tay
jsr MusicBaseAddress
rts
;===============================================================================
libMusicUpdate
lda #0
tax
tay
jsr MusicFrameAddress
rts
;===============================================================================
defm LIBSOUND_PLAY_VAA ; /1 = Voice (Value)
; /2 = Command Buffer Ptr High (Address)
; /3 = Command Buffer Ptr Low (Address)
ldx #/1
lda /2
sta soundVoiceCmdPtrHigh,X
lda /3
sta soundVoiceCmdPtrLow,X
lda #True
sta soundVoiceActive,X
lda #0
sta soundVoiceCmdIndex,X
endm
;===============================================================================
defm LIBSOUND_BOMB_START_AA ; /1 = Frequency High (Address)
; /2 = Frequency Low (Address)
lda #255
sbc /1
tax
lda #230
sbc /2
tay
jsr libSoundBombStart
endm
libSoundBombStart
stx FREHI1
sty FRELO1
lda #Attack_2ms+Decay_6ms
sta ATDCY1
lda #Sustain_Vol7+Release_300ms
sta SUREL1
lda #SawtoothStart
sta VCREG1
rts
defm LIBSOUND_BOMB_UPDATE_AA ; /1 = Frequency High (Address)
; /2 = Frequency Low (Address)
lda #255
sbc /1
sta FREHI1
lda #230
sbc /2
sta FRELO1
endm
defm LIBSOUND_BOMB_STOP
jsr libSoundBombStop
endm
libSoundBombStop
lda #SawtoothEnd
sta VCREG1
rts
;===============================================================================
libSoundUpdate
ldx #0
lSULoop
lda soundVoiceActive,X
bne lSUActive
jmp lSUDone
lSUActive
lda soundVoiceDelay,X
beq lSUProcess
dec soundVoiceDelay,X
jmp lSUDone
lSUProcess
lda soundVoiceCmdPtrLow,X ; load low address byte
sta ZeroPageLow
lda soundVoiceCmdPtrHigh,X ; load high address byte
sta ZeroPageHigh
ldy soundVoiceCmdIndex,X ; load x position into Y register
lda (ZeroPageLow),Y ; load the command
lSUProcessLoop
; process the command
LIBSOUND_PROCESSWAVE
LIBSOUND_PROCESSATTACKDECAY
LIBSOUND_PROCESSSUSTAINRELEASE
LIBSOUND_PROCESSFREQUENCYHIGH
LIBSOUND_PROCESSFREQUENCYLOW
LIBSOUND_PROCESSPULSEWIDTHHIGH
LIBSOUND_PROCESSPULSEWIDTHLOW
LIBSOUND_PROCESSDELAY
LIBSOUND_PROCESSEND
jmp lSUProcessLoop
lSUDone
inx
cpx #3
beq lSUFinished
jmp lSULoop
lSUFinished
rts
;===============================================================================
defm LIBSOUND_PROCESSWAVE
lSUWave
cmp #CmdWave
bne @skip
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
cpx #0
bne @reg2
sta VCREG1
jmp @regdone
@reg2
cpx #1
bne @reg3
sta VCREG2
jmp @regdone
@reg3
sta VCREG3
@regdone
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
@skip
endm
;===============================================================================
defm LIBSOUND_PROCESSATTACKDECAY
lSUAttackDecay
cmp #CmdAttackDecay
bne @skip
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
cpx #0
bne @reg2
sta ATDCY1
jmp @regdone
@reg2
cpx #1
bne @reg3
sta ATDCY2
jmp @regdone
@reg3
sta ATDCY3
@regdone
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
@skip
endm
;===============================================================================
defm LIBSOUND_PROCESSSUSTAINRELEASE
lSUSustainRelease
cmp #CmdSustainRelease
bne @skip
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
cpx #0
bne @reg2
sta SUREL1
jmp @regdone
@reg2
cpx #1
bne @reg3
sta SUREL2
jmp @regdone
@reg3
sta SUREL3
@regdone
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
@skip
endm
;===============================================================================
defm LIBSOUND_PROCESSFREQUENCYHIGH
lSUFrequencyHigh
cmp #CmdFrequencyHigh
bne @skip
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
cpx #0
bne @reg2
sta FREHI1
jmp @regdone
@reg2
cpx #1
bne @reg3
sta FREHI2
jmp @regdone
@reg3
sta FREHI3
@regdone
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
@skip
endm
;===============================================================================
defm LIBSOUND_PROCESSFREQUENCYLOW
lSUFrequencyLow
cmp #CmdFrequencyLow
bne @skip
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
cpx #0
bne @reg2
sta FRELO1
jmp @regdone
@reg2
cpx #1
bne @reg3
sta FRELO2
jmp @regdone
@reg3
sta FRELO3
@regdone
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
@skip
endm
;===============================================================================
defm LIBSOUND_PROCESSPULSEWIDTHHIGH
lSUPulseWidthHigh
cmp #CmdPulseWidthHigh
bne @skip
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
cpx #0
bne @reg2
sta PWHI1
jmp @regdone
@reg2
cpx #1
bne @reg3
sta PWHI2
jmp @regdone
@reg3
sta PWHI3
@regdone
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
@skip
endm
;===============================================================================
defm LIBSOUND_PROCESSPULSEWIDTHLOW
lSUPulseWidthLow
cmp #CmdPulseWidthLow
bne @skip
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
cpx #0
bne @reg2
sta PWLO1
jmp @regdone
@reg2
cpx #1
bne @reg3
sta PWLO2
jmp @regdone
@reg3
sta PWLO3
@regdone
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
@skip
endm
;===============================================================================
defm LIBSOUND_PROCESSDELAY
lSUDelay
cmp #CmdDelay
bne @skip
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
sta soundVoiceDelay,X
inc soundVoiceCmdIndex,X ; move to next byte
ldy soundVoiceCmdIndex,X
lda (ZeroPageLow),Y
jmp lSUDone
@skip
endm
;===============================================================================
defm LIBSOUND_PROCESSEND
lSUEnd
cmp #CmdEnd
bne @skip
lda #False
sta soundVoiceActive,X
sta soundVoiceCmdIndex,X
jmp lSUDone
@skip
endm