-
Notifications
You must be signed in to change notification settings - Fork 1
/
PLAYER.ASM
2264 lines (1618 loc) · 45.7 KB
/
PLAYER.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
; TND Player Code Version 1.1
; by Jan Knipperts Dec,Jan 20/21
;
; This code plays sound files for the TI SN76496A sound generator chip created with my TNDY tracker.
;
; Main player functions:
;
; TND_Init_Player
; Initialises the TND player and sets all variables.
; ES:00 must point to the song data and CS:IOPort must indicate the port to be used to address the sound chip.
;
; TND_Start
; Starts the playback
;
; TND_Stop
; Stops the playback
;
;
; Usage:
;
; Include this code in your programme. Load a TND song at ES:00 and call TND_Init_Player.
; Now everything is ready to play. TND_Start starts the playback of the song, TND_Stop stops it.
;
; You can read the variables CS:Pos, CS:Pattern, CS:Row , CS:Songlength etc. in your programme to get the status of the player.
;
; If you want to start playback from a certain point within the song, set the variables CS:Pos and CS:Row accordingly after
; calling TND_Init_Player and then call TND_Update_Pattern. Then you can start the song at the desired position with TND_Start.
;
; Have a look at the example files TNDTEST.ASM and TNDPLAY.ASM if something is still unclear.
locals
jumps
; ****************************************************************
; This routine initialises the player.
; IN:
; ES:00 - points to tune in memory
;
; OUT:
; Carry - set on error (such as invalid module);
; All variables used by the player set to inital values
; ****************************************************************
TND_Init_Player:
mov cs:modulseg,es
cmp word ptr es:[0],'NT' ;correct file ID?
jne @error
cmp word ptr es:[2],'YD'
jne @error
cmp byte ptr es:[4],2 ;correct version?
jne @error
mov al,es:[5]
mov cs:NumTracks,al
mov al,es:[6]
mov cs:Songlength,al
mov al,es:[7]
mov cs:LoopPos,al
mov al,es:[8]
mov cs:NumPattern,al
mov al,es:[9]
mov cs:Speed,al
mov cs:SpeedCnt,al
mov al,es:[10]
mov cs:Tempo,al
mov cs:OldTempo,al ; Save initial tempo
mov ax,es:[11] ;Read size of the song description
mov si,13
add si,ax ;and skip it
mov cs:order_offset,si ;Save offset of order list
xor ax,ax
mov al,cs:Songlength
add si,ax ;And skip it for now
call Create_TOT ;Scan through the module and save start offset of each track
xor ax,ax
mov cs:pos,al ; Start with position 0 and..
call TND_Update_Pattern ; ..prepare pattern data
jc @error
call TANDY_Mute_All ; Mute all output on Tandy..
call Speaker_off ; and PC Speaker
push es
mov ax,0040h
mov es,ax
mov ax,es:[08h] ; Get adress of first Parallel port
pop es
mov dx,cs:IOPort
cmp dx,ax
jae @Parallel ; test if a parallel port should be used
jmp @Success ; successfull initialisation
@Parallel:
call Init_TLPT ; start initalising a TLPT device
jmp @Success
@error:
stc ; set carry on error
xor al,al
mov cs:initialized,al
jmp @Init_End
@Success:
clc ; clear carray if everything is ok
mov al,1
mov cs:initialized,al
@Init_End:
ret
;*******************************************************
; GET PACKED TRACK SIZE
;
;IN: si = start of track
; dl = size of one event -1
;
;OUT ax = Size of packed track
; si = start of next track
; *******************************************************
Get_PTrack_Size:
push cx bx dx
xor dh,dh
xor cx,cx
mov bx,si ;Save start offset
@TSize_Loop:
mov al,es:[si]
inc si
cmp al,0FFh ;empty event marker?
jb @@Event ;No? Then handle this as a real event
mov al,es:[si] ;Get number of empty events
inc si
add cl,al ;add them to counter
jmp @@Check_next
@@Event:
add si,dx ;Add remaining size of event to offset
inc cl ;Increase event counter
jmp @@Check_next
@@Check_next:
cmp cl,64
jb @TSize_Loop
mov ax,si
sub ax,bx ;AX size of saved track in byte
pop dx bx cx
ret
; *******************************************************
; GET TRACK NUMBER
; Converts Pattern/Track to absolute Track number
;
; IN:
; AH - Pattern number
; AL - Track number
;
; OUT:
; AX - Number of that Track
; CF - set if track / pattern is invalid
; *******************************************************
Get_Track_Number:
push bx cx dx
pushf
mov dl,cs:NumTracks
dec dl
cmp al,dl ;Valid track number?
ja @GTN_Error
cmp ah,cs:NumPattern ;Valid Pattern?
ja @GTN_Error
clc
xor bx,bx
xor cx,cx
mov cl,al ;move track number to cl
shr ax,8 ;move pattern number down to al
mov bl,cs:NumTracks
mul bx ;calc Pattern * Tracks per Pattern
add ax,cx ;add current Track in Pattern to result
jmp @GTN_Exit
@GTN_Error:
xor ax,ax
stc
@GTN_Exit:
popf
pop dx cx bx
ret
; *******************************************************
; GET TRACK START
; Returns the start offset of a track
;
; IN:
; AH - Pattern number
; AL - Track number
;
; OUT:
; SI - Offset to the start of that Track
; 0 on error
; CF - Set on error
; *******************************************************
Get_Track_Start:
push dx ax
clc
call Get_Track_Number
jc @GTS_Error
mov dx,2
mul dx ;Track number * 2 for word offsets
add ax,offset cs:TStart
mov si,ax ;add start offset of array
mov ax,cs:[si]
mov si,ax
jmp @GTS_Exit
@GTS_Error:
xor ax,ax
mov si,ax
@GTS_Exit:
pop ax dx
ret
; *******************************************************
; GET TRACK OFFSET
; Returns current offset for that track
;
; IN:
; CL - Track number
;
; OUT:
; SI - Current offset in that Track
; 0 on error
; *******************************************************
Get_Track_Offset:
push ax dx
xor ax,ax
mov al,cl
mov dx,2
mul dx ;Track number * 2 for word offsets
mov si,offset cs:COT
add si,ax
mov ax,cs:[si]
mov si,ax
pop dx ax
ret
; *******************************************************
; UPDATE TRACK OFFSET
; Saves current offset in that track
;
; IN:
;
; CL - Track number
; SI - Offset to save for this track
;
; OUT:
; none
;
; *******************************************************
Update_Track_Offset:
push ax dx di
xor ax,ax
mov al,cl
mov dx,2
mul dx ;Track number * 2 for word offsets
mov di, offset cs:COT
add di,ax
mov cs:[di],si
pop di dx ax
ret
; *******************************************************
; CREATE TRACK OFFSET TABLE
;
;IN: SI : Start of pattern data
;
;OUT Tstart set to track offsets
; *******************************************************
Create_TOT:
push ax dx cx di
mov cl,cs:NumPattern
xor ch,ch
mov di,offset cs:TStart
@Pcount:
push cx
mov cl,cs:NumTracks
dec cl
mov word ptr cs:[di],si ;Save start of track 0
add di,2
mov dl,2 ;consider the two additonal data bytes for voice events
@tcount:
call Get_PTrack_Size
mov word ptr cs:[di],si
add di,2
loop @tcount
xor dl,dl ;no additonal data for noise
call Get_PTrack_Size ;Si points to start of track 0 of the next pattern
pop cx
loop @PCount
pop di cx dx ax
ret
; *******************************************************
; This routine prepares next patterns data for playing
; IN:
; Actual position of the player in POS variable
; (All varibales have to be initialised first!)
;
; OUT:
; PATTERN holds the new pattern number
; COT points to start offsets of the tracks
; EMPTY is cleared
; SI points to start of new pattern data
; CF set if pattern is invalid
; *******************************************************
TND_Update_Pattern:
xor bx,bx
xor cx,cx
mov bl,cs:pos
cmp bl,cs:songlength ;Check for invalid position
ja @PNP_failed
mov si,cs:order_offset
add si,bx
mov al,es:[si] ;Get pattern number from order list
cmp al,cs:NumPattern ; Is the Pattern number valid?
ja @PNP_failed
mov cs:Pattern,al
mov ah,al
xor al,al
call Get_Track_Start ; Now SI points to start of first track
mov cl,cs:NumTracks
@Update_Offsets: ; Set COT (Current offset in track) array to initial values
mov al,cs:NumTracks
sub al,cl
call Get_Track_Start
jc @PNP_failed
push cx
mov cx,ax
call Update_Track_Offset
pop cx
loop @Update_Offsets
xor ax,ax
lea di,cs:Empty ; Clear empty event buffer
mov word ptr CS:[di],ax
mov word ptr CS:[di+2],ax
mov byte ptr CS:[di+4],al
mov cl,cs:row
cmp cl,0
je @PNP_exit
@Skip_rows: ; If cs:row is not 0 move forward to the desired row
xor al,al
@Chan_cnt:
call get_next_event
inc al
cmp al,cs:NumTracks
jb @Chan_cnt
loop @skip_rows
jmp @PNP_exit
@PNP_failed:
stc
ret
@PNP_exit:
clc
ret
; *******************************************************
; GET EVENT DATA
; This routine decompresses the packed data of one single
; event
;
; IN:
; ES:SI point to current data
; CL = Track
;
; OUT:
; Unpacked event data in Variables
; DL = number of empty events that follow
; SI = Offset of next event
; *******************************************************
Get_Event_Data:
xor ax,ax
xor dx,dx
mov al,es:[si] ;get first byte of event
inc si ;increase offset
cmp al,0FFh ;empty event marker?
jb @Decrunsh_Event ;No? Then hanlde it as real event
mov al,es:[si] ;get number of empty events to add in track
inc si ;increase offset
dec al ;sub current event from count
mov dl,al ;save the result in dl
jmp @Return
@Decrunsh_Event:
mov bl,cs:numtracks ;Get number of tracks in module
dec bl ;-1
cmp cl,bl ;is this the last (noise) track?
je @Decrunsh_Noise_Event
@Decrunsh_Voice_Event:
mov bl,al
shr al,4
lea di,cs:note
call Save_Byte ;Get note
mov al,bl
and al,0Fh
lea di,cs:octave
call Save_Byte ;Get octave
mov al,es:[si]
inc si
mov bl,al
shr al,4
inc al
lea di,cs:volume
call Save_Byte ;Get volume (+1)
mov al,bl
and al,0Fh
lea di,cs:effect
call Save_Byte ;Get effect
mov al,es:[si]
inc si
lea di,cs:parameter
call Save_Byte ;Get effects parameter
jmp @Return
@Decrunsh_Noise_Event:
mov bl,al
shr al,5
mov cs:divider,al ;Get frequency divider
mov al,bl
and al,0Fh
lea di,cs:volume
call Save_Byte ;Get volume
mov al,bl
test al,00010000b
jnz .bit_set
xor al,al ;Feedback 0
jmp .save_feedback
.bit_set:
mov al,1 ;Feedback 1
.save_feedback:
mov cs:feedback,al ;Get feedback / noise type
@Return:
ret
; *******************************************************
; GET NEXT EVENT
; Returns next event for the given track
;
; IN:
; AL - Track number
; OUT:
; Variables note,octave,volume,effect,parameter
; or (divider, feedback, volume for noise) filled
; *******************************************************
Get_next_event:
push ax bx cx dx
mov cl,al ;save current track in cl
mov dl,cs:NumTracks
dec dl
cmp cl,dl
je @GNE_noise
xor ax,ax ;Clear last used events data for this track
lea di,cs:note
call Save_Byte
lea di,cs:octave
call Save_Byte
lea di,cs:volume
call Save_Byte
lea di,cs:effect
call Save_Byte
lea di,cs:parameter
call Save_Byte
jmp @GNE_check_empty
@GNE_noise:
xor ax,ax ;Clear last used events data for noise track
mov cs:divider,al
mov cs:feedback,al
lea di,cs:volume
call Save_Byte
@GNE_check_empty:
lea si,cs:Empty ;Are there still empty events to add?
call Get_Byte
cmp al,0
je @GNE_next ;No? Proceed event
dec al ;Yes? Decrease empty event counter...
lea di,cs:Empty
call Save_Byte ;save the new value...
jmp @GNE_quit ;and exit
@GNE_next:
call Get_Track_Offset ;Get current offset in track data
call Get_Event_Data ;Decrunsh event
call Update_Track_Offset ;Save new offset
cmp dl,0 ;Empty events following?
je @GNE_quit
mov al,dl ;Set the empty event counter
lea di,cs:Empty ;and store new value for this track
call Save_Byte
@GNE_quit:
pop dx cx bx ax
ret
; *******************************************************
; GET BYTE
; Returns a word from a track buffer
;
; IN:
; CL - Track number
; SI - Offset of buffer
;
; OUT:
; AL - byte from buffer
; *******************************************************
Get_Byte:
pushf
push dx cx
mov dl,cs:NumTracks
dec dl
cmp cl,dl
ja @GB_Error
xor ch,ch
add si,cx ;add start offset of array
mov al,cs:[si]
inc si
jmp @GB_Exit
@GB_Error:
xor al,al
@GB_Exit:
pop cx dx
popf
ret
; *******************************************************
; SAVE BYTE
; Saves current frequency of a channel
;
; IN:
; CL - Track number
; DI - Offset of buffer
; AL - Byte to save
;
; OUT:
; nothing
; *******************************************************
Save_Byte:
pushf
push cx dx ax
mov dl,cs:NumTracks
dec dl
cmp cl,dl
ja @SW_Exit ;track number not valid?
xor ch,ch
add di,cx ;add start offset of array
pop ax
mov cs:[di],al
@SB_Exit:
pop dx cx
popf
ret
; *******************************************************
; GET WORD
; Returns a word from a track buffer
;
; IN:
; CL - Track number
; SI - Offset of buffer
;
; OUT:
; AX - word from buffer
; *******************************************************
Get_Word:
pushf
push dx cx
mov dl,cs:NumTracks
dec dl
cmp cl,dl
ja @GW_Error
mov al,cl
xor ah,ah
mov dx,2
mul dx ;mult 2 for words
add si,ax ;add start offset of array
mov ax,cs:[si]
jmp @GW_Exit
@GW_Error:
xor ax,ax
@GW_Exit:
pop cx dx
popf
ret
; *******************************************************
; SAVE WORD
; Saves current frequency of a channel
;
; IN:
; CL - Track number
; DI - Offset of buffer
; AX - Word to save
;
; OUT:
; nothing
; *******************************************************
Save_Word:
pushf
push cx dx ax
mov dl,cs:NumTracks
dec dl
cmp cl,dl
ja @SW_Exit ;track number not valid?
mov al,cl
xor ah,ah
mov dx,2
mul dx ;mult 2 for word offsets
add di,ax ;add start offset of array
pop ax
mov cs:[di],ax
@SW_Exit:
pop dx cx
popf
ret
;in: cl = track
Key_off:
push ax bx dx
push cx
mov al,cs:NumTracks
cmp al,5
je @Five_tracks_off
@Tandy_off:
xor ax,ax
xor bx,bx
xor dx,dx
mov al,cl
mov bl,020h
mul bl
add al,09Fh
mov dx,cs:IOPort
out dx,al
jmp @Exit_key_off
@Speaker_off:
call Speaker_off
jmp @Exit_key_off
@Five_tracks_off:
cmp cl,3
je @Speaker_off
cmp cl,4
jne @Tandy_off
dec cl
jmp @Tandy_off
@Exit_key_off:
pop cx
xor ax,ax
lea di,cs:freq
call Save_Word
pop dx bx ax
ret
;in: cl = track, dl = volume, bx = frequency
Play_voice:
push ax dx cx
mov al,cs:NumTracks
cmp al,5
je @Play_five_tracks
@Play_tandy:
call Freq_to_tone
mov al,dl
call TANDY_voice
jmp @PV_Exit
@Play_five_tracks:
cmp cl,3
je @Play_Speaker
cmp cl,4
jne @Play_tandy
dec cl
jmp @Play_tandy
@Play_Speaker:
call Speaker_play ;Play frequency on pc speaker
@PV_exit:
pop cx dx ax
ret
; *******************************************************
; PLAY EVENT
; Plays the Current event
;
; IN:
; AL - Track number
; Variables set
; *******************************************************
Play_Event:
push si ax bx cx dx
mov cl,al ;Move Track to cl
mov bl,cs:NumTracks ;Get number of tracks in bl
dec bl ;-1
cmp cl,bl ;Is it the last (noise) track?
je @PE_Play_Noise
lea si,cs:volume ;Get event data
call Get_Byte ;al = note, ah = octave, dl = volume
mov dl,al
lea si,cs:octave
call Get_Byte
mov ah,al
lea si,cs:note
call Get_Byte
cmp ax,0 ;No note/octave?
je @PE_no_note
cmp dl,0Fh ;Volume 15?
je @PE_Key_off
cmp al,0Dh ;Key-off?
je @PE_Key_off
cmp al,0Eh ;FX without new note?
je @PE_just_fx
lea di,cs:last_note ;Save note/octave for effects
call Save_Word
call Note_to_Freq ;Convert note/octave to frequency in Hz (BX = frequency)
lea si,cs:parameter
call Get_Byte
mov ah,al
lea si,cs:effect
call Get_Byte ;Now al = effect, ah = parameter
cmp ax,0 ;No effect?
je @PE_Play_note
call Handle_pre_note_FX ;Handle effects that take place BEFORE the note has been played
cmp al,03h ;Skip playing the note if it belongs to a tone slide
je @PE_Skip_note
cmp al,05h
je @PE_Skip_note
@PE_Play_note:
push ax
mov ax,bx ;Save fequency
lea di,cs:freq
call Save_Word
mov al,dl
lea di,cs:last_volume ;Save volume
call Save_byte
pop ax
dec dl ;Correct volume
call Play_voice ;Play current note
@PE_Skip_note:
call Handle_post_note_FX ;Handle effects
jmp @PE_Exit
@PE_Key_off:
call Key_off
jmp @PE_Exit
@PE_Play_noise:
mov dl,cs:divider
cmp dl,0
je @PE_Exit ;No new frequency to play
cmp dl,5 ;Key-off?
jae @PE_Key_off
dec dl
lea si,cs:volume ;Get volume
call Get_Byte
cmp al,0Fh ;Volume 15?
je @PE_Key_off
mov dh,cs:feedback
;al = volume, dl = divider, dh = feedback
call TANDY_Noise
jmp @PE_Exit
@PE_no_note:
cmp dl,0 ;No volume?
je @PE_Skip_Note
lea si,cs:last_volume ;get last used volume
call Get_Byte
cmp al,dl ;Is there a change?
je @PE_Skip_Note