-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.asm
1719 lines (1584 loc) · 36.8 KB
/
audio.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
INCLUDE "hardware.inc"
SECTION "Audio", ROM0[$6480]
SquareSFXStartPointers::
dw StartTinkSFX ; 1 - Menu cursor movement, TODO name
dw StartChangeScreenSFX ; 2 - Menu change screen
dw StartRotatePieceSFX ; 3 - Rotate piece
dw StartShiftPieceSFX ; 4 - Shift piece
dw StartGarbageAttackSFX; 5 - Garbage attack
dw StartLineClearSFX ; 6 - Line clear
dw StartTetrisSFX ; 7 - Tetris
dw StartLevelUpSFX ; 8 - Level up
SquareSFXContinuePointers::
dw ContinueTinkSFX
dw ContinueGenericSquareSFX
dw ContinueRotatePieceSFX
dw ContinueGenericSquareSFX
dw ContinueGenericSquareSFX
dw ContinueLineClearSFX
dw ContinueTetrisSFX
dw ContinueLevelUpSFX
NoiseSFXStartPointers::
dw StartStackFallSFX ; 1 - Stack settles after a line clear
dw StartLockPieceSFX ; 2 -
dw StartIgnitionSFX ; 3 - Rocket ignition
dw StartLiftoffSFX ; 4 - Rocket flying
NoiseSFXContinuePointers::
dw ContinueGenericNoiseSFX
dw ContinueGenericNoiseSFX
dw ContinueGenericNoiseSFX
dw ContinueLiftoffSFX
MusicPointers:: ; TODO
dw $6F3F ; 1 - Top Score
dw $6F4A ; 2 - Stage clear
dw $6F55 ; 3 - Title screen
dw $6F60 ; 4 - Game over
dw $6F6B ; 5 - Type A - Korobeiniki
dw $6F76 ; 6 - Type B - ?
dw $6F81 ; 7 - Type C - Bach, French Suite №3 in Bm, Menuet
dw $6F8C ; 8 - Danger, March of the Toreadors from Carmen
dw $6F97 ; 9 - Multiplayer Round over
dw $6FA2 ; A - Type B Jingle #1
dw $6FAD ; B - Type B Jingle #2
dw $6FB8 ; C - Type B Jingle #3
dw $6FC3 ; D - Type B Jingle #4
dw $6FCE ; E - Type B Jingle #5
dw $6FD9 ; F - Type B Jingle #6
dw $6FE4 ; 10 - Rocket launch
dw $6FEF ; 11 - Multiplayer victory
DoNothing::
ret
_UpdateAudio::
push af
push bc
push de
push hl
ld a, [wPauseUnpauseSound]
cp a, 1
jr z, .pauseAudio
cp a, 2
jr z, .unpauseAudio
ld a, [wPauseTuneTimer]
and a
jr nz, .playPauseTune
.updateAudio
ldh a, [hDemoNumber]
and a
jr z, .playSounds
xor a
ld [wNewSquareSFXID], a
ld [wNewMusicID], a
ld [wNewWaveSFXID], a
ld [wNewNoiseSFXID], a
.playSounds
call DoNothing ; Simply returns. No corresponding routine in SML
call PlaySquareSFX
call PlayNoiseSFX
call PlayWaveSFX
call StartMusic
call PlayMusic
call PanStereo
.out
xor a
ld [wNewSquareSFXID], a
ld [wNewMusicID], a
ld [wNewWaveSFXID], a
ld [wNewNoiseSFXID], a
ld [wPauseUnpauseSound], a
pop hl
pop de
pop bc
pop af
ret
.pauseAudio
call _InitAudio.muteChannels
xor a
ld [wCurrentSquareSFXID], a
ld [wCurrentWaveSFXID], a
ld [wCurrentNoiseSFXID], a
ld hl, $DFBF
res 7, [hl]
ld hl, $DF9F
res 7, [hl]
ld hl, $DFAF
res 7, [hl]
ld hl, $DFCF
res 7, [hl]
ld hl, DefaultWavePattern
call LoadWavePattern
ld a, 48 ; 48 frames, ~0.8 seconds, but of course, it stops at 16
ld [wPauseTuneTimer], a
.playFirstNote
ld hl, .pauseTuneFirstNoteData
.playNote
call SetupChannel.square2
jr .out
.playSecondNote
ld hl, .pauseTuneSecondNoteData
jr .playNote
.unpauseAudio
xor a
ld [wPauseTuneTimer], a
jr .updateAudio
.playPauseTune
ld hl, wPauseTuneTimer
dec [hl]
ld a, [hl]
cp a, 40
jr z, .playSecondNote
cp a, 32
jr z, .playFirstNote
cp a, 24
jr z, .playSecondNote
cp a, 16
jr nz, .out
inc [hl] ; Keep the music paused by keeping wPauseTuneTimer
jr .out ; non-zero. Seems like a hack...
; Exactly the same as Super Mario Land
; 50% duty cycle, 14/256 seconds, volume 15/16, decreasing envelope, 3/64 seconds per step (pointless?)
.pauseTuneFirstNoteData
db $B2, $E3, $83, $C7 ; 1048.6 Hz ~ C6
.pauseTuneSecondNoteData
db $B2, $E3, $C1, $C7 ; 2080.5 Hz ~ C7
CheckPlayingTetrisSweep::
ld a, [wCurrentWaveSFXID]
cp a, $01 ; End of Tetris sweep
ret
CheckPlayingGarbageAttack::
ld a, [wCurrentSquareSFXID]
cp a, $05 ; Garbage attack sweep
ret
CheckPlayingTetris::
ld a, [wCurrentSquareSFXID]
cp a, $07 ; Tetris
ret
CheckPlayingLevelUp::
ld a, [wCurrentSquareSFXID]
cp a, $08 ; Level Up
ret
Data_659B::
db $00, $B5, $D0, $40, $C7
Data_65A0::
db $00, $B5, $20, $40, $C7
Data_65A5::
db $00, $B6, $A1, $80, $C7
StartTinkSFX::
ld a, 5
ld hl, Data_659B
jp StartSFXCommon
ContinueTinkSFX::
call UpdateSFXProgress
and a
ret nz
ld hl, $DFE4
inc [hl]
ld a, [hl]
cp a, $02
jr z, ContinueGenericSquareSFX.stop
ld hl, Data_65A0
jp SetupChannel.square1
StartChangeScreenSFX::
ld a, 3
ld hl, Data_65A5
jp StartSFXCommon
ContinueGenericSquareSFX::
call UpdateSFXProgress
and a
ret nz
.stop
xor a
ld [wCurrentSquareSFXID], a
ldh [rNR10], a
ld a, $08 ; Keep envelope direction at increase for some reason?
ldh [rNR12], a
ld a, $80 ; Trigger channel??
ldh [rNR14], a
ld hl, $DF9F ; Channel lock
res 7, [hl]
ret
Data_65E7::
db $00, $80, $E1, $C1, $87
Data_65EC::
db $00, $80, $E1, $AC, $87
StartTetrisSFX::
ld hl, Data_65E7
jp StartSFXCommon
ContinueTetrisSFX::
ld hl, $DFE4
inc [hl]
ld a, [hl]
cp a, 4
jr z, .label_6617
cp a, 11
jr z, .label_661D
cp a, 15
jr z, .label_6617
cp a, 24
jp z, .label_660E ; Should be a JR, bug
ret
.label_660E
ld a, 1
ld hl, wNewWaveSFXID
ld [hl], a
jp ContinueGenericSquareSFX.stop
.label_6617
ld hl, Data_65EC
jp SetupChannel.square1
.label_661D
ld hl, Data_65E7
jp SetupChannel.square1
Data_6623::
db $48, $BC, $42, $66, $87
StartShiftPieceSFX::
call CheckPlayingTetrisSweep
ret z
call CheckPlayingLevelUp
ret z
call CheckPlayingTetris
ret z
call CheckPlayingGarbageAttack
ret z
ld a, 2
ld hl, Data_6623
jp StartSFXCommon
LevelUpNote1::
db $00, $B0, $F1, $B6, $C7 ; A 6
LevelUpNote2::
db $00, $B0, $F1, $C4, $C7 ; C#7
LevelUpNote3::
db $00, $B0, $F1, $CE, $C7 ; E 7
LevelUpNote4::
db $00, $B0, $F1, $DB, $C7 ; A 7
StartLevelUpSFX::
call CheckPlayingTetris
ret z
ld a, 7
ld hl, LevelUpNote1
jp StartSFXCommon
ContinueLevelUpSFX::
call UpdateSFXProgress
and a
ret nz
ld hl, wSquareSFXNoteCounter
inc [hl]
ld a, [hl]
cp a, 1
jr z, .note2
cp a, 2
jr z, .note3
cp a, 3
jr z, .note4
cp a, 4
jr z, .note5
cp a, 5
jp z, ContinueGenericSquareSFX.stop
ret
.note2
ld hl, LevelUpNote2
jr .playNote
.note3
ld hl, LevelUpNote3
jr .playNote
.note4
ld hl, LevelUpNote4
jr .playNote
.note5
ld hl, LevelUpNote1
.playNote
jp SetupChannel.square1
Data_6695::
db $3E, $80, $E3, $00, $C4
Data_669A::
db $93, $83, $83, $73, $63, $53, $43, $33, $23, $13, $00
Data_66A5::
db $00, $23, $43, $63, $83, $A3, $C3, $D3, $E3, $FF
StartLineClearSFX::
call CheckPlayingTetrisSweep
ret z
call CheckPlayingLevelUp
ret z
call CheckPlayingTetris
ret z
ld a, 6
ld hl, Data_6695
jp StartSFXCommon
ContinueLineClearSFX::
call UpdateSFXProgress
and a
ret nz
ld hl, $DFE4
ld c, [hl]
inc [hl]
ld b, 0
ld hl, Data_669A
add hl, bc
ld a, [hl]
and a
jp z, ContinueGenericSquareSFX.stop
ld e, a
ld hl, Data_66A5
add hl, bc
ld a, [hl]
ld d, a
ld b, $86
.label_66E1
ld c, LOW(rNR12)
ld a, e
ldh [c], a ; NR12
inc c
ld a, d
ldh [c], a ; NR13
inc c
ld a, b
ldh [c], a ; NR14
ret
Data_66EC::
db $3B, $80, $B2, $87, $87
Data_66F1::
db $A2, $93, $62, $43, $23, $00
Data_66F7::
db $80, $40, $80, $40, $80
StartRotatePieceSFX::
call CheckPlayingTetrisSweep
ret z
call CheckPlayingLevelUp
ret z
call CheckPlayingTetris
ret z
call CheckPlayingGarbageAttack
ret z
ld a, 3
ld hl, Data_66EC
jp StartSFXCommon
ContinueRotatePieceSFX::
call UpdateSFXProgress
and a
ret nz
ld hl, $DFE4
ld c, [hl]
inc [hl]
ld b, $00
ld hl, Data_66F1
add hl, bc
ld a, [hl]
and a
jp z, ContinueGenericSquareSFX.stop
ld e, a
ld hl, Data_66F7
add hl, bc
ld a, [hl]
ld d, a
ld b, $87
jr ContinueLineClearSFX.label_66E1
StartGarbageAttackSFX::
call CheckPlayingTetris
ret z
ld a, 40
ld hl, Data_6740
jp StartSFXCommon
; Sets high bit of NR10, which does nothing? Bug?
; 3/128 Hz sweep up with shift 7, starting from frequency 127.9 Hz ~ C3
Data_6740::
db $B7, $80, $90, $FF, $83
Data_6745::
db $00, $D1, $45, $80
Data_6749::
db $00, $F1, $54, $80
Data_674D::
db $00, $D5, $65, $80
Data_6751::
db $00, $70, $66, $80
LiftOffNoiseData::
db $65, $65, $65, $64, $57, $56, $55, $54, $54, $54, $54, $54
db $47, $46, $46, $45, $45, $45, $44, $44, $44, $34, $34, $34
db $34, $34, $34, $34, $34, $34, $34, $34, $34, $34, $34, $34
LiftOffVolumeData::
db $70, $60, $70, $70, $70, $80, $90, $A0, $D0, $F0, $E0, $D0
db $C0, $B0, $A0, $90, $80, $70, $60, $50, $40, $30, $30, $20
db $20, $20, $20, $20, $20, $20, $20, $20, $20, $20, $10, $10
StartIgnitionSFX::
ld a, 48
ld hl, Data_674D
jp StartSFXCommon
StartLiftoffSFX::
ld a, 48
ld hl, Data_6751
jp StartSFXCommon
ContinueLiftoffSFX::
call UpdateSFXProgress
and a
ret nz
ld hl, $DFFC
ld a, [hl] ; Note counter
ld c, a
cp a, 36
jp z, ContinueGenericNoiseSFX.stop
inc [hl]
ld b, $00
push bc
ld hl, LiftOffNoiseData
add hl, bc
ld a, [hl]
ldh [rNR43], a ; Polynomial counter
pop bc
ld hl, LiftOffVolumeData
add hl, bc
ld a, [hl]
ldh [rNR42], a ; Volume envelope
ld a, $80 ; Trigger channel
ldh [rNR44], a
ret
StartStackFallSFX::
ld a, 32
ld hl, Data_6749
jp StartSFXCommon
StartLockPieceSFX::
ld a, 18
ld hl, Data_6745
jp StartSFXCommon
ContinueGenericNoiseSFX::
call UpdateSFXProgress
and a
ret nz
.stop
xor a
ld [wCurrentNoiseSFXID], a
ld a, $08
ldh [rNR42], a ; Stop envelope operation
ld a, $80
ldh [rNR44], a ; Trigger channel?
ld hl, $DFCF ; Channel lock
res 7, [hl]
ret
; Sound on, 198/256s, 100% volume, 157.5 Hz (~D#3),
Data_67FB::
db $80, $3A, $20, $60, $C6
StartGameOverSFX::
ld hl, GameOverWavePattern
call LockChannelsAndPrepareWaveChannel
ldh a, [rDIV]
and a, $1F
ld b, a
ld a, $D0
add b ; Generate a "random" number between 0xD0 and 0xEF
ld [$DFF5], a
ld hl, Data_67FB
jp SetupChannel.wave
ContinueGameOverSFX::
ldh a, [rDIV]
and a, $0F
ld b, a
ld hl, $DFF4
inc [hl]
ld a, [hl]
ld hl, $DFF5
cp a, 14
jr nc, .label_6832
inc [hl]
inc [hl]
.label_682A
ld a, [hl]
and a, $F0
or b
ld c, LOW(rNR33) ; Frequency lower 8 bits
ldh [c], a
ret
.label_6832
cp a, 30
jp z, Label_68E2
dec [hl]
dec [hl]
dec [hl]
jr .label_682A
PlayWaveSFX::
ld a, [wNewWaveSFXID]
cp a, 1
jp z, StartTetrisSweepSFX ; Bug. Could've been a JR
cp a, 2
jp z, StartGameOverSFX
ld a, [wCurrentWaveSFXID]
cp a, 1
jp z, ContinueTetrisSweepSFX
cp a, 2
jp z, ContinueGameOverSFX
ret
Data_6857::
db $80, $80, $20
Data_685A::
db $9D, $87
Data_685C::
db $80, $F8, $20
Data_685F::
db $98, $87
Data_6861::
db $80, $FB, $20
Data_6864::
db $96, $87
Data_6866::
db $80, $F6, $20
Data_6869::
db $95, $87
StartTetrisSweepSFX::
ld hl, WavePattern_6EA9
call LockChannelsAndPrepareWaveChannel
ld hl, Data_685A
ld a, [hl]
ld [$DFF6], a
ld a, $01
ld [$DFF5], a
ld hl, Data_6857
.label_6880
jp SetupChannel.wave
Label_6883::
ld a, $00
ld [$DFF5], a
ld hl, Data_685F
ld a, [hl]
ld [$DFF6], a
ld hl, Data_685C
jr StartTetrisSweepSFX.label_6880
Label_6894::
ld a, $01
ld [$DFF5], a
ld hl, Data_6864
ld a, [hl]
ld [$DFF6], a
ld hl, Data_6861
jr StartTetrisSweepSFX.label_6880
Label_68A5::
ld a, $02
ld [$DFF5], a
ld hl, Data_6869
ld a, [hl]
ld [$DFF6], a
ld hl, Data_6866
jr StartTetrisSweepSFX.label_6880
ContinueTetrisSweepSFX::
ld hl, $DFF4
inc [hl]
ldi a, [hl]
cp a, 9
jr z, Label_6883
cp a, 19
jr z, Label_6894
cp a, 23
jr z, Label_68A5
cp a, 32
jr z, Label_68E2
ldi a, [hl]
cp a, 0
ret z
cp a, 1
jr z, .label_68D8
cp a, 2
jr z, .label_68DC
ret
.label_68D8
inc [hl]
inc [hl]
jr .label_68DE
.label_68DC
dec [hl]
dec [hl]
.label_68DE
ld a, [hl]
ldh [rNR33], a
ret
Label_68E2::
xor a
ld [wCurrentWaveSFXID], a
ldh [rNR30], a
ld hl, $DFBF ; Disable all locks
res 7, [hl]
ld hl, $DF9F
res 7, [hl]
ld hl, $DFAF
res 7, [hl]
ld hl, $DFCF
res 7, [hl]
ld a, [wCurrentMusicID]
cp a, 5 ; TODO Korobeiniki
jr z, .korobeinikiException
ld hl, DefaultWavePattern
jr LockChannelsAndPrepareWaveChannel.loadWavePattern
.korobeinikiException
ld hl, WavePattern_6EC9
jr LockChannelsAndPrepareWaveChannel.loadWavePattern
LockChannelsAndPrepareWaveChannel::
push hl
ld [wCurrentWaveSFXID], a
ld hl, $DFBF
set 7, [hl] ; Lock wave channel
xor a
ld [$DFF4], a
ld [$DFF5], a
ld [$DFF6], a
ldh [rNR30], a ; Channel 3 off
ld hl, $DF9F ; Ah heck, lock all the channels!
set 7, [hl]
ld hl, $DFAF
set 7, [hl]
ld hl, $DFCF
set 7, [hl]
pop hl
.loadWavePattern
call LoadWavePattern
ret
; HL contains channel specific data
; A is note length
StartSFXCommon::
push af
dec e ; Back to DFE(0,10,18) + 1, i.e. currently playing SFX
ld a, [$DF71] ; Temp?
ld [de], a ; Commit to new SFX
inc e
pop af
inc e
ld [de], a ; DFx3, note length
dec e
xor a
ld [de], a ; DFx2 frame counter?
inc e
inc e
ld [de], a ; DFx4 note counter (i.e. counts notes)
inc e
ld [de], a ; DFx5?
ld a, e
cp a, $E5
jr z, SetupChannel.square1
cp a, $F5
jr z, SetupChannel.wave
cp a, $FD
jr z, SetupChannel.noise
ret
SetupChannel::
.square1
push bc
ld c, LOW(rNR10)
ld b, 5
jr .copy
.square2
push bc
ld c, LOW(rNR21)
ld b, 4
jr .copy
.wave
push bc
ld c, LOW(rNR30)
ld b, 5
jr .copy
.noise
push bc
ld c, LOW(rNR41)
ld b, 4
.copy
ldi a, [hl]
ldh [c], a
inc c
dec b
jr nz, .copy
pop bc
ret
LookupSoundPointer::
inc e ; DFE1, DFF1 or DFF9
ld [$DF71], a
.lookup
inc e ; DFE2, DFF2 or DFFA (why tho)
dec a ; SFX start counting from 1, but to calculate the
sla a ; into the table we need to go back to 0-based
ld c, a
ld b, $00
add hl, bc
ld c, [hl]
inc hl
ld b, [hl]
ld l, c
ld h, b
ld a, h ; Why? A is usually immediately overwritten? Bug?
ret
UpdateSFXProgress::
push de ; DE contains DFE(0,8,10,18) + 2
ld l, e
ld h, d
inc [hl] ; Increment
ldi a, [hl]
cp [hl] ; and compare with sound length
jr nz, .out
dec l
xor a
ld [hl], a
.out
pop de
ret
; From HL
LoadWavePattern::
push bc
ld c, LOW(_AUD3WAVERAM)
.loop
ldi a, [hl]
ldh [c], a
inc c
ld a, c
cp a, LOW(_AUD3WAVERAM) + $10 ; 32 4-bit samples
jr nz, .loop
pop bc
ret
_InitAudio::
xor a
ld [wCurrentSquareSFXID], a
ld [wCurrentMusicID], a
ld [wCurrentWaveSFXID], a
ld [wCurrentNoiseSFXID], a
ld [$DF9F], a
ld [$DFAF], a
ld [$DFBF], a
ld [$DFCF], a
ld a, $FF
ldh [rNR51], a ; Output all channels to both terminals
ld a, $03
ld [$DF78], a
.muteChannels
ld a, $08 ; On channels with a volume envelope, set volume to
ldh [rNR12], a ; zero and envelope to increase (fade-in?)
ldh [rNR22], a
ldh [rNR42], a
ld a, $80
ldh [rNR14], a ; Restart sound, continuous mode
ldh [rNR24], a
ldh [rNR44], a
xor a
ldh [rNR10], a ; Disable tone-sweep
ldh [rNR30], a ; Disable wave
ret
PlaySquareSFX::
ld de, wNewSquareSFXID
ld a, [de]
and a
jr z, .continue
ld hl, $DF9F ; Enable channel lock
set 7, [hl]
ld hl, SquareSFXStartPointers
call LookupSoundPointer
jp hl
.continue
inc e
ld a, [de]
and a
jr z, .out
ld hl, SquareSFXContinuePointers
call LookupSoundPointer.lookup
jp hl
.out ; Just like in Super Mario Land, they seem to have
ret ; forgotten about the RET Z instruction
PlayNoiseSFX::
ld de, wNewNoiseSFXID
ld a, [de]
and a
jr z, .continue
ld hl, $DFCF ; Lock channel
set 7, [hl]
ld hl, NoiseSFXStartPointers
call LookupSoundPointer
jp hl
.continue
inc e
ld a, [de]
and a
jr z, .out
ld hl, NoiseSFXContinuePointers
call LookupSoundPointer.lookup
jp hl
.out
ret
; Seems like pointless indirection, but whatever
_StopAudio::
call _InitAudio
ret
StartMusic::
ld hl, wNewMusicID
ldi a, [hl]
and a
ret z
cp a, $FF
jr z, _StopAudio ; Unreachable in SML due to boundary check
ld [hl], a
ld b, a
ld hl, MusicPointers
and a, $1F ; Curious, there are only $11 songs?
call LookupSoundPointer.lookup
call InitMusicChannels
call InitStereo
ret
InitStereo:
ld a, [wCurrentMusicID]
and a
ret z
ld hl, StereoData
.loop
dec a
jr z, .writeStereoData
inc hl
inc hl
inc hl
inc hl
jr .loop
.writeStereoData
ldi a, [hl]
ld [wMonoOrStereo], a
ldi a, [hl]
ld [wPanInterval], a
ldi a, [hl]
ld [wChannelEnableMask1], a
ldi a, [hl]
ld [wChannelEnableMask2], a
xor a
ld [wPanFrameCounter], a
ld [wPanCounter], a
ret
PanStereo::
ld a, [wCurrentMusicID]
and a
jr z, .enableAllChannels
ld hl, wPanFrameCounter
ld a, [wMonoOrStereo]
cp a, 1
jr z, .applyMask1
cp a, 3
jr z, .enableAllChannels
inc [hl]
ldi a, [hl]
cp [hl] ; Compare framecounter with interval
jr nz, .checkNoiseAndWave
dec l
ld [hl], 0 ; Reset framecounter
inc l
inc l
inc [hl] ; And add one to the pan-counter
ld a, [wChannelEnableMask1]
bit 0, [hl] ; Alternate between mask 1 and mask 2
jp z, .checkWaveSFX
ld a, [wChannelEnableMask2]
.checkWaveSFX
ld b, a
ld a, [wCurrentWaveSFXID]
and a
jr z, .checkNoiseSFX
set 2, b ; If a sound effect is playing which uses the wave
set 6, b ; channel, override the mask to play on both terminals
.checkNoiseSFX
ld a, [wCurrentNoiseSFXID]
and a
jr z, .out
set 3, b ; Et pour le bruit la même chose
set 7, b
.out
ld a, b
.applyChannelEnableMask
ldh [rNR51], a
ret
.enableAllChannels
ld a, $FF
jr .applyChannelEnableMask
.applyMask1
ld a, [wChannelEnableMask1]
jr .checkWaveSFX
.checkNoiseAndWave
ld a, [wCurrentNoiseSFXID]
and a
jr nz, .enableAllChannels
ld a, [wCurrentWaveSFXID]
and a
jr nz, .enableAllChannels
ret
; TODO is the second mask just unused for mono?
; TODO Is this all just pointless?
StereoData::
db 1, 36, $EF, $56
db 1, 0, $E5, $00
db 1, 32, $FD, $00
db 1, 32, $DE, $F7
db 3, 24, $7F, $F7
db 3, 24, $F7, $7F
db 3, 72, $DF, $5B
db 1, 24, $DB, $E7
db 1, 0, $FD, $F7
db 3, 32, $7F, $F7
db 1, 32, $ED, $F7
db 1, 32, $ED, $F7