-
Notifications
You must be signed in to change notification settings - Fork 0
/
bank4.s
8939 lines (8665 loc) · 291 KB
/
bank4.s
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 "globals.inc"
/*
MEGAMAN 1 BANK 4 ROM MAP
*/
;---------------------------------------------------------------------------
;The music player is located in bank 4.
;Bank 4 also contains the Elecman stage and game startup screen
;settings, and therefore this code begins from $9000, not $8000.
;
; Note: Mega Man 2 has almost the same code!
;
Var41 := $41 ;If this address has low bit set, the engine pauses music (read only)
Priorities := $E0
; $E0 = #ssssmmmm
; mmmm = Music priority (always #$F?...)
; ssss = sfx priority
SFX_ChannelUsage := $E1
; $E1 = #????3210
; channels used for SFX
;
SongDataPointer := $E2 ; E2,E3
SuspendPlayback := $E4
ChannelsRemaining := $E5
PriorityTemp := $E5 ;alias
TempVibratoPointer := $E5 ;alias; occupies E5,E6
RegisterOffsetUnknown := $E6 ; mirrors the purpose of $EB? I don't understand...
SpeedUpFactor := $E7 ; If 2, each note is played at half length!
FadingSpeed := $E8 ; Set by Command $FD
VolumeFadeStrength := $E9 ; Controlled by FadingSpeed
SoundFrameCounter := $EA
CurrentChannelRegisterOffset := $EB
CurChanDataPtr := $EC ; EC,ED it's an index to AudioStatusBuffer
CurrentChannelNumber := $EE
UnknownEF := $EF
SFX_Addr := $F0 ; F0,F1
SFX_FrameWaitCount := $F2 ; Number of frames before next SFX tick
SFX_LoopCounter := $F3
; F4,F5 various temps
MUSCMD_SetSpeed = 0
MUSCMD_SetVibratoCtrl = 1 ;probably wrong
MUSCMD_SetDutyCycle = 2
MUSCMD_SetVolumeAndEnv = 3
MUSCMD_Loop = 4
MUSCMD_SetNoteBaseOffset = 5
MUSCMD_PlayNoteExtraLength = 6
MUSCMD_SetVolumeCurve = 7
MUSCMD_SetVibratoIndex = 8
MUSCMD_EndOfData = 9
MUSCMD_SetNoteDelay = $20
MUSCMD_TripletSpeed = $30
SFXCMD_SetFrameWaitCount = 0
SFXCMD_SetVibratoCtrl = 1
SFXCMD_SetDutyCycle = 2
SFXCMD_SetVolumeAndEnv = 3
SFXCMD_Loop = 4
SFXCMD_SetVibratoBuffer = 5
SFXCMD_EndOfData = 6
SFXCMD_SetNotePeriod = $80
SFXCMD_Exec_80 = $80
; Each channel data is 31 bytes ($1F)
CHNVAR_MusicDataAddr = $00 ; Pointer to the current executing position of the track
CHNVAR_NoteLengthWord = $02 ; This value is only read at 96DF
CHNVAR_TickSpeed = $04
CHNVAR_LoopCounter = $05
CHNVAR_VibratoDefs = $06 ;high 3 bits: delay in pitch? low 5 bits: vibrato depth
CHNVAR_NoteBaseOffset = $07 ;word
CHNVAR_VibratoCtrl = $09 ;probably wrong
CHNVAR_NotePeriodWord = $0A ;looks right
CHNVAR_DutyCycle = $0C ;high two bits=duty, low 6=volume and env
CHNVAR_VolumeCurveLength = $0D ;00..7F=ascending, 80..FF=descending
CHNVAR_VolumeCurvePosition = $0E
CHNVAR_VolumeCurveStrength = $0F ;high 4 bits used only
CHNVAR_VibratoCtrlSFX = $10 ;probably wrong
CHNVAR_NotePeriodWordSFX = $11 ;looks right
CHNVAR_DutyCycleSFX = $13
CHNVAR_VibratoBuffer = $14 ;14,15,16,17
CHNVAR_VibratoVar1 = $18
CHNVAR_VibratoVar2 = $19
CHNVAR_PeriodLo = $1A ; Added together with NotePeriodWord.Lo when written
CHNVAR_PeriodHi = $1B ; Added together with NotePeriodWord.Hi when written
CHNVAR_PeriodHiOld = $1C ; Cache of PeriodHi
CHNVAR_TremoloVar1 = $1D
CHNVAR_TremoloVar2 = $1E
AudioStatusBuffer := $0500 ;ends at $57C
AudioChannelStatusSize = 31
VibratoBufferPointer := $57C ;pointer to the "fifth" channel data
; Vibrato bytes;
; First byte seems to be the vibrato wavelength (aka. speed)
; Second byte:
; High nibble: Vibrato depth
; Low nibble: Vibrato wavelength (again?)
; Third byte:
; Tremolo depth | $80
; Fourth byte:
; Tremolo speed? 0=disabled
.segment "BANK4"
inc_bank_part 4, 0, $1000
SoundCodePlay: ; at $9000
jmp UpdateSound
SoundCodeInit: ; at $9003
cmp #$FC
bne :+
jmp SoundCodeCommandFC
:
cmp #$FD
bne :+
jmp SoundCodeCommandFD
:
cmp #$FE
bne :+
SoundCodeCommandFE: ; at 9015
lda #$01
sta SuspendPlayback
lda #$00
sta CurChanDataPtr
jmp ResetSoundSystem
:
cmp #$ff
bne :+
SoundCodeCommandFF: ; at 9024
lda #$01
sta SuspendPlayback
lda #$00
sta CurChanDataPtr
jmp StopMusic
:
; address of sound => SongDataPointer
asl
tax
lda MusicAndSfxTable,x
sta SongDataPointer+0
inx
lda MusicAndSfxTable,x
sta SongDataPointer+1
ldy #$00
lda (SongDataPointer),y
tax
and #$0f
beq StartSFX
StartMusicTrack:
; If this music score has lower priority than the currently playing one,
; then just return...
lda Priorities
and #$0f
sta PriorityTemp
cpx PriorityTemp
bcs :+
rts
:
; Set the priority to this new music
stx PriorityTemp
lda Priorities
and #$F0
ora PriorityTemp
sta Priorities
; Suspend playback so the sound code won't interrupt us
lda #$01
sta SuspendPlayback
lda #$00
sta CurChanDataPtr
lda #$00
sta SpeedUpFactor
sta FadingSpeed
lda #$04 ; Number of channels to loop thru
sta ChannelsRemaining
; SongDataPointer += 1
lda #1
; Loop thru channels
:
clc
adc SongDataPointer+0
sta SongDataPointer+0
lda #0
adc SongDataPointer+1
sta SongDataPointer+1
ldx CurChanDataPtr
; Copy channel's music data address to work RAM
ldy #CHNVAR_MusicDataAddr
:
lda (SongDataPointer),y
sta AudioStatusBuffer,x
inx
iny
cpy #$02
bne :-
; Clear rest of the channel's vars (02..0F). 10..1E is left intact.
ldy #$0E
lda #$00
:
sta AudioStatusBuffer,x
inx
dey
bne :-
lda SFX_ChannelUsage
lsr
bcs :+
jsr ClearSFXRAM
:
jsr SFX_NextChannel
dec ChannelsRemaining
beq :+
lda #$02
jmp :----
:
ldy #$02
lda (SongDataPointer),y
sta VibratoBufferPointer+0
iny
lda (SongDataPointer),y
sta VibratoBufferPointer+1
jsr SFX_FirstChannel
; Let music callback play again
lda #$00
sta SuspendPlayback
rts
StartSFX:; at 90BC
; If SFX priority has a lower priority then the current one, then exit
lda Priorities
and #$f0
sta PriorityTemp
cpx PriorityTemp
bcs :+
rts
:
; Set the sfx priority to this new sfx
stx PriorityTemp
lda Priorities
and #$0f
ora PriorityTemp
sta Priorities
; Suspend playback so the sound code won't interrupt us
lda #$01
sta SuspendPlayback
lda #$00
sta CurChanDataPtr
ldx #$00
lda #$02
clc
adc SongDataPointer+0
sta SFX_Addr+0
txa
adc SongDataPointer+1
sta SFX_Addr+1
stx SFX_FrameWaitCount
stx SFX_LoopCounter
; ChannelUsage
ldy #$01
lda (SongDataPointer),y
and #$0f
tax
ora SFX_ChannelUsage
pha
stx SFX_ChannelUsage
; Loop thru all channels
lda #$04
sta ChannelsRemaining
lda #$02
sta RegisterOffsetUnknown
:
pla
lsr
pha
bcc :+
; this SFX uses this channel so clear SFX RAM
jsr ClearSFXRAM
lda SFX_ChannelUsage
lsr
bcs :+
jsr UpdateChannelUsedBySFX
:
jsr SFX_NextChannel
lda #$04
clc
adc RegisterOffsetUnknown
sta RegisterOffsetUnknown
dec ChannelsRemaining
bne :--
jsr SFX_FirstChannel
lda SFX_ChannelUsage
sta UnknownEF
pla
; Let music callback play again
lda #$00
sta SuspendPlayback
rts
;
; Sound effect = #$FC
;
; Speed up the music
;
SoundCodeCommandFC: ; at 912A
iny
sty SpeedUpFactor
rts
;
; Sound effect = #$FD
;
; Fade in (negative number) or out (positive number)
;
SoundCodeCommandFD: ; at 912E
lda FadingSpeed
bne :+
sty FadingSpeed
lda #$01
sta VolumeFadeStrength
lda SoundFrameCounter ; $EA
and #$01
sta SoundFrameCounter ; $EA
:
rts
ResetSoundSystem: ; at 913F
; Set SFX priority to 0
lda Priorities
and #$0f
sta Priorities
lda #$04
sta ChannelsRemaining
lda #$02
sta RegisterOffsetUnknown
:
lda SFX_ChannelUsage
lsr
bcc :+
jsr ClearSFXRAM
jsr UpdateChannelUsedBySFX
:
jsr SFX_NextChannel
lda #$04
clc
adc RegisterOffsetUnknown
sta RegisterOffsetUnknown
dec ChannelsRemaining
bne :--
lda #$00
sta SFX_ChannelUsage
sta UnknownEF
lda #$00
sta SuspendPlayback
rts
;Loads a SFX-related pointer(?)
UpdateChannelUsedBySFX: ; at 9171
lda CurChanDataPtr
clc
adc #CHNVAR_NotePeriodWord
tax
lda AudioStatusBuffer,x ;If NotePeriodWord is nonzero?
inx
ora AudioStatusBuffer,x
bne ReloadVibrato
ldy ChannelsRemaining ;NotePeriodWord was zero.
ldx RegisterOffsetUnknown
jsr MuteChannel
ldx CurChanDataPtr
lda AudioStatusBuffer+CHNVAR_MusicDataAddr,x ;Reads MusicDataAddr
inx
ora AudioStatusBuffer+CHNVAR_MusicDataAddr,x
bne ReloadVibrato
rts
StopMusic: ; at 9193
; Set music priority to 0
lda Priorities
and #$F0
sta Priorities
lda #$00
sta SpeedUpFactor
sta FadingSpeed
; Loop thru all channels
lda #$04
sta ChannelsRemaining
:
lda #$00
ldx CurChanDataPtr ;Zeroes MusicDataAddr
sta AudioStatusBuffer,x
inx
sta AudioStatusBuffer,x
jsr NextChannelPtr
dec ChannelsRemaining
bne :-
lda #$00
sta SuspendPlayback
rts
ClearSFXRAM: ;at 91BA ;Zeroes the region $10..$1E of the current channel.
ldy #$0F
lda #$10
clc
adc CurChanDataPtr
tax
lda #$00
:
sta AudioStatusBuffer,x
inx
dey
bne :-
rts
; This routine copies the data from the currently selected
; vibrato buffer of the music track into the 14..17 range
; in the channel.
;
; It is the same region as updated by SFXCMD_SetVibratoBuffer.
;
ReloadVibrato: ; at 91CC
lda TempVibratoPointer+0 ;save this var
pha
lda TempVibratoPointer+1 ;(used temporarily for other purpose)
pha
lda VibratoBufferPointer+0
sta TempVibratoPointer+0
lda VibratoBufferPointer+1
sta TempVibratoPointer+1
lda CurChanDataPtr
clc
adc #CHNVAR_VibratoDefs
tax
lda AudioStatusBuffer,x
and #$1F
beq :++ ; No vibrato?
; Vibrato
tay
lda #$00
:
clc
adc #$04
dey
bne :-
:
tay
txa
clc
adc #(CHNVAR_VibratoBuffer - CHNVAR_VibratoDefs)
tax
lda #$04
:
pha
lda (TempVibratoPointer),y
sta AudioStatusBuffer,x
iny
inx
pla
sec
sbc #$01
bne :-
pla
sta TempVibratoPointer+1
pla
sta TempVibratoPointer+0
rts
; Rotate SFX_ChannelUsage right without involving carry
SFX_NextChannel: ; at 920F
lsr SFX_ChannelUsage
bcc NextChannelPtr
lda SFX_ChannelUsage
ora #$80
sta SFX_ChannelUsage
NextChannelPtr: ; at 9219
; CurChanDataPtr += 31
lda #AudioChannelStatusSize
clc
adc CurChanDataPtr
sta CurChanDataPtr
rts
SFX_FirstChannel: ; at 9221
lsr SFX_ChannelUsage
lsr SFX_ChannelUsage
lsr SFX_ChannelUsage
lsr SFX_ChannelUsage
rts
;
; Mutes channel Y by setting period to #0 (or disabling it if noise)
;
; X must be set to 4 * Y + 2
;
MuteChannel: ;at 922A
cpy #$01 ; if chn# = 1 then disable noise channel
beq :+
lda #$00
sta $4000,x
inx
sta $4000,x
dex
rts
:
lda #$07 ; Disable noise channel
sta $4015
rts
UpdateSound: ;at 923F
inc SoundFrameCounter ; $EA
lda SuspendPlayback
beq :+
rts
:
; CurChanDataPtr = #$500
ldx #<(AudioStatusBuffer)
ldy #>(AudioStatusBuffer)
stx CurChanDataPtr+0
sty CurChanDataPtr+1
; Set sounds registers offset to 0
lda #$00
sta CurrentChannelRegisterOffset
; Loop thru all channels
lda #$04
sta CurrentChannelNumber
UpdateSound_ChannelLoop:
lda #$01
ldy #CHNVAR_VibratoVar1
clc
adc (CurChanDataPtr),y ; adds 1 to VibratoVar1
sta (CurChanDataPtr),y
lda #$01
ldy #CHNVAR_TremoloVar1
clc
adc (CurChanDataPtr),y ; adds 1 to TremoloVar1
sta (CurChanDataPtr),y
; Call SFX_TickChannel if bit set in UnknownEF for this channel...
lda UnknownEF
lsr
bcc :+
jsr SFX_TickChannel
:
; Jump if bit set in Var41 for this channel...
lda Var41
lsr
bcc :+
jmp :++
:
; Skip processing if music address = 0
ldy #CHNVAR_MusicDataAddr+0
lda (CurChanDataPtr),y
iny
ora (CurChanDataPtr),y
beq :+
lda #$01
ldy #CHNVAR_VolumeCurvePosition
clc
adc (CurChanDataPtr),y ; adds 1 to CHNVAR_VolumeCurvePosition
sta (CurChanDataPtr),y
jsr MUS_TickChannel
jmp :++
; Music data for this channel is missing, so mute it
:
lda UnknownEF
lsr
bcs :+
ldx CurrentChannelRegisterOffset
inx
inx
ldy CurrentChannelNumber
jsr MuteChannel
:
; Rotate UnknownEF right without involving carry
lsr UnknownEF
bcc :+
lda UnknownEF
ora #$80
sta UnknownEF
:
dec CurrentChannelNumber
beq UpdateSound_AllChannelsDone
; CurrentChannelRegisterOffset += #4, CurChanDataPtr += #AudioChannelStatusSize
lda #4
clc
adc CurrentChannelRegisterOffset
sta CurrentChannelRegisterOffset
lda #AudioChannelStatusSize
clc
adc CurChanDataPtr+0
sta CurChanDataPtr+0
lda #0
adc CurChanDataPtr+1
sta CurChanDataPtr+1
jmp UpdateSound_ChannelLoop
UpdateSound_AllChannelsDone:
; Do something that semi-resets frame counter sometimes (not very often)
lda FadingSpeed
and #$7f
beq :++
cmp SoundFrameCounter ; $EA
bne :++
lda SoundFrameCounter ; $EA
and #$01
sta SoundFrameCounter ; $EA
inc VolumeFadeStrength
lda #$10
cmp VolumeFadeStrength
bne :++
lda FadingSpeed
bmi :+
lda #$00
sta FadingSpeed
:
lda #$0f
sta VolumeFadeStrength
:
; This seems to be the length of an SFX or something of the like...
lda SFX_FrameWaitCount
beq :+
dec SFX_FrameWaitCount
:
lsr UnknownEF
lsr UnknownEF
lsr UnknownEF
lsr UnknownEF
rts
IterateNote: ;at 92F6
; This function is arrived when
; both NoteLength > 0 and NotePeriodWord > 0.
;
ldy #CHNVAR_DutyCycle
lda (CurChanDataPtr),y
ldy #$02
cpy CurrentChannelNumber
beq :+ ;Skip 'and #$0F' for triangle channel
and #$0f
:
sta $F4
lda FadingSpeed
and #$7F
beq :++++++
lda VolumeFadeStrength
ldy #$02
cpy CurrentChannelNumber ;triangle channel can't be faded
bne :++
ldx #$0c
:
clc
adc VolumeFadeStrength
dex
bne :-
:
tay
lda FadingSpeed
bmi :++
ldx #$ff
:
inx
cpx $F4
beq :+++
dey
bne :-
stx $F4
jmp :+++
:
:
dec $F4
beq :+
dey
bne :-
:
lda #$02
cmp CurrentChannelNumber
beq DoneVolumeCurve
ldy #CHNVAR_VolumeCurveLength
lda (CurChanDataPtr),y
tax
and #$7f
beq DoneVolumeCurve
; This part omitted if channel is triangle or VolumeCurveLength had no low 7 bits
iny
cmp (CurChanDataPtr),y ;VolumeCurvePosition
beq :+
iny
lda (CurChanDataPtr),y ;VolumeCurveStrength
and #$0f
jmp :+++
:
lda #$00
sta (CurChanDataPtr),y ;VolumeCurvePosition
iny
lda (CurChanDataPtr),y ;VolumeCurveStrength
lsr
lsr
lsr
lsr
sta $F5 ; $F5 <- (VolumeCurveStrength >> 4)
txa ; A = VolumeCurveLength
bpl :+
lda #$00
sec
sbc $F5
sta $F5 ; $F5 = 0 - $F5
:
lda (CurChanDataPtr),y ; VolumeCurveStrength
and #$0f ; low 4 bits thereof
clc
adc $F5
bpl :+
lda #$00
jmp :++
:
cmp $F4
bcc :+
lda $F4
:
sta $F4
lda (CurChanDataPtr),y ; VolumeCurveStrength
and #$F0
ora $F4
sta (CurChanDataPtr),y ; VolumeCurveStrength
; Arrived here if channel was triangle or VolumeCurveLength had no low 7 bits
DoneVolumeCurve:
lda UnknownEF
lsr
bcs :+
lda #CHNVAR_DutyCycle
sta $F5
jmp ProcessTremolo
:
lda #CHNVAR_VibratoCtrl
sta $F5
jmp ProcessVibrato
ProcessTremolo: ;volume vibrato
; $F5 points to DutyCycle or DutyCycleSFX|$80
ldy #CHNVAR_VibratoBuffer+2
lda (CurChanDataPtr),y
and #$7f
beq :++++++
ldy #CHNVAR_TremoloVar1
cmp (CurChanDataPtr),y
beq :+
jmp :+++++
:
lda #$00
sta (CurChanDataPtr),y
ldy #CHNVAR_VibratoBuffer+3
lda (CurChanDataPtr),y
ldy #CHNVAR_TremoloVar2
clc
adc (CurChanDataPtr),y
beq :+
bpl :++
:
lda #$01
sta (CurChanDataPtr),y
jmp :++
:
sta (CurChanDataPtr),y
cmp #$10
bcc :++
lda #$0f
sta (CurChanDataPtr),y
:
lda #$00
ldy #CHNVAR_VibratoBuffer+3
sec
sbc (CurChanDataPtr),y
sta (CurChanDataPtr),y
:
ldy #CHNVAR_TremoloVar2
lda (CurChanDataPtr),y
cmp $F4
bcs :+
sta $F4
:
ldy #$02
cpy CurrentChannelNumber
beq :+
lda $F5
and #$7f
tay
lda (CurChanDataPtr),y
and #$f0
ora $F4
sta $F4
:
ldx CurrentChannelRegisterOffset
lda $F4
sta $4000,x ;Write duty,decaysettings,length_disable
lda $F5
bpl :+
lda #CHNVAR_VibratoCtrlSFX | $80
sta $F5
jmp ProcessVibrato
:
lda #CHNVAR_VibratoCtrl
sta $F5
ProcessVibrato:
; $F5 points to VibratoCtrl or VibratoCtrlSFX|$80
lda $F5
and #$7f
tay
ldx #$00
lda (CurChanDataPtr),y
; The value read comes usually from VibratoCtrl, but may come from VibratoCtrlSFX too
beq :++
bpl :+
dex
:
iny ;Either NotePeriodWord or NotePeriodWordSFX
clc
adc (CurChanDataPtr),y
sta (CurChanDataPtr),y
txa
iny
adc (CurChanDataPtr),y
sta (CurChanDataPtr),y
:
lda $F5
bmi :+
lda UnknownEF
lsr
bcc :+
rts
:
ldy #CHNVAR_VibratoBuffer+0
lda (CurChanDataPtr),y
and #$7f
bne :+
jmp :++++++
:
ldy #CHNVAR_VibratoVar1
cmp (CurChanDataPtr),y
beq :+
jmp :+++++
:
lda #$00 ;If VibratoVar1 was 0
sta (CurChanDataPtr),y
tax
ldy #CHNVAR_VibratoBuffer+1
lda (CurChanDataPtr),y
rol
rol
rol
rol
and #$07
sta $F4
ldy #CHNVAR_VibratoVar2
lda (CurChanDataPtr),y
asl
bcc :+
lda #$00 ;If VibratoVar2 had high bit set
sec
sbc $F4
sta $F4
dex
:
lda $F4
clc
ldy #CHNVAR_PeriodLo
adc (CurChanDataPtr),y
sta (CurChanDataPtr),y
iny
txa
adc (CurChanDataPtr),y
sta (CurChanDataPtr),y
ldy #CHNVAR_VibratoBuffer+1
lda (CurChanDataPtr),y
and #$1f
sta $F4
ldy #CHNVAR_VibratoVar2
lda (CurChanDataPtr),y ;Adds 1 to VibratoVar2
clc
adc #$01
sta (CurChanDataPtr),y
and #$7f
cmp $F4
bne :+++
lda (CurChanDataPtr),y
and #$80
sta (CurChanDataPtr),y
ldy #CHNVAR_VibratoBuffer+0
lda (CurChanDataPtr),y
asl
bcs :++
lda (CurChanDataPtr),y
ora #$80
sta (CurChanDataPtr),y
ldy #CHNVAR_VibratoVar2
lda (CurChanDataPtr),y
bpl :+
and #$7f
sta (CurChanDataPtr),y
jmp :+++
:
ora #$80
sta (CurChanDataPtr),y
jmp :++
:
lda (CurChanDataPtr),y
and #$7f
sta (CurChanDataPtr),y
:
; $F5 points to VibratoCtrl or VibratoCtrlSFX|$80
lda $F5
and #$7f
sta $F5
inc $F5 ; Now points to NotePeriodWord or NotePeriodWordSFX (no high bit)
ldy #CHNVAR_PeriodLo
lda (CurChanDataPtr),y
ldy $F5
clc
adc (CurChanDataPtr),y