-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathengine.asm
2850 lines (2646 loc) · 46.8 KB
/
engine.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 entire sound engine. Uses section "audio" in WRAM.
; Interfaces are in bank 0.
; Notable functions:
; FadeMusic
; PlayStereoSFX
_MapSetup_Sound_Off::
; restart sound operation
; clear all relevant hardware registers & wram
push hl
push de
push bc
push af
call MusicOff
ld hl, rNR50 ; channel control registers
xor a
ld [hli], a ; rNR50 ; volume/vin
ld [hli], a ; rNR51 ; sfx channels
ld a, $80 ; all channels on
ld [hli], a ; ff26 ; music channels
ld hl, rNR10 ; sound channel registers
ld e, NUM_MUSIC_CHANS
.clearsound
; sound channel 1 2 3 4
xor a
ld [hli], a ; rNR10, rNR20, rNR30, rNR40 ; sweep = 0
ld [hli], a ; rNR11, rNR21, rNR31, rNR41 ; length/wavepattern = 0
ld a, $8
ld [hli], a ; rNR12, rNR22, rNR32, rNR42 ; envelope = 0
xor a
ld [hli], a ; rNR13, rNR23, rNR33, rNR43 ; frequency lo = 0
ld a, $80
ld [hli], a ; rNR14, rNR24, rNR34, rNR44 ; restart sound (freq hi = 0)
dec e
jr nz, .clearsound
ld hl, wChannels ; start of channel data
ld de, wChannelsEnd - wChannels ; length of area to clear (entire sound wram area)
.clearchannels
xor a
ld [hli], a
dec de
ld a, e
or d
jr nz, .clearchannels
ld a, MAX_VOLUME
ld [wVolume], a
call MusicOn
pop af
pop bc
pop de
pop hl
ret
MusicFadeRestart:
; restart but keep the music id to fade in to
ld a, [wMusicFadeID + 1]
push af
ld a, [wMusicFadeID]
push af
call _MapSetup_Sound_Off
pop af
ld [wMusicFadeID], a
pop af
ld [wMusicFadeID + 1], a
ret
MusicOn:
ld a, 1
ld [wMusicPlaying], a
ret
MusicOff:
xor a
ld [wMusicPlaying], a
ret
_UpdateSound::
; called once per frame
; no use updating audio if it's not playing
ld a, [wMusicPlaying]
and a
ret z
; start at ch1
xor a
ld [wCurChannel], a ; just
ld [wSoundOutput], a ; off
ld bc, wChannel1
.loop
; is the channel active?
ld hl, CHANNEL_FLAGS1
add hl, bc
bit SOUND_CHANNEL_ON, [hl]
jp z, .nextchannel
; check time left in the current note
ld hl, CHANNEL_NOTE_DURATION
add hl, bc
ld a, [hl]
cp $2 ; 1 or 0?
jr c, .noteover
dec [hl]
jr .continue_sound_update
.noteover
; reset vibrato delay
ld hl, CHANNEL_VIBRATO_DELAY
add hl, bc
ld a, [hl]
ld hl, CHANNEL_VIBRATO_DELAY_COUNT
add hl, bc
ld [hl], a
; turn vibrato off for now
ld hl, CHANNEL_FLAGS2
add hl, bc
res SOUND_PITCH_WHEEL, [hl]
; get next note
call ParseMusic
.continue_sound_update
call ApplyPitchWheel
; duty cycle
ld hl, CHANNEL_DUTY_CYCLE
add hl, bc
ld a, [hli]
ld [wCurTrackDuty], a
; intensity
ld a, [hli]
ld [wCurTrackIntensity], a
; frequency
ld a, [hli]
ld [wCurTrackFrequency], a
ld a, [hl]
ld [wCurTrackFrequency + 1], a
; vibrato, noise
call HandleTrackVibrato ; handle vibrato and other things
call HandleNoise
; turn off music when playing sfx?
ld a, [wSFXPriority]
and a
jr z, .next
; are we in a sfx channel right now?
ld a, [wCurChannel]
cp NUM_MUSIC_CHANS
jr nc, .next
; are any sfx channels active?
; if so, mute
ld hl, wChannel5Flags1
bit SOUND_CHANNEL_ON, [hl]
jr nz, .restnote
ld hl, wChannel6Flags1
bit SOUND_CHANNEL_ON, [hl]
jr nz, .restnote
ld hl, wChannel7Flags1
bit SOUND_CHANNEL_ON, [hl]
jr nz, .restnote
ld hl, wChannel8Flags1
bit SOUND_CHANNEL_ON, [hl]
jr z, .next
.restnote
ld hl, CHANNEL_NOTE_FLAGS
add hl, bc
set NOTE_REST, [hl] ; Rest
.next
; are we in a sfx channel right now?
ld a, [wCurChannel]
cp NUM_MUSIC_CHANS
jr nc, .sfx_channel
ld hl, CHANNEL_STRUCT_LENGTH * NUM_MUSIC_CHANS + CHANNEL_FLAGS1
add hl, bc
bit SOUND_CHANNEL_ON, [hl]
jr nz, .sound_channel_on
.sfx_channel
call UpdateChannels
ld hl, CHANNEL_TRACKS
add hl, bc
ld a, [wSoundOutput]
or [hl]
ld [wSoundOutput], a
.sound_channel_on
; clear note flags
ld hl, CHANNEL_NOTE_FLAGS
add hl, bc
xor a
ld [hl], a
.nextchannel
; next channel
ld hl, CHANNEL_STRUCT_LENGTH
add hl, bc
ld c, l
ld b, h
ld a, [wCurChannel]
inc a
ld [wCurChannel], a
cp NUM_CHANNELS ; are we done?
jp nz, .loop ; do it all again
call PlayDanger
; fade music in/out
call FadeMusic
; write volume to hardware register
ld a, [wVolume]
ldh [rNR50], a
; write SO on/off to hardware register
ld a, [wSoundOutput]
ldh [rNR51], a
ret
UpdateChannels:
ld hl, .ChannelFnPtrs
ld a, [wCurChannel]
and $7
add a
ld e, a
ld d, 0
add hl, de
ld a, [hli]
ld h, [hl]
ld l, a
jp hl
.ChannelFnPtrs:
dw .Channel1
dw .Channel2
dw .Channel3
dw .Channel4
; sfx ch ptrs are identical to music chs
; ..except 5
dw .Channel5
dw .Channel6
dw .Channel7
dw .Channel8
.Channel1:
ld a, [wLowHealthAlarm]
bit DANGER_ON_F, a
ret nz
.Channel5:
ld hl, CHANNEL_NOTE_FLAGS
add hl, bc
bit NOTE_UNKN_3, [hl]
jr z, .asm_e8159
;
ld a, [wSoundInput]
ldh [rNR10], a
.asm_e8159
bit NOTE_REST, [hl] ; rest
jr nz, .ch1rest
bit NOTE_NOISE_SAMPLING, [hl]
jr nz, .asm_e81a2
bit NOTE_FREQ_OVERRIDE, [hl]
jr nz, .frequency_override
bit NOTE_VIBRATO_OVERRIDE, [hl]
jr nz, .asm_e8184
jr .check_duty_override
.frequency_override
ld a, [wCurTrackFrequency]
ldh [rNR13], a
ld a, [wCurTrackFrequency + 1]
ldh [rNR14], a
.check_duty_override
bit NOTE_DUTY_OVERRIDE, [hl]
ret z
ld a, [wCurTrackDuty]
ld d, a
ldh a, [rNR11]
and $3f ; sound length
or d
ldh [rNR11], a
ret
.asm_e8184
ld a, [wCurTrackDuty]
ld d, a
ldh a, [rNR11]
and $3f ; sound length
or d
ldh [rNR11], a
ld a, [wCurTrackFrequency]
ldh [rNR13], a
ret
.ch1rest
ldh a, [rNR52]
and %10001110 ; ch1 off
ldh [rNR52], a
ld hl, rNR10
call ClearChannel
ret
.asm_e81a2
ld hl, wCurTrackDuty
ld a, $3f ; sound length
or [hl]
ldh [rNR11], a
ld a, [wCurTrackIntensity]
ldh [rNR12], a
ld a, [wCurTrackFrequency]
ldh [rNR13], a
ld a, [wCurTrackFrequency + 1]
or $80
ldh [rNR14], a
ret
.Channel2:
.Channel6:
ld hl, CHANNEL_NOTE_FLAGS
add hl, bc
bit NOTE_REST, [hl] ; rest
jr nz, .ch2rest
bit NOTE_NOISE_SAMPLING, [hl]
jr nz, .asm_e8204
bit NOTE_VIBRATO_OVERRIDE, [hl]
jr nz, .asm_e81e6
bit NOTE_DUTY_OVERRIDE, [hl]
ret z
ld a, [wCurTrackDuty]
ld d, a
ldh a, [rNR21]
and $3f ; sound length
or d
ldh [rNR21], a
ret
.asm_e81db ; unused
ld a, [wCurTrackFrequency]
ldh [rNR23], a
ld a, [wCurTrackFrequency + 1]
ldh [rNR24], a
ret
.asm_e81e6
ld a, [wCurTrackDuty]
ld d, a
ldh a, [rNR21]
and $3f ; sound length
or d
ldh [rNR21], a
ld a, [wCurTrackFrequency]
ldh [rNR23], a
ret
.ch2rest
ldh a, [rNR52]
and %10001101 ; ch2 off
ldh [rNR52], a
ld hl, rNR20
call ClearChannel
ret
.asm_e8204
ld hl, wCurTrackDuty
ld a, $3f ; sound length
or [hl]
ldh [rNR21], a
ld a, [wCurTrackIntensity]
ldh [rNR22], a
ld a, [wCurTrackFrequency]
ldh [rNR23], a
ld a, [wCurTrackFrequency + 1]
or $80 ; initial (restart)
ldh [rNR24], a
ret
.Channel3:
.Channel7:
ld hl, CHANNEL_NOTE_FLAGS
add hl, bc
bit NOTE_REST, [hl] ; rest
jr nz, .ch3rest
bit NOTE_NOISE_SAMPLING, [hl]
jr nz, .asm_e824d
bit NOTE_VIBRATO_OVERRIDE, [hl]
jr nz, .asm_e823a
ret
.asm_e822f ; unused
ld a, [wCurTrackFrequency]
ldh [rNR33], a
ld a, [wCurTrackFrequency + 1]
ldh [rNR34], a
ret
.asm_e823a
ld a, [wCurTrackFrequency]
ldh [rNR33], a
ret
.ch3rest
ldh a, [rNR52]
and %10001011 ; ch3 off
ldh [rNR52], a
ld hl, rNR30
call ClearChannel
ret
.asm_e824d
ld a, $3f ; sound length
ldh [rNR31], a
xor a
ldh [rNR30], a
call .asm_e8268
ld a, $80
ldh [rNR30], a
ld a, [wCurTrackFrequency]
ldh [rNR33], a
ld a, [wCurTrackFrequency + 1]
or $80
ldh [rNR34], a
ret
.asm_e8268
push hl
ld a, [wCurTrackIntensity]
and $f ; only 0-9 are valid
ld l, a
ld h, 0
; hl << 4
; each wavepattern is $f bytes long
; so seeking is done in $10s
rept 4
add hl, hl
endr
ld de, WaveSamples
add hl, de
; load wavepattern into rWave_0-rWave_f
ld a, [hli]
ldh [rWave_0], a
ld a, [hli]
ldh [rWave_1], a
ld a, [hli]
ldh [rWave_2], a
ld a, [hli]
ldh [rWave_3], a
ld a, [hli]
ldh [rWave_4], a
ld a, [hli]
ldh [rWave_5], a
ld a, [hli]
ldh [rWave_6], a
ld a, [hli]
ldh [rWave_7], a
ld a, [hli]
ldh [rWave_8], a
ld a, [hli]
ldh [rWave_9], a
ld a, [hli]
ldh [rWave_a], a
ld a, [hli]
ldh [rWave_b], a
ld a, [hli]
ldh [rWave_c], a
ld a, [hli]
ldh [rWave_d], a
ld a, [hli]
ldh [rWave_e], a
ld a, [hli]
ldh [rWave_f], a
pop hl
ld a, [wCurTrackIntensity]
and $f0
sla a
ldh [rNR32], a
ret
.Channel4:
.Channel8:
ld hl, CHANNEL_NOTE_FLAGS
add hl, bc
bit NOTE_REST, [hl] ; rest
jr nz, .ch4rest
bit NOTE_NOISE_SAMPLING, [hl]
jr nz, .asm_e82d4
ret
.asm_e82c1 ; unused
ld a, [wCurTrackFrequency]
ldh [rNR43], a
ret
.ch4rest
ldh a, [rNR52]
and %10000111 ; ch4 off
ldh [rNR52], a
ld hl, rNR40
call ClearChannel
ret
.asm_e82d4
ld a, $3f ; sound length
ldh [rNR41], a
ld a, [wCurTrackIntensity]
ldh [rNR42], a
ld a, [wCurTrackFrequency]
ldh [rNR43], a
ld a, $80
ldh [rNR44], a
ret
_CheckSFX:
; return carry if any sfx channels are active
ld hl, wChannel5Flags1
bit SOUND_CHANNEL_ON, [hl]
jr nz, .sfxon
ld hl, wChannel6Flags1
bit SOUND_CHANNEL_ON, [hl]
jr nz, .sfxon
ld hl, wChannel7Flags1
bit SOUND_CHANNEL_ON, [hl]
jr nz, .sfxon
ld hl, wChannel8Flags1
bit SOUND_CHANNEL_ON, [hl]
jr nz, .sfxon
and a
ret
.sfxon
scf
ret
PlayDanger:
ld a, [wLowHealthAlarm]
bit DANGER_ON_F, a
ret z
; Don't do anything if SFX is being played
and $ff ^ (1 << DANGER_ON_F)
ld d, a
call _CheckSFX
jr c, .increment
; Play the high tone
and a
jr z, .begin
; Play the low tone
cp 16
jr z, .halfway
jr .increment
.halfway
ld hl, DangerSoundLow
jr .applychannel
.begin
ld hl, DangerSoundHigh
.applychannel
xor a
ldh [rNR10], a
ld a, [hli]
ldh [rNR11], a
ld a, [hli]
ldh [rNR12], a
ld a, [hli]
ldh [rNR13], a
ld a, [hli]
ldh [rNR14], a
.increment
ld a, d
inc a
cp 30 ; Ending frame
jr c, .noreset
xor a
.noreset
; Make sure the danger sound is kept on
or 1 << DANGER_ON_F
ld [wLowHealthAlarm], a
; Enable channel 1 if it's off
ld a, [wSoundOutput]
and $11
ret nz
ld a, [wSoundOutput]
or $11
ld [wSoundOutput], a
ret
DangerSoundHigh:
db $80 ; duty 50%
db $e2 ; volume 14, envelope decrease sweep 2
db $50 ; frequency: $750
db $87 ; restart sound
DangerSoundLow:
db $80 ; duty 50%
db $e2 ; volume 14, envelope decrease sweep 2
db $ee ; frequency: $6ee
db $86 ; restart sound
FadeMusic:
; fade music if applicable
; usage:
; write to wMusicFade
; song fades out at the given rate
; load song id in wMusicFadeID
; fade new song in
; notes:
; max # frames per volume level is $3f
; fading?
ld a, [wMusicFade]
and a
ret z
; has the count ended?
ld a, [wMusicFadeCount]
and a
jr z, .update
; count down
dec a
ld [wMusicFadeCount], a
ret
.update
ld a, [wMusicFade]
ld d, a
; get new count
and $3f
ld [wMusicFadeCount], a
; get SO1 volume
ld a, [wVolume]
and VOLUME_SO1_LEVEL
; which way are we fading?
bit MUSIC_FADE_IN_F, d
jr nz, .fadein
; fading out
and a
jr z, .novolume
dec a
jr .updatevolume
.novolume
; make sure volume is off
xor a
ld [wVolume], a
; did we just get on a bike?
ld a, [wPlayerState]
cp PLAYER_BIKE
jr z, .bicycle
push bc
; restart sound
call MusicFadeRestart
; get new song id
ld a, [wMusicFadeID]
and a
jr z, .quit ; this assumes there are fewer than 256 songs!
ld e, a
ld a, [wMusicFadeID + 1]
ld d, a
; load new song
call _PlayMusic
.quit
; cleanup
pop bc
; stop fading
xor a
ld [wMusicFade], a
ret
.bicycle
push bc
; restart sound
call MusicFadeRestart
; this turns the volume up
; turn it back down
xor a
ld [wVolume], a
; get new song id
ld a, [wMusicFadeID]
ld e, a
ld a, [wMusicFadeID + 1]
ld d, a
; load new song
call _PlayMusic
pop bc
; fade in
ld hl, wMusicFade
set MUSIC_FADE_IN_F, [hl]
ret
.fadein
; are we done?
cp MAX_VOLUME & $f
jr nc, .maxvolume
; inc volume
inc a
jr .updatevolume
.maxvolume
; we're done
xor a
ld [wMusicFade], a
ret
.updatevolume
; hi = lo
ld d, a
swap a
or d
ld [wVolume], a
ret
LoadNote:
; wait for pitch wheel to finish
ld hl, CHANNEL_FLAGS2
add hl, bc
bit SOUND_PITCH_WHEEL, [hl]
ret z
; get note duration
ld hl, CHANNEL_NOTE_DURATION
add hl, bc
ld a, [hl]
ld hl, wCurNoteDuration
sub [hl]
jr nc, .ok
ld a, 1
.ok
ld [hl], a
; get frequency
ld hl, CHANNEL_FREQUENCY
add hl, bc
ld e, [hl]
inc hl
ld d, [hl]
; get direction of pitch wheel
ld hl, CHANNEL_PITCH_WHEEL_TARGET
add hl, bc
ld a, e
sub [hl]
ld e, a
ld a, d
sbc 0
ld d, a
ld hl, CHANNEL_PITCH_WHEEL_TARGET + 1
add hl, bc
sub [hl]
jr nc, .greater_than
ld hl, CHANNEL_FLAGS3
add hl, bc
set SOUND_PITCH_WHEEL_DIR, [hl]
; get frequency
ld hl, CHANNEL_FREQUENCY
add hl, bc
ld e, [hl]
inc hl
ld d, [hl]
; ????
ld hl, CHANNEL_PITCH_WHEEL_TARGET
add hl, bc
ld a, [hl]
sub e
ld e, a
ld a, d
sbc 0
ld d, a
; ????
ld hl, CHANNEL_PITCH_WHEEL_TARGET + 1
add hl, bc
ld a, [hl]
sub d
ld d, a
jr .resume
.greater_than
ld hl, CHANNEL_FLAGS3
add hl, bc
res SOUND_PITCH_WHEEL_DIR, [hl]
; get frequency
ld hl, CHANNEL_FREQUENCY
add hl, bc
ld e, [hl]
inc hl
ld d, [hl]
; get distance from pitch wheel target
ld hl, CHANNEL_PITCH_WHEEL_TARGET
add hl, bc
ld a, e
sub [hl]
ld e, a
ld a, d
sbc 0
ld d, a
ld hl, CHANNEL_PITCH_WHEEL_TARGET + 1
add hl, bc
sub [hl]
ld d, a
.resume
; de = x * [wCurNoteDuration] + y
; x + 1 -> d
; y -> a
push bc
ld hl, wCurNoteDuration
ld b, 0 ; quotient
.loop
inc b
ld a, e
sub [hl]
ld e, a
jr nc, .loop
ld a, d
and a
jr z, .quit
dec d
jr .loop
.quit
ld a, e ; remainder
add [hl]
ld d, b ; quotient
pop bc
ld hl, CHANNEL_PITCH_WHEEL_AMOUNT
add hl, bc
ld [hl], d ; quotient
ld hl, CHANNEL_PITCH_WHEEL_AMOUNT_FRACTION
add hl, bc
ld [hl], a ; remainder
ld hl, CHANNEL_FIELD25
add hl, bc
xor a
ld [hl], a
ret
HandleTrackVibrato:
; handle duty, cry pitch, and vibrato
ld hl, CHANNEL_FLAGS2
add hl, bc
bit SOUND_DUTY, [hl] ; duty
jr z, .next
ld hl, CHANNEL_SFX_DUTY_LOOP
add hl, bc
ld a, [hl]
rlca
rlca
ld [hl], a
and $c0
ld [wCurTrackDuty], a
ld hl, CHANNEL_NOTE_FLAGS
add hl, bc
set NOTE_DUTY_OVERRIDE, [hl]
.next
ld hl, CHANNEL_FLAGS2
add hl, bc
bit SOUND_CRY_PITCH, [hl]
jr z, .vibrato
ld hl, CHANNEL_CRY_PITCH
add hl, bc
ld e, [hl]
inc hl
ld d, [hl]
ld hl, wCurTrackFrequency
ld a, [hli]
ld h, [hl]
ld l, a
add hl, de
ld e, l
ld d, h
ld hl, wCurTrackFrequency
ld [hl], e
inc hl
ld [hl], d
.vibrato
; is vibrato on?
ld hl, CHANNEL_FLAGS2
add hl, bc
bit SOUND_VIBRATO, [hl] ; vibrato
jr z, .quit
; is vibrato active for this note yet?
; is the delay over?
ld hl, CHANNEL_VIBRATO_DELAY_COUNT
add hl, bc
ld a, [hl]
and a
jr nz, .subexit
; is the extent nonzero?
ld hl, CHANNEL_VIBRATO_EXTENT
add hl, bc
ld a, [hl]
and a
jr z, .quit
; save it for later
ld d, a
; is it time to toggle vibrato up/down?
ld hl, CHANNEL_VIBRATO_RATE
add hl, bc
ld a, [hl]
and $f ; count
jr z, .toggle
.subexit
dec [hl]
jr .quit
.toggle
; refresh count
ld a, [hl]
swap [hl]
or [hl]
ld [hl], a
; ????
ld a, [wCurTrackFrequency]
ld e, a
; toggle vibrato up/down
ld hl, CHANNEL_FLAGS3
add hl, bc
bit SOUND_VIBRATO_DIR, [hl] ; vibrato up/down
jr z, .down
; up
; vibrato down
res SOUND_VIBRATO_DIR, [hl]
; get the delay
ld a, d
and $f ; lo
;
ld d, a
ld a, e
sub d
jr nc, .no_carry
ld a, 0
jr .no_carry
.down
; vibrato up
set SOUND_VIBRATO_DIR, [hl]
; get the delay
ld a, d
and $f0 ; hi
swap a ; move it to lo
;
add e
jr nc, .no_carry
ld a, $ff
.no_carry
ld [wCurTrackFrequency], a
;
ld hl, CHANNEL_NOTE_FLAGS
add hl, bc
set NOTE_VIBRATO_OVERRIDE, [hl]
.quit
ret
ApplyPitchWheel:
; quit if pitch wheel inactive
ld hl, CHANNEL_FLAGS2
add hl, bc
bit SOUND_PITCH_WHEEL, [hl]
ret z
; de = Frequency
ld hl, CHANNEL_FREQUENCY
add hl, bc
ld e, [hl]
inc hl
ld d, [hl]
; check whether pitch wheel is going up or down
ld hl, CHANNEL_FLAGS3
add hl, bc
bit SOUND_PITCH_WHEEL_DIR, [hl]
jr z, .decreasing
; frequency += [Channel*PitchWheelAmount]
ld hl, CHANNEL_PITCH_WHEEL_AMOUNT
add hl, bc
ld l, [hl]
ld h, 0
add hl, de
ld d, h
ld e, l
; [Channel*Field25] += [Channel*PitchWheelAmountFraction]
; if rollover: Frequency += 1
ld hl, CHANNEL_PITCH_WHEEL_AMOUNT_FRACTION
add hl, bc
ld a, [hl]
ld hl, CHANNEL_FIELD25
add hl, bc
add [hl]
ld [hl], a
ld a, 0
adc e
ld e, a
ld a, 0
adc d
ld d, a
; Compare the dw at [Channel*PitchWheelTarget] to de.
; If frequency is greater, we're finished.
; Otherwise, load the frequency and set two flags.
ld hl, CHANNEL_PITCH_WHEEL_TARGET + 1
add hl, bc
ld a, [hl]
cp d
jp c, .finished_pitch_wheel
jr nz, .continue_pitch_wheel
ld hl, CHANNEL_PITCH_WHEEL_TARGET
add hl, bc
ld a, [hl]
cp e
jp c, .finished_pitch_wheel
jr .continue_pitch_wheel