-
Notifications
You must be signed in to change notification settings - Fork 5
/
Flamedriver.asm
5582 lines (5199 loc) · 205 KB
/
Flamedriver.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
; ---------------------------------------------------------------------------
; ===========================================================================
; | |
; | SONIC&K SOUND DRIVER |
; | |
; ===========================================================================
; Disassembled by MarkeyJester
; Routines, pointers and stuff by Linncaki
; Thoroughly commented and improved by Flamewing
; ===========================================================================
; Permission to use, copy, modify, and/or distribute this software for any
; purpose with or without fee is hereby granted.
;
; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
; WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
; MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
; ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
; WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
; ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
; OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
; ===========================================================================
; Constants
; ===========================================================================
; Used by SMPS2ASM include file.
SonicDriverVer = 5
; Set the following to non-zero to use all S2 DAC samples, or to zero otherwise.
; The S1 samples are a subset of this.
use_s2_samples = 1
; Set the following to non-zero to use all S3D DAC samples, or to zero
; otherwise. Most of the S3D samples are also present in S3/S&K, but
; there are two samples specific to S3D.
use_s3d_samples = 1
; Set the following to non-zero to use all S3 DAC samples,
; or to zero otherwise.
use_s3_samples = 1
; Set the following to non-zero to use all S&K DAC samples,
; or to zero otherwise.
use_sk_samples = 1
; ---------------------------------------------------------------------------
z80_SoundDriverStart:
; ---------------------------------------------------------------------------
zTrack STRUCT DOTS
; Playback control bits:
; 0 (01h) Noise channel (PSG) or FM3 special mode (FM)
; 1 (02h) Do not attack next note
; 2 (04h) SFX is overriding this track
; 3 (08h) 'Alternate frequency mode' flag
; 4 (10h) 'Track is resting' flag
; 5 (20h) 'Pitch slide' flag
; 6 (40h) 'Sustain frequency' flag -- prevents frequency from changing again for the lifetime of the track
; 7 (80h) Track is playing
PlaybackControl: ds.b 1 ; S&K: 0
; Voice control bits:
; 0-1 FM channel assignment bits (00 = FM1 or FM4, 01 = FM2 or FM5, 10 = FM3 or FM6/DAC, 11 = invalid)
; 2 (04h) For FM/DAC channels, selects if reg/data writes are bound for part II (set) or part I (unset)
; 3 (08h) Unknown/unused
; 4 (10h) Unknown/unused
; 5-6 PSG Channel assignment bits (00 = PSG1, 01 = PSG2, 10 = PSG3, 11 = Noise)
; 7 (80h) PSG track if set, FM or DAC track otherwise
VoiceControl: ds.b 1 ; S&K: 1
TempoDivider: ds.b 1 ; S&K: 2
DataPointerLow: ds.b 1 ; S&K: 3
DataPointerHigh: ds.b 1 ; S&K: 4
Transpose: ds.b 1 ; S&K: 5
Volume: ds.b 1 ; S&K: 6
ModulationCtrl: ds.b 1 ; S&K: 7 ; Modulation is on if nonzero. If only bit 7 is set, then it is normal modulation; otherwise, this-1 is index on modulation envelope pointer table
VoiceIndex: ds.b 1 ; S&K: 8 ; FM instrument/PSG voice
StackPointer: ds.b 1 ; S&K: 9 ; For call subroutine coordination flag
AMSFMSPan: ds.b 1 ; S&K: 0Ah
DurationTimeout: ds.b 1 ; S&K: 0Bh
SavedDuration: ds.b 1 ; S&K: 0Ch ; Already multiplied by timing divisor
; ---------------------------------
; Alternate names for same offset:
SavedDAC: ; S&K: 0Dh ; For DAC channel
FreqLow: ds.b 1 ; S&K: 0Dh ; For FM/PSG channels
; ---------------------------------
FreqHigh: ds.b 1 ; S&K: 0Eh ; For FM/PSG channels
VoiceSongID: ds.b 1 ; S&K: 0Fh ; For using voices from a different song
DACSFXPlaying:
Detune: ds.b 1 ; S&K: 10h/11h ; In S&K, some places used 11h instead of 10h
VolEnv: ds.b 1 ; S&K: 17h ; Used for dynamic volume adjustments
; ---------------------------------
; Alternate names for same offsets:
FMVolEnv: ; S&K: 18h
HaveSSGEGFlag: ds.b 1 ; S&K: 18h ; For FM channels, if track has SSG-EG data
FMVolEnvMask: ; S&K: 19h
SSGEGPointerLow: ds.b 1 ; S&K: 19h ; For FM channels, custom SSG-EG data pointer
PSGNoise: ; S&K: 1Ah
SSGEGPointerHigh: ds.b 1 ; S&K: 1Ah ; For FM channels, custom SSG-EG data pointer
; ---------------------------------
TLPtrLow: ds.b 1 ; S&K: 1Ch
TLPtrHigh: ds.b 1 ; S&K: 1Dh
NoteFillTimeout: ds.b 1 ; S&K: 1Eh
NoteFillMaster: ds.b 1 ; S&K: 1Fh
ModulationPtrLow: ds.b 1 ; S&K: 20h
ModulationPtrHigh: ds.b 1 ; S&K: 21h
; ---------------------------------
; Alternate names for same offset:
ModulationValLow: ; S&K: 22h
ModEnvSens: ds.b 1 ; S&K: 22h
; ---------------------------------
ModulationValHigh: ds.b 1 ; S&K: 23h
ModulationWait: ds.b 1 ; S&K: 24h
; ---------------------------------
; Alternate names for same offset:
ModulationSpeed: ; S&K: 25h
ModEnvIndex: ds.b 1 ; S&K: 25h
; ---------------------------------
ModulationDelta: ds.b 1 ; S&K: 26h
ModulationSteps: ds.b 1 ; S&K: 27h
LoopCounters: ds.b 2 ; S&K: 28h ; Might overflow into the following data
VoicesLow: ds.b 1 ; S&K: 2Ah ; Low byte of pointer to track's voices, used only if zUpdatingSFX is set
VoicesHigh: ds.b 1 ; S&K: 2Bh ; High byte of pointer to track's voices, used only if zUpdatingSFX is set
Stack_top: ds.b 4 ; S&K: 2Ch-2Fh ; Track stack; can be used by LoopCounters
zTrack ENDSTRUCT
; ---------------------------------------------------------------------------
; Playback control bits:
bitPSGNoise = 0
bitFM3Special = 0
bitNoAttack = 1
bitSFXOverride = 2
bitAltFreqMode = 3
bitTrackAtRest = 4
bitPitchSlide = 5
bitSustainFreq = 6
bitTrackPlaying = 7
maskSkipFMNoteOn = (1<<bitNoAttack)|(1<<bitSFXOverride)|(1<<bitNoAttack)
maskSkipFMNoteOff = (1<<bitSFXOverride)|(1<<bitNoAttack)
maskPlayRest = (1<<bitTrackPlaying)|(1<<bitTrackAtRest)
maskFM6Unused = (1<<bitSFXOverride)|(1<<bitTrackAtRest)
; Voice control values:
ymFM1 = 0
ymFM2 = 1
ymFM3 = 2
ymFM4 = 4
ymFM5 = 5
ymFM6 = 6
ymDAC = 6
ymPartII = 2 ; Bit value
snPSGTone = 0
snPSGVol = $10
snPSG1 = $80
snPSG2 = $A0
snPSG3 = $C0
snNoise = $E0
bitIsPSG = 7 ; Bit value
; ---------------------------------------------------------------------------
; equates: standard (for Genesis games) addresses in the memory map
zYM2612_A0 = $4000
zYM2612_D0 = $4001
zYM2612_A1 = $4002
zYM2612_D1 = $4003
zBankRegister = $6000
zPSG = $7F11
zROMWindow = $8000
; ---------------------------------------------------------------------------
; YM2612 register equates
ymLFO = $22
maskLFOFrequency = 7
bitLFOEnable = 3
ymTimerAFrequencyHigh = $24
ymTimerAFrequencyLow = $25
ymTimerBFrequency = $26
ymTimerControlFm3Mode = $27
maskFM3Normal = 0
maskFM3Special = $40
bitTimerALoad = 0
bitTimerBLoad = 1
bitTimerAEnable = 2
bitTimerBEnable = 3
bitTimerAReset = 4
bitTimerBReset = 5
maskEnableLoadTimers = (1<<bitTimerBEnable)|(1<<bitTimerAEnable)|(1<<bitTimerBLoad)|(1<<bitTimerALoad)
ymKeyOnOff = $28
bitOperator1 = 4
bitOperator2 = 5
bitOperator3 = 6
bitOperator4 = 7
maskAllOperators = (1<<bitOperator4)|(1<<bitOperator3)|(1<<bitOperator2)|(1<<bitOperator1)
ymDACPCM = $2A
ymDACEnable = $2B
maskDACDisable = 0
maskDACEnable = $80
ymDetuneMultiply1 = $30
ymDetuneMultiply2 = $34
ymDetuneMultiply3 = $38
ymDetuneMultiply4 = $3C
ymTotalLevel1 = $40
ymTotalLevel2 = $44
ymTotalLevel3 = $48
ymTotalLevel4 = $4C
ymRateScaleAttackRate1 = $50
ymRateScaleAttackRate2 = $54
ymRateScaleAttackRate3 = $58
ymRateScaleAttackRate4 = $5C
maskAttackRate = $1F
maxAttackRate = maskAttackRate
maskRateScale = $C0
ymAMDecayRate1 = $60
ymAMDecayRate2 = $64
ymAMDecayRate3 = $68
ymAMDecayRate4 = $6C
ymSustainRate1 = $70
ymSustainRate2 = $74
ymSustainRate3 = $78
ymSustainRate4 = $7C
ymSustainLevelReleaseRate1 = $80
ymSustainLevelReleaseRate2 = $84
ymSustainLevelReleaseRate3 = $88
ymSustainLevelReleaseRate4 = $8C
maskReleaseRate = $F
maxReleaseRate = maskReleaseRate
maskSustainLevel = $F0
maxSustainLevel = maskSustainLevel
ymSSGEG1 = $90
ymSSGEG2 = $94
ymSSGEG3 = $98
ymSSGEG4 = $9C
maskSSGEGEnvelopeShape = 7
bitSSGEGEnable = 3
maskSSGEGEnable = 1<<bitSSGEGEnable
ymFrequencyLow = $A0
ymFrequencyHigh = $A4
ymCH3FrequencyLow1 = $A9
ymCH3FrequencyLow2 = $AA
ymCH3FrequencyLow3 = $A8
ymCH3FrequencyLow4 = $A2
ymCH3FrequencyHigh1 = $AD
ymCH3FrequencyHigh2 = $AE
ymCH3FrequencyHigh3 = $AC
ymCH3FrequencyHigh4 = $A6
ymAlgorithmFeedback = $B0
maskAlgorithm = 7
maskFeedback = $38
ymPanningAMSensFMSens = $B4
maskFMSensitivity = 7
maskAMSensitivity = $30
bitOutputRight = 6
bitOutputLeft = 7
maskPanning = $C0
; ---------------------------------------------------------------------------
; Envelope-related constants
ModEnvReset = $80
ModEnvSustain1 = $81
ModEnvJumpTo = $82
ModEnvSustain = $83
ModEnvAlterSens = $84
VolEnvReset = $80
VolEnvRestTrack = $81
VolEnvJumpTo = $82
VolEnvStopTrack = $83
; ---------------------------------------------------------------------------
; z80 RAM:
zDataStart = $1C1A
phase zDataStart
z80_stack_top: ds.b $60
z80_stack:
zDACEnable: ds.b 1
zDACEnableSave: ds.b 1
zSpecFM3Freqs: ds.b 8
zSpecFM3FreqsSFX: ds.b 8
zQueueVariables:
zPalFlag: ds.b 1
zPalDblUpdCounter: ds.b 1
zSoundQueue0: ds.b 1
zSoundQueue1: ds.b 1
zSoundQueue2: ds.b 1
zTempoSpeedup: ds.b 1
zTempoSpeedupReq: ds.b 1
zNextSound: ds.b 1
; The following 3 variables are used for M68K input
zMusicNumber: ds.b 1 ; Play_Sound
zSFXNumber0: ds.b 1 ; Play_Sound_2
zSFXNumber1: ds.b 1 ; Play_Sound_2
shared zQueueVariables,zMusicNumber,zSFXNumber0,zSFXNumber1
if (zQueueVariables&1)<>0
fatal "zQueueVariables must be at an even address."
endif
zContinuousSFX: ds.b 1
zContinuousSFXFlag: ds.b 1
zContSFXLoopCnt: ds.b 1 ; Used as a loop counter for continuous SFX
zFadeOutTimeout: ds.b 1
zFadeDelay: ds.b 1
zFadeDelayTimeout: ds.b 1
zPauseFlag: ds.b 1
zHaltFlag: ds.b 1
zTempoAccumulator: ds.b 1
zFadeToPrevFlag: ds.b 1
zUpdatingSFX: ds.b 1
zCurrentTempo: ds.b 1
zSpindashRev: ds.b 1
zRingSpeaker: ds.b 1
zFadeInTimeout: ds.b 1
zVoiceTblPtrSave: ds.b 2 ; For 1-up
zCurrentTempoSave: ds.b 1 ; For 1-up
zSongBankSave: ds.b 1 ; For 1-up
zTempoSpeedupSave: ds.b 1 ; For 1-up
zSpeedupTimeout: ds.b 1
zDACIndex: ds.b 1 ; bit 7 = 1 if playing, 0 if not; remaining 7 bits are index into DAC tables (1-based)
zSongPosition: ds.b 2
zTrackInitPos: ds.b 2 ; 2 bytes
zVoiceTblPtr: ds.b 2 ; 2 bytes
zSongBank: ds.b 1 ; Bits 15 to 22 of M68K bank address
PlaySegaPCMFlag: ds.b 1
zSFXVoiceTblPtr: ds.b 2 ; 2 bytes
zSFXTempoDivider: ds.b 1
; Now starts song and SFX z80 RAM
; Max number of music channels: 6 FM + 3 PSG or 1 DAC + 5 FM + 3 PSG
zTracksStart:
zSongDAC: zTrack
zSongFM1: zTrack
zSongFM2: zTrack
zSongFM3: zTrack
zSongFM4: zTrack
zSongFM5: zTrack
zSongFM6: zTrack
zSongPSG1: zTrack
zSongPSG2: zTrack
zSongPSG3: zTrack
zTracksEnd:
; This is RAM for backup of songs (when 1-up jingle is playing)
; and for SFX channels. Note these two overlap.
; Max number of SFX channels: 4 FM + 3 PSG
zTracksSFXStart:
zSFX_FM3: zTrack
zSFX_FM4: zTrack
zSFX_FM5: zTrack
zSFX_FM6: zTrack
zSFX_PSG1: zTrack
zSFX_PSG2: zTrack
zSFX_PSG3: zTrack
zTracksSFXEnd:
dephase
phase zTracksSFXStart
zTracksSaveStart:
zSaveSongDAC: zTrack
zSaveSongFM1: zTrack
zSaveSongFM2: zTrack
zSaveSongFM3: zTrack
zSaveSongFM4: zTrack
zSaveSongFM5: zTrack
zSaveSongFM6: zTrack
zSaveSongPSG1: zTrack
zSaveSongPSG2: zTrack
zSaveSongPSG3: zTrack
zTracksSaveEnd:
if (zQueueVariables&1)<>0
fatal "zQueueVariables must be at an even address as it is used as a longword by the 68k!"
endif
if * > $2000 ; Don't declare more space than the RAM can contain!
fatal "The RAM variable declarations are too large by $\{$} bytes."
endif
dephase
zNumMusicTracks = (zTracksEnd-zTracksStart)/zTrack.len
zNumMusicFMorPSGTracks = (zTracksEnd-zSongFM1)/zTrack.len
zNumMusicFMorDACTracks = (zSongPSG1-zTracksStart)/zTrack.len
zNumMusicFMTracks = (zSongPSG1-zSongFM1)/zTrack.len
zNumMusicFM1Tracks = (zSongFM4-zSongFM1)/zTrack.len
zNumMusicFM2Tracks = (zSongPSG1-zSongFM4)/zTrack.len
zNumMusicPSGTracks = (zTracksEnd-zSongPSG1)/zTrack.len
zNumSFXTracks = (zTracksSFXEnd-zTracksSFXStart)/zTrack.len
zNumSaveTracks = (zTracksSaveEnd-zTracksSaveStart)/zTrack.len
; ---------------------------------------------------------------------------
!org z80_SoundDriverStart
z80_SoundDriver:
save
!org 0 ; z80 Align, handled by the build process
CPU Z80
listing purecode
; ---------------------------------------------------------------------------
ifndef MusID__First
ifdef Mus__First
MusID__First = Mus__First
else
ifdef bgm__First
MusID__First = bgm__First
endif
endif
ifndef MusID__First
MusID__First = 01h
endif
endif
ifndef MusID_ExtraLife
ifdef mus_ExtraLife
MusID_ExtraLife = mus_ExtraLife
else
ifdef bgm_ExtraLife
MusID_ExtraLife = bgm_ExtraLife
endif
endif
ifndef MusID_ExtraLife
MusID_ExtraLife = 2Ah
endif
endif
ifndef MusID__End
ifdef Mus__End
MusID__End = Mus__End
else
ifdef bgm__Last
MusID__End = bgm__Last
endif
endif
ifndef MusID__End
MusID__End = 33h
endif
endif
ifdef MusID_SKCredits
if MusID_SKCredits>=MusID__End
fatal "S&K Credits music must have an ID within the music range of [$\{MusID__First}, $\{MusID__End}), but it has ID $\{MusID_SKCredits}"
endif
endif
ifdef mus_CreditsK
if mus_CreditsK>=MusID__End
fatal "S&K Credits music must have an ID within the music range of [$\{MusID__First}, $\{MusID__End}), but it has ID $\{mus_CreditsK}"
endif
endif
ifndef SndID__First
ifdef sfx_First
SndID__First = sfx_First
if sfx_First>1
message "You can gain more IDs for SFX by changing the the definition of the sfx_First constant to 1 (it is currently $\{sfx_First})"
endif
else
ifdef sfx__First
SndID__First = sfx__First
if sfx_First>1
message "You can gain more IDs for SFX by changing the the definition of the sfx_First constant to 1 (it is currently $\{sfx__First})"
endif
endif
endif
ifndef SndID__First
SndID__First = 01h
endif
elseif SndID__First>1
message "You can gain more IDs for SFX by changing the the definition of the SndID__First constant to 1 (it is currently $\{SndID__First})"
endif
ifndef SndID_Ring
ifdef sfx_RingRight
SndID_Ring = sfx_RingRight
else
ifdef sfx_Ring
SndID_Ring = sfx_Ring
endif
endif
ifndef SndID_Ring
SndID_Ring = SndID__First
endif
endif
ifndef SndID_RingLeft
ifdef sfx_RingLeft
SndID_RingLeft = sfx_RingLeft
endif
ifndef SndID_RingLeft
SndID_RingLeft = SndID_Ring+1
endif
endif
if SndID_RingLeft==SndID_Ring+1
RingSoundsAdjacent := 1
else
RingSoundsAdjacent := 0
warning "You should make sure SndID_RingLeft is immediately after SndID_Ring"
endif
ifndef SndID_SpindashRev
ifdef sfx_Spindash
SndID_SpindashRev = sfx_Spindash
else
ifdef sfx_Roll
SndID_SpindashRev = sfx_Roll
warning "Approximating spindash rev sound by rolling sound. Please provide an adequate equate for the ported spindash rev sound"
endif
endif
ifndef SndID_SpindashRev
SndID_SpindashRev = 0ABh-33h+SndID__First
endif
endif
ifndef SndID__End
ifdef sfx__End
SndID__End = sfx__End
else
ifdef sfx__Last
SndID__End = sfx__Last
endif
endif
ifndef SndID__End
SndID__End = 0E0h-33h+SndID__First
endif
endif
ifndef SndID__FirstContinuous
ifdef sfx__FirstContinuous
SndID__FirstContinuous = sfx__FirstContinuous
else
SndID__FirstContinuous = 0BCh-33h+SndID__First
endif
endif
ifndef SndID__FirstContinuous
SndID__FirstContinuous = SndID__End
endif
ifndef DACID__First
ifdef dac__First
DACID__First = dac__First
else
DACID__First = SndID__End
endif
endif
ifndef DACID__End
ifdef dac__End
DACID__End = dac__End
else
DACID__End = SndID__End
endif
endif
ifndef FadeID__First
ifdef mus__FirstCmd
FadeID__First = mus__FirstCmd
else
ifdef flg__First
FadeID__First = flg__First
endif
endif
ifndef FadeID__First
FadeID__First = 0E1h
endif
endif
ifndef FadeID__End
ifdef Mus__EndCmd
FadeID__End = Mus__EndCmd
else
ifdef flg__Last
FadeID__End = flg__Last
endif
endif
ifndef FadeID__End
FadeID__End = 0E6h
endif
endif
ifndef MusID_StopSega
ifdef mus_StopSEGA
MusID_StopSega = mus_StopSEGA
else
ifndef MusID_StopSega
MusID_StopSega = 0FEh
endif
endif
endif
ifndef MusID_SegaSound
ifdef mus_SEGA
MusID_SegaSound = mus_SEGA
else
ifdef sfx_Sega
MusID_SegaSound = sfx_Sega
endif
endif
ifndef MusID_SegaSound
MusID_SegaSound = 0FFh
endif
endif
; ---------------------------------------------------------------------------
NoteRest = 080h
FirstCoordFlag = 0E0h
; ---------------------------------------------------------------------------
zID_MusicPointers = 0
zID_SFXPointers = 2
zID_ModEnvPointers = 4
zID_VolEnvPointers = 6
; ---------------------------------------------------------------------------
; ===========================================================================
; Macros
; ===========================================================================
bankswitch macro
ld hl, zBankRegister
ld (hl), a
rept 7
rrca
ld (hl), a
endm
ld (hl), h ; The low bit of h is 0
endm
bankswitchLoop macro
ld b, 8
.bankloop:
ld (zBankRegister), a
rrca
djnz .bankloop
xor a
ld (zBankRegister), a
endm
bankswitchToMusic macro
ld a, (zSongBank)
bankswitch
endm
; macro to make a certain error message clearer should you happen to get it...
rsttarget macro {INTLABEL}
if ($&7)||($>38h)
fatal "Function __LABEL__ is at 0\{$}h, but must be at a multiple of 8 bytes <= 38h to be used with the rst instruction."
endif
if "__LABEL__"<>""
__LABEL__ label $
endif
endm
setMaxAR macro
or maxAttackRate ; Set AR to maximum
endm
calcVolume macro
or a ; Is it positive?
jp p, .skip_track_vol ; Branch if yes
add a, (ix+zTrack.Volume) ; Add track's volume to it
jr nc, .skip_track_vol
sbc a, a ; Clamp volume attenuation as it overflowed
.skip_track_vol:
endm
zFastWriteFM macro reg, data, dataMacro
ld a, reg ; Get register to write to
add a, c ; Add the channel bits to the register address
ld (iy+0), a ; Select YM2612 register
ld a, data ; a = data to send
if "dataMacro"<>""
dataMacro
endif
ld (iy+1), a ; Send data to register
endm
zGetFMPartPointer macro
ld c, (ix+zTrack.VoiceControl) ; Get voice control bits for future use
ld iy, zYM2612_A0 ; Point to part I
bit ymPartII, c ; Is this the DAC channel or FM4 or FM5 or FM6?
jr z, .notFMII ; If not, write reg/data pair to part I
res ymPartII, c ; Strip 'bound to part II regs' bit
ld iy, zYM2612_A1 ; Point to part II
.notFMII:
endm
; function to turn a 68k address into a word the Z80 can use to access it
zmake68kPtr function addr,zROMWindow+(addr&7FFFh)
; function to turn a 68k address into a bank byte
; Note: This discards a bit (should be 0FF8000h instead of 7F8000h). This is
; relatively harmless since the driver only uses 8 bits anyway.
zmake68kBank function addr,(((addr&7F8000h)>>15))
; ---------------------------------------------------------------------------
; ===========================================================================
; Entry Point
; ===========================================================================
; EntryPoint:
di ; Disable interrupts
di ; Disable interrupts
im 1 ; set interrupt mode 1
jp zInitAudioDriver
; ---------------------------------------------------------------------------
; =============== S U B R O U T I N E =======================================
;
; Gets the correct pointer to pointer table for the data type in question
; (music, sfx, voices, etc.).
;
; Input: c ID for data type.
; Output: hl Master pointer table for index
; af' Trashed
; b Trashed
; sub_8
align 8
GetPointerTable: rsttarget
ld hl,z80_SoundDriverPointers ; Load pointer table
ld b, 0 ; b = 0
add hl, bc ; Add offset into pointer table
ex af, af' ; Back up af
ld a, (hl) ; Read low byte of pointer into a
inc hl
ld h, (hl) ; Read high byte of pointer into h
ld l, a ; Put low byte of pointer into l
ex af, af' ; Restore af
jp PointerTableOffset
; End of function GetPointerTable
; =============== S U B R O U T I N E =======================================
;
; Reads an offset into a pointer table and returns dereferenced pointer.
;
;
; Input: a Index into pointer table
; hl Pointer to pointer table
; Output: hl Selected pointer in pointer table
; bc Trashed
; sub_18
align 8
PointerTableOffset: rsttarget
ld c, a ; c = index into pointer table
ld b, 0 ; b = 0
add hl, bc ; hl += bc
add hl, bc ; hl += bc
jp ReadPointer ; 10 clock cycles, 3 bytes
; End of function PointerTableOffset
; =============== S U B R O U T I N E =======================================
;
; Dereferences a pointer.
;
; Input: hl Pointer
; output: hl Equal to what that was being pointed to by hl
; loc_20
align 8
ReadPointer: rsttarget
ld a, (hl) ; Read low byte of pointer into a
inc hl
ld h, (hl) ; Read high byte of pointer into h
ld l, a ; Put low byte of pointer into l
ret
; End of function ReadPointer
; ---------------------------------------------------------------------------
; There is room for two more 'rsttarget's here
; ---------------------------------------------------------------------------
align 38h
; =============== S U B R O U T I N E =======================================
;
; This subroutine is called every V-Int. After it is processed, the z80
; returns to the digital audio loop to comtinue playing DAC samples.
;
; If the SEGA PCM is being played, it disables interrupts -- this means that
; this procedure will NOT be called while the SEGA PCM is playing.
;
;zsub_38
zVInt: rsttarget
di ; Disable interrupts
push af ; Save af
push iy ; Save iy
exx ; Save bc,de,hl
.doupdate:
call zUpdateEverything ; Update all tracks
ld a, (zPalFlag) ; Get PAL flag
or a ; Is it set?
jr z, .not_pal ; Branch if not (NTSC)
ld a, (zPalDblUpdCounter) ; Get PAL double-update timeout counter
or a ; Is it zero?
jr nz, .pal_timer ; Branch if not
ld a, 5 ; Set it back to 5...
ld (zPalDblUpdCounter), a ; ... and save it
jp .doupdate ; Go again
.pal_timer:
dec a ; Decrease PAL double-update timeout counter
ld (zPalDblUpdCounter), a ; Store it
.not_pal:
ld a, (zDACIndex) ; Get index of playing DAC sample
and 7Fh ; Strip 'DAC playing' bit
ld c, a ; c = a
ld b, 0 ; Sign extend c to bc
ld hl, DAC_Banks ; Make hl point to DAC bank table
add hl, bc ; Offset into entry for current sample
ld a, (hl) ; Get bank index
bankswitch ; Switch to current DAC sample's bank
exx ; Restore bc,de,hl
pop iy ; Restore iy
pop af ; Restore af
ld b, 1 ; b = 1
ret
; ---------------------------------------------------------------------------
;loc_85
zInitAudioDriver:
ld sp, z80_stack ; set the stack pointer to 0x2000 (end of z80 RAM)
; The following instruction block keeps the z80 in a tight loop.
ld c, 0 ; c = 0
.loop:
ld b, 0 ; b = 0
djnz $ ; Loop in this instruction, decrementing b each iteration, until b = 0
dec c ; c--
jr z, .loop ; Loop if c = 0
call zMusicFade ; Stop all music
ld a, zmake68kBank(DacBank2) ; Set song bank to second DAC bank (default value)
ld (zSongBank), a ; Store it
xor a ; a = 0
ld (zSpindashRev), a ; Reset spindash rev
ld (zDACIndex), a ; Clear current DAC sample index
ld (PlaySegaPCMFlag), a ; Clear the Sega sound flag
ld (zRingSpeaker), a ; Make rings play on left speaker
ld a, 5 ; Set PAL double-update counter to 5
ld (zPalDblUpdCounter), a ; (that is, do not double-update for 5 frames)
ei ; Enable interrupts
jp zPlayDigitalAudio ; Start digital audio loop
; ---------------------------------------------------------------------------
; =============== S U B R O U T I N E =======================================
;
; Writes a reg/data pair to part I or II
;
; Input: a Value for register
; c Value for data
; ix Pointer to track RAM
;sub_AF
zWriteFMIorII:
bit bitIsPSG, (ix+zTrack.VoiceControl) ; Is this a PSG track?
ret nz ; Is so, quit
bit bitSFXOverride, (ix+zTrack.PlaybackControl) ; Is SFX overriding this track?
ret nz ; Return if yes
add a, (ix+zTrack.VoiceControl) ; Add the channel bits to the register address
bit ymPartII, (ix+zTrack.VoiceControl) ; Is this the DAC channel or FM4 or FM5 or FM6?
jr nz, zWriteFMII_reduced ; If yes, write reg/data pair to part II;
; otherwise, write reg/data pair as is to part I.
; End of function zWriteFMIorII
; =============== S U B R O U T I N E =======================================
;
; Writes a reg/data pair to part I
;
; Input: a Value for register
; c Value for data
;sub_C2
zWriteFMI:
ld (zYM2612_A0), a ; Select YM2612 register
nop ; Wait
ld a, c ; a = data to send
ld (zYM2612_D0), a ; Send data to register
ret
; End of function zWriteFMI
; ---------------------------------------------------------------------------
;loc_CB
zWriteFMII_reduced:
sub 1<<ymPartII ; Strip 'bound to part II regs' bit
; ---------------------------------------------------------------------------
; =============== S U B R O U T I N E =======================================
;
; Writes a reg/data pair to part II
;
; Input: a Value for register
; c Value for data
;sub_CD
zWriteFMII:
ld (zYM2612_A1), a ; Select YM2612 register
nop ; Wait
ld a, c ; a = data to send
ld (zYM2612_D1), a ; Send data to register
ret
; End of function zWriteFMII
; ---------------------------------------------------------------------------
; =============== S U B R O U T I N E =======================================
;
;sub_11B
zUpdateEverything:
call zPauseUnpause ; Pause/unpause according to M68K input
call zUpdateSFXTracks ; Do SFX tracks
;loc_121
zUpdateMusic:
call TempoWait ; Delay song tracks as appropriate for main tempo mod
call zDoMusicFadeOut ; Check if music should be faded out and fade if needed
call zDoMusicFadeIn ; Check if music should be faded in and fade if needed
ld a, (zFadeToPrevFlag) ; Get fade-to-prev flag
cp MusID_ExtraLife-1 ; Is it still 1-Up?
jr nz, .check_fade_in ; Branch if not
ld a, (zMusicNumber) ; Get next music to play
cp MusID_ExtraLife ; Is it another 1-Up?
jr z, .clr_queue ; Branch if yes
cp MusID__End-1 ; Is it music?
jr c, .clr_sfx ; Branch if not
.clr_queue:
xor a ; a = 0
ld (zMusicNumber), a ; Clear queue entry
.clr_sfx:
xor a ; a = 0
ld (zSFXNumber0), a ; Clear first queued SFX
ld (zSFXNumber1), a ; Clear second queued SFX
jr .update_music
;loc_149
.check_fade_in:
ld a, (zFadeToPrevFlag) ; Get fade-to-previous flag
cp 0FFh ; Is it 0FFh?
jr z, .update_music ; Branch if yes
ld hl, zMusicNumber ; Point hl to M68K input
ld e, (hl) ; e = next song to play
inc hl ; Advance pointer
ld d, (hl) ; d = next SFX to play
inc hl ; Advance pointer
ld a, (hl) ; a = next SFX to play
or d ; Combine bits of a and d
or e ; Is anything in the play queue?
jr z, .update_music ; Branch if not
call zFillSoundQueue ; Transfer M68K input
call zCycleMusicQueue ; Cycle queue and play first entry (moves on to second)
call zCycleSoundQueue ; Cycle queue and play second entry
call zCycleSoundQueue ; Cycle queue and play third entry
.update_music:
bankswitchToMusic
xor a ; a = 0
ld (zUpdatingSFX), a ; Updating music
ld a, (zFadeToPrevFlag) ; Get fade-to-previous flag
cp 0FFh ; Is it 0FFh?
call z, zFadeInToPrevious ; Fade to previous if yes
ld ix, zSongDAC ; ix = DAC track RAM
bit bitTrackPlaying, (ix+zTrack.PlaybackControl) ; Is DAC track playing?
call nz, zUpdateDACTrack ; Branch if yes
ld b, zNumMusicFMorPSGTracks ; Number of FM+PSG tracks
ld ix, zSongFM1 ; ix = FM1 track RAM
jr zTrackUpdLoop ; Play all tracks
; =============== S U B R O U T I N E =======================================
;
;sub_19E
zUpdateSFXTracks:
ld a, 1 ; a = 1
ld (zUpdatingSFX), a ; Updating SFX
ld a, zmake68kBank(SndBank) ; Get SFX bank ID
bankswitch ; Bank switch to SFX
ld ix, zTracksSFXStart ; ix = start of SFX track RAM
ld b, zNumSFXTracks ; Number of channels
zTrackUpdLoop:
push bc ; Save bc
bit bitTrackPlaying, (ix+zTrack.PlaybackControl) ; Is track playing?
call nz, zUpdateFMorPSGTrack ; Call routine if yes
ld de, zTrack.len ; Spacing between tracks
add ix, de ; Advance to next track
pop bc ; Restore bc
djnz zTrackUpdLoop ; Loop for all tracks
ld a, (zTempoSpeedup) ; Get tempo speed-up value
or a ; Is music sped up?
ret z ; Return if not
ld a, (zSpeedupTimeout) ; Get extra tempo timeout
or a ; Has it expired?
jp nz, .no_dbl_update ; Branch if not
ld a, (zTempoSpeedup) ; Get master tempo speed-up value
ld (zSpeedupTimeout), a ; Reset extra tempo timeout to it
jp zUpdateMusic ; Update music again
; ---------------------------------------------------------------------------
.no_dbl_update:
dec a ; Decrement timeout...
ld (zSpeedupTimeout), a ; ... then store new value
ret
; End of function zUpdateSFXTracks
; =============== S U B R O U T I N E =======================================
; Updates FM or PSG track.
;
;sub_1E9
zUpdateFMorPSGTrack:
bit bitIsPSG, (ix+zTrack.VoiceControl) ; Is this a PSG channel?