-
Notifications
You must be signed in to change notification settings - Fork 3
/
ptplayer.asm
2961 lines (2537 loc) · 70.6 KB
/
ptplayer.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
;**************************************************
;* ----- Protracker V2.3B Playroutine ----- *
;**************************************************
;
; Version 5.1
; Written by Frank Wille in 2013, 2016, 2017, 2018.
;
; I, the copyright holder of this work, hereby release it into the
; public domain. This applies worldwide.
;
; The default version (single section, local base register) should
; work with most assemblers. Tested are: Devpac, vasm, PhxAss,
; Barfly-Asm, SNMA, AsmOne, AsmPro.
;
; The small data model can be enabled by defining the symbol SDATA. In
; this case all functions expect a4 to be initialised with the small
; data base register. Interrupt functions restore a4 from _LinkerDB.
; Small data may work with vasm and PhxAss only.
;
; Exported functions and variables:
; (CUSTOM is the custom-chip register set base address $dff000.)
;
; _mt_install_cia(a6=CUSTOM, a0=AutoVecBase, d0=PALflag.b)
; Install a CIA-B interrupt for calling _mt_music or mt_sfxonly.
; The music module is replayed via _mt_music when _mt_Enable is non-zero.
; Otherwise the interrupt handler calls mt_sfxonly to play sound
; effects only.
;
; _mt_remove_cia(a6=CUSTOM)
; Remove the CIA-B music interrupt and restore the old vector.
;
; _mt_init(a6=CUSTOM, a0=TrackerModule, a1=Samples|NULL, d0=InitialSongPos.b)
; Initialize a new module.
; Reset speed to 6, tempo to 125 and start at the given position.
; Master volume is at 64 (maximum).
; When a1 is NULL the samples are assumed to be stored after the patterns.
;
; _mt_end(a6=CUSTOM)
; Stop playing current module.
;
; _mt_soundfx(a6=CUSTOM, a0=SamplePointer,
; d0=SampleLength.w, d1=SamplePeriod.w, d2=SampleVolume.w)
; Request playing of an external sound effect on the most unused channel.
; This function is for compatibility with the old API only!
; You should call _mt_playfx instead.
;
; _mt_playfx(a6=CUSTOM, a0=SfxStructurePointer)
; Request playing of a prioritized external sound effect, either on a
; fixed channel or on the most unused one.
; Structure layout of SfxStructure:
; APTR sfx_ptr (pointer to sample start in Chip RAM, even address)
; WORD sfx_len (sample length in words)
; WORD sfx_per (hardware replay period for sample)
; WORD sfx_vol (volume 0..64, is unaffected by the song's master volume)
; BYTE sfx_cha (0..3 selected replay channel, -1 selects best channel)
; BYTE sfx_pri (unsigned priority, must be non-zero)
; When multiple samples are assigned to the same channel the lower
; priority sample will be replaced. When priorities are the same, then
; the older sample is replaced.
; The chosen channel is blocked for music until the effect has
; completely been replayed.
;
; _mt_musicmask(a6=CUSTOM, d0=ChannelMask.b)
; Set bits in the mask define which specific channels are reserved
; for music only. Set bit 0 for channel 0, ..., bit 3 for channel 3.
; When calling _mt_soundfx or _mt_playfx with automatic channel selection
; (sfx_cha=-1) then these masked channels will never be picked.
; The mask defaults to 0.
;
; _mt_mastervol(a6=CUSTOM, d0=MasterVolume.w)
; Set a master volume from 0 to 64 for all music channels.
; Note that the master volume does not affect the volume of external
; sound effects (which is desired).
;
; _mt_music(a6=CUSTOM)
; The replayer routine. Is called automatically after mt_install_cia().
;
; Variables:
;
; _mt_Enable
; Set this byte to non-zero to play music, zero to pause playing.
; Note that you can still play sound effects, while music is stopped.
;
; _mt_E8Trigger
; This byte reflects the value of the last E8 command.
; It is reset to 0 after mt_init().
;
; _mt_MusicChannels
; This byte defines the number of channels which should be dedicated
; for playing music. So sound effects will never use more
; than 4 - _mt_MusicChannels channels at once. Defaults to 0.
;
include "custom.i"
include "cia.i"
; Audio channel registers
AUDLC equ 0
AUDLEN equ 4
AUDPER equ 6
AUDVOL equ 8
; Sound effects structure, passed into _mt_playfx
rsreset
sfx_ptr rs.l 1
sfx_len rs.w 1
sfx_per rs.w 1
sfx_vol rs.w 1
sfx_cha rs.b 1
sfx_pri rs.b 1
sfx_sizeof rs.b 0
; Channel Status
rsreset
n_note rs.w 1
n_cmd rs.b 1
n_cmdlo rs.b 1
n_start rs.l 1
n_loopstart rs.l 1
n_length rs.w 1
n_replen rs.w 1
n_period rs.w 1
n_volume rs.w 1
n_pertab rs.l 1
n_dmabit rs.w 1
n_noteoff rs.w 1
n_toneportspeed rs.w 1
n_wantedperiod rs.w 1
n_pattpos rs.w 1
n_funk rs.w 1
n_wavestart rs.l 1
n_reallength rs.w 1
n_intbit rs.w 1
n_audreg rs.w 1
n_sfxlen rs.w 1
n_sfxptr rs.l 1
n_sfxper rs.w 1
n_sfxvol rs.w 1
n_looped rs.b 1
n_minusft rs.b 1
n_vibratoamp rs.b 1
n_vibratospd rs.b 1
n_vibratopos rs.b 1
n_vibratoctrl rs.b 1
n_tremoloamp rs.b 1
n_tremolospd rs.b 1
n_tremolopos rs.b 1
n_tremoloctrl rs.b 1
n_gliss rs.b 1
n_sampleoffset rs.b 1
n_loopcount rs.b 1
n_funkoffset rs.b 1
n_retrigcount rs.b 1
n_sfxpri rs.b 1
n_freecnt rs.b 1
n_musiconly rs.b 1
n_sizeof rs.b 0
ifd SDATA
xref _LinkerDB ; small data base from linker
near a4
code
endc
;---------------------------------------------------------------------------
xdef _mt_install_cia
_mt_install_cia:
; Install a CIA-B interrupt for calling _mt_music.
; a6 = CUSTOM
; a0 = AutoVecBase
; d0 = PALflag.b (0 is NTSC)
ifnd SDATA
move.l a4,-(sp)
lea mt_data(pc),a4
endc
clr.b mt_Enable(a4)
lea mt_Lev6Int(pc),a1
lea $78(a0),a0 ; Level 6 interrupt vector
move.l a0,(a1)
move.w #$2000,d1
and.w INTENAR(a6),d1
or.w #$8000,d1
move.w d1,mt_Lev6Ena(a4) ; remember level 6 interrupt enable
; disable level 6 EXTER interrupts, set player interrupt vector
move.w #$2000,INTENA(a6)
move.l (a0),mt_oldLev6(a4)
lea mt_TimerAInt(pc),a1
move.l a1,(a0)
; disable CIA-B interrupts, stop and save all timers
lea CIAB,a0
move.b #$7f,CIAICR(a0)
move.b #$10,CIACRA(a0)
move.b #$10,CIACRB(a0)
lea mt_oldtimers(a4),a1
move.b CIATALO(a0),(a1)+
move.b CIATAHI(a0),(a1)+
move.b CIATBLO(a0),(a1)+
move.b CIATBHI(a0),(a1)
; determine if 02 clock for timers is based on PAL or NTSC
tst.b d0
bne .1
move.l #1789773,d0 ; NTSC
bra .2
.1: move.l #1773447,d0 ; PAL
.2: move.l d0,mt_timerval(a4)
; load TimerA in continuous mode for the default tempo of 125
divu #125,d0
move.b d0,CIATALO(a0)
lsr.w #8,d0
move.b d0,CIATAHI(a0)
move.b #$11,CIACRA(a0) ; load timer, start continuous
; load TimerB with 496 ticks for setting DMA and repeat
move.b #496&255,CIATBLO(a0)
move.b #496>>8,CIATBHI(a0)
; TimerA and TimerB interrupt enable
move.b #$83,CIAICR(a0)
; enable level 6 interrupts
move.w #$a000,INTENA(a6)
bra mt_reset
mt_Lev6Int:
dc.l 0
;---------------------------------------------------------------------------
xdef _mt_remove_cia
_mt_remove_cia:
; Remove CIA-B music interrupt and restore the old vector.
; a6 = CUSTOM
ifnd SDATA
move.l a4,-(sp)
lea mt_data(pc),a4
endc
; disable level 6 and CIA-B interrupts
lea CIAB,a0
move.b #$7f,CIAICR(a0)
move.w #$2000,INTENA(a6)
; restore old timer values
lea mt_oldtimers(a4),a1
move.b (a1)+,CIATALO(a0)
move.b (a1)+,CIATAHI(a0)
move.b (a1)+,CIATBLO(a0)
move.b (a1),CIATBHI(a0)
move.b #$10,CIACRA(a0)
move.b #$10,CIACRB(a0)
; restore original level 6 interrupt vector
move.l mt_Lev6Int(pc),a1
move.l mt_oldLev6(a4),(a1)
; reenable CIA-B ALRM interrupt, which was set by AmigaOS
move.b #$84,CIAICR(a0)
; reenable previous level 6 interrupt
move.w mt_Lev6Ena(a4),INTENA(a6)
ifnd SDATA
move.l (sp)+,a4
endc
rts
;---------------------------------------------------------------------------
mt_TimerAInt:
; TimerA interrupt calls _mt_music at a selectable tempo (Fxx command),
; which defaults to 50 times per second.
movem.l d0-d7/a0-a6,-(sp)
lea CUSTOM,a6
ifd SDATA
lea _LinkerDB,a4
else
lea mt_data(pc),a4
endc
; clear EXTER interrupt flag
move.w #$2000,INTREQ(a6)
; check and clear CIAB interrupt flags
btst #0,CIAB+CIAICR
beq .2
; it was a TA interrupt, do music when enabled
tst.b mt_Enable(a4)
beq .1
bsr _mt_music ; music with sfx inserted
movem.l (sp)+,d0-d7/a0-a6
nop
rte
.1: bsr mt_sfxonly ; no music, only sfx
.2: movem.l (sp)+,d0-d7/a0-a6
nop
rte
;---------------------------------------------------------------------------
mt_TimerBdmaon:
; One-shot TimerB interrupt to enable audio DMA after 496 ticks.
; clear EXTER interrupt flag
move.w #$2000,CUSTOM+INTREQ
; check and clear CIAB interrupt flags
btst #1,CIAB+CIAICR
beq .1
; it was a TB interrupt, restart timer to set repeat, enable DMA
move.b #$19,CIAB+CIACRB
move.w mt_dmaon(pc),CUSTOM+DMACON
; set level 6 interrupt to mt_TimerBsetrep
move.l a0,-(sp)
pea mt_TimerBsetrep(pc)
move.l mt_Lev6Int(pc),a0
move.l (sp)+,(a0)
move.l (sp)+,a0
.1: nop
rte
mt_dmaon:
dc.w $8000
;---------------------------------------------------------------------------
mt_TimerBsetrep:
; One-shot TimerB interrupt to set repeat samples after another 496 ticks.
move.l a6,-(sp)
lea CUSTOM+INTREQ,a6
; check and clear CIAB interrupt flags
btst #1,CIAB+CIAICR
beq .1
; clear EXTER and possible audio interrupt flags
move.l a4,-(sp)
move.l d0,a4
moveq #$2000>>7,d0 ; EXTER-flag
or.b mt_dmaon+1(pc),d0
lsl.w #7,d0
move.w d0,(a6)
move.l a4,d0
; it was a TB interrupt, set repeat sample pointers and lengths
ifd SDATA
lea _LinkerDB,a4
else
lea mt_data(pc),a4
endc
move.l mt_chan1+n_loopstart(a4),AUD0LC-INTREQ(a6)
move.w mt_chan1+n_replen(a4),AUD0LEN-INTREQ(a6)
move.l mt_chan2+n_loopstart(a4),AUD1LC-INTREQ(a6)
move.w mt_chan2+n_replen(a4),AUD1LEN-INTREQ(a6)
move.l mt_chan3+n_loopstart(a4),AUD2LC-INTREQ(a6)
move.w mt_chan3+n_replen(a4),AUD2LEN-INTREQ(a6)
move.l mt_chan4+n_loopstart(a4),AUD3LC-INTREQ(a6)
move.w mt_chan4+n_replen(a4),AUD3LEN-INTREQ(a6)
; restore TimerA music interrupt vector
move.l mt_Lev6Int(pc),a4
lea mt_TimerAInt(pc),a6
move.l a6,(a4)
move.l (sp)+,a4
move.l (sp)+,a6
nop
rte
; just clear EXTER interrupt flag and return
.1: move.w #$2000,(a6)
move.w #$2000,(a6)
move.l (sp)+,a6
nop
rte
;---------------------------------------------------------------------------
xdef _mt_init
_mt_init:
; Initialize new module.
; Reset speed to 6, tempo to 125 and start at given song position.
; Master volume is at 64 (maximum).
; a6 = CUSTOM
; a0 = module pointer
; a1 = sample pointer (NULL means samples are stored within the module)
; d0 = initial song position
ifnd SDATA
move.l a4,-(sp)
lea mt_data(pc),a4
endc
move.l a0,mt_mod(a4)
movem.l d2/a2,-(sp)
; set initial song position
cmp.b 950(a0),d0
blo .1
moveq #0,d0
.1: move.b d0,mt_SongPos(a4)
move.l a1,d0 ; sample data location is given?
bne .4
; get number of highest pattern
lea 952(a0),a1 ; song arrangement list
moveq #127,d0
moveq #0,d2
.2: move.b (a1)+,d1
cmp.b d2,d1
bls .3
move.b d1,d2
.3: dbf d0,.2
addq.b #1,d2 ; number of patterns
; now we can calculate the base address of the sample data
moveq #10,d0
asl.l d0,d2
add.l #1084,d2
lea (a0,d2.l),a1
move.l a1,d2 ; use as pointer for empty samples
; save start address of each sample
.4: lea mt_SampleStarts(a4),a2
moveq #31-1,d0
.5: move.l a1,(a2)+
moveq #0,d1
move.w 42(a0),d1
beq .6
clr.w (a1) ; make sure sample starts with two 0-bytes
add.l d1,d1
add.l d1,a1
.6: lea 30(a0),a0
dbf d0,.5
movem.l (sp)+,d2/a2
; reset CIA timer A to default (125)
move.l mt_timerval(a4),d0
divu #125,d0
move.b d0,CIAB+CIATALO
lsr.w #8,d0
move.b d0,CIAB+CIATAHI
mt_reset:
; a4 must be initialised with base register
; reset speed and counters
move.b #6,mt_Speed(a4)
clr.b mt_Counter(a4)
clr.w mt_PatternPos(a4)
; disable the filter
or.b #2,CIAA+CIAPRA
; set master volume to 64
lea MasterVolTab64(pc),a0
move.l a0,mt_MasterVolTab(a4)
; initialise channel DMA, interrupt bits and audio register base
move.w #$0001,mt_chan1+n_dmabit(a4)
move.w #$0002,mt_chan2+n_dmabit(a4)
move.w #$0004,mt_chan3+n_dmabit(a4)
move.w #$0008,mt_chan4+n_dmabit(a4)
move.w #$0080,mt_chan1+n_intbit(a4)
move.w #$0100,mt_chan2+n_intbit(a4)
move.w #$0200,mt_chan3+n_intbit(a4)
move.w #$0400,mt_chan4+n_intbit(a4)
move.w #AUD0LC,mt_chan1+n_audreg(a4)
move.w #AUD1LC,mt_chan2+n_audreg(a4)
move.w #AUD2LC,mt_chan3+n_audreg(a4)
move.w #AUD3LC,mt_chan4+n_audreg(a4)
; make sure n_period doesn't start as 0
move.w #320,d0
move.w d0,mt_chan1+n_period(a4)
move.w d0,mt_chan2+n_period(a4)
move.w d0,mt_chan3+n_period(a4)
move.w d0,mt_chan4+n_period(a4)
; disable sound effects
clr.w mt_chan1+n_sfxlen(a4)
clr.w mt_chan2+n_sfxlen(a4)
clr.w mt_chan3+n_sfxlen(a4)
clr.w mt_chan4+n_sfxlen(a4)
clr.b mt_SilCntValid(a4)
clr.b mt_E8Trigger(a4)
ifnd SDATA
move.l (sp)+,a4
endc
;---------------------------------------------------------------------------
xdef _mt_end
_mt_end:
; Stop playing current module.
; a6 = CUSTOM
ifd SDATA
clr.b mt_Enable(a4)
else
lea mt_data+mt_Enable(pc),a0
clr.b (a0)
endc
moveq #0,d0
move.w d0,AUD0VOL(a6)
move.w d0,AUD1VOL(a6)
move.w d0,AUD2VOL(a6)
move.w d0,AUD3VOL(a6)
move.w #$000f,DMACON(a6)
rts
;---------------------------------------------------------------------------
xdef _mt_soundfx
_mt_soundfx:
; Request playing of an external sound effect on the most unused channel.
; This function is for compatibility with the old API only!
; You should call _mt_playfx instead!
; a6 = CUSTOM
; a0 = sample pointer
; d0.w = sample length in words
; d1.w = sample period
; d2.w = sample volume
lea -sfx_sizeof(sp),sp
move.l a0,a1
move.l sp,a0
move.l a1,sfx_ptr(a0)
movem.w d0-d2,sfx_len(a0)
move.w #$ff01,sfx_cha(a0) ; any channel, priority=1
bsr _mt_playfx
lea sfx_sizeof(sp),sp
rts
;---------------------------------------------------------------------------
xdef _mt_playfx
_mt_playfx:
; Request playing of a prioritized external sound effect, either on a
; fixed channel or on the most unused one.
; A negative channel specification means to use the best one.
; The priority is unsigned and should be greater than zero.
; This channel will be blocked for music until the effect has finished.
; a6 = CUSTOM
; a0 = sfx-structure pointer with the following layout:
; 0: ptr, 4: len.w, 6: period.w, 8: vol.w, 10: channel.b, 11: priority.b
ifd SDATA
movem.l d2-d7/a0-a3/a5,-(sp)
else
movem.l d2-d7/a0-a5,-(sp)
lea mt_data(pc),a4
endc
moveq #0,d0
move.b sfx_cha(a0),d0
bpl channelsfx ; use fixed channel for effect
; Did we already calculate the n_freecnt values for all channels?
tst.b mt_SilCntValid(a4)
bne freecnt_valid
; Look at the next 8 pattern steps to find the longest sequence
; of silence (no new note or instrument).
moveq #8,d2
move.l #$fffff000,d3 ; mask to ignore effects
; remember which channels are not available for sound effects
move.b mt_chan1+n_musiconly(a4),d4
move.b mt_chan2+n_musiconly(a4),d5
move.b mt_chan3+n_musiconly(a4),d6
move.b mt_chan4+n_musiconly(a4),d7
; reset freecnts for all channels
moveq #0,d0
move.b d0,mt_chan1+n_freecnt(a4)
move.b d0,mt_chan2+n_freecnt(a4)
move.b d0,mt_chan3+n_freecnt(a4)
move.b d0,mt_chan4+n_freecnt(a4)
; get pattern pointer
move.l mt_mod(a4),a3 ; a3 mod pointer
lea 952(a3),a5 ; a5 song arrangement list
move.w mt_PatternPos(a4),d1
move.b mt_SongPos(a4),d0
.1: move.b (a5,d0.w),d0
swap d0
lea 1084(a3),a1
lsr.l #6,d0
add.l d0,a1
lea 1024(a1),a2 ; a2 end of pattern
add.w d1,a1 ; a1 current pattern pos
.2: moveq #4,d0
move.l (a1)+,d1
tst.b d4
bne .3
addq.b #1,mt_chan1+n_freecnt(a4)
and.l d3,d1
sne d4
.3: add.b d4,d0
move.l (a1)+,d1
tst.b d5
bne .4
addq.b #1,mt_chan2+n_freecnt(a4)
and.l d3,d1
sne d5
.4: add.b d5,d0
move.l (a1)+,d1
tst.b d6
bne .5
addq.b #1,mt_chan3+n_freecnt(a4)
and.l d3,d1
sne d6
.5: add.b d6,d0
move.l (a1)+,d1
tst.b d7
bne .6
addq.b #1,mt_chan4+n_freecnt(a4)
and.l d3,d1
sne d7
.6: add.b d7,d0
; break the loop when no channel has any more free pattern steps
beq .7
; otherwise break after 8 pattern steps
subq.w #1,d2
beq .7
; End of pattern reached? Then load next pattern pointer.
cmp.l a2,a1
blo .2
moveq #0,d1
moveq #1,d0
add.b mt_SongPos(a4),d0
and.w #$007f,d0
cmp.b 950(a3),d0 ; end of song reached?
blo .1
moveq #0,d0
bra .1
.7: st mt_SilCntValid(a4)
freecnt_valid:
move.w #$4000,INTENA(a6)
sub.l a2,a2
move.b sfx_pri(a0),d2
; Determine which channels are already allocated for sound
; effects and check if the limit was reached. In this case only
; replace sound effect channels by higher priority.
moveq #3,d0
sub.b mt_MusicChannels(a4),d0
move.b mt_chan1+n_sfxpri(a4),d4
sne d1
add.b d1,d0
move.b mt_chan2+n_sfxpri(a4),d5
sne d1
add.b d1,d0
move.b mt_chan3+n_sfxpri(a4),d6
sne d1
add.b d1,d0
move.b mt_chan4+n_sfxpri(a4),d7
sne d1
add.b d1,d0
bmi .overwrite
; We will prefer a music channel which had an audio interrupt,
; because that means the last instrument sample has been played
; completely, and the channel is now in an idle loop.
; Also exclude channels which have set a repeat loop.
; Try not to break them!
moveq #0,d1
tst.b mt_chan1+n_looped(a4)
bne .1
or.w #$0080,d1
.1: tst.b mt_chan2+n_looped(a4)
bne .2
or.w #$0100,d1
.2: tst.b mt_chan3+n_looped(a4)
bne .3
or.w #$0200,d1
.3: tst.b mt_chan4+n_looped(a4)
bne .4
or.w #$0400,d1
.4: and.w INTREQR(a6),d1
bne .6
; All channels are busy, then it doesn't matter which one we break...
.5: move.w #$0780,d1
; first look for the best unused channel
.6: moveq #0,d3
btst #7,d1
seq d0
or.b d4,d0
bne .7
lea mt_chan1+n_freecnt(a4),a1
cmp.b (a1),d3
bhi .7
move.l a1,a2
move.b (a1),d3
.7: btst #8,d1
seq d0
or.b d5,d0
bne .8
lea mt_chan2+n_freecnt(a4),a1
cmp.b (a1),d3
bhi .8
move.l a1,a2
move.b (a1),d3
.8: btst #9,d1
seq d0
or.b d6,d0
bne .9
lea mt_chan3+n_freecnt(a4),a1
cmp.b (a1),d3
bhi .9
move.l a1,a2
move.b (a1),d3
.9: btst #10,d1
seq d0
or.b d7,d0
bne .10
lea mt_chan4+n_freecnt(a4),a1
cmp.b (a1),d3
bhi .10
move.l a1,a2
bra found_sfx_ch
.10: move.l a2,d3
bne found_sfx_ch
.overwrite:
; finally try to overwrite a sound effect with lower/equal priority
moveq #0,d3
tst.b d4
beq .11
cmp.b d4,d2
blo .11
lea mt_chan1+n_freecnt(a4),a1
cmp.b (a1),d3
bhi .11
move.l a1,a2
move.b (a1),d3
.11: tst.b d5
beq .12
cmp.b d5,d2
blo .12
lea mt_chan2+n_freecnt(a4),a1
cmp.b (a1),d3
bhi .12
move.l a1,a2
move.b (a1),d3
.12: tst.b d6
beq .13
cmp.b d6,d2
blo .13
lea mt_chan3+n_freecnt(a4),a1
cmp.b (a1),d3
bhi .13
move.l a1,a2
move.b (a1),d3
.13: tst.b d7
beq .14
cmp.b d7,d2
blo .14
lea mt_chan4+n_freecnt(a4),a1
cmp.b (a1),d3
bhi .14
move.l a1,a2
.14: move.l a2,d3
beq exit_playfx ; ignore new sfx due to low priority
found_sfx_ch:
lea -n_freecnt(a2),a2
bra set_sfx
channel_offsets:
dc.w 0*n_sizeof,1*n_sizeof,2*n_sizeof,3*n_sizeof
channelsfx:
; a0 = sfx structure
; d0 = fixed channel for new sound effect
add.w d0,d0
lea mt_chan1(a4),a2
add.w channel_offsets(pc,d0.w),a2
; priority high enough to replace a present effect on this channel?
move.w #$4000,INTENA(a6)
move.b sfx_pri(a0),d2
cmp.b n_sfxpri(a2),d2
blo exit_playfx
set_sfx:
; activate the sound effect on this channel
; a0 = sfx structure
; d2 = sfx priority
; a2 = channel status
move.l (a0)+,n_sfxptr(a2) ; sfx_ptr
move.w (a0)+,n_sfxlen(a2) ; sfx_len
move.w (a0)+,n_sfxper(a2) ; sfx_per
move.w (a0),n_sfxvol(a2) ; sfx_vol
move.b d2,n_sfxpri(a2)
exit_playfx:
move.w #$c000,INTENA(a6)
ifd SDATA
movem.l (sp)+,d2-d7/a0-a3/a5
else
movem.l (sp)+,d2-d7/a0-a5
endc
rts
;---------------------------------------------------------------------------
xdef _mt_musicmask
_mt_musicmask:
; Set bits in the mask define which specific channels are reserved
; for music only.
; a6 = CUSTOM
; d0.b = channel-mask (bit 0 for channel 0, ..., bit 3 for channel 3)
ifnd SDATA
move.l a4,-(sp)
lea mt_data(pc),a4
endc
move.w #$4000,INTENA(a6)
lsl.b #5,d0
scs mt_chan4+n_musiconly(a4)
add.b d0,d0
scs mt_chan3+n_musiconly(a4)
add.b d0,d0
scs mt_chan2+n_musiconly(a4)
add.b d0,d0
scs mt_chan1+n_musiconly(a4)
move.w #$c000,INTENA(a6)
ifnd SDATA
move.l (sp)+,a4
endc
rts
;---------------------------------------------------------------------------
xdef _mt_mastervol
_mt_mastervol:
; Set a master volume from 0 to 64 for all music channels.
; Note that the master volume does not affect the volume of external
; sound effects (which is desired).
; a6 = CUSTOM
; d0.w = master volume
; stingray, since each volume table has a size of 65 bytes
; we simply multiply (optimised of course) by 65 to get the
; offset to the correct table
lea MasterVolTab0(pc),a0
add.w d0,a0
lsl.w #6,d0
add.w d0,a0
move.w #$4000,INTENA(a6)
ifd SDATA
move.l a0,mt_MasterVolTab(a4)
else
lea mt_data+mt_MasterVolTab(pc),a1
move.l a0,(a1)
endc
move.w #$c000,INTENA(a6)
rts
;---------------------------------------------------------------------------
xdef _mt_music
_mt_music:
; Called from interrupt.
; Play next position when Counter equals Speed.
; Effects are always handled.
; a6 = CUSTOM
moveq #0,d7 ; d7 is always zero
lea mt_dmaon+1(pc),a0
move.b d7,(a0)
addq.b #1,mt_Counter(a4)
move.b mt_Counter(a4),d0
cmp.b mt_Speed(a4),d0
blo no_new_note
; handle a new note
move.b d7,mt_Counter(a4)
tst.b mt_PattDelTime2(a4)
beq get_new_note
; we have a pattern delay, check effects then step
lea AUD0LC(a6),a5
lea mt_chan1(a4),a2
bsr mt_checkfx
lea AUD1LC(a6),a5
lea mt_chan2(a4),a2
bsr mt_checkfx
lea AUD2LC(a6),a5
lea mt_chan3(a4),a2
bsr mt_checkfx
lea AUD3LC(a6),a5
lea mt_chan4(a4),a2
bsr mt_checkfx
bra settb_step
no_new_note:
; no new note, just check effects, don't step to next position
lea AUD0LC(a6),a5
lea mt_chan1(a4),a2
bsr mt_checkfx
lea AUD1LC(a6),a5
lea mt_chan2(a4),a2
bsr mt_checkfx
lea AUD2LC(a6),a5
lea mt_chan3(a4),a2
bsr mt_checkfx
lea AUD3LC(a6),a5
lea mt_chan4(a4),a2
bsr mt_checkfx
; set one-shot TimerB interrupt for enabling DMA, when needed
move.b mt_dmaon+1(pc),d0
beq same_pattern
move.l mt_Lev6Int(pc),a0
lea mt_TimerBdmaon(pc),a1
move.l a1,(a0)
move.b #$19,CIAB+CIACRB ; load/start timer B, one-shot
bra same_pattern
get_new_note:
; determine pointer to current pattern line
move.l mt_mod(a4),a0
lea 12(a0),a3 ; sample info table
lea 1084(a0),a1 ; pattern data
lea 952(a0),a0
moveq #0,d0
move.b mt_SongPos(a4),d0
move.b (a0,d0.w),d0 ; current pattern number
swap d0
lsr.l #6,d0
add.l d0,a1 ; pattern base
add.w mt_PatternPos(a4),a1 ; a1 pattern line
; play new note for each channel, apply some effects
lea AUD0LC(a6),a5
lea mt_chan1(a4),a2
bsr mt_playvoice
lea AUD1LC(a6),a5
lea mt_chan2(a4),a2
bsr mt_playvoice
lea AUD2LC(a6),a5
lea mt_chan3(a4),a2