-
Notifications
You must be signed in to change notification settings - Fork 23
/
IT_D_WM.INC
1785 lines (1343 loc) · 48.4 KB
/
IT_D_WM.INC
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
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
;
; Write module functions
;
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
Proc WriteITSampleBlock
PushAD ; BX = file handle.
Push DS
Mov DS, CS:DiskDataArea
Mov DX, [DS:1996]
Mov CX, [DS:1998]
Mov AX, 4200h
Int 21h
Mov AX, 80
Mul Word Ptr [DS:24h]
Mov CX, AX
Mov DX, 2000
Call D_SaveBlock
WriteITSampleBlock1:
Pop DS
PopAD
Ret
EndP WriteITSampleBlock
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
NUMS3MCHANNELS EQU 16
BitLUT Label Byte
DB 1, 2, 3, 3, 4, 4, 4, 4
DB 5, 5, 5, 5, 5, 5, 5, 5
DB 4, 4, 4, 4, 3, 3, 2
BitLUT16 Label Byte
DB 1, 10 SHL 3 + 3, 11 SHL 3 + 5, 11 SHL 3 + 3
DB 3 DUP (12 SHL 3 + 5), 12 SHL 3 + 3
DB 7 DUP (13 SHL 3 + 5), 13 SHL 3 + 3
DB 13 SHL 3 + 4, 7 DUP (13 SHL 3 + 5)
DB 12 SHL 3 + 4, 3 DUP (12 SHL 3 + 5)
DB 11 SHL 3 + 4, 11 SHL 3 + 5, 10 SHL 3 + 4, 2
Proc D_SaveIT Far
Call PE_SaveCurrentPattern
Call D_UpdateFileName
Push CS
Pop DS
Assume DS:Disk
Mov NoSaveError, 0
Mov AH, 3Ch
Xor CX, CX ; File type is normal (archive)
Mov DX, Offset SaveFileName ; DS:DX points to filename
Int 21h
JNC D_SaveIT3
Call PE_RestoreCurrentPattern
Jmp D_NoSaveMessage
D_SaveIT3:
Mov BX, AX ; BX = handle
Mov ES, DiskDataArea
Call Music_GetSongSegment
Mov DS, AX
Assume DS:Nothing ; DS = MusicData
Xor SI, SI
Xor DI, DI
Mov CX, 192
Rep MovsB ; Get header.
; Now to figure out the
; orders.
Mov SI, 511
StD
Mov CX, 256
D_SaveIT4:
LodsB
Cmp AL, 0FFh
LoopE D_SaveIT4
ClD
Add CX, 2
Mov [ES:20h], CX
Dec CX
Mov SI, 256
Rep MovsB ; Transfer orders.
Mov AL, 0FFh
StosB
; Mov AX, 0
; Test Byte Ptr [DS:2Ch], 4
; JZ D_SaveIT6
;
Call Music_GetNumberOfInstruments
;
; D_SaveIT6:
Mov [ES:22h], AX ; Number of instruments
Call Music_GetNumberOfSamples
Mov [ES:24h], AX ; Number of samples
Call PE_GetMaxPattern
Inc AX
Mov [ES:26h], AX ; Number of patterns.
Xor CX, CX ; CX:DX = 'long' offsets.
Mov DX, 0C0h
Add DX, [ES:20h]
Mov AX, [ES:22h]
ShL AX, 2
Add DX, AX
Mov AX, [ES:24h]
ShL AX, 2
Add DX, AX
Mov AX, [ES:26h]
ShL AX, 2
Add DX, AX
Mov DWord Ptr [ES:36h], 0
Mov Word Ptr [ES:2Eh], 4 ; Row hilight information
Push DS
Push Pattern
Pop DS
Assume DS:Pattern
Mov AX, Word Ptr [RowHilight1]
Pop DS
Assume DS:Nothing
Mov [ES:1Eh], AX
Call GetTimerCounter
Sub EAX, EditTimer
Cmp TimerData, 0
JE NoTimerData
Or Byte Ptr [ES:2Eh], 2 ; Contains timer information
Push ES ; Put time into table.
Push DI
Mov ES, TimerData
Mov DI, NumTimerData
ShL DI, 3
Add DI, 4
StosD
Add DI, 2
Add DX, DI ; Long offset progress.
AdC CX, 0
Pop DI
Pop ES
NoTimerData:
; OK.. check for MIDI config save.
Test Byte Ptr [DS:2Ch], 128
JZ D_SaveITNoMIDIConfig1
Add DX, 4896 ; MIDI Config size
AdC CX, 0
Or Byte Ptr [ES:2Eh], 8 ; MIDI Config.
D_SaveITNOMIDIConfig1:
Add EAX, [ES:3Ch]
Xor EAX, 'JTHL'
RoR EAX, 4
Neg EAX
RoL EAX, 7
Xor EAX, 'ITRK'
Mov [ES:3Ch], EAX
Call Msg_GetMessageLength
Cmp AX, 1
JE D_SaveITMsg1
Or Byte Ptr [ES:2Eh], 1
Mov Word Ptr [ES:3Ah], 0
Mov [ES:38h], DX
Mov [ES:36h], AX
Add DX, AX
D_SaveITMsg1:
Mov BP, [ES:22h] ; Number of instruments
Mov AX, 214h
Cmp CS:SaveFormat, 0
JE D_SaveNewFormat
Inc AX
Cmp CS:SaveFormat, 3
JE D_SaveNewFormat
Mov AX, 200h
D_SaveOldIT2_1:
Test BP, BP
JNZ D_SaveNewFormat
Mov AX, 100h
D_SaveNewFormat:
Inc BP
Mov Word Ptr [ES:28h], TRACKERVERSION
Mov [ES:2Ah], AX
And EBP, 0FFFFh
D_SaveIT7:
Dec BP
JZ D_SaveIT8
Mov SI, [DS:EBP+EBP+64710]
Test Byte Ptr [SI+1D4h], 80h
JZ D_SaveIT216
Mov Word Ptr [ES:2Ah], 216h
D_SaveIT216:
Mov AX, DX
StosW
Mov AX, CX
StosW
Add DX, 554
AdC CX, 0
Jmp D_SaveIT7
D_SaveIT8:
Mov BP, [ES:24h] ; Number of samples
Inc BP
Mov [ES:1996], DX ; Store sample position.
Mov [ES:1998], CX
D_SaveIT9:
Dec BP
JZ D_SaveIT10
Mov AX, DX
StosW
Mov AX, CX
StosW
Add DX, 80
AdC CX, 0
Jmp D_SaveIT9
D_SaveIT10:
Xor BP, BP
D_SaveIT11:
Cmp BP, [ES:26h] ; Number of patterns
JAE D_SaveIT12
Push DS
Mov AX, BP
Call Music_GetPattern ; DS:SI has pattern
Mov AX, DS
Cmp AX, Music
JE D_SaveIT1
Mov AX, DX
StosW
Mov AX, CX
StosW
Add DX, [DS:SI]
AdC CX, 0
Add DX, 8 ; pattern header length = 8.
AdC CX, 0
Jmp D_SaveIT22
D_SaveIT1:
Xor EAX, EAX
StosD
D_SaveIT22:
Pop DS
Inc BP
Jmp D_SaveIT11
D_SaveIT12:
; CX:DX now point to first sample data.
Push CX
Push DX
Push DS
Push DI
Push CS
Pop DS
Mov DI, (4+17*80)*2
Mov SI, Offset HeaderMsg
Mov AH, 5
Call S_DrawString
Pop CX ; CX = length of header.
Push ES
Pop DS
Xor DX, DX
Call D_SaveBlock ; Header.
Test Byte Ptr [DS:2Eh], 2 ; Time information?
JZ D_SaveITTimeData
Push DS
Push CS
Pop DS
Mov CX, 2
Mov DX, Offset NumTimerData
Inc [DS:NumTimerData]
Call D_SaveBlock
Mov CX, [DS:NumTimerData]
Dec [DS:NumTimerData]
ShL CX, 3
Mov DS, [DS:TimerData]
Xor DX, DX
Call D_SaveBlock
Pop DS
D_SaveITTimeData:
Test Byte Ptr [DS:2Ch], 128
JZ D_SaveITMIDIConfig
Push DS
Mov CX, 4896
Xor DX, DX
Call Music_GetMIDIDataArea ; Gets DS
Call D_SaveBlock
Pop DS
D_SaveITMIDIConfig:
; Message?
Test Byte Ptr [DS:2Eh], 1
JZ D_SaveITMsg2
Mov CX, [DS:36h]
Push DS
Call Msg_GetMessageOffset
Call D_SaveBlock
Pop DS
D_SaveITMsg2:
Push CS
Pop DS
Mov DI, (4+18*80)*2
Mov SI, Offset InstrumentHeaderMsg
Mov AH, 5
Call S_DrawString
Pop DS
Cmp Word Ptr [ES:22h], 0
JE D_SaveIT13
Mov AX, 554
Mul Word Ptr [ES:22h]
Mov DX, 512
Mov CX, AX
Call D_SaveBlock ; Instrument headers...
D_SaveIT13:
Push DS
Push CS
Pop DS
Mov DI, (4+19*80)*2
Mov SI, Offset SampleHeaderMsg
Mov AH, 5
Call S_DrawString
Pop DS ; DS = song data segment
Mov SI, 55912 ; Start of samples in mem.
Mov AX, 80
Mul Word Ptr [ES:24h]
Mov DI, 2000
Mov CX, AX
Rep MovsB
Pop DX
Pop CX ; CX:DX = sample point.
Mov BP, [ES:24h] ; Number of samples.
Mov SI, 2000
Inc BP
D_SaveIT14:
Dec BP
JZ D_SaveIT16
Mov AL, [ES:SI+12h]
Mov AH, 1
Cmp CS:SaveFormat, 3
JNE D_SaveNoDD
Or AH, 4
D_SaveNoDD:
Test AL, 1
JZ D_SaveIT15
Or AL, 8
Cmp CS:SaveFormat, 2
JNE D_SaveIT214_1
And AL, Not 8
D_SaveIT214_1:
Mov [ES:SI+48h], DX
Mov [ES:SI+4Ah], CX
Test AL, 2
Mov [ES:SI+12h], AL
Mov [ES:SI+2Eh], AH
Mov EAX, [ES:SI+30h]
JZ D_SaveIT16Bit
Add EAX, EAX
D_SaveIT16Bit:
Add DX, AX
PushF
ShR EAX, 16
PopF
AdC CX, AX ; Advance sample pointer.
D_SaveIT15:
Add SI, 80
Jmp D_SaveIT14
D_SaveIT16:
Call WriteITSampleBlock
Xor BP, BP
D_SaveIT17:
Push DS
Push CS
Pop DS
Mov DI, (4+20*80)*2
Mov SI, Offset PatternMsg
Mov AH, 5
Push BP
Call S_DrawString
Pop AX
Call Music_GetPattern
Mov DX, DS
Cmp DX, Music
JE D_SaveIT2
Mov DX, SI
Mov CX, [DS:SI]
Add CX, 8
Call D_SaveBlock
D_SaveIT2:
Pop DS
Inc BP
Cmp BP, [ES:26h]
JB D_SaveIT17
; Setup compression block
Push DS
Push CS
Pop DS
Mov SI, Offset BitLUT
Mov DI, 10240
Mov CX, 16/2
Rep MovsW
; Setup table.
Mov CX, 16
Mov AL, 6
Rep StosB
Mov CX, 28
Inc AX
Rep StosB
Mov CX, 64
Inc AX
Rep StosB
Mov CX, 8
Inc AX
Rep StosB
Mov CX, 64
Dec AX
Rep StosB
Mov CX, 29
Dec AX
Rep StosB
Mov CX, 16
Dec AX
Rep StosB
Mov CX, 15
Sub SI, 8
Rep MovsB
; 16 bit Main LUT
Mov SI, Offset BitLUT16
Mov CX, 16
Rep MovsB
Mov CX, 15
Mov AL, 14 SHL 3 + 5
Rep StosB
Mov AL, 14 SHL 3 + 3
StosB
Mov CX, 31
Mov AL, 15 SHL 3 + 5
Rep StosB
Mov AL, 15 SHL 3 + 3
StosB
Mov CX, 63
Mov AL, 16 SHL 3 + 5
Rep StosB
Mov AL, 16 SHL 3 + 3
StosB
Inc AX
; Mov AL, 16 SHL 3 + 4
StosB
Mov CX, 63
Inc AX
; Mov AL, 16 SHL 3 + 5
Rep StosB
Mov AL, 15 SHL 3 + 4
StosB
Mov CX, 31
Mov AL, 15 SHL 3 + 5
Rep StosB
Mov AL, 14 SHL 3 + 4
StosB
Mov CX, 15
Mov AL, 14 SHL 3 + 5
Rep StosB
Mov CX, 16
Rep MovsB
; 16 bit LUT 1, for 16-bit samples with high byte = 0
Mov SI, Offset BitLUT
Mov CX, 16
Rep MovsB
Mov CX, 16
Mov AL, 6
Rep StosB ; AL = 6 from before
Mov CX, 24
Inc AX
Rep StosB
Mov CX, 64
Inc AX
Rep StosB
Mov CX, 128
Inc AX
Rep StosB
; 16-Bit LUT 2, for 16-bit samples with high byte = 0xFF
Mov CX, 16
Inc AX
Rep StosB
Mov CX, 128
Dec AX
Rep StosB
Mov CX, 64
Dec AX
Rep StosB
Mov CX, 25
Dec AX
Rep StosB
Mov CX, 16
Dec AX
Rep StosB
Mov CX, 15
Sub SI, 8
Rep MovsB
Mov CX, 256-8
Xor AX, AX
Rep StosB
Mov CX, 16
Inc AX
Rep StosB
Mov CX, 256-8+256
Dec AX
Rep StosB
Pop DS
Xor BP, BP ; BP = sample number.
Mov DI, 2000+48h
D_SaveIT18:
Push DS
Push DI
Xor CX, CX
Xor DX, DX
Mov AX, 4201h
Int 21h
Mov [ES:DI], AX
Mov [ES:DI+2], DX
Mov SI, BP
Add SI, SI
Mov SI, [64912+SI] ; DS:SI points to sample head.
Test Byte Ptr [SI+12h], 1 ; Check if sample is present.
JZ D_SaveIT19
Cmp DWord Ptr [SI+30h], 0 ; For bodgy sample crap.
JZ D_SaveIT19
D_SaveIT21:
Push DS ; DS:DX points to sample...
Push SI
Push CS
Pop DS
Mov DI, (4+21*80)*2
Mov SI, Offset SampleMsg
Mov AX, BP
Inc AX
Push AX
Mov AH, 5
Call S_DrawString
Pop AX
Pop SI
Pop DS ; Sample encoding to go here..
; But sample position needs to
; be recoded then...
Cmp CS:SaveFormat, 2
JNE D_SaveSampleDataCompressedCall
Call D_SaveSampleData
Jmp D_SaveIT19
D_SaveSampleDataCompressedCall:
Call D_SaveSampleDataCompressed
D_SaveIT19:
Pop DI
Pop DS
Add DI, 80
Inc BP
Cmp BP, [ES:24h]
JB D_SaveIT18
Call WriteITSampleBlock
Mov AH, 3Eh ; Close file
Int 21h
Push CS
Pop DS
Mov DI, (4+23*80)*2
Mov SI, Offset CompleteMsg
Mov AH, 5
Call S_DrawString
EndSaveModule:
Mov DX, Offset SaveFileName
Call D_DeleteIfError
Call PE_RestoreCurrentPattern
Cmp NoSaveError, 0
JNE SaveModuleError
Call PEResetModified
SaveModuleError:
Mov AX, 1
Ret
EndP D_SaveIT
Assume DS:Nothing
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
Proc D_SaveS3M Far
Call PE_SaveCurrentPattern
Call D_UpdateFileName
Mov BP, 60h ; BP = length of header.
; Call PE_StoreCurrentPattern
Push CS
Pop DS
Assume DS:Disk
Mov SaveFormatError, 0
Mov NoSaveError, 0
Mov AH, 3Ch
Xor CX, CX ; File type is normal (archive)
Mov DX, Offset SaveFileName ; DS:DX points to filename
Int 21h
JNC D_SaveS3M1
Call PE_RestoreCurrentPattern
Jmp D_NoSaveMessage
D_SaveS3M1:
Mov BX, AX
Mov ES, DiskDataArea
Call Music_GetSongSegment
Mov DS, AX
Assume DS:Nothing ; DS = MusicData
Mov SI, 4
Xor DI, DI
Mov CX, 26/2
Rep MovsW
Xor AX, AX
StosW
Mov AX, 1Ah+16*256 ; St3 module identification.
StosW
Xor AX, AX
StosW
; Now to figure out the
; orders.
Mov SI, 511
StD
Mov CX, 256
D_SaveS3M2:
LodsB
Cmp AL, 0FFh
LoopE D_SaveS3M2
ClD
Inc CX
Mov AX, CX
Inc AX ; Number of orders.
Add BP, AX
StosW
Call Music_GetNumberOfSamples
Add BP, AX
Add BP, AX
StosW ; Number of samples.
Call PE_GetMaxPattern
Inc AX
Add BP, AX
Add BP, AX
Cmp AX, 100
JBE D_SaveS3M4
Mov AX, 100
Push Offset D_SaveS3M4
Push SI
Push DI
Mov SI, Offset TooManyPatternsMsg
Mov DI, (4+23*80)*2
Jmp D_SaveS3MFormatError
D_SaveS3M4:
StosW ; Number of patterns
; Flags... 8 = vol0opt on.
Mov AX, [DS:2Ch]
And AX, 2
ShL AX, 2
StosW ; Flags stored.
Mov AX, 3000h+TRACKERVERSION
StosW ; Impulse Tracker
Mov AX, 2
StosW ; Standard unsigned samples.
Mov EAX, 'MRCS'
StosD ; ID done.
Mov AH, [DS:2Ch]
Test AH, 4
JZ D_SaveS3MNoInsError
Push Offset D_SaveS3MNoInsError
Push SI
Push DI
Mov SI, Offset ST3InstrumentErrorMsg
Mov DI, (4+29*80)*2
Jmp D_SaveS3MFormatError
D_SaveS3MNoInsError:
Mov AL, [DS:30h]
ShR AL, 1
StosB
Mov AX, [DS:32h] ; Tempo and speed
StosW
Mov AL, [DS:31h] ; Mixing volume
Mov AH, [DS:2Ch] ; Grab stereo setting.
Cmp AL, 80h
JB D_SaveNoMixOverflow
Mov AL, 7Fh
D_SaveNoMixOverflow:
ShL AL, 1
ShR AX, 1
StosB
Mov AX, 252*256
StosW
Xor AX, AX
StosW
Call GetTimerCounter
Sub EAX, EditTimer
Add EAX, [DS:3Ch]
Xor EAX, 'JTHL'
RoR EAX, 4
Neg EAX
RoL EAX, 7
Xor EAX, 'ITRK'
StosD
Xor AX, AX
StosW
Mov SI, 256 ; Orders.
Mov DI, 60h
JCXZ D_SaveS3M3
D_SaveS3M5:
LodsB
Cmp AL, 0FEh
JAE D_SaveS3M6
Cmp AL, 100
JB D_SaveS3M6
Mov AL, 0FFh ; So that pattern numbers >= 100 aren't listed
D_SaveS3M6:
StosB
Loop D_SaveS3M5
D_SaveS3M3:
Mov AL, 0FFh
StosB
; Do channel settings.
Push BX
Mov BX, BP
Mov SI, 40h
Mov DI, SI
Sub BX, DI
Mov CX, 32
Xor AH, AH
D_SaveS3M7:
LodsB
Test AL, 80h
JNZ D_SaveS3M9
Cmp AL, 64
JBE D_SaveS3M8
Mov AL, 32 ; For surround.
D_SaveS3M8:
; Convert 0->64 to 0->15
Mov DL, AL
ShR DL, 1
Sub DL, 1
AdC DL, 0
ShR DL, 1
Or DL, 32 ; Reqd...
Mov AL, AH
And AL, 1
ShL AL, 3
Mov DH, AH
ShR DH, 1
Or AL, DH
Inc AH
Jmp D_SaveS3M10
D_SaveS3M9: ; Muted/nonexistant
Mov AL, 0FFh
Mov DL, 0
D_SaveS3M10:
Mov [ES:BX+DI], DL
StosB
Loop D_SaveS3M7
Pop BX
Add BP, 20h
; Check that channel volumes are all 64...
Mov CX, 32
Mov SI, 80h
D_SaveS3M11:
LodsB
Cmp AL, 64
JNE D_SaveS3M12
Loop D_SaveS3M11
Jmp D_SaveS3M13
D_SaveS3M12:
Push Offset D_SaveS3M13
Push SI
Push DI
Mov SI, Offset ChannelVolumeErrorMsg
Mov DI, (4+24*80)*2
Jmp D_SaveS3MFormatError
D_SaveS3M13:
Test Byte Ptr [DS:2Ch], 8