-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathptplayer.asm
3434 lines (2974 loc) · 82.3 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 6.3
; Written by Frank Wille in 2013, 2016-2023.
;
; 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 initialized 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=VectorBase, 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. VectorBase is 0 for 68000, otherwise set it to the CPU's
; VBR register. A non-zero PALflag selects PAL-clock for the CIA timers
; (NTSC otherwise).
;
; _mt_remove_cia(a6=CUSTOM)
; Remove the CIA-B music interrupt, restore the previous handler and
; reset the CIA timer registers to their original values.
;
; _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 song 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 and sound effects.
;
; _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 may prefer _mt_playfx instead. MINIMAL=0 only.
;
; channelStatus(d0) = _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:
; void *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 (priority, must be in the range 1..127)
; 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.
; Returns a pointer to a channel-status structure when the sample
; is scheduled for playing, and NULL when the request was ignored.
; MINIMAL=0 only.
;
; _mt_loopfx(a6=CUSTOM, a0=SfxStructurePointer)
; Request playing of a looped sound effect on a fixed channel, which
; will be blocked for music until the effect is stopped (_mt_stopfx).
; It uses the same sfx-structure as _mt_playfx, but the priority is
; ignored. A looped sound effect has always highest priority and will
; replace a previous effect on the same channel. No automatic channel
; selection possible!
; Also make sure the sample starts with a zero-word, which is used
; for idling when the effect is stopped. This word is included in the
; total length calculation, but excluded when actually playing the loop.
; MINIMAL=0 only.
;
; _mt_stopfx(a6=CUSTOM, d0=Channel.b)
; Immediately stop a currently playing sound effect on a channel (0..3)
; and make it available for music, or other effects, again. This is the
; only way to stop a looped sound effect (_mt_loopfx) besides stopping
; replay completely (_mt_end). MINIMAL=0 only.
;
; _mt_musicmask(a6=CUSTOM, d0=ChannelMask.b)
; Bits set in the ChannelMask 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. MINIMAL=0 only.
;
; _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). MINIMAL=0 only.
;
; _mt_samplevol(d0=SampleNumber.w, d1=Volume.b)
; Redefine a sample's volume. May also be done while the song is playing.
; Warning: Does not check arguments for valid range! You must have done
; _mt_init before calling this function!
; The new volume is persistent. Even when the song is restarted.
; MINIMAL=0 only.
;
; _mt_channelmask(a6=CUSTOM, d0=ChannelMask.b)
; Bits cleared in the ChannelMask define which specific channels are muted
; for music replay. Clear bit 0 for channel 0, ..., bit 3 for channel 3.
; Additionally a cleared bit prevents any access to the sample pointers
; of this channel.
; The mask defaults to all channels unmuted (bits set) and is reset to
; this state on _mt_init and _mt_end. MINIMAL=0 only.
;
; _mt_music(a6=CUSTOM)
; The replayer routine. Can be called from your own VBlank interrupt
; handler when VBLANK_MUSIC is set. Is otherwise called automatically
; by Timer-A interrupts after _mt_install_cia.
;
; Byte 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.
; It is set to 0 by _mt_install_cia. VBLANK_MUSIC=0 only.
;
; _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.
; MINIMAL=0 only.
;
; Optionally you can build a minimal version, which includes only
; the basic player. No sound effects insertion, no master volume, no
; sample volume, etc. Define the symbol MINIMAL=1 for it.
ifnd MINIMAL
MINIMAL equ 0
endc
; You may disable sawtooth and rectangle vibratos/tremolos here, which
; will be replaced by sine-waves. They are rarely used and disabling
; them will free a lot of memory for the tables.
ifnd ENABLE_SAWRECT
ENABLE_SAWRECT equ 1
endc
; Set this if you can guarantee that the word at $0 is cleared and if
; you want to use it for idle-looping of samples.
ifnd NULL_IS_CLEARED
NULL_IS_CLEARED equ 0
endc
; This disables initialization of CIA Timer-A interrupts for music
; replay, which means you cannot set the tempo with the F-command
; anymore and you have to call _mt_music yourself out of your own
; VBlank interrupt handler. Also sound effects will no longer work,
; when no music is playing (_mt_Enable=0).
ifnd VBLANK_MUSIC
VBLANK_MUSIC equ 0
endc
; Delay in CIA-ticks, which guarantees that at least one Audio-DMA
; took place, even with the lowest periods.
; 496 should be the correct value. But there are some A1200 which
; need at least 550.
DMADELAY equ 576 ; was 496
; Custom chip registers
CUSTOM equ $dff000
INTREQR equ $01e
INTENAR equ $01c
DMACON equ $096
INTENA equ $09a
INTREQ equ $09c
AUD0LC equ $0a0
AUD0LEN equ $0a4
AUD0VOL equ $0a8
AUD1LC equ $0b0
AUD1LEN equ $0b4
AUD1VOL equ $0b8
AUD2LC equ $0c0
AUD2LEN equ $0c4
AUD2VOL equ $0c8
AUD3LC equ $0d0
AUD3LEN equ $0d4
AUD3VOL equ $0d8
; Audio channel registers
AUDLC equ 0
AUDLEN equ 4
AUDPER equ 6
AUDVOL equ 8
; CIA registers
CIAA equ $bfe001
CIAB equ $bfd000
CIAPRA equ $000
CIATALO equ $400
CIATAHI equ $500
CIATBLO equ $600
CIATBHI equ $700
CIAICR equ $d00
CIACRA equ $e00
CIACRB equ $f00
; 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_index rs.b 1
n_sfxpri rs.b 1
n_reserved1 rs.b 2
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_sfxptr rs.l 1
n_sfxlen rs.w 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
ifeq MINIMAL
n_freecnt rs.b 1
n_musiconly rs.b 1
n_enable rs.b 1
else
n_reserved2 rs.b 3
endc
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 = VectorBase
; d0 = PALflag.b (0 is NTSC)
ifnd SDATA
move.l a4,-(sp)
lea mt_data(pc),a4
endc
clr.b mt_Enable(a4)
; remember level 6 vector and interrupt enable
lea $78(a0),a0
move.l a0,mt_Lev6Int(a4)
move.w #$2000,d1
and.w INTENAR(a6),d1
or.w #$8000,d1
move.w d1,mt_Lev6Ena(a4)
; 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)
; reset TimerB toggle
lea TB_toggle(pc),a0
clr.b (a0)
; disable CIA-B interrupts, stop and save all timers
lea CIAB,a0
moveq #0,d1
move.b #$7f,CIAICR(a0)
move.b d1,CIACRA(a0)
move.b d1,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)
ifeq VBLANK_MUSIC
; 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
endc ; !VBLANK_MUSIC
; load TimerB with DMADELAY ticks for setting DMA and repeat
move.b #DMADELAY&255,CIATBLO(a0)
move.b #DMADELAY>>8,CIATBHI(a0)
; Ack. pending interrupts, TimerA and TimerB interrupt enable
tst.b CIAICR(a0)
move.w #$2000,INTREQ(a6)
ifne VBLANK_MUSIC
move.b #$82,CIAICR(a0) ; TimerB only for VBLANK_MUSIC
else
move.b #$83,CIAICR(a0)
endc
; enable level 6 interrupts
move.w #$a000,INTENA(a6)
bra mt_reset
;---------------------------------------------------------------------------
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.w #$2000,d0
move.b #$7f,CIAICR(a0)
move.w d0,INTENA(a6)
tst.b CIAICR(a0)
move.w d0,INTREQ(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(a4),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.
; check for TB interrupt and clear CIAB interrupt flags
btst #1,CIAB+CIAICR
bne mt_TimerBInt
ifne VBLANK_MUSIC
move.w #$2000,CUSTOM+INTREQ ; ack interrupt and leave
nop
rte
else
; Now it should be a TA interrupt.
; Other level 6 interrupt sources have to be handled elsewhere.
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)
; do music when enabled
tst.b mt_Enable(a4)
ifeq MINIMAL
beq .2
else
beq .1
endc
bsr mt_music ; music with sfx inserted
.1: movem.l (sp)+,d0-d7/a0-a6
nop
rte
ifeq MINIMAL
.2: bsr mt_sfxonly ; no music, only sfx
movem.l (sp)+,d0-d7/a0-a6
nop
rte
endc
endc ; !VBLANK_MUSIC
;---------------------------------------------------------------------------
mt_TimerBInt:
; Handle one-shot TimerB interrupt.
; TB_toggle-technique suggested by Ross/EAB.
move.l a0,-(sp)
lea TB_toggle(pc),a0
not.b (a0)
lea CUSTOM+INTREQ,a0
beq mt_TimerBsetrep
; restart timer for repeat, enable audio DMA after DMADELAY ticks
move.w #$2000,(a0) ; clear EXTER interrupt flag
move.b #$19,CIAB+CIACRB
move.w mt_dmaon(pc),DMACON-INTREQ(a0)
move.l (sp)+,a0
nop
rte
mt_dmaon:
dc.w $8000
TB_toggle:
dc.b 0
even
;---------------------------------------------------------------------------
mt_TimerBsetrep:
; Oneshot TimerB interrupt to set repeat samples after another DMADELAY ticks.
; a0 = INTREQ
; 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,(a0)
move.l a4,d0
; set repeat sample pointers and lengths
ifd SDATA
lea _LinkerDB,a4
else
lea mt_data(pc),a4
endc
ifne MINIMAL
move.l mt_chan1+n_loopstart(a4),AUD0LC-INTREQ(a0)
move.w mt_chan1+n_replen(a4),AUD0LEN-INTREQ(a0)
move.l mt_chan2+n_loopstart(a4),AUD1LC-INTREQ(a0)
move.w mt_chan2+n_replen(a4),AUD1LEN-INTREQ(a0)
move.l mt_chan3+n_loopstart(a4),AUD2LC-INTREQ(a0)
move.w mt_chan3+n_replen(a4),AUD2LEN-INTREQ(a0)
move.l mt_chan4+n_loopstart(a4),AUD3LC-INTREQ(a0)
move.w mt_chan4+n_replen(a4),AUD3LEN-INTREQ(a0)
else ; !MINIMAL
tst.b mt_chan1+n_enable(a4)
beq .1
move.l mt_chan1+n_loopstart(a4),AUD0LC-INTREQ(a0)
move.w mt_chan1+n_replen(a4),AUD0LEN-INTREQ(a0)
.1: tst.b mt_chan2+n_enable(a4)
beq .2
move.l mt_chan2+n_loopstart(a4),AUD1LC-INTREQ(a0)
move.w mt_chan2+n_replen(a4),AUD1LEN-INTREQ(a0)
.2: tst.b mt_chan3+n_enable(a4)
beq .3
move.l mt_chan3+n_loopstart(a4),AUD2LC-INTREQ(a0)
move.w mt_chan3+n_replen(a4),AUD2LEN-INTREQ(a0)
.3: tst.b mt_chan4+n_enable(a4)
beq .4
move.l mt_chan4+n_loopstart(a4),AUD3LC-INTREQ(a0)
move.w mt_chan4+n_replen(a4),AUD3LEN-INTREQ(a0)
.4:
endc
move.l (sp)+,a4
move.l (sp)+,a0
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
lea (a0,d2.l),a1
add.w #1084,a1
; save start address of each sample and do some fixes for broken mods
.4: lea mt_SampleStarts(a4),a2
moveq #1,d2
moveq #31-1,d0
.5: move.l a1,(a2)+
moveq #0,d1
move.w 42(a0),d1
cmp.w d2,d1 ; length 0 and 1 define unused samples
bls .6
add.l d1,d1
add.l d1,a1
bra .7
.6: clr.w 42(a0) ; length 1 means zero -> no sample
.7: lea 30(a0),a0
dbf d0,.5
movem.l (sp)+,d2/a2
ifeq VBLANK_MUSIC
; 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
endc
mt_reset:
; a4 must be initialized with base register
; reset speed and counters
move.b #6,mt_Speed(a4)
clr.b mt_Counter(a4)
clr.w mt_PatternPos(a4)
clr.b mt_PattDelTime(a4)
clr.b mt_PattDelTime2(a4)
clr.w mt_PBreakPos(a4)
clr.b mt_PBreakFlag(a4)
clr.b mt_PosJumpFlag(a4)
; disable the filter
or.b #2,CIAA+CIAPRA
ifeq MINIMAL
; set master volume to 64
lea MasterVolTab64(pc),a0
move.l a0,mt_MasterVolTab(a4)
endc
; set channel index
clr.b mt_chan1+n_index(a4)
move.b #1,mt_chan2+n_index(a4)
move.b #2,mt_chan3+n_index(a4)
move.b #3,mt_chan4+n_index(a4)
; initialize channel DMA and interrupt bits
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)
clr.b mt_E8Trigger(a4)
ifeq MINIMAL
clr.b mt_SongEnd(a4)
clr.b mt_SilCntValid(a4)
endc
ifnd SDATA
move.l (sp)+,a4
endc
;---------------------------------------------------------------------------
xdef _mt_end
_mt_end:
; Stop playing current module.
; a6 = CUSTOM
move.w #$4000,INTENA(a6)
ifd SDATA
clr.b mt_Enable(a4)
lea mt_chan1(a4),a0
bsr resetch
lea mt_chan2(a4),a0
bsr resetch
lea mt_chan3(a4),a0
bsr resetch
lea mt_chan4(a4),a0
bsr resetch
else
lea mt_data(pc),a1
clr.b mt_Enable(a1)
lea mt_chan1(a1),a0
bsr resetch
lea mt_chan2(a1),a0
bsr resetch
lea mt_chan3(a1),a0
bsr resetch
lea mt_chan4(a1),a0
bsr resetch
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)
move.w #$c000,INTENA(a6)
rts
resetch:
; a0 = channel status
; All registers are preserved!
move.w #320,n_period(a0) ; make sure period is not illegal
clr.w n_volume(a0)
clr.w n_sfxlen(a0)
clr.w n_funk(a0)
clr.b n_sfxpri(a0)
clr.b n_looped(a0)
clr.b n_gliss(a0)
ifeq MINIMAL
clr.b n_musiconly(a0)
st n_enable(a0)
endc
rts
ifeq MINIMAL
;---------------------------------------------------------------------------
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,sfx_ptr(sp)
movem.w d0-d2,sfx_len(sp)
move.w #$ff01,sfx_cha(sp) ; any channel, priority=1
move.l sp,a0
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
; -> d0 = pointer to channel status or NULL when sample was ignored
ifd SDATA
movem.l d2-d7/a0-a3/a5,-(sp)
else
movem.l d2-d7/a0-a5,-(sp)
lea mt_data(pc),a4
endc
move.w #$4000,INTENA(a6)
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
; 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)
moveq #-1,d4
moveq #-1,d5
moveq #-1,d6
moveq #-1,d7
; 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 #0,d0
move.l (a1)+,d1
and.l d3,d1
sne d1
and.b d1,d4
sub.b d4,mt_chan1+n_freecnt(a4)
add.b d4,d0
move.l (a1)+,d1
and.l d3,d1
sne d1
and.b d1,d5
sub.b d5,mt_chan2+n_freecnt(a4)
add.b d5,d0
move.l (a1)+,d1
and.l d3,d1
sne d1
and.b d1,d6
sub.b d6,mt_chan3+n_freecnt(a4)
add.b d6,d0
move.l (a1)+,d1
and.l d3,d1
sne d1
and.b d1,d7
sub.b d7,mt_chan4+n_freecnt(a4)
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:
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
or.b mt_chan1+n_musiconly(a4),d4
sne d1
add.b d1,d0
move.b mt_chan2+n_sfxpri(a4),d5
or.b mt_chan2+n_musiconly(a4),d5
sne d1
add.b d1,d0
move.b mt_chan3+n_sfxpri(a4),d6
or.b mt_chan3+n_musiconly(a4),d6
sne d1
add.b d1,d0
move.b mt_chan4+n_sfxpri(a4),d7
or.b mt_chan4+n_musiconly(a4),d7
sne d1
add.b d1,d0
bmi .overwrite ; all channels reserved/playing effects
; 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,d3
tst.b mt_chan1+n_looped(a4)
bne .1
or.w #$0080,d3
.1: tst.b mt_chan2+n_looped(a4)
bne .2
or.w #$0100,d3
.2: tst.b mt_chan3+n_looped(a4)
bne .3
or.w #$0200,d3
.3: tst.b mt_chan4+n_looped(a4)
bne .4
or.w #$0400,d3
.4: move.w INTREQR(a6),d1
and.w d3,d1
bne .6
; All channels are busy. Then break the non-looped ones first...
move.w d3,d1
bne .6
; ..except there are none. Then it doesn't matter. :|
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
channelsfx:
; a0 = sfx structure
; d0 = fixed channel for new sound effect
add.w d0,d0