forked from sonicretro/s1disasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s1.sounddriver.asm
2804 lines (2634 loc) · 94.3 KB
/
s1.sounddriver.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
; ---------------------------------------------------------------------------
; Modified SMPS 68k Type 1b sound driver
; The source code to a similar version of the driver can be found here:
; https://hiddenpalace.org/News/Sega_of_Japan_Sound_Documents_and_Source_Code
; ---------------------------------------------------------------------------
; Go_SoundTypes:
Go_SoundPriorities: dc.l SoundPriorities
; Go_SoundD0:
Go_SpecSoundIndex: dc.l SpecSoundIndex
Go_MusicIndex: dc.l MusicIndex
Go_SoundIndex: dc.l SoundIndex
; off_719A0:
Go_SpeedUpIndex: dc.l SpeedUpIndex
Go_PSGIndex: dc.l PSG_Index
; ---------------------------------------------------------------------------
; PSG instruments used in music
; ---------------------------------------------------------------------------
PSG_Index:
dc.l PSG1, PSG2, PSG3
dc.l PSG4, PSG5, PSG6
dc.l PSG7, PSG8, PSG9
PSG1: binclude "sound/psg/psg1.bin"
PSG2: binclude "sound/psg/psg2.bin"
PSG3: binclude "sound/psg/psg3.bin"
PSG4: binclude "sound/psg/psg4.bin"
PSG6: binclude "sound/psg/psg6.bin"
PSG5: binclude "sound/psg/psg5.bin"
PSG7: binclude "sound/psg/psg7.bin"
PSG8: binclude "sound/psg/psg8.bin"
PSG9: binclude "sound/psg/psg9.bin"
; ---------------------------------------------------------------------------
; New tempos for songs during speed shoes
; ---------------------------------------------------------------------------
; DANGER! several songs will use the first few bytes of MusicIndex as their main
; tempos while speed shoes are active. If you don't want that, you should add
; their "correct" sped-up main tempos to the list.
; byte_71A94:
SpeedUpIndex:
dc.b 7 ; GHZ
dc.b $72 ; LZ
dc.b $73 ; MZ
dc.b $26 ; SLZ
dc.b $15 ; SYZ
dc.b 8 ; SBZ
dc.b $FF ; Invincibility
dc.b 5 ; Extra Life
;dc.b ? ; Special Stage
;dc.b ? ; Title Screen
;dc.b ? ; Ending
;dc.b ? ; Boss
;dc.b ? ; FZ
;dc.b ? ; Sonic Got Through
;dc.b ? ; Game Over
;dc.b ? ; Continue Screen
;dc.b ? ; Credits
;dc.b ? ; Drowning
;dc.b ? ; Get Emerald
; ---------------------------------------------------------------------------
; Music Pointers
; ---------------------------------------------------------------------------
MusicIndex:
ptr_mus81: dc.l Music81
ptr_mus82: dc.l Music82
ptr_mus83: dc.l Music83
ptr_mus84: dc.l Music84
ptr_mus85: dc.l Music85
ptr_mus86: dc.l Music86
ptr_mus87: dc.l Music87
ptr_mus88: dc.l Music88
ptr_mus89: dc.l Music89
ptr_mus8A: dc.l Music8A
ptr_mus8B: dc.l Music8B
ptr_mus8C: dc.l Music8C
ptr_mus8D: dc.l Music8D
ptr_mus8E: dc.l Music8E
ptr_mus8F: dc.l Music8F
ptr_mus90: dc.l Music90
ptr_mus91: dc.l Music91
ptr_mus92: dc.l Music92
ptr_mus93: dc.l Music93
ptr_musend
; ---------------------------------------------------------------------------
; Priority of sound. New music or SFX must have a priority higher than or equal
; to what is stored in v_sndprio or it won't play. If bit 7 of new priority is
; set ($80 and up), the new music or SFX will not set its priority -- meaning
; any music or SFX can override it (as long as it can override whatever was
; playing before). Usually, SFX will only override SFX, special SFX ($D0-$DF)
; will only override special SFX and music will only override music.
; ---------------------------------------------------------------------------
; SoundTypes:
SoundPriorities:
dc.b $90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90 ; $81
dc.b $90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90,$90 ; $90
dc.b $80,$70,$70,$70,$70,$70,$70,$70,$70,$70,$68,$70,$70,$70,$60,$70 ; $A0
dc.b $70,$60,$70,$60,$70,$70,$70,$70,$70,$70,$70,$70,$70,$70,$70,$7F ; $B0
dc.b $60,$70,$70,$70,$70,$70,$70,$70,$70,$70,$70,$70,$70,$70,$70,$70 ; $C0
dc.b $80,$80,$80,$80,$80,$80,$80,$80,$80,$80,$80,$80,$80,$80,$80,$80 ; $D0
dc.b $90,$90,$90,$90,$90 ; $E0
; ---------------------------------------------------------------------------
; Subroutine to update music more than once per frame
; (Called by horizontal & vert. interrupts)
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71B4C:
UpdateMusic:
stopZ80
nop
nop
nop
; loc_71B5A:
.updateloop:
btst #0,(z80_bus_request).l ; Is the z80 busy?
bne.s .updateloop ; If so, wait
btst #7,(z80_dac_status).l ; Is DAC accepting new samples?
beq.s .driverinput ; Branch if yes
startZ80
nop
nop
nop
nop
nop
bra.s UpdateMusic
; ===========================================================================
; loc_71B82:
.driverinput:
lea (v_snddriver_ram&$FFFFFF).l,a6
clr.b f_voice_selector(a6)
tst.b f_pausemusic(a6) ; is music paused?
bne.w PauseMusic ; if yes, branch
subq.b #1,v_main_tempo_timeout(a6) ; Has main tempo timer expired?
bne.s .skipdelay
jsr TempoWait(pc)
; loc_71B9E:
.skipdelay:
move.b v_fadeout_counter(a6),d0
beq.s .skipfadeout
jsr DoFadeOut(pc)
; loc_71BA8:
.skipfadeout:
tst.b f_fadein_flag(a6)
beq.s .skipfadein
jsr DoFadeIn(pc)
; loc_71BB2:
.skipfadein:
if FixBugs
moveq #0,d0
or.b v_soundqueue2(a6),d0
or.w v_soundqueue0(a6),d0
else
; DANGER! The following line only checks v_soundqueue0 and v_soundqueue1, breaking v_soundqueue2.
tst.w v_soundqueue0(a6) ; is a music or sound queued for playing?
endif
beq.s .nosndinput ; if not, branch
jsr CycleSoundQueue(pc)
; loc_71BBC:
.nosndinput:
cmpi.b #$80,v_sound_id(a6) ; is song queue set for silence (empty)?
beq.s .nonewsound ; If yes, branch
jsr PlaySoundID(pc)
; loc_71BC8:
.nonewsound:
lea v_music_dac_track(a6),a5
tst.b TrackPlaybackControl(a5) ; Is DAC track playing?
bpl.s .dacdone ; Branch if not
jsr DACUpdateTrack(pc)
; loc_71BD4:
.dacdone:
clr.b f_updating_dac(a6)
moveq #((v_music_fm_tracks_end-v_music_fm_tracks)/TrackSz)-1,d7 ; 6 FM tracks
; loc_71BDA:
.bgmfmloop:
adda.w #TrackSz,a5
tst.b TrackPlaybackControl(a5) ; Is track playing?
bpl.s .bgmfmnext ; Branch if not
jsr FMUpdateTrack(pc)
; loc_71BE6:
.bgmfmnext:
dbf d7,.bgmfmloop
moveq #((v_music_psg_tracks_end-v_music_psg_tracks)/TrackSz)-1,d7 ; 3 PSG tracks
; loc_71BEC:
.bgmpsgloop:
adda.w #TrackSz,a5
tst.b TrackPlaybackControl(a5) ; Is track playing?
bpl.s .bgmpsgnext ; Branch if not
jsr PSGUpdateTrack(pc)
; loc_71BF8:
.bgmpsgnext:
dbf d7,.bgmpsgloop
move.b #$80,f_voice_selector(a6) ; Now at SFX tracks
moveq #((v_sfx_fm_tracks_end-v_sfx_fm_tracks)/TrackSz)-1,d7 ; 3 FM tracks (SFX)
; loc_71C04:
.sfxfmloop:
adda.w #TrackSz,a5
tst.b TrackPlaybackControl(a5) ; Is track playing?
bpl.s .sfxfmnext ; Branch if not
jsr FMUpdateTrack(pc)
; loc_71C10:
.sfxfmnext:
dbf d7,.sfxfmloop
moveq #((v_sfx_psg_tracks_end-v_sfx_psg_tracks)/TrackSz)-1,d7 ; 3 PSG tracks (SFX)
; loc_71C16:
.sfxpsgloop:
adda.w #TrackSz,a5
tst.b TrackPlaybackControl(a5) ; Is track playing?
bpl.s .sfxpsgnext ; Branch if not
jsr PSGUpdateTrack(pc)
; loc_71C22:
.sfxpsgnext:
dbf d7,.sfxpsgloop
move.b #$40,f_voice_selector(a6) ; Now at special SFX tracks
adda.w #TrackSz,a5
tst.b TrackPlaybackControl(a5) ; Is track playing?
bpl.s .specfmdone ; Branch if not
jsr FMUpdateTrack(pc)
; loc_71C38:
.specfmdone:
adda.w #TrackSz,a5
tst.b TrackPlaybackControl(a5) ; Is track playing
bpl.s DoStartZ80 ; Branch if not
jsr PSGUpdateTrack(pc)
; loc_71C44:
DoStartZ80:
startZ80
rts
; End of function UpdateMusic
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71C4E: UpdateDAC:
DACUpdateTrack:
subq.b #1,TrackDurationTimeout(a5) ; Has DAC sample timeout expired?
bne.s .locret ; Return if not
move.b #$80,f_updating_dac(a6) ; Set flag to indicate this is the DAC
;DACDoNext:
movea.l TrackDataPointer(a5),a4 ; DAC track data pointer
; loc_71C5E:
.sampleloop:
moveq #0,d5
move.b (a4)+,d5 ; Get next SMPS unit
cmpi.b #$E0,d5 ; Is it a coord. flag?
blo.s .notcoord ; Branch if not
jsr CoordFlag(pc)
bra.s .sampleloop
; ===========================================================================
; loc_71C6E:
.notcoord:
tst.b d5 ; Is it a sample?
bpl.s .gotduration ; Branch if not (duration)
move.b d5,TrackSavedDAC(a5) ; Store new sample
move.b (a4)+,d5 ; Get another byte
bpl.s .gotduration ; Branch if it is a duration
subq.w #1,a4 ; Put byte back
move.b TrackSavedDuration(a5),TrackDurationTimeout(a5) ; Use last duration
bra.s .gotsampleduration
; ===========================================================================
; loc_71C84:
.gotduration:
jsr SetDuration(pc)
; loc_71C88:
.gotsampleduration:
move.l a4,TrackDataPointer(a5) ; Save pointer
btst #2,TrackPlaybackControl(a5) ; Is track being overridden?
bne.s .locret ; Return if yes
moveq #0,d0
move.b TrackSavedDAC(a5),d0 ; Get sample
cmpi.b #$80,d0 ; Is it a rest?
beq.s .locret ; Return if yes
btst #3,d0 ; Is bit 3 set (samples between $88-$8F)?
bne.s .timpani ; Various timpani
move.b d0,(z80_dac_sample).l
; locret_71CAA:
.locret:
rts
; ===========================================================================
; loc_71CAC:
.timpani:
subi.b #$88,d0 ; Convert into an index
move.b DAC_sample_rate(pc,d0.w),d0
; Warning: this affects the raw pitch of sample $83, meaning it will
; use this value from then on.
move.b d0,(z80_dac_timpani_pitch).l
move.b #$83,(z80_dac_sample).l ; Use timpani
rts
; End of function DACUpdateTrack
; ===========================================================================
; Note: this only defines rates for samples $88-$8D, meaning $8E-$8F are invalid.
; Also, $8C-$8D are so slow you may want to skip them.
; byte_71CC4:
DAC_sample_rate:
dc.b dpcmLoopCounter(9750)
dc.b dpcmLoopCounter(8750)
dc.b dpcmLoopCounter(7150)
dc.b dpcmLoopCounter(7000)
dc.b $FF, $FF
even
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71CCA:
FMUpdateTrack:
subq.b #1,TrackDurationTimeout(a5) ; Update duration timeout
bne.s .notegoing ; Branch if it hasn't expired
bclr #4,TrackPlaybackControl(a5) ; Clear 'do not attack next note' bit
jsr FMDoNext(pc)
jsr FMPrepareNote(pc)
bra.w FMNoteOn
; ===========================================================================
; loc_71CE0:
.notegoing:
jsr NoteTimeoutUpdate(pc)
jsr DoModulation(pc)
bra.w FMUpdateFreq
; End of function FMUpdateTrack
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71CEC:
FMDoNext:
movea.l TrackDataPointer(a5),a4 ; Track data pointer
bclr #1,TrackPlaybackControl(a5) ; Clear 'track at rest' bit
; loc_71CF4:
.noteloop:
moveq #0,d5
move.b (a4)+,d5 ; Get byte from track
cmpi.b #$E0,d5 ; Is this a coord. flag?
blo.s .gotnote ; Branch if not
jsr CoordFlag(pc)
bra.s .noteloop
; ===========================================================================
; loc_71D04:
.gotnote:
jsr FMNoteOff(pc)
tst.b d5 ; Is this a note?
bpl.s .gotduration ; Branch if not
jsr FMSetFreq(pc)
move.b (a4)+,d5 ; Get another byte
bpl.s .gotduration ; Branch if it is a duration
subq.w #1,a4 ; Otherwise, put it back
bra.w FinishTrackUpdate
; ===========================================================================
; loc_71D1A:
.gotduration:
jsr SetDuration(pc)
bra.w FinishTrackUpdate
; End of function FMDoNext
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71D22:
FMSetFreq:
subi.b #$80,d5 ; Make it a zero-based index
beq.s TrackSetRest
add.b TrackTranspose(a5),d5 ; Add track transposition
andi.w #$7F,d5 ; Clear high byte and sign bit
lsl.w #1,d5
lea FMFrequencies(pc),a0
move.w (a0,d5.w),d6
move.w d6,TrackFreq(a5) ; Store new frequency
rts
; End of function FMSetFreq
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71D40:
SetDuration:
move.b d5,d0
move.b TrackTempoDivider(a5),d1 ; Get dividing timing
; loc_71D46:
.multloop:
subq.b #1,d1
beq.s .donemult
add.b d5,d0
bra.s .multloop
; ===========================================================================
; loc_71D4E:
.donemult:
move.b d0,TrackSavedDuration(a5) ; Save duration
move.b d0,TrackDurationTimeout(a5) ; Save duration timeout
rts
; End of function SetDuration
; ===========================================================================
; loc_71D58:
TrackSetRest:
bset #1,TrackPlaybackControl(a5) ; Set 'track at rest' bit
clr.w TrackFreq(a5) ; Clear frequency
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71D60:
FinishTrackUpdate:
move.l a4,TrackDataPointer(a5) ; Store new track position
move.b TrackSavedDuration(a5),TrackDurationTimeout(a5) ; Reset note timeout
btst #4,TrackPlaybackControl(a5) ; Is track set to not attack note?
bne.s .locret ; If so, branch
move.b TrackNoteTimeoutMaster(a5),TrackNoteTimeout(a5) ; Reset note fill timeout
clr.b TrackVolEnvIndex(a5) ; Reset PSG volume envelope index (even on FM tracks...)
btst #3,TrackPlaybackControl(a5) ; Is modulation on?
beq.s .locret ; If not, return
movea.l TrackModulationPtr(a5),a0 ; Modulation data pointer
move.b (a0)+,TrackModulationWait(a5) ; Reset wait
move.b (a0)+,TrackModulationSpeed(a5) ; Reset speed
move.b (a0)+,TrackModulationDelta(a5) ; Reset delta
move.b (a0)+,d0 ; Get steps
lsr.b #1,d0 ; Halve them
move.b d0,TrackModulationSteps(a5) ; Then store
clr.w TrackModulationVal(a5) ; Reset frequency change
; locret_71D9C:
.locret:
rts
; End of function FinishTrackUpdate
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71D9E: NoteFillUpdate
NoteTimeoutUpdate:
tst.b TrackNoteTimeout(a5) ; Is note fill on?
beq.s .locret
subq.b #1,TrackNoteTimeout(a5) ; Update note fill timeout
bne.s .locret ; Return if it hasn't expired
bset #1,TrackPlaybackControl(a5) ; Put track at rest
tst.b TrackVoiceControl(a5) ; Is this a PSG track?
bmi.w .psgnoteoff ; If yes, branch
jsr FMNoteOff(pc)
addq.w #4,sp ; Do not return to caller
rts
; ===========================================================================
; loc_71DBE:
.psgnoteoff:
jsr PSGNoteOff(pc)
addq.w #4,sp ; Do not return to caller
; locret_71DC4:
.locret:
rts
; End of function NoteTimeoutUpdate
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71DC6:
DoModulation:
addq.w #4,sp ; Do not return to caller (but see below)
btst #3,TrackPlaybackControl(a5) ; Is modulation active?
beq.s .locret ; Return if not
tst.b TrackModulationWait(a5) ; Has modulation wait expired?
beq.s .waitdone ; If yes, branch
subq.b #1,TrackModulationWait(a5) ; Update wait timeout
rts
; ===========================================================================
; loc_71DDA:
.waitdone:
subq.b #1,TrackModulationSpeed(a5) ; Update speed
beq.s .updatemodulation ; If it expired, want to update modulation
rts
; ===========================================================================
; loc_71DE2:
.updatemodulation:
movea.l TrackModulationPtr(a5),a0 ; Get modulation data
move.b 1(a0),TrackModulationSpeed(a5) ; Restore modulation speed
tst.b TrackModulationSteps(a5) ; Check number of steps
bne.s .calcfreq ; If nonzero, branch
move.b 3(a0),TrackModulationSteps(a5) ; Restore from modulation data
neg.b TrackModulationDelta(a5) ; Negate modulation delta
rts
; ===========================================================================
; loc_71DFE:
.calcfreq:
subq.b #1,TrackModulationSteps(a5) ; Update modulation steps
move.b TrackModulationDelta(a5),d6 ; Get modulation delta
ext.w d6
add.w TrackModulationVal(a5),d6 ; Add cumulative modulation change
move.w d6,TrackModulationVal(a5) ; Store it
add.w TrackFreq(a5),d6 ; Add note frequency to it
subq.w #4,sp ; In this case, we want to return to caller after all
; locret_71E16:
.locret:
rts
; End of function DoModulation
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; sub_71E18:
FMPrepareNote:
btst #1,TrackPlaybackControl(a5) ; Is track resting?
bne.s locret_71E48 ; Return if so
move.w TrackFreq(a5),d6 ; Get current note frequency
beq.s FMSetRest ; Branch if zero
; loc_71E24:
FMUpdateFreq:
move.b TrackDetune(a5),d0 ; Get detune value
ext.w d0
add.w d0,d6 ; Add note frequency
btst #2,TrackPlaybackControl(a5) ; Is track being overridden?
bne.s locret_71E48 ; Return if so
move.w d6,d1
lsr.w #8,d1
move.b #$A4,d0 ; Register for upper 6 bits of frequency
jsr WriteFMIorII(pc)
move.b d6,d1
move.b #$A0,d0 ; Register for lower 8 bits of frequency
jsr WriteFMIorII(pc) ; (It would be better if this were a jmp)
; locret_71E48:
locret_71E48:
rts
; ===========================================================================
; loc_71E4A:
FMSetRest:
bset #1,TrackPlaybackControl(a5) ; Set 'track at rest' bit
rts
; End of function FMPrepareNote
; ===========================================================================
; loc_71E50:
PauseMusic:
bmi.s .unpausemusic ; Branch if music is being unpaused
cmpi.b #2,f_pausemusic(a6)
beq.w .unpausedallfm
move.b #2,f_pausemusic(a6)
moveq #2,d3
move.b #$B4,d0 ; Command to set AMS/FMS/panning
moveq #0,d1 ; No panning, AMS or FMS
; loc_71E6A:
.killpanloop:
jsr WriteFMI(pc)
jsr WriteFMII(pc)
addq.b #1,d0
dbf d3,.killpanloop
moveq #2,d3
moveq #$28,d0 ; Key on/off register
; loc_71E7C:
.noteoffloop:
move.b d3,d1 ; FM1, FM2, FM3
jsr WriteFMI(pc)
addq.b #4,d1 ; FM4, FM5, FM6
jsr WriteFMI(pc)
dbf d3,.noteoffloop
jsr PSGSilenceAll(pc)
bra.w DoStartZ80
; ===========================================================================
; loc_71E94:
.unpausemusic:
clr.b f_pausemusic(a6)
moveq #TrackSz,d3
lea v_music_fmdac_tracks(a6),a5
moveq #((v_music_fmdac_tracks_end-v_music_fmdac_tracks)/TrackSz)-1,d4 ; 6 FM + 1 DAC tracks
; loc_71EA0:
.bgmfmloop:
btst #7,TrackPlaybackControl(a5) ; Is track playing?
beq.s .bgmfmnext ; Branch if not
btst #2,TrackPlaybackControl(a5) ; Is track being overridden?
bne.s .bgmfmnext ; Branch if yes
move.b #$B4,d0 ; Command to set AMS/FMS/panning
move.b TrackAMSFMSPan(a5),d1 ; Get value from track RAM
jsr WriteFMIorII(pc)
; loc_71EB8:
.bgmfmnext:
adda.w d3,a5
dbf d4,.bgmfmloop
lea v_sfx_fm_tracks(a6),a5
moveq #((v_sfx_fm_tracks_end-v_sfx_fm_tracks)/TrackSz)-1,d4 ; 3 FM tracks (SFX)
; loc_71EC4:
.sfxfmloop:
btst #7,TrackPlaybackControl(a5) ; Is track playing?
beq.s .sfxfmnext ; Branch if not
btst #2,TrackPlaybackControl(a5) ; Is track being overridden?
bne.s .sfxfmnext ; Branch if yes
move.b #$B4,d0 ; Command to set AMS/FMS/panning
move.b TrackAMSFMSPan(a5),d1 ; Get value from track RAM
jsr WriteFMIorII(pc)
; loc_71EDC:
.sfxfmnext:
adda.w d3,a5
dbf d4,.sfxfmloop
lea v_spcsfx_track_ram(a6),a5
btst #7,TrackPlaybackControl(a5) ; Is track playing?
beq.s .unpausedallfm ; Branch if not
btst #2,TrackPlaybackControl(a5) ; Is track being overridden?
bne.s .unpausedallfm ; Branch if yes
move.b #$B4,d0 ; Command to set AMS/FMS/panning
move.b TrackAMSFMSPan(a5),d1 ; Get value from track RAM
jsr WriteFMIorII(pc)
; loc_71EFE:
.unpausedallfm:
bra.w DoStartZ80
; ---------------------------------------------------------------------------
; Subroutine to play a sound or music track
; ---------------------------------------------------------------------------
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; Sound_Play:
CycleSoundQueue:
movea.l (Go_SoundPriorities).l,a0
lea v_soundqueue0(a6),a1 ; load music track number
_move.b v_sndprio(a6),d3 ; Get priority of currently playing SFX
moveq #v_soundqueue_end-v_soundqueue_start-1,d4
; loc_71F12:
.inputloop:
move.b (a1),d0 ; move track number to d0
move.b d0,d1
clr.b (a1)+ ; Clear entry
subi.b #bgm__First,d0 ; Make it into 0-based index
bcs.s .nextinput ; If negative (i.e., it was $80 or lower), branch
cmpi.b #$80,v_sound_id(a6) ; Is v_sound_id a $80 (silence/empty)?
beq.s .havesound ; If yes, branch
move.b d1,v_soundqueue0(a6) ; Put sound into v_soundqueue0
bra.s .nextinput
; ===========================================================================
; loc_71F2C:
.havesound:
andi.w #$7F,d0 ; Clear high byte and sign bit
move.b (a0,d0.w),d2 ; Get sound type
cmp.b d3,d2 ; Is it a lower priority sound?
blo.s .nextinput ; Branch if yes
move.b d2,d3 ; Store new priority
move.b d1,v_sound_id(a6) ; Queue sound for playing
; loc_71F3E:
.nextinput:
dbf d4,.inputloop
tst.b d3 ; We don't want to change sound priority if it is negative
bmi.s .locret
_move.b d3,v_sndprio(a6) ; Set new sound priority
; locret_71F4A:
.locret:
rts
; End of function CycleSoundQueue
; ||||||||||||||| S U B R O U T I N E |||||||||||||||||||||||||||||||||||||||
; Sound_ChkValue:
PlaySoundID:
moveq #0,d7
move.b v_sound_id(a6),d7
beq.w StopAllSound
bpl.s .locret ; If >= 0, return (not a valid sound, bgm or command)
move.b #$80,v_sound_id(a6) ; reset music flag
if FixBugs
cmpi.b #bgm__Last,d7 ; Is this music ($81-$93)?
else
; DANGER! Music ends at $93, yet this checks until $9F; attempting to
; play sounds $94-$9F will cause a crash!
; See LevSel_NoCheat for more.
cmpi.b #bgm__Last+$C,d7 ; Is this music ($81-$9F)?
endif
bls.w Sound_PlayBGM ; Branch if yes
cmpi.b #sfx__First,d7 ; Is this after music but before sfx? (redundant check)
blo.w .locret ; Return if yes
cmpi.b #sfx__Last,d7 ; Is this sfx ($A0-$CF)?
bls.w Sound_PlaySFX ; Branch if yes
cmpi.b #spec__First,d7 ; Is this after sfx but before special sfx? (redundant check)
blo.w .locret ; Return if yes
if FixBugs
cmpi.b #spec__Last,d7 ; Is this special sfx ($D0-$D0)?
bls.w Sound_PlaySpecial ; Branch if yes
cmpi.b #flg__First,d7 ; Is this after special sfx but before $E0?
blo.w .locret ; Return if yes
else
; DANGER! Special SFXes end at $D0, yet this checks until $DF; attempting to
; play sounds $D1-$DF will cause a crash!
cmpi.b #spec__Last+$10,d7 ; Is this special sfx ($D0-$DF)?
blo.w Sound_PlaySpecial ; Branch if yes
endif
cmpi.b #flg__Last,d7 ; Is this $E0-$E4?
bls.s Sound_E0toE4 ; Branch if yes
; locret_71F8C:
.locret:
rts
; ===========================================================================
Sound_E0toE4:
subi.b #flg__First,d7
lsl.w #2,d7
jmp Sound_ExIndex(pc,d7.w)
; ===========================================================================
Sound_ExIndex:
ptr_flgE0: bra.w FadeOutMusic ; $E0
ptr_flgE1: bra.w PlaySegaSound ; $E1
ptr_flgE2: bra.w SpeedUpMusic ; $E2
ptr_flgE3: bra.w SlowDownMusic ; $E3
ptr_flgE4: bra.w StopAllSound ; $E4
ptr_flgend
; ===========================================================================
; ---------------------------------------------------------------------------
; Play "Say-gaa" PCM sound
; ---------------------------------------------------------------------------
; Sound_E1: PlaySega:
PlaySegaSound:
move.b #$88,(z80_dac_sample).l ; Queue Sega PCM
startZ80
move.w #$11,d1
; loc_71FC0:
.busyloop_outer:
move.w #-1,d0
; loc_71FC4:
.busyloop:
nop
dbf d0,.busyloop
dbf d1,.busyloop_outer
addq.w #4,sp ; Tamper return value so we don't return to caller
rts
; ===========================================================================
; ---------------------------------------------------------------------------
; Play music track $81-$9F
; ---------------------------------------------------------------------------
; Sound_81to9F:
Sound_PlayBGM:
cmpi.b #bgm_ExtraLife,d7 ; is the "extra life" music to be played?
bne.s .bgmnot1up ; if not, branch
tst.b f_1up_playing(a6) ; Is a 1-up music playing?
bne.w .locdblret ; if yes, branch
lea v_music_track_ram(a6),a5
moveq #((v_music_track_ram_end-v_music_track_ram)/TrackSz)-1,d0 ; 1 DAC + 6 FM + 3 PSG tracks
; loc_71FE6:
.clearsfxloop:
bclr #2,TrackPlaybackControl(a5) ; Clear 'SFX is overriding' bit
adda.w #TrackSz,a5
dbf d0,.clearsfxloop
lea v_sfx_track_ram(a6),a5
moveq #((v_sfx_track_ram_end-v_sfx_track_ram)/TrackSz)-1,d0 ; 3 FM + 3 PSG tracks (SFX)
; loc_71FF8:
.cleartrackplayloop:
bclr #7,TrackPlaybackControl(a5) ; Clear 'track is playing' bit
adda.w #TrackSz,a5
dbf d0,.cleartrackplayloop
_clr.b v_sndprio(a6) ; Clear priority
movea.l a6,a0
lea v_1up_ram_copy(a6),a1
move.w #((v_music_track_ram_end-v_startofvariables)/4)-1,d0 ; Backup $220 bytes: all variables and music track data
; loc_72012:
.backupramloop:
move.l (a0)+,(a1)+
dbf d0,.backupramloop
move.b #$80,f_1up_playing(a6)
_clr.b v_sndprio(a6) ; Clear priority again (?)
bra.s .bgm_loadMusic
; ===========================================================================
; loc_72024:
.bgmnot1up:
clr.b f_1up_playing(a6)
clr.b v_fadein_counter(a6)
; loc_7202C:
.bgm_loadMusic:
jsr InitMusicPlayback(pc)
movea.l (Go_SpeedUpIndex).l,a4
subi.b #bgm__First,d7
move.b (a4,d7.w),v_speeduptempo(a6)
movea.l (Go_MusicIndex).l,a4
lsl.w #2,d7
movea.l (a4,d7.w),a4 ; a4 now points to (uncompressed) song data
moveq #0,d0
move.w (a4),d0 ; load voice pointer
add.l a4,d0 ; It is a relative pointer
move.l d0,v_voice_ptr(a6)
move.b 5(a4),d0 ; load tempo
move.b d0,v_tempo_mod(a6)
tst.b f_speedup(a6)
beq.s .nospeedshoes
move.b v_speeduptempo(a6),d0
; loc_72068:
.nospeedshoes:
move.b d0,v_main_tempo(a6)
move.b d0,v_main_tempo_timeout(a6)
moveq #0,d1
movea.l a4,a3
addq.w #6,a4 ; Point past header
moveq #0,d7
move.b 2(a3),d7 ; load number of FM+DAC tracks
beq.w .bgm_fmdone ; branch if zero
subq.b #1,d7
move.b #$C0,d1 ; Default AMS+FMS+Panning
move.b 4(a3),d4 ; load tempo dividing timing
moveq #TrackSz,d6
move.b #1,d5 ; Note duration for first "note"
lea v_music_fmdac_tracks(a6),a1
lea FMDACInitBytes(pc),a2
; loc_72098:
.bgm_fmloadloop:
bset #7,TrackPlaybackControl(a1) ; Initial playback control: set 'track playing' bit
move.b (a2)+,TrackVoiceControl(a1) ; Voice control bits
move.b d4,TrackTempoDivider(a1)
move.b d6,TrackStackPointer(a1) ; set "gosub" (coord flag $F8) stack init value
move.b d1,TrackAMSFMSPan(a1) ; Set AMS/FMS/Panning
move.b d5,TrackDurationTimeout(a1) ; Set duration of first "note"
moveq #0,d0
move.w (a4)+,d0 ; load DAC/FM pointer
add.l a3,d0 ; Relative pointer
move.l d0,TrackDataPointer(a1) ; Store track pointer
move.w (a4)+,TrackTranspose(a1) ; load FM channel modifier
adda.w d6,a1
dbf d7,.bgm_fmloadloop
cmpi.b #7,2(a3) ; Are 7 FM tracks defined?
bne.s .silencefm6
moveq #$2B,d0 ; DAC enable/disable register
moveq #0,d1 ; Disable DAC
jsr WriteFMI(pc)
bra.w .bgm_fmdone
; ===========================================================================
; loc_720D8:
.silencefm6:
moveq #$28,d0 ; Key on/off register
moveq #6,d1 ; Note off on all operators of channel 6
jsr WriteFMI(pc)
move.b #$42,d0 ; TL for operator 1 of FM6
moveq #$7F,d1 ; Total silence
jsr WriteFMII(pc)
move.b #$4A,d0 ; TL for operator 3 of FM6
moveq #$7F,d1 ; Total silence
jsr WriteFMII(pc)
move.b #$46,d0 ; TL for operator 2 of FM6
moveq #$7F,d1 ; Total silence
jsr WriteFMII(pc)
move.b #$4E,d0 ; TL for operator 4 of FM6
moveq #$7F,d1 ; Total silence
jsr WriteFMII(pc)
move.b #$B6,d0 ; AMS/FMS/panning of FM6
move.b #$C0,d1 ; Stereo
jsr WriteFMII(pc)
; loc_72114:
.bgm_fmdone:
moveq #0,d7
move.b 3(a3),d7 ; Load number of PSG tracks
beq.s .bgm_psgdone ; branch if zero
subq.b #1,d7
lea v_music_psg_tracks(a6),a1
lea PSGInitBytes(pc),a2
; loc_72126:
.bgm_psgloadloop:
bset #7,TrackPlaybackControl(a1) ; Initial playback control: set 'track playing' bit
move.b (a2)+,TrackVoiceControl(a1) ; Voice control bits
move.b d4,TrackTempoDivider(a1)
move.b d6,TrackStackPointer(a1) ; set "gosub" (coord flag $F8) stack init value
move.b d5,TrackDurationTimeout(a1) ; Set duration of first "note"
moveq #0,d0
move.w (a4)+,d0 ; load PSG channel pointer
add.l a3,d0 ; Relative pointer
move.l d0,TrackDataPointer(a1) ; Store track pointer
move.w (a4)+,TrackTranspose(a1) ; load PSG modifier
move.b (a4)+,d0 ; load redundant byte
move.b (a4)+,TrackVoiceIndex(a1) ; Initial PSG tone
adda.w d6,a1
dbf d7,.bgm_psgloadloop
; loc_72154:
.bgm_psgdone:
lea v_sfx_track_ram(a6),a1
moveq #((v_sfx_track_ram_end-v_sfx_track_ram)/TrackSz)-1,d7 ; 6 SFX tracks
; loc_7215A:
.sfxstoploop:
tst.b TrackPlaybackControl(a1) ; Is SFX playing?
bpl.w .sfxnext ; Branch if not
moveq #0,d0
move.b TrackVoiceControl(a1),d0 ; Get voice control bits
bmi.s .sfxpsgchannel ; Branch if this is a PSG channel
subq.b #2,d0 ; SFX can't have FM1 or FM2
lsl.b #2,d0 ; Convert to index
bra.s .gotchannelindex
; ===========================================================================
; loc_7216E:
.sfxpsgchannel:
lsr.b #3,d0 ; Convert to index
; loc_72170:
.gotchannelindex:
lea SFX_BGMChannelRAM(pc),a0
movea.l (a0,d0.w),a0
bset #2,TrackPlaybackControl(a0) ; Set 'SFX is overriding' bit
; loc_7217C:
.sfxnext:
adda.w d6,a1
dbf d7,.sfxstoploop
tst.w v_spcsfx_fm4_track+TrackPlaybackControl(a6) ; Is special SFX being played?
bpl.s .checkspecialpsg ; Branch if not
bset #2,v_music_fm4_track+TrackPlaybackControl(a6) ; Set 'SFX is overriding' bit
; loc_7218E:
.checkspecialpsg:
tst.w v_spcsfx_psg3_track+TrackPlaybackControl(a6) ; Is special SFX being played?
bpl.s .sendfmnoteoff ; Branch if not
bset #2,v_music_psg3_track+TrackPlaybackControl(a6) ; Set 'SFX is overriding' bit
; loc_7219A:
.sendfmnoteoff:
lea v_music_fm_tracks(a6),a5
moveq #((v_music_fm_tracks_end-v_music_fm_tracks)/TrackSz)-1,d4 ; 6 FM tracks
; loc_721A0:
.fmnoteoffloop:
jsr FMNoteOff(pc)
adda.w d6,a5
dbf d4,.fmnoteoffloop ; run all FM tracks
moveq #((v_music_psg_tracks_end-v_music_psg_tracks)/TrackSz)-1,d4 ; 3 PSG tracks
; loc_721AC:
.psgnoteoffloop:
jsr PSGNoteOff(pc)
adda.w d6,a5
dbf d4,.psgnoteoffloop ; run all PSG tracks
; loc_721B6:
.locdblret:
addq.w #4,sp ; Tamper with return value to not return to caller
rts
; ===========================================================================
; byte_721BA:
FMDACInitBytes: dc.b 6, 0, 1, 2, 4, 5, 6 ; first byte is for DAC; then notice the 0, 1, 2 then 4, 5, 6; this is the gap between parts I and II for YM2612 port writes
even
; byte_721C2:
PSGInitBytes: dc.b $80, $A0, $C0 ; Specifically, these configure writes to the PSG port for each channel
even
; ===========================================================================
; ---------------------------------------------------------------------------
; Play normal sound effect
; ---------------------------------------------------------------------------
; Sound_A0toCF:
Sound_PlaySFX:
tst.b f_1up_playing(a6) ; Is 1-up playing?
bne.w .clear_sndprio ; Exit is it is
tst.b v_fadeout_counter(a6) ; Is music being faded out?
bne.w .clear_sndprio ; Exit if it is
tst.b f_fadein_flag(a6) ; Is music being faded in?
bne.w .clear_sndprio ; Exit if it is
cmpi.b #sfx_Ring,d7 ; is ring sound effect played?
bne.s .sfx_notRing ; if not, branch
tst.b v_ring_speaker(a6) ; Is the ring sound playing on right speaker?
bne.s .gotringspeaker ; Branch if not
move.b #sfx_RingLeft,d7 ; play ring sound in left speaker
; loc_721EE:
.gotringspeaker:
bchg #0,v_ring_speaker(a6) ; change speaker
; Sound_notB5:
.sfx_notRing:
cmpi.b #sfx_Push,d7 ; is "pushing" sound played?
bne.s .sfx_notPush ; if not, branch
tst.b f_push_playing(a6) ; Is pushing sound already playing?
bne.w .locret ; Return if not
move.b #$80,f_push_playing(a6) ; Mark it as playing
; Sound_notA7:
.sfx_notPush:
movea.l (Go_SoundIndex).l,a0
subi.b #sfx__First,d7 ; Make it 0-based
lsl.w #2,d7 ; Convert sfx ID into index
movea.l (a0,d7.w),a3 ; SFX data pointer
movea.l a3,a1
moveq #0,d1
move.w (a1)+,d1 ; Voice pointer
add.l a3,d1 ; Relative pointer
move.b (a1)+,d5 ; Dividing timing
if FixBugs
moveq #0,d7
else
; DANGER! there is a missing 'moveq #0,d7' here, without which SFXes whose
; index entry is above $3F will cause a crash.
; This bug is fixed in Ristar's driver.
endif
move.b (a1)+,d7 ; Number of tracks (FM + PSG)
subq.b #1,d7
moveq #TrackSz,d6
; loc_72228:
.sfx_loadloop:
moveq #0,d3
move.b 1(a1),d3 ; Channel assignment bits
move.b d3,d4
bmi.s .sfxinitpsg ; Branch if PSG
subq.w #2,d3 ; SFX can only have FM3, FM4 or FM5
lsl.w #2,d3
lea SFX_BGMChannelRAM(pc),a5
movea.l (a5,d3.w),a5
bset #2,TrackPlaybackControl(a5) ; Mark music track as being overridden
bra.s .sfxoverridedone
; ===========================================================================
; loc_72244:
.sfxinitpsg:
lsr.w #3,d3
lea SFX_BGMChannelRAM(pc),a5
movea.l (a5,d3.w),a5
bset #2,TrackPlaybackControl(a5) ; Mark music track as being overridden