-
Notifications
You must be signed in to change notification settings - Fork 1k
/
engine_2.asm
1790 lines (1728 loc) · 33.6 KB
/
engine_2.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
; The second of three duplicated sound engines.
; This copy has a few differences relating to battle sound effects
; and the low health alarm that plays in battle
Audio2_UpdateMusic::
ld c, CHAN1
.loop
ld b, 0
ld hl, wChannelSoundIDs
add hl, bc
ld a, [hl]
and a
jr z, .nextChannel
ld a, c
cp CHAN5
jr nc, .applyAffects ; if sfx channel
ld a, [wMuteAudioAndPauseMusic]
and a
jr z, .applyAffects
bit BIT_MUTE_AUDIO, a
jr nz, .nextChannel
set BIT_MUTE_AUDIO, a
ld [wMuteAudioAndPauseMusic], a
xor a ; disable all channels' output
ldh [rNR51], a
ldh [rNR30], a
ld a, $80
ldh [rNR30], a
jr .nextChannel
.applyAffects
call Audio2_ApplyMusicAffects
.nextChannel
ld a, c
inc c ; inc channel number
cp CHAN8
jr nz, .loop
ret
; this routine checks flags for music effects currently applied
; to the channel and calls certain functions based on flags.
Audio2_ApplyMusicAffects:
ld b, $0
ld hl, wChannelNoteDelayCounters ; delay until next note
add hl, bc
ld a, [hl]
cp 1 ; if the delay is 1, play next note
jp z, Audio2_PlayNextNote
dec a ; otherwise, decrease the delay timer
ld [hl], a
ld a, c
cp CHAN5
jr nc, .startChecks ; if a sfx channel
ld hl, wChannelSoundIDs + CHAN5
add hl, bc
ld a, [hl]
and a
jr z, .startChecks
ret
.startChecks
ld hl, wChannelFlags1
add hl, bc
bit BIT_ROTATE_DUTY_CYCLE, [hl]
jr z, .checkForExecuteMusic
call Audio2_ApplyDutyCyclePattern
.checkForExecuteMusic
ld b, 0
ld hl, wChannelFlags2
add hl, bc
bit BIT_EXECUTE_MUSIC, [hl]
jr nz, .checkForPitchSlide
ld hl, wChannelFlags1
add hl, bc
bit BIT_NOISE_OR_SFX, [hl]
jr nz, .skipPitchSlideVibrato
.checkForPitchSlide
ld hl, wChannelFlags1
add hl, bc
bit BIT_PITCH_SLIDE_ON, [hl]
jr z, .checkVibratoDelay
jp Audio2_ApplyPitchSlide
.checkVibratoDelay
ld hl, wChannelVibratoDelayCounters
add hl, bc
ld a, [hl]
and a ; check if delay is over
jr z, .checkForVibrato
dec [hl] ; otherwise, dec delay
.skipPitchSlideVibrato
ret
.checkForVibrato
ld hl, wChannelVibratoExtents
add hl, bc
ld a, [hl]
and a
jr nz, .vibrato
ret ; no vibrato
.vibrato
ld d, a
ld hl, wChannelVibratoRates
add hl, bc
ld a, [hl]
and $f
and a
jr z, .applyVibrato
dec [hl] ; decrement counter
ret
.applyVibrato
ld a, [hl]
swap [hl]
or [hl]
ld [hl], a ; reload the counter
ld hl, wChannelFrequencyLowBytes
add hl, bc
ld e, [hl] ; get note pitch
ld hl, wChannelFlags1
add hl, bc
; This is the only code that sets/resets the vibrato direction bit, so it
; continuously alternates which path it takes.
bit BIT_VIBRATO_DIRECTION, [hl]
jr z, .unset
res BIT_VIBRATO_DIRECTION, [hl]
ld a, d
and $f
ld d, a
ld a, e
sub d
jr nc, .noCarry
ld a, 0
.noCarry
jr .done
.unset
set BIT_VIBRATO_DIRECTION, [hl]
ld a, d
and $f0
swap a
add e
jr nc, .done
ld a, $ff
.done
ld d, a
ld b, REG_FREQUENCY_LO
call Audio2_GetRegisterPointer
ld [hl], d
ret
; this routine executes all music commands that take up no time,
; like tempo changes, duty cycle changes etc. and doesn't return
; until the first note is reached
Audio2_PlayNextNote:
; reload the vibrato delay counter
ld hl, wChannelVibratoDelayCounterReloadValues
add hl, bc
ld a, [hl]
ld hl, wChannelVibratoDelayCounters
add hl, bc
ld [hl], a
ld hl, wChannelFlags1
add hl, bc
res BIT_PITCH_SLIDE_ON, [hl]
res BIT_PITCH_SLIDE_DECREASING, [hl]
; --- this section is only present in this copy of the sound engine
ld a, c
cp CHAN5
jr nz, .beginChecks
ld a, [wLowHealthAlarm]
bit BIT_LOW_HEALTH_ALARM, a
ret nz
.beginChecks
; ---
call Audio2_sound_ret
ret
Audio2_sound_ret:
call Audio2_GetNextMusicByte
ld d, a
cp sound_ret_cmd
jp nz, Audio2_sound_call
ld b, 0
ld hl, wChannelFlags1
add hl, bc
bit BIT_SOUND_CALL, [hl]
jr nz, .returnFromCall
ld a, c
cp CHAN4
jr nc, .noiseOrSfxChannel
jr .disableChannelOutput
.noiseOrSfxChannel
res BIT_NOISE_OR_SFX, [hl]
ld hl, wChannelFlags2
add hl, bc
res BIT_EXECUTE_MUSIC, [hl]
cp CHAN7
jr nz, .skipSfxChannel3
; restart hardware channel 3 (wave channel) output
ld a, $0
ldh [rNR30], a
ld a, $80
ldh [rNR30], a
.skipSfxChannel3
jr nz, .dontDisable
ld a, [wDisableChannelOutputWhenSfxEnds]
and a
jr z, .dontDisable
xor a
ld [wDisableChannelOutputWhenSfxEnds], a
jr .disableChannelOutput
.dontDisable
jr .afterDisable
.returnFromCall
res BIT_SOUND_CALL, [hl]
ld d, $0
ld a, c
add a
ld e, a
ld hl, wChannelCommandPointers
add hl, de
push hl ; store current channel address
ld hl, wChannelReturnAddresses
add hl, de
ld e, l
ld d, h
pop hl
ld a, [de]
ld [hli], a
inc de
ld a, [de]
ld [hl], a ; loads channel address to return to
jp Audio2_sound_ret
.disableChannelOutput
ld hl, Audio2_HWChannelDisableMasks
add hl, bc
ldh a, [rNR51]
and [hl]
ldh [rNR51], a
.afterDisable
ld a, [wChannelSoundIDs + CHAN5]
cp CRY_SFX_START
jr nc, .maybeCry
jr .skipCry
.maybeCry
ld a, [wChannelSoundIDs + CHAN5]
cp CRY_SFX_END
jr z, .skipCry
jr c, .cry
jr .skipCry
.cry
ld a, c
cp CHAN5
jr z, .skipRewind
call Audio2_GoBackOneCommandIfCry
ret c
.skipRewind
ld a, [wSavedVolume]
ldh [rNR50], a
xor a
ld [wSavedVolume], a
.skipCry
ld hl, wChannelSoundIDs
add hl, bc
ld [hl], b
ret
Audio2_sound_call:
cp sound_call_cmd
jp nz, Audio2_sound_loop
call Audio2_GetNextMusicByte
push af
call Audio2_GetNextMusicByte
ld d, a
pop af
ld e, a
push de ; store pointer
ld d, $0
ld a, c
add a
ld e, a
ld hl, wChannelCommandPointers
add hl, de
push hl
ld hl, wChannelReturnAddresses
add hl, de
ld e, l
ld d, h
pop hl
ld a, [hli]
ld [de], a
inc de
ld a, [hld]
ld [de], a ; copy current channel address
pop de
ld [hl], e
inc hl
ld [hl], d ; overwrite current address with pointer
ld b, $0
ld hl, wChannelFlags1
add hl, bc
set BIT_SOUND_CALL, [hl] ; set the call flag
jp Audio2_sound_ret
Audio2_sound_loop:
cp sound_loop_cmd
jp nz, Audio2_note_type
call Audio2_GetNextMusicByte
ld e, a
and a
jr z, .infiniteLoop
ld b, 0
ld hl, wChannelLoopCounters
add hl, bc
ld a, [hl]
cp e
jr nz, .loopAgain
ld a, $1 ; if no more loops to make,
ld [hl], a
call Audio2_GetNextMusicByte ; skip pointer
call Audio2_GetNextMusicByte
jp Audio2_sound_ret
.loopAgain ; inc loop count
inc a
ld [hl], a
; fall through
.infiniteLoop ; overwrite current address with pointer
call Audio2_GetNextMusicByte
push af
call Audio2_GetNextMusicByte
ld b, a
ld d, $0
ld a, c
add a
ld e, a
ld hl, wChannelCommandPointers
add hl, de
pop af
ld [hli], a
ld [hl], b
jp Audio2_sound_ret
Audio2_note_type:
and $f0
cp note_type_cmd
jp nz, Audio2_toggle_perfect_pitch
ld a, d
and $f
ld b, $0
ld hl, wChannelNoteSpeeds
add hl, bc
ld [hl], a ; store low nibble as speed
ld a, c
cp CHAN4
jr z, .noiseChannel ; noise channel has 0 params
call Audio2_GetNextMusicByte
ld d, a
ld a, c
cp CHAN3
jr z, .musicChannel3
cp CHAN7
jr nz, .skipChannel3
ld hl, wSfxWaveInstrument
jr .channel3
.musicChannel3
ld hl, wMusicWaveInstrument
.channel3
ld a, d
and $f
ld [hl], a ; store low nibble of param as wave instrument
ld a, d
and $30
sla a
ld d, a
; fall through
; if channel 3, store high nibble as volume
; else, store volume (high nibble) and fade (low nibble)
.skipChannel3
ld b, 0
ld hl, wChannelVolumes
add hl, bc
ld [hl], d
.noiseChannel
jp Audio2_sound_ret
Audio2_toggle_perfect_pitch:
ld a, d
cp toggle_perfect_pitch_cmd
jr nz, Audio2_vibrato
ld b, 0
ld hl, wChannelFlags1
add hl, bc
ld a, [hl]
xor 1 << BIT_PERFECT_PITCH
ld [hl], a
jp Audio2_sound_ret
Audio2_vibrato:
cp vibrato_cmd
jr nz, Audio2_pitch_slide
call Audio2_GetNextMusicByte
ld b, 0
ld hl, wChannelVibratoDelayCounters
add hl, bc
ld [hl], a ; store delay
ld hl, wChannelVibratoDelayCounterReloadValues
add hl, bc
ld [hl], a ; store delay
call Audio2_GetNextMusicByte
ld d, a
; The high nybble of the command byte is the extent of the vibrato.
; Let n be the extent.
; The upper nybble of the channel's byte in the wChannelVibratoExtents
; array will store the extent above the note: (n / 2) + (n % 2).
; The lower nybble will store the extent below the note: (n / 2).
; These two values add to the total extent, n.
and $f0
swap a
ld b, 0
ld hl, wChannelVibratoExtents
add hl, bc
srl a
ld e, a
adc b
swap a
or e
ld [hl], a
; The low nybble of the command byte is the rate of the vibrato.
; The high and low nybbles of the channel's byte in the wChannelVibratoRates
; array are both initialised to this value because the high nybble is the
; counter reload value and the low nybble is the counter itself, which should
; start at its value upon reload.
ld a, d
and $f
ld d, a
ld hl, wChannelVibratoRates
add hl, bc
swap a
or d
ld [hl], a
jp Audio2_sound_ret
Audio2_pitch_slide:
cp pitch_slide_cmd
jr nz, Audio2_duty_cycle
call Audio2_GetNextMusicByte
ld b, 0
ld hl, wChannelPitchSlideLengthModifiers
add hl, bc
ld [hl], a
call Audio2_GetNextMusicByte
ld d, a
and $f0
swap a
ld b, a
ld a, d
and $f
call Audio2_CalculateFrequency
ld b, 0
ld hl, wChannelPitchSlideTargetFrequencyHighBytes
add hl, bc
ld [hl], d
ld hl, wChannelPitchSlideTargetFrequencyLowBytes
add hl, bc
ld [hl], e
ld b, 0
ld hl, wChannelFlags1
add hl, bc
set BIT_PITCH_SLIDE_ON, [hl]
call Audio2_GetNextMusicByte
ld d, a
jp Audio2_note_length
Audio2_duty_cycle:
cp duty_cycle_cmd
jr nz, Audio2_tempo
call Audio2_GetNextMusicByte
rrca
rrca
and $c0
ld b, 0
ld hl, wChannelDutyCycles
add hl, bc
ld [hl], a ; store duty cycle
jp Audio2_sound_ret
Audio2_tempo:
cp tempo_cmd
jr nz, Audio2_stereo_panning
ld a, c
cp CHAN5
jr nc, .sfxChannel
call Audio2_GetNextMusicByte
ld [wMusicTempo], a ; store first param
call Audio2_GetNextMusicByte
ld [wMusicTempo + 1], a ; store second param
xor a
ld [wChannelNoteDelayCountersFractionalPart], a ; clear RAM
ld [wChannelNoteDelayCountersFractionalPart + 1], a
ld [wChannelNoteDelayCountersFractionalPart + 2], a
ld [wChannelNoteDelayCountersFractionalPart + 3], a
jr .musicChannelDone
.sfxChannel
call Audio2_GetNextMusicByte
ld [wSfxTempo], a ; store first param
call Audio2_GetNextMusicByte
ld [wSfxTempo + 1], a ; store second param
xor a
ld [wChannelNoteDelayCountersFractionalPart + 4], a ; clear RAM
ld [wChannelNoteDelayCountersFractionalPart + 5], a
ld [wChannelNoteDelayCountersFractionalPart + 6], a
ld [wChannelNoteDelayCountersFractionalPart + 7], a
.musicChannelDone
jp Audio2_sound_ret
Audio2_stereo_panning:
cp stereo_panning_cmd
jr nz, Audio2_unknownmusic0xef
call Audio2_GetNextMusicByte
ld [wStereoPanning], a ; store panning
jp Audio2_sound_ret
; this appears to never be used
Audio2_unknownmusic0xef:
cp unknownmusic0xef_cmd
jr nz, Audio2_duty_cycle_pattern
call Audio2_GetNextMusicByte
push bc
call Audio2_PlaySound
pop bc
ld a, [wDisableChannelOutputWhenSfxEnds]
and a
jr nz, .skip
ld a, [wChannelSoundIDs + CHAN8]
ld [wDisableChannelOutputWhenSfxEnds], a
xor a
ld [wChannelSoundIDs + CHAN8], a
.skip
jp Audio2_sound_ret
Audio2_duty_cycle_pattern:
cp duty_cycle_pattern_cmd
jr nz, Audio2_volume
call Audio2_GetNextMusicByte
ld b, 0
ld hl, wChannelDutyCyclePatterns
add hl, bc
ld [hl], a ; store full pattern
and %11000000
ld hl, wChannelDutyCycles
add hl, bc
ld [hl], a ; store first duty cycle
ld hl, wChannelFlags1
add hl, bc
set BIT_ROTATE_DUTY_CYCLE, [hl]
jp Audio2_sound_ret
Audio2_volume:
cp volume_cmd
jr nz, Audio2_execute_music
call Audio2_GetNextMusicByte
ldh [rNR50], a ; store volume
jp Audio2_sound_ret
Audio2_execute_music:
cp execute_music_cmd
jr nz, Audio2_octave
ld b, $0
ld hl, wChannelFlags2
add hl, bc
set BIT_EXECUTE_MUSIC, [hl]
jp Audio2_sound_ret
Audio2_octave:
and $f0
cp octave_cmd
jr nz, Audio2_sfx_note
ld hl, wChannelOctaves
ld b, 0
add hl, bc
ld a, d
and $f
ld [hl], a ; store low nibble as octave
jp Audio2_sound_ret
; sfx_note is either square_note or noise_note depending on the channel
Audio2_sfx_note:
cp sfx_note_cmd
jr nz, Audio2_pitch_sweep
ld a, c
cp CHAN4 ; is this a noise or sfx channel?
jr c, Audio2_pitch_sweep ; no
ld b, 0
ld hl, wChannelFlags2
add hl, bc
bit BIT_EXECUTE_MUSIC, [hl] ; is execute_music being used?
jr nz, Audio2_pitch_sweep ; yes
call Audio2_note_length
; This code seems to do the same thing as what Audio2_ApplyDutyCycleAndSoundLength
; does below.
ld d, a
ld b, 0
ld hl, wChannelDutyCycles
add hl, bc
ld a, [hl]
or d
ld d, a
ld b, REG_DUTY_SOUND_LEN
call Audio2_GetRegisterPointer
ld [hl], d
call Audio2_GetNextMusicByte
ld d, a
ld b, REG_VOLUME_ENVELOPE
call Audio2_GetRegisterPointer
ld [hl], d
call Audio2_GetNextMusicByte
ld e, a
ld a, c
cp CHAN8
ld a, 0
jr z, .skip
; Channels 1 through 3 have 2 registers that control frequency, but the noise
; channel a single register (the polynomial counter) that controls frequency,
; so this command has one less byte on the noise channel.
push de
call Audio2_GetNextMusicByte
pop de
.skip
ld d, a
push de
call Audio2_ApplyDutyCycleAndSoundLength
call Audio2_EnableChannelOutput
pop de
call Audio2_ApplyWavePatternAndFrequency
ret
Audio2_pitch_sweep:
ld a, c
cp CHAN5
jr c, Audio2_note ; if not a sfx
ld a, d
cp pitch_sweep_cmd
jr nz, Audio2_note
ld b, $0
ld hl, wChannelFlags2
add hl, bc
bit BIT_EXECUTE_MUSIC, [hl]
jr nz, Audio2_note ; no
call Audio2_GetNextMusicByte
ldh [rNR10], a
jp Audio2_sound_ret
Audio2_note:
ld a, c
cp CHAN4
jr nz, Audio2_note_length ; if not noise channel
ld a, d
and $f0
cp drum_note_cmd
jr z, .drum_note
jr nc, Audio2_note_length
; this executes when on the noise channel and
; the command id is less than drum_note_cmd ($b0)
; in this case, the upper nybble is used as the noise instrument ($1-$a)
; and the lower nybble is the length minus 1 (0-15)
; however, this doesn't work for instrument #2 because the command id
; is captured by the noise_note command (command id $2x)
; this essentially acts like a drum_note command that is only 1 byte
; instead of 2 and can only be used with instruments 1 and 3 through 10
; this is unused by the game
swap a
ld b, a
ld a, d
and $f
ld d, a
ld a, b
push de
push bc
jr .playDnote
.drum_note
ld a, d
and $f
push af
push bc
call Audio2_GetNextMusicByte ; get drum_note instrument
.playDnote
ld d, a
ld a, [wDisableChannelOutputWhenSfxEnds]
and a
jr nz, .skipDnote
ld a, d
call Audio2_PlaySound
.skipDnote
pop bc
pop de
Audio2_note_length:
ld a, d
push af
and $f
inc a
ld b, 0
ld e, a ; store note length (in 16ths)
ld d, b
ld hl, wChannelNoteSpeeds
add hl, bc
ld a, [hl]
ld l, b
call Audio2_MultiplyAdd
ld a, c
cp CHAN5
jr nc, .sfxChannel
ld a, [wMusicTempo]
ld d, a
ld a, [wMusicTempo + 1]
ld e, a
jr .skip
.sfxChannel
ld d, $1
ld e, $0
cp CHAN8
jr z, .skip ; if noise channel
call Audio2_SetSfxTempo
ld a, [wSfxTempo]
ld d, a
ld a, [wSfxTempo + 1]
ld e, a
.skip
ld a, l ; a = note_length * note_speed
ld b, 0
ld hl, wChannelNoteDelayCountersFractionalPart
add hl, bc
ld l, [hl]
call Audio2_MultiplyAdd
ld e, l
ld d, h ; de = note_delay_frac_part + (note_length * note_speed * tempo)
ld hl, wChannelNoteDelayCountersFractionalPart
add hl, bc
ld [hl], e
ld a, d
ld hl, wChannelNoteDelayCounters
add hl, bc
ld [hl], a
ld hl, wChannelFlags2
add hl, bc
bit BIT_EXECUTE_MUSIC, [hl]
jr nz, Audio2_note_pitch
ld hl, wChannelFlags1
add hl, bc
bit BIT_NOISE_OR_SFX, [hl]
jr z, Audio2_note_pitch
pop hl
ret
Audio2_note_pitch:
pop af
and $f0
cp rest_cmd
jr nz, .notRest
ld a, c
cp CHAN5
jr nc, .next
; If this isn't an SFX channel, try the corresponding SFX channel.
ld hl, wChannelSoundIDs + CHAN5
add hl, bc
ld a, [hl]
and a
jr nz, .done
; fall through
.next
ld a, c
cp CHAN3
jr z, .channel3
cp CHAN7
jr nz, .notChannel3
.channel3
ld b, 0
ld hl, Audio2_HWChannelDisableMasks
add hl, bc
ldh a, [rNR51]
and [hl]
ldh [rNR51], a ; disable hardware channel 3's output
jr .done
.notChannel3
ld b, REG_VOLUME_ENVELOPE
call Audio2_GetRegisterPointer
ld a, $8 ; fade in sound
ld [hli], a
inc hl
ld a, $80 ; restart sound
ld [hl], a
.done
ret
.notRest
swap a
ld b, 0
ld hl, wChannelOctaves
add hl, bc
ld b, [hl]
call Audio2_CalculateFrequency
ld b, 0
ld hl, wChannelFlags1
add hl, bc
bit BIT_PITCH_SLIDE_ON, [hl]
jr z, .skipPitchSlide
call Audio2_InitPitchSlideVars
.skipPitchSlide
push de
ld a, c
cp CHAN5
jr nc, .sfxChannel ; if sfx channel
; If this isn't an SFX channel, try the corresponding SFX channel.
ld hl, wChannelSoundIDs + CHAN5
ld d, 0
ld e, a
add hl, de
ld a, [hl]
and a
jr nz, .noSfx
jr .sfxChannel
.noSfx
pop de
ret
.sfxChannel
ld b, 0
ld hl, wChannelVolumes
add hl, bc
ld d, [hl]
ld b, REG_VOLUME_ENVELOPE
call Audio2_GetRegisterPointer
ld [hl], d
call Audio2_ApplyDutyCycleAndSoundLength
call Audio2_EnableChannelOutput
pop de
ld b, $0
ld hl, wChannelFlags1
add hl, bc
bit BIT_PERFECT_PITCH, [hl] ; has toggle_perfect_pitch been used?
jr z, .skipFrequencyInc
inc e ; if yes, increment the frequency by 1
jr nc, .skipFrequencyInc ; Likely a mistake, because `inc` does not set flag C.
; Fortunately this does not seem to affect any notes that actually occur.
inc d
.skipFrequencyInc
ld hl, wChannelFrequencyLowBytes
add hl, bc
ld [hl], e
call Audio2_ApplyWavePatternAndFrequency
ret
Audio2_EnableChannelOutput:
ld b, 0
ld hl, Audio2_HWChannelEnableMasks
add hl, bc
ldh a, [rNR51]
or [hl] ; set this channel's bits
ld d, a
ld a, c
cp CHAN8
jr z, .noiseChannelOrNoSfx
cp CHAN5
jr nc, .skip ; if sfx channel
; If this isn't an SFX channel, try the corresponding SFX channel.
ld hl, wChannelSoundIDs + CHAN5
add hl, bc
ld a, [hl]
and a
jr nz, .skip
.noiseChannelOrNoSfx
; If this is the SFX noise channel or a music channel whose corresponding
; SFX channel is off, apply stereo panning.
ld a, [wStereoPanning]
ld hl, Audio2_HWChannelEnableMasks
add hl, bc
and [hl]
ld d, a
ldh a, [rNR51]
ld hl, Audio2_HWChannelDisableMasks
add hl, bc
and [hl] ; reset this channel's output bits
or d ; set this channel's output bits that enabled in [wStereoPanning]
ld d, a
.skip
ld a, d
ldh [rNR51], a
ret
Audio2_ApplyDutyCycleAndSoundLength:
ld b, 0
ld hl, wChannelNoteDelayCounters ; use the note delay as sound length
add hl, bc
ld d, [hl]
ld a, c
cp CHAN3
jr z, .skipDuty ; if music channel 3
cp CHAN7
jr z, .skipDuty ; if sfx channel 3
; include duty cycle (except on channel 3 which doesn't have it)
ld a, d
and $3f
ld d, a
ld hl, wChannelDutyCycles
add hl, bc
ld a, [hl]
or d
ld d, a
.skipDuty
ld b, REG_DUTY_SOUND_LEN
call Audio2_GetRegisterPointer
ld [hl], d
ret
Audio2_ApplyWavePatternAndFrequency:
ld a, c
cp CHAN3
jr z, .channel3
cp CHAN7
jr nz, .notChannel3
; fall through
.channel3
push de
ld de, wMusicWaveInstrument
cp CHAN3
jr z, .next
ld de, wSfxWaveInstrument
.next
ld a, [de]
add a
ld d, 0
ld e, a
ld hl, Audio2_WavePointers
add hl, de
ld e, [hl]
inc hl
ld d, [hl]
ld hl, rWave_0
ld b, $f
ld a, $0 ; stop hardware channel 3
ldh [rNR30], a
.loop
ld a, [de]
inc de
ld [hli], a
ld a, b
dec b
and a
jr nz, .loop
ld a, $80 ; start hardware channel 3
ldh [rNR30], a
pop de
.notChannel3
ld a, d
or $80 ; use counter mode (i.e. disable output when the counter reaches 0)
and $c7 ; zero the unused bits in the register
ld d, a
ld b, REG_FREQUENCY_LO
call Audio2_GetRegisterPointer
ld [hl], e ; store frequency low byte
inc hl
ld [hl], d ; store frequency high byte
; --- this section is only present in this copy of the sound engine
ld a, c
cp CHAN5
jr c, .musicChannel
call Audio2_ApplyFrequencyModifier
.musicChannel
; ---
ret
; --- this section is only present in this copy of the sound engine
; unused
Audio2_ResetCryModifiers:
ld a, c
cp CHAN5
jr nz, .skip
ld a, [wLowHealthAlarm]
bit BIT_LOW_HEALTH_ALARM, a
jr z, .skip
xor a
ld [wFrequencyModifier], a
ld a, $80
ld [wTempoModifier], a
.skip
ret
; ---
Audio2_SetSfxTempo:
call Audio2_IsCry
jr c, .skipCryCheck
call Audio2_IsBattleSFX
jr nc, .notCry
.skipCryCheck
ld d, 0
ld a, [wTempoModifier]
add $80
jr nc, .next