-
Notifications
You must be signed in to change notification settings - Fork 1
/
s2.sounddriver.asm
5122 lines (4788 loc) · 201 KB
/
s2.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
; ---------------------------------------------------------------------------
; ===========================================================================
; º º
; º SONIC&K SOUND DRIVER º
; º º
; ===========================================================================
; Disassembled by MarkeyJester
; Routines, pointers and stuff by Linncaki
; Throroungly commented and improved (including optional bugfixes) by Flamewing
; ===========================================================================
; Constants
; ===========================================================================
; Set this to 1 to fix some bugs in the driver.
SonicDriverVer = 5
fix_sndbugs = 1
; 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_SoundDriver:
!org 0 ; z80 Align, handled by the build process
CPU Z80
listing off
MusID_1UP = 2Ah
SndID_Spindash = 0ABh
SndID__FirstContinuous = 0BCh
MusID_SKCredits = 0DCh
FadeID__First = 0E1h
FadeID__End = 0E6h
SndID_StopSega = 0FEh
SndID_Sega = 0FFh
; ---------------------------------------------------------------------------
NoteRest = 080h
FirstCoordFlag = 0E0h
; ---------------------------------------------------------------------------
zID_MusicPointers0 = 0
zID_UniVoiceBank = 2
zID_MusicPointers4 = 4
zID_SFXPointers = 6
zID_FreqFlutterPointers = 8
zID_PSGTonePointers = 0Ah
; ---------------------------------------------------------------------------
z80_stack = 2000h
; equates: standard (for Genesis games) addresses in the memory map
zYM2612_A0 = 4000h
zYM2612_D0 = 4001h
zYM2612_A1 = 4002h
zYM2612_D1 = 4003h
zBankRegister = 6000h
zPSG = 7F11h
zROMWindow = 8000h
; z80 RAM:
if fix_sndbugs
zVariablesStart = 1BF0h
zSpecFM3Freqs = zVariablesStart
zSpecFM3FreqsSFX = zSpecFM3Freqs + 8
else
zVariablesStart = 1C00h
endif
zPalFlag = 1C02h
zPalDblUpdCounter = 1C04h
zSoundQueue0 = 1C05h
zSoundQueue1 = 1C06h
zSoundQueue2 = 1C07h
zTempoSpeedup = 1C08h
zNextSound = 1C09h
; The following 3 variables are used for M68K input
zMusicNumber = 1C0Ah ; Play_Sound
zSFXNumber0 = 1C0Bh ; Play_Sound_2
zSFXNumber1 = 1C0Ch ; Play_Sound_2
zFadeOutTimeout = 1C0Dh
zFadeDelay = 1C0Eh
zFadeDelayTimeout = 1C0Fh
zPauseFlag = 1C10h
zHaltFlag = 1C11h
zFM3Settings = 1C12h
zTempoAccumulator = 1C13h
unk_1C15 = 1C15h ; Set twice, never read
zFadeToPrevFlag = 1C16h
unk_1C17 = 1C17h ; Set once, never read
unk_1C18 = 1C18h
zUpdatingSFX = 1C19h
unk_1C21 = 1C21h
zCurrentTempo = 1C24h
zContinousSFX = 1C25h
zContinousSFXFlag = 1C26h
zSpindashRev = 1C27h
zRingSpeaker = 1C28h
zFadeInTimeout = 1C29h
zVoiceTblPtrSave = 1C2Ah ; For 1-up
zCurrentTempoSave = 1C2Ch ; For 1-up
zSongBankSave = 1C2Dh ; For 1-up
zTempoSpeedupSave = 1C2Eh ; For 1-up
zSpeedupTimeout = 1C2Fh
zDACIndex = 1C30h ; bit 7 = 1 if playing, 0 if not; remaining 7 bits are index into DAC tables (1-based)
zContSFXLoopCnt = 1C31h ; Used as a loop counter for continuous SFX
zSFXSaveIndex = 1C32h
zSongPosition = 1C33h
zTrackInitPos = 1C35h
zVoiceTblPtr = 1C37h ; 2 bytes
zSFXVoiceTblPtr = 1C39h ; 2 bytes
zSFXTempoDivider = 1C3Bh
zSongBank = 1C3Eh ; Bits 15 to 22 of M68K bank address
PlaySegaPCMFlag = 1C3Fh
; Now starts song and SFX z80 RAM
; Max number of music channels: 6 FM + 3 PSG or 1 DAC + 5 FM + 3 PSG
zTracksStart = 1C40h
zSongFM6_DAC = zTracksStart+0*zTrackSz ; Music DAC or FM6 track
zSongFM1 = zTracksStart+1*zTrackSz
zSongFM2 = zTracksStart+2*zTrackSz
zSongFM3 = zTracksStart+3*zTrackSz
zSongFM4 = zTracksStart+4*zTrackSz
zSongFM5 = zTracksStart+5*zTrackSz
zSongPSG1 = zTracksStart+6*zTrackSz
zSongPSG2 = zTracksStart+7*zTrackSz
zSongPSG3 = zTracksStart+8*zTrackSz
zTracksEnd = zTracksStart+9*zTrackSz
; This is RAM for backup of songs (e.g., for 1-up jingle)
zTracksSaveStart = zTracksEnd
zSaveSongFM6_DAC = zTracksSaveStart+0*zTrackSz
zSaveSongFM1 = zTracksSaveStart+1*zTrackSz
zSaveSongFM2 = zTracksSaveStart+2*zTrackSz
zSaveSongFM3 = zTracksSaveStart+3*zTrackSz
zSaveSongFM4 = zTracksSaveStart+4*zTrackSz
zSaveSongFM5 = zTracksSaveStart+5*zTrackSz
zSaveSongPSG1 = zTracksSaveStart+6*zTrackSz
zSaveSongPSG2 = zTracksSaveStart+7*zTrackSz
zSaveSongPSG3 = zTracksSaveStart+8*zTrackSz
zTracksSaveEnd = zTracksSaveStart+9*zTrackSz
; This is RAM for SFX channels
; Note this overlaps with the save RAM for 1-up sound, above
; Max number of SFX channels: 4 FM + 3 PSG
zTracksSFXStart = zTracksEnd
zSFX_FM3 = zTracksSFXStart+0*zTrackSz
zSFX_FM4 = zTracksSFXStart+1*zTrackSz
zSFX_FM5 = zTracksSFXStart+2*zTrackSz
zSFX_FM6 = zTracksSFXStart+3*zTrackSz
zSFX_PSG1 = zTracksSFXStart+4*zTrackSz
zSFX_PSG2 = zTracksSFXStart+5*zTrackSz
zSFX_PSG3 = zTracksSFXStart+6*zTrackSz
zTracksSFXEnd = zTracksSFXStart+7*zTrackSz
; Track data (each song track)
; 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 SMPS mode' flag
; 4 (10h) 'Track is resting' flag
; 5 (20h) Unknown/unused
; 6 (40h) 'Sustain frequency' flag -- prevents frequency from changing again for the lifetime of the track
; 7 (80h) Track is playing
zTrackPlaybackControl = 0
; Track data (each song track)
; 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
zTrackVoiceControl = 1
zTrackTempoDivider = 2
zTrackDataPointerLow = 3
zTrackDataPointerHigh = 4
zTrackKeyOffset = 5
zTrackVolume = 6
zTrackModulationCtrl = 7 ; Modulation is on if nonzero. If only bit 7 is set, then it is normal modulation; otherwise, this-1 is index on frequency flutter pointer table
zTrackVoiceIndex = 8 ; FM instrument/PSG voice
zTrackStackPointer = 9 ; For call subroutine coordination flag
zTrackAMSFMSPan = 0Ah
zTrackDurationTimeout = 0Bh
zTrackSavedDuration = 0Ch ; Already multiplied by timing divisor
; ---------------------------------
; Alternate names for same offset:
zTrackSavedDAC = 0Dh ; For DAC channel
; ---------------------------------
zTrackFreqLow = 0Dh ; For FM/PSG channels
; ---------------------------------
zTrackFreqHigh = 0Eh ; For FM/PSG channels
zTrackVoiceSongID = 0Fh ; For using voices from a different song
zTrackFreqDisplacement = 10h
zTrackUnk11h = 11h
zTrackVolFlutter = 17h ; Used for dynamic volume adjustments
; ---------------------------------
; Alternate names for same offsets:
zTrackFMFlutter = 18h
zTrackFMFlutterMask = 19h
zTrackPSGNoise = 1Ah
; ---------------------------------
zTrackHaveSSGEGFlag = 18h ; For FM channels, if track has SSG-EG data
zTrackSSGEGPointerLow = 19H ; For FM channels, custom SSG-EG data pointer
zTrackSSGEGPointerHigh = 1AH ; For FM channels, custom SSG-EG data pointer
; ---------------------------------
zTrackFeedbackAlgo = 1Bh
zTrackTLPtrLow = 1Ch
zTrackTLPtrHigh = 1Dh
zTrackNoteFillTimeout = 1Eh
zTrackNoteFillMaster = 1Fh
zTrackModulationPtrLow = 20h
zTrackModulationPtrHigh = 21h
; ---------------------------------
; Alternate names for same offset:
zTrackModulationValLow = 22h
; ---------------------------------
zTrackFreqFlutterSens = 22h
; ---------------------------------
zTrackModulationValHigh = 23h
zTrackModulationWait = 24h
; ---------------------------------
; Alternate names for same offset:
zTrackModulationSpeed = 25h
; ---------------------------------
zTrackFreqFlutterIndex = 25h
; ---------------------------------
zTrackModulationDelta = 26h
zTrackModulationSteps = 27h
zTrackLoopCounters = 28h ; May end u overwriting following data
zTrackVoicesLow = 2Ah ; Low byte of pointer to track's voices, used only if zUpdatingSFX is set
zTrackVoicesHigh = 2Bh ; High byte of pointer to track's voices, used only if zUpdatingSFX is set
zTrackSz = 30h ; Size of all tracks
; ===========================================================================
; Macros
; ===========================================================================
bankswitch1 macro
ld hl, zBankRegister
ld (hl), a
rept 7
rrca
ld (hl), a
endm
xor a
ld (hl), a
endm
bankswitch2 macro
ld hl, zBankRegister
ld (hl), a
rept 7
rra
ld (hl), a
endm
xor a
ld (hl), a
endm
bankswitch3 macro
ld b, 8
-
ld (zBankRegister), a
rrca
djnz -
xor a
ld (zBankRegister), a
endm
bankswitchToMusic macro
ld hl, zBankRegister
ld a, (zSongBank)
ld (hl), a
rept 7
rra
ld (hl), a
endm
xor a
ld (hl), a
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
; 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
zmake68kBank function addr,(((addr&3F8000h)/zROMWindow))
; ---------------------------------------------------------------------------
; ===========================================================================
; Entry Point
; ===========================================================================
; EntryPoint:
di ; Disable interrupts
di ; Disable interrupts
im 1 ; set interrupt mode 1
jp zInitAudioDriver
; ---------------------------------------------------------------------------
db 0F2h ; Filler; broken jp p,loc?
; =============== 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, (ptrMasterIndex) ; Read pointer to (pointer to pointer table) 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
ret
; End of function GetPointerTable
; ---------------------------------------------------------------------------
;word_15
ptrMasterIndex:
dw z80_SoundDriverPointers
; =============== 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
nop
nop
nop
; 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 PointerTableOffset
; ---------------------------------------------------------------------------
db 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
; ---------------------------------------------------------------------------
align 8
; =============== 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
- ld a, r ; Get memory refresh register
ld (unk_1C17), a ; Save it
call zUpdateEverything ; Update all tracks
ld a, (zPalFlag) ; Get PAL flag
or a ; Is it set?
jr z, ++ ; Branch if not (NTSC)
ld a, (zPalDblUpdCounter) ; Get PAL double-update timeout counter
or a ; Is it zero?
jr nz, + ; Branch if not
ld a, 5 ; Set it back to 5...
ld (zPalDblUpdCounter), a ; ... and save it
jp - ; Go again
+
dec a ; Decrease PAL double-update timeout counter
ld (zPalDblUpdCounter), a ; Store it
+
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
bankswitch1 ; 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
-
ld b, 0 ; b = 0
djnz $ ; Loop in this instruction, decrementing b each iteration, until b = 0
dec c ; c--
jr z, - ; 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 7, (ix+zTrackVoiceControl) ; Is this a PSG track?
ret nz ; Is so, quit
bit 2, (ix+zTrackPlaybackControl) ; Is SFX overriding this track?
ret nz ; Return if yes
add a, (ix+zTrackVoiceControl) ; Add the channel bits to the register address
bit 2, (ix+zTrackVoiceControl) ; 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
; ---------------------------------------------------------------------------
; START OF FUNCTION CHUNK FOR zWriteFMIorII
;loc_CB
zWriteFMII_reduced:
sub 4 ; Strip 'bound to part II regs' bit
; END OF FUNCTION CHUNK FOR zWriteFMIorII
; =============== 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
; ---------------------------------------------------------------------------
; ===========================================================================
; DAC BANKS
; ===========================================================================
; Note: this table has a dummy first entry for the case when there is no DAC
; sample being played -- the code still results in a valid bank switch, and
; does not need to worry about special cases.
DAC_Banks:
; Set to zero to not use S3/S&K DAC samples:
db zmake68kBank(DacBank1)
if (use_s3_samples<>0)||(use_sk_samples<>0)||(use_s3d_samples<>0)
db zmake68kBank(DAC_81_Data) ,zmake68kBank(DAC_82_83_84_85_Data) ,zmake68kBank(DAC_82_83_84_85_Data) ,zmake68kBank(DAC_82_83_84_85_Data)
db zmake68kBank(DAC_82_83_84_85_Data) ,zmake68kBank(DAC_86_Data) ,zmake68kBank(DAC_87_Data) ,zmake68kBank(DAC_88_Data)
db zmake68kBank(DAC_89_Data) ,zmake68kBank(DAC_8A_8B_Data) ,zmake68kBank(DAC_8A_8B_Data) ,zmake68kBank(DAC_8C_Data)
db zmake68kBank(DAC_8D_8E_Data) ,zmake68kBank(DAC_8D_8E_Data) ,zmake68kBank(DAC_8F_Data) ,zmake68kBank(DAC_90_91_92_93_Data)
db zmake68kBank(DAC_90_91_92_93_Data) ,zmake68kBank(DAC_90_91_92_93_Data) ,zmake68kBank(DAC_90_91_92_93_Data) ,zmake68kBank(DAC_94_95_96_97_Data)
db zmake68kBank(DAC_94_95_96_97_Data) ,zmake68kBank(DAC_94_95_96_97_Data) ,zmake68kBank(DAC_94_95_96_97_Data) ,zmake68kBank(DAC_98_99_9A_Data)
db zmake68kBank(DAC_98_99_9A_Data) ,zmake68kBank(DAC_98_99_9A_Data) ,zmake68kBank(DAC_9B_Data) ,zmake68kBank(DAC_9C_Data)
db zmake68kBank(DAC_9D_Data) ,zmake68kBank(DAC_9E_Data)
endif
if (use_s3_samples<>0)||(use_sk_samples<>0)
db zmake68kBank(DAC_9F_Data) ,zmake68kBank(DAC_A0_Data) ,zmake68kBank(DAC_A1_Data) ,zmake68kBank(DAC_A2_Data)
db zmake68kBank(DAC_A3_Data) ,zmake68kBank(DAC_A4_Data) ,zmake68kBank(DAC_A5_Data) ,zmake68kBank(DAC_A6_Data)
db zmake68kBank(DAC_A7_Data) ,zmake68kBank(DAC_A8_Data) ,zmake68kBank(DAC_A9_Data) ,zmake68kBank(DAC_AA_Data)
db zmake68kBank(DAC_AB_Data) ,zmake68kBank(DAC_AC_Data) ,zmake68kBank(DAC_AD_AE_Data) ,zmake68kBank(DAC_AD_AE_Data)
db zmake68kBank(DAC_AF_B0_Data) ,zmake68kBank(DAC_AF_B0_Data) ,zmake68kBank(DAC_B1_Data) ,zmake68kBank(DAC_B2_B3_Data)
db zmake68kBank(DAC_B2_B3_Data) ,zmake68kBank(DAC_B4_C1_C2_C3_C4_Data),zmake68kBank(DAC_B5_Data) ,zmake68kBank(DAC_B6_Data)
db zmake68kBank(DAC_B7_Data) ,zmake68kBank(DAC_B8_B9_Data) ,zmake68kBank(DAC_B8_B9_Data) ,zmake68kBank(DAC_BA_Data)
db zmake68kBank(DAC_BB_Data) ,zmake68kBank(DAC_BC_Data) ,zmake68kBank(DAC_BD_Data) ,zmake68kBank(DAC_BE_Data)
db zmake68kBank(DAC_BF_Data) ,zmake68kBank(DAC_C0_Data) ,zmake68kBank(DAC_B4_C1_C2_C3_C4_Data),zmake68kBank(DAC_B4_C1_C2_C3_C4_Data)
db zmake68kBank(DAC_B4_C1_C2_C3_C4_Data),zmake68kBank(DAC_B4_C1_C2_C3_C4_Data)
endif
if (use_s2_samples<>0)
db zmake68kBank(DAC_C5_Data) ,zmake68kBank(DAC_C6_Data) ,zmake68kBank(DAC_C7_Data) ,zmake68kBank(DAC_C8_Data)
db zmake68kBank(DAC_C9_CC_CD_CE_CF_Data),zmake68kBank(DAC_CA_D0_D1_D2_Data) ,zmake68kBank(DAC_CB_D3_D4_D5_Data) ,zmake68kBank(DAC_C9_CC_CD_CE_CF_Data)
db zmake68kBank(DAC_C9_CC_CD_CE_CF_Data),zmake68kBank(DAC_C9_CC_CD_CE_CF_Data),zmake68kBank(DAC_C9_CC_CD_CE_CF_Data),zmake68kBank(DAC_CA_D0_D1_D2_Data)
db zmake68kBank(DAC_CA_D0_D1_D2_Data) ,zmake68kBank(DAC_CA_D0_D1_D2_Data) ,zmake68kBank(DAC_CB_D3_D4_D5_Data) ,zmake68kBank(DAC_CB_D3_D4_D5_Data)
db zmake68kBank(DAC_CB_D3_D4_D5_Data)
endif
if (use_s3d_samples<>0)
db zmake68kBank(DAC_D6_Data) ,zmake68kBank(DAC_D7_Data)
endif
if (use_s3_samples<>0)
db zmake68kBank(DAC_D8_D9_Data) ,zmake68kBank(DAC_D8_D9_Data)
endif
; =============== 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_1UP-1 ; Is it still 1-Up?
jr nz, zlocCheckFadeIn ; Branch if not
ld a, (zMusicNumber) ; Get next music to play
cp MusID_1UP ; Is it another 1-Up?
jr z, + ; Branch if yes
cp MusID__End-1 ; Is it music (except credits song)?
jr c, ++ ; Branch if not
+
xor a ; a = 0
ld (zMusicNumber), a ; Clear queue entry
+
xor a ; a = 0
ld (zSFXNumber0), a ; Clear first queued SFX
ld (zSFXNumber1), a ; Clear second queued SFX
jr +
;loc_149
zlocCheckFadeIn:
ld a, (zFadeToPrevFlag) ; Get fade-to-previous flag
cp 0FFh ; Is it 0FFh?
jr z, + ; 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, + ; Branch if not
call zFillSoundQueue ; Transfer M68K input
call zCycleSoundQueue ; Cycle queue and play first entry
call zCycleSoundQueue ; Cycle queue and play second entry
call zCycleSoundQueue ; Cycle queue and play third entry
+
ld a, (zSongBank) ; Get bank ID for music
bankswitch2 ; Bank switch to it
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, zTracksStart ; ix = track RAM
bit 7, (ix+zTrackPlaybackControl) ; Is FM6/DAC track playing?
call nz, zUpdateDACTrack ; Branch if yes
ld b, (zTracksEnd-zSongFM1)/zTrackSz ; Number of tracks
ld ix, zSongFM1 ; ix = FM1 track RAM
jr + ; 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
bankswitch2 ; Bank switch to SFX
ld ix, zTracksSFXStart ; ix = start of SFX track RAM
ld b, (zTracksSFXEnd-zTracksSFXStart)/zTrackSz ; Number of channels
/ push bc ; Save bc
bit 7, (ix+zTrackPlaybackControl) ; Is track playing?
call nz, zUpdateFMorPSGTrack ; Call routine if yes
ld de, zTrackSz ; Spacing between tracks
add ix, de ; Advance to next track
pop bc ; Restore bc
djnz - ; 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, + ; 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
; ---------------------------------------------------------------------------
+
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 7, (ix+zTrackVoiceControl) ; Is this a PSG channel?
jp nz, zUpdatePSGTrack ; Branch if yes
call zTrackRunTimer ; Run note timer
jr nz, + ; Branch if note hasn't expired yet
call zGetNextNote ; Get next note for FM track
bit 4, (ix+zTrackPlaybackControl) ; Is track resting?
ret nz ; Return if yes
call zPrepareModulation ; Initialize modulation
call zUpdateFreq ; Add frequency displacement to frequency
call zDoModulation ; Apply modulation
call zFMSendFreq ; Send frequancy to YM2612
jp zFMNoteOn ; Note on on all operators
; ---------------------------------------------------------------------------
+
bit 4, (ix+zTrackPlaybackControl) ; Is track resting?
ret nz ; Return if yes
call zDoFMFlutter ; Do FM flutter for track
ld a, (ix+zTrackNoteFillTimeout) ; Get note fill timeout
or a ; Has timeout expired?
jr z, + ; Branch if yes
dec (ix+zTrackNoteFillTimeout) ; Update note fill timeout
jp z, zKeyOffIfActive ; Send key off if needed
+
call zUpdateFreq ; Add frequency displacement to frequency
bit 6, (ix+zTrackPlaybackControl) ; Is 'sustain frequency' bit set?
ret nz ; Return if yes
call zDoModulation ; Apply modulation then fall through
; End of function zUpdateFMorPSGTrack
; =============== S U B R O U T I N E =======================================
; Uploads track's frequency to YM2612.
;
; Input: ix Pointer to track RAM
; hl Frequency to upload
; de For FM3 in special mode, pointer to extra FM3 frequency data (never correctly set)
; Output: a Trashed
; bc Trashed
; hl Trashed
; de Increased by 8
;
;sub_22B
zFMSendFreq:
bit 2, (ix+zTrackPlaybackControl) ; Is SFX overriding this track?
ret nz ; Return if yes
bit 0, (ix+zTrackPlaybackControl) ; Is track in special mode (FM3 only)?
jp nz, + ; Branch if yes
- ld a, 0A4h ; Command to update frequency MSB
ld c, h ; High byte of frequency
call zWriteFMIorII ; Send it to YM2612
ld a, 0A0h ; Command to update frequency LSB
ld c, l ; Low byte of frequency
call zWriteFMIorII ; Send it to YM2612
ret
; ---------------------------------------------------------------------------
+
ld a, (ix+zTrackVoiceControl) ; a = voice control byte
cp 2 ; Is this FM3?
jr nz, - ; Branch if not
if fix_sndbugs
call zGetSpecialFM3DataPointer ; de = pointer to saved FM3 frequency shifts
endif
ld b, zSpecialFreqCommands_End-zSpecialFreqCommands ; Number of entries
ld hl, zSpecialFreqCommands ; Lookup table
; DANGER! de is unset here, and could be pointing anywhere! Luckily,
; only reads are performed from it.
- push bc ; Save bc
ld a, (hl) ; a = register selector
inc hl ; Advance pointer
push hl ; Save hl
ex de, hl ; Exchange de and hl
ld c, (hl) ; Get byte from whatever the hell de was pointing to
inc hl ; Advance pointer
ld b, (hl) ; Get byte from whatever the hell de was pointing to
inc hl ; Advance pointer
ex de, hl ; Exchange de and hl
ld l, (ix+zTrackFreqLow) ; l = low byte of track frequency
ld h, (ix+zTrackFreqHigh) ; h = high byte of track frequency
add hl, bc ; hl = full frequency for operator
push af ; Save af
ld c, h ; High byte of frequency
call zWriteFMI ; Sent it to YM2612
pop af ; Restore af
sub 4 ; Move on to frequency LSB
ld c, l ; Low byte of frequency
call zWriteFMI ; Sent it to YM2612
pop hl ; Restore hl
pop bc ; Restore bc
djnz - ; Loop for all operators
ret
; End of function zFMSendFreq
; ---------------------------------------------------------------------------
;loc_272
zSpecialFreqCommands:
db 0ADh ; Operator 4 frequency MSB
db 0AEh ; Operator 3 frequency MSB
db 0ACh ; Operator 2 frequency MSB
db 0A6h ; Operator 1 frequency MSB
zSpecialFreqCommands_End
; =============== S U B R O U T I N E =======================================
;
if fix_sndbugs
zGetSpecialFM3DataPointer:
ld de,zSpecFM3Freqs ; de = pointer to saved FM3 frequency shifts
ld a, (zUpdatingSFX) ; Get flag
or a ; Is this a SFX track?
ret z ; Return if not
ld de,zSpecFM3FreqsSFX ; de = pointer to saved FM3 frequency shifts
endif
znullsub_A:
ret
; End of function nullsub_A
; =============== S U B R O U T I N E =======================================
; Gets next note from the track's data stream. If any coordination flags are
; found, they are handled and then the function keeps looping until a note is
; found.
;
; Input: ix Pointer to track's RAM
; Output: de Pointer to current position on track data
; hl Note frequency
; All others possibly trashed due to coordination flags
;
;sub_277
zGetNextNote:
ld e, (ix+zTrackDataPointerLow) ; e = low byte of track data pointer
ld d, (ix+zTrackDataPointerHigh) ; d = high byte of track data pointer
res 1, (ix+zTrackPlaybackControl) ; Clear 'do not attack next note' flag
res 4, (ix+zTrackPlaybackControl) ; Clear 'track is at rest' flag
;loc_285
zGetNextNote_cont:
ld a, (de) ; Get next byte from track
inc de ; Advance pointer
cp FirstCoordFlag ; Is it a coordination flag?
jp nc, zHandleFMorPSGCoordFlag ; Branch if yes
ex af, af' ; Save af
call zKeyOffIfActive ; Kill note
ex af, af' ; Restore af
bit 3, (ix+zTrackPlaybackControl) ; Is alternate SMPS mode flag set?
jp nz, zAlternateSMPS ; Branch if yes
or a ; Is this a duration?
jp p, zStoreDuration ; Branch if yes
sub 81h ; Make the note into a 0-based index
jp p, + ; Branch if it is a note and not a rest
call zKillTrack ; Put track at rest
jr zGetNoteDuration
; ---------------------------------------------------------------------------
+
add a, (ix+zTrackKeyOffset) ; Add in key displacement
ld hl, zPSGFrequencies ; PSG frequency lookup table
push af ; Save af
rst PointerTableOffset ; hl = frequency value for note
pop af ; Restore af
bit 7, (ix+zTrackVoiceControl) ; Is this a PSG track?
jr nz, zGotNoteFreq ; Branch if yes
push de ; Save de
ld d, 8 ; Each octave above the first adds this to frequency high bits
ld e, 0Ch ; 12 notes per octave
ex af, af' ; Exchange af with af'
xor a ; Clear a (which will clear a')
- ex af, af' ; Exchange af with af'
sub e ; Subtract 1 octave from the note
jr c, + ; If this is less than zero, we are done
ex af, af' ; Exchange af with af'
add a, d ; One octave up
jr - ; Loop
; ---------------------------------------------------------------------------
if fix_sndbugs=0
ex af, af' ; Exchange af with af' (dead code)
endif
; ---------------------------------------------------------------------------
+
add a, e ; Add 1 octave back (so note index is positive)
ld hl, zFMFrequencies ; FM first octave frequency lookup table
rst PointerTableOffset ; hl = frequency of the note on the first octave
ex af, af' ; Exchange af with af'
or h ; a = high bits of frequency (including octave bits, which were in a)
ld h, a ; h = high bits of frequency (including octave bits)
pop de ; Restore de
;loc_2CE
zGotNoteFreq:
ld (ix+zTrackFreqLow), l ; Store low byte of note frequency
ld (ix+zTrackFreqHigh), h ; Store high byte of note frequency
;loc_2D4
zGetNoteDuration:
ld a, (de) ; Get duration from the track
or a ; Is it an actual duration?
jp p, zGotNoteDuration ; Branch if yes
ld a, (ix+zTrackSavedDuration) ; Get saved duration
ld (ix+zTrackDurationTimeout), a ; Set it as next timeout duration
jr zFinishTrackUpdate
; ---------------------------------------------------------------------------
if fix_sndbugs=0
; Unused/dead code:
ld a, (de)
inc de
ld (ix+zTrackUnk11h),a
jr loc_306
endif
; ---------------------------------------------------------------------------
;loc_2E8
zAlternateSMPS:
; Setting bit 3 on zTrackPlaybackControl puts the song in a weird mode.
;
; This weird mode has literal frequencies and durations on the track.
; Each byte on the track is either a coordination flag (0E0h to 0FFh) or
; the high byte of a frequency. For the latter case, the following byte
; is then the low byte of this same frequency.
; If the frequency is nonzero, the (sign extended) key displacement is
; simply *added* to this frequency.
; After the frequency, there is then a byte that is unused.
; Finally, there is a raw duration byte following.
;
; To put the track in this mode, coord. flag 0FDh can be used; if the
; parameter byte is 1, the mode is toggled on. To turn the mode off,
; coord. flag 0FDh can be used with a parameter != 1.
ld h, a ; h = byte from track
ld a, (de) ; a = next byte from track
inc de ; Advance pointer
ld l, a ; l = last byte read from track
or h ; Is hl nonzero?
jr z, ++ ; Branch if not
ld a, (ix+zTrackKeyOffset) ; a = key displacement
ld b, 0 ; b = 0
or a ; Is a negative?
jp p, + ; Branch if not
dec b ; Set b to -1
+
ld c, a ; bc = sign extension of key displacement
add hl, bc ; hl += key displacement
+
ld (ix+zTrackFreqLow), l ; Store low byte of note frequency
ld (ix+zTrackFreqHigh), h ; Store high byte of note frequency
ld a, (de) ; Get another byte from the track
inc de ; Advance to next byte in track
ld (ix+zTrackUnk11h), a ; Store unknown byte to otherwise unused location
loc_306:
ld a, (de) ; Get raw duration from track
;loc_307
zGotNoteDuration:
inc de ; Advance to next byte in track
;loc_308
zStoreDuration:
call zComputeNoteDuration ; Multiply note by tempo divider
ld (ix+zTrackSavedDuration), a ; Store it for next note
;loc_30E
zFinishTrackUpdate:
ld (ix+zTrackDataPointerLow), e ; Save low byte of current location in song
ld (ix+zTrackDataPointerHigh), d ; Save high byte of current location in song
ld a, (ix+zTrackSavedDuration) ; Get current saved duration
ld (ix+zTrackDurationTimeout), a ; Set it as duration timeout
bit 1, (ix+zTrackPlaybackControl) ; Is 'do not attack next note' flag set?
ret nz ; Branch of yes
xor a ; Clear a
ld (ix+zTrackModulationSpeed), a ; Clear modulation speed
ld (ix+zTrackModulationValLow), a ; Clear low byte of accumulated modulation
ld (ix+zTrackVolFlutter), a ; Reset flutter
ld a, (ix+zTrackNoteFillMaster) ; Get master note fill
ld (ix+zTrackNoteFillTimeout), a ; Set note fill timeout
ret
; End of function zGetNextNote
; =============== S U B R O U T I N E =======================================
; This routine multiplies the note duration by the tempo divider. This can
; easily overflow, as the result is stored in a byte.
;
; Input: a Note duration
; Output: a Final note duration
; b zero
; c Damaged
;sub_330
zComputeNoteDuration:
ld b, (ix+zTrackTempoDivider) ; Get tempo divider for this track
dec b ; Make it into a loop conuter
ret z ; Return if it was 1
ld c, a ; c = a
- add a, c ; a += c
djnz - ; Loop
ret
; End of function zComputeNoteDuration
; =============== S U B R O U T I N E =======================================
; Reduces note duration timeout for current track.
;
; Input: ix Track data
; Output: a New duration
;sub_33A
zTrackRunTimer:
ld a, (ix+zTrackDurationTimeout) ; Get track duration timeout
dec a ; Decrement it...
ld (ix+zTrackDurationTimeout), a ; ... and save new value
ret
; End of function zTrackRunTimer
; ---------------------------------------------------------------------------
; START OF FUNCTION CHUNK FOR zUpdateFMorPSGTrack
;loc_342
zFMNoteOn:
ld a, (ix+zTrackFreqLow) ; Get low byte of note frequency
or (ix+zTrackFreqHigh) ; Is the note frequency zero?
ret z ; Return if yes
ld a, (ix+zTrackPlaybackControl) ; Get playback control byte for track
if fix_sndbugs
and 14h ; Is either bit 4 ("track at rest") or 2 ("SFX overriding this track") set?
else
and 6 ; Is either bit 1 ("do not attack next note") or 2 ("SFX overriding this track") set?
endif
ret nz ; Return if yes
ld a, (ix+zTrackVoiceControl) ; Get voice control byte from track
or 0F0h ; We want only the FM channel assignment bits
ld c, a ; Key on for all operators
ld a, 28h ; Select key on/of register
call zWriteFMI ; Send command to YM2612
ret
; END OF FUNCTION CHUNK FOR zUpdateFMorPSGTrack
; =============== S U B R O U T I N E =======================================
; Writes reg/data pair to register 28h (key on/off) if track active
;