aboutsummaryrefslogtreecommitdiff
path: root/devtools/tasmrecover/dreamweb/sblaster.asm
blob: 9eb9afc08f95321a61f32695f7e781975d866858 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
;Copyright (c) 1990-2011 by Neil Dodwell
;Released with permission from Neil Dodwell under GPLv2
;See LICENSE file for full license text
; Creative Reality Sound Blaster Drivers . (C) 1994 Creative Reality

; Very sparsly commented.
 


;These drivers are not stand alone. We had them as an integral part of the
;game.
;
;Put interupt no. into SOUNDINT, base address (eg 220h) into SOUNDBASEADD.
;If interupt is 255 then no card is assumed.
;
;Call soundstartup at beginning of program to test card and initialise.
;
;This code assumes that EMS has been initialised
;Emm page frame is in variable EMMPAGEFRAME. Handle is in EMMHANDLE.
;
;Call loadsample with a filename in CS:DX (ie. in the code somewhere)
;
;To play a sample call playchannel0 or playchannel1 with sound no. in al.
;
;Call endsample to restore interupts and halt sound.
;
;




;------------------------------------------- Initial sound set up and end ---

Loadspeech	proc	near
	
	cmp	soundint,255
	jz	dontbother8

	call	cancelch1

	mov	speechloaded,0
	call	createname
	
	mov	speechlength,0
	mov	dx,offset cs:speechfilename
	call	openfilenocheck
	jc	dontbother8

	mov	bx,speechemmpage

moreloadspeech:	push	dx bx
	
	push	es di bx
	mov	al,2
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	cmp	ah,0
	jnz	emmerror
	mov	ds,emmpageframe
	pop	bx di es
 	inc	bx
	push	es di
	mov	al,3
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	cmp	ah,0
	jnz	emmerror
	mov	ds,emmpageframe
	mov	es,emmpageframe
	mov	di,8000h
	mov	cx,4000h
	mov	ax,0
	rep	stosw
	pop	di es 

	mov	cx,8000h
	mov	dx,8000h
	call	readfromfile
	mov	cl,11
	shr	ax,cl
	add	speechlength,ax
	pop	bx dx
	add	bx,2
	cmp	ax,0 	
	jnz	moreloadspeech
	call	closefile

	mov	es,sounddata2
	mov	di,50*6
	mov	ax,speechemmpage
	mov	[es:di],al
	mov	ax,0
	mov	[es:di+1],ax
	mov	ax,speechlength
	mov	[es:di+3],ax
	mov	speechloaded,1
dontbother8:	ret

speechfilename:	db	"SPEECH\"
speechfile:	db	"R24C0005.RAW",0

	endp



Createname	proc	near

	push	ax
	mov	di,offset cs:speechfile
	mov	byte ptr [cs:di+0],dl ;"R"
	mov	[cs:di+3],cl
	
	mov	al,dh ;reallocation
	mov	ah,"0"-1
findten:	inc	ah
	sub	al,10
	jnc	findten
	mov	[cs:di+1],ah
	add	al,10+"0"
	mov	[cs:di+2],al
	pop	ax
	
	mov	cl,"0"-1
thousandsc:	inc	cl
	sub	ax,1000
	jnc	thousandsc
	add	ax,1000
	mov	[cs:di+4],cl
	mov	cl,"0"-1
hundredsc:	inc	cl
	sub	ax,100
	jnc	hundredsc
	add	ax,100
	mov	[cs:di+5],cl
	mov	cl,"0"-1
tensc:	inc	cl
	sub	ax,10
	jnc	tensc
	add	ax,10
	mov	[cs:di+6],cl
	add	al,"0"
	mov	[cs:di+7],al
	ret

	endp






Loadsample	proc	near

	cmp	soundint,255
	jz	dontbother
      
	call	openfile
	call	readheader
	mov	bx,[es:di]
	push	es di bx
	mov	ds,sounddata
	pop	cx
	mov	dx,0
	call	readfromfile
	pop	di es

	add	di,2
	mov	bx,0
	mov	dx,[es:di]
	add	dx,1
	shr	dx,1
	
	mov	soundemmpage,0

moreload:	push	dx bx
	
	push	es di bx
	mov	al,2
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	cmp	ah,0
	jnz	emmerror
	mov	ds,emmpageframe
	pop	bx di es
 	inc	bx
	push	es di
	mov	al,3
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	cmp	ah,0
	jnz	emmerror
	mov	ds,emmpageframe
	pop	di es 

	mov	cx,8000h
	mov	dx,8000h
	call	readfromfile
	pop	bx dx
	add	bx,2
	add	soundemmpage,2
	dec	dx	
	jnz	moreload
	;inc	soundemmpage
	call	closefile
dontbother:	ret

emmerror:	mov	gameerror,7
	jmp	quickquit2

	endp






Loadsecondsample	proc	near

	cmp	soundint,255
	jz	dontbother9

	cmp	ch0playing,12
	jc	ch0oksecond
	cmp	ch0playing,255
	jz	ch0oksecond
	call	cancelch0
	;mov	cx,100
           	;call	hangon
	jmp	ch0oksecond
justcancel:	call	cancelch0
ch0oksecond:	cmp	ch1playing,12
	jc	ch1oksecond
            	call	cancelch1

ch1oksecond:	call	openfile
	call	readheader
	mov	bx,[es:di]
	push	es di bx
	mov	ds,sounddata2
	pop	cx
	mov	dx,0
	call	readfromfile

	mov	cx,100
	mov	di,0
	mov	es,sounddata2
	mov	bx,soundemmpage
adjustemmpage:	mov	al,[es:di]
	add	al,bl
	mov	[es:di],al
	add	di,6
	loop	adjustemmpage

	pop	di es

	add	di,2
	mov	bx,soundemmpage
	mov	speechemmpage,bx
	mov	dx,[es:di]
	add	dx,1
	shr	dx,1

moreload2:	push	dx bx
	
	push	es di bx
	mov	al,2
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	cmp	ah,0
	jnz	emmerror2
	mov	ds,emmpageframe
	pop	bx di es
 	inc	bx
	push	es di
	mov	al,3
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	cmp	ah,0
	jnz	emmerror2
	mov	ds,emmpageframe
	pop	di es 

	mov	cx,8000h
	mov	dx,8000h
	call	readfromfile
	pop	bx dx
	add	bx,2
	add	speechemmpage,2
	dec	dx	
	jnz	moreload2
	call	closefile
dontbother9:	ret

emmerror2:	mov	gameerror,7
	jmp	quickquit2

	endp






Soundstartup	proc	near

	cmp	soundint,255
	jz	dontbother2

	mov	dx,soundbaseadd
	add	dx,0eh
	mov	DSP_status,dx
	mov	dx,soundbaseadd
	add	dx,0ch
	mov	DSP_write,dx
	
	mov	al,1
	mov	dx,soundbaseadd
	add	dx,0006h
	out	dx,al
	push	ax ax ax ax ax ax ax ax
	pop	ax ax ax ax ax ax ax ax
	mov	al,0
	out	dx,al

	mov	dx,DSP_status
	mov	cx,2000
waitinit:	in	al,dx
	and	al,128
	jz	waitinit
	mov	dx,soundbaseadd
	add	dx,000ah
	in	al,dx
	cmp	al,0aah
	jz	dspready
	loop	waitinit
	mov	gameerror,2
	jmp	quickquit

dspready:	call	trysoundalloc

	cli
	mov	ah,40h	;set sample rate
	call	out22c
	mov	ah,210	;of 22050Hz
	call	out22c
	sti

	call	checksoundint

	mov	ah,35h
	mov	al,soundint
	add	al,8
	int	21h
	mov	oldsoundintseg,es	; Save es:bx to temp memory
	mov	oldsoundintadd,bx
	push	cs
	pop	ds
	mov	dx,offset cs:dmaend
	mov	ah,25h
	mov	al,soundint
	add	al,8
	int	21h	; Set to new

	call	enablesoundint
	
	mov	al,sounddmachannel
	xor	ah,ah
	mov	bx,offset cs:dmaaddresses
	add	bx,ax
	mov	al,[cs:bx]
	mov	dmaaddress,al

	mov	ah,0d1h	;speaker on
	call	out22c
	mov	ah,0d0h
	call	out22c

dontbother2:	ret

dmaaddresses	db	87h,83h,81h,82h

	endp





Trysoundalloc	proc	near

	cmp	needsoundbuff,1
	jz	gotsoundbuff
	inc	soundtimes
	mov	bx,(16384+2048)/16
	call	allocatemem
	mov	soundbuffer,ax
	push	ax
	mov	al,ah
	mov	cl,4
	shr	al,cl
	mov	soundbufferpage,al
	pop	ax
	mov	cl,4
	shl	ax,cl
	mov	soundbufferad,ax
	cmp	ax,0b7ffh
	jnc	soundfail

	mov	es,soundbuffer
	mov	di,0
	mov	cx,16384/2
	mov	ax,7f7fh
	rep	stosw
	mov	needsoundbuff,1
	ret

soundfail:	mov	es,soundbuffer
	call	deallocatemem
gotsoundbuff:    	ret

	endp






Setsoundoff	proc	near

	cmp	soundint,255
	jz	dontbother28
	mov	soundbufferwrite,0
	cli
	call	setupPIT
	mov	soundbufferwrite,4096
	call	startdmablock
	sti
dontbother28:	ret	        

	endp






Checksoundint	proc	near
	
	mov	ah,0d3h	;speaker off
	call	out22c

	mov	testresult,0
	mov	ah,35h
	mov	al,soundint
	add	al,8
	int	21h
	mov	oldsoundintseg,es
	mov	oldsoundintadd,bx
	push	cs
	pop	ds
	mov	dx,offset cs:interupttest
	mov	ah,25h
	mov	al,soundint
	add	al,8
	int	21h

	call	enablesoundint
	
	mov	ah,0f2h
	call	out22c

	mov	cx,20
	call	hangon

	call	disablesoundint

	mov	dx,oldsoundintseg	
	mov	ds,dx
	mov	dx,oldsoundintadd	;Restore old interupt vector
	mov	ah,25h
	mov	al,soundint
	add	al,8
	int	21h

	cmp	testresult,1
	jz	interuptworked
	mov	gameerror,6	;interupt wrong
	jmp	quickquit	;exit to DOS with error

interuptworked:	ret

	endp





Enablesoundint	proc	near

	mov	dx,21h	; Enable int?
	in	al,dx
	mov	currentirq,al
	mov	ah,11111110b
	mov	cl,soundint
	rol	ah,cl
	and	al,ah
	out	dx,al
	ret

	endp





Disablesoundint	proc	near

	mov	al,soundint
	mov	dx,21h
	mov	al,currentirq
	out	dx,al
	ret

	endp




Interupttest	proc	near

	cli
	push	ax dx
	mov	testresult,1
	mov	dx,DSP_status
	in	al,dx
	mov	al,20h
	out	20h,al
	pop	dx ax
	iret
	
	endp





Soundend	proc	near

	cmp	soundint,255
	jz	dontbother3

	call	getridofPIT

	mov	ah,0d0h
	call	out22c
	
	call	disablesoundint

	mov	ds,oldsoundintseg		;for keys
	mov	dx,oldsoundintadd		;Restore old interupt vector
	mov	ah,25h
	mov	al,soundint
	add	al,8
	int	21h

dontbother3:	ret

	endp





Out22c	proc	near

    	mov	dx,DSP_write
notclear:	in	al,dx
	or	al,al
	js	notclear
	mov	al,ah
	out	dx,al
	ret

	endp





;---------------------------------------------------------------------------




Playchannel0	proc	near	;al=sound no
			;ah=times to repeat
	cmp	soundint,255
	jz	dontbother4
	
	push	es ds bx cx di si

	mov	ch0playing,al
	mov	es,sounddata
	cmp	al,12
	jc	notsecondbank
	mov	es,sounddata2
	sub	al,12
notsecondbank:	mov	ch0repeat,ah
	mov	ah,0
	add	ax,ax
	mov	bx,ax
	add	ax,ax
	add	bx,ax

	mov	al,[es:bx]
	mov	ah,0
	mov	ch0emmpage,ax
	mov	ax,[es:bx+1]
	mov	ch0offset,ax
	mov	ax,[es:bx+3]
	mov	ch0blockstocopy,ax
	
	cmp	ch0repeat,0
	jz	nosetloop
	mov	ax,ch0emmpage
	mov	ch0oldemmpage,ax
	mov	ax,ch0offset
	mov	ch0oldoffset,ax
	mov	ax,ch0blockstocopy
	mov	ch0oldblockstocopy,ax

nosetloop:	pop	si di cx bx ds es

dontbother4:	ret

	endp







Playchannel1	proc	near	;al=sound no

	cmp	soundint,255
	jz	dontbother5
	cmp	ch1playing,7
	jz	dontbother5
                  	push	es ds bx cx di si

	mov	ch1playing,al
	mov	es,sounddata
	cmp	al,12
	jc	notsecondbank1
	mov	es,sounddata2
	sub	al,12
notsecondbank1:	mov	ah,0
	add	ax,ax
	mov	bx,ax
	add	ax,ax
	add	bx,ax

	mov	al,[es:bx]
	mov	ah,0
	mov	ch1emmpage,ax
	mov	ax,[es:bx+1]
	mov	ch1offset,ax
	mov	ax,[es:bx+3]
	mov	ch1blockstocopy,ax
	
	pop	si di cx bx ds es

dontbother5:	ret

	endp








Makenextblock	proc	near
		              
	call	volumeadjust

	call	loopchannel0
	cmp	ch1blockstocopy,0
	jz	mightbeonlych0
	cmp	ch0blockstocopy,0
	jz	mightbeonlych1

	dec	ch0blockstocopy
	dec	ch1blockstocopy
	call	bothchannels
	ret

mightbeonlych1:	mov	ch0playing,255
	cmp	ch1blockstocopy,0
	jz	notch1only
	dec	ch1blockstocopy
	call	channel1only
notch1only:	ret

mightbeonlych0:	mov	ch1playing,255
	cmp	ch0blockstocopy,0
	jz	notch0only
	dec	ch0blockstocopy
	call	channel0only
	ret
notch0only:	mov	es,soundbuffer
	mov	di,soundbufferwrite
	mov	cx,1024
	mov	ax,7f7fh
	rep	stosw
	and	di,16384-1
	mov	soundbufferwrite,di
	ret
		
	endp




Volumeadjust	proc	near

	mov	al,volumedirection
	cmp	al,0
	jz	volok
	mov	al,volume
	cmp	al,volumeto
	jz	volfinish
	add	volumecount,64 
	jnz	volok
	mov	al,volume
	add	al,volumedirection
	mov	volume,al
	ret
volfinish:	mov	volumedirection,0
volok:	ret

	endp



Loopchannel0	proc	near

	cmp	ch0blockstocopy,0
	jnz	notloop
	cmp	ch0repeat,0
	jz	notloop
	cmp	ch0repeat,255
	jz	endlessloop
            	dec	ch0repeat
endlessloop:	mov	ax,ch0oldemmpage
	mov	ch0emmpage,ax
	mov	ax,ch0oldoffset
	mov	ch0offset,ax
	mov	ax,ch0blockstocopy
	add	ax,ch0oldblockstocopy
	mov	ch0blockstocopy,ax
	ret		
notloop:	ret

	endp







Cancelch0	proc	near

	mov	ch0repeat,0
	mov	ch0blockstocopy,0
	mov	ch0playing,255
	ret

	endp



Cancelch1	proc	near

	mov	ch1blockstocopy,0
	mov	ch1playing,255
	ret

	endp




Channel0only	proc	near

	call	saveems
	mov	al,0
	mov	bx,ch0emmpage
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	
	mov	es,soundbuffer
	mov	ds,emmpageframe
	mov	di,soundbufferwrite
	mov	si,ch0offset
	
	call	channel0tran
	call	restoreems

	and	di,16384-1
	mov	soundbufferwrite,di
           	and	si,16384-1
	mov	ch0offset,si
	cmp	si,0
	jnz	notch0endofpage0
	inc	ch0emmpage
notch0endofpage0:	ret

	endp




Channel1only	proc	near

	call	saveems
	mov	al,1
	mov	bx,ch1emmpage
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	
	mov	es,soundbuffer
	mov	ds,emmpageframe
	mov	di,soundbufferwrite
	mov	si,ch1offset
	add	si,16384

	mov	cx,1024
	rep	movsw
	call	restoreems

	and	di,16384-1
	mov	soundbufferwrite,di
           	and	si,16384-1
	mov	ch1offset,si
	cmp	si,0
	jnz	notch1endofpage1
	inc	ch1emmpage
notch1endofpage1:	ret

	endp





Channel0tran	proc	near

	cmp	volume,0
	jnz	lowvolumetran
	mov	cx,1024
	rep	movsw
	ret

lowvolumetran:	mov	cx,1024
	mov	bh,volume
	mov	bl,0
	add	bx,16384-256
volloop:	lodsw
	mov	bl,al
	mov	al,[es:bx]
	mov	bl,ah
	mov	ah,[es:bx]
	stosw
	loop	volloop
	ret
	

	endp








Bothchannels	proc	near 	;rather slow routine
			;to mix two channels

	call	saveems
	mov	al,0
	mov	bx,ch0emmpage
	mov	dx,emmhandle
	mov	ah,44h
	int	67h
	mov	al,1
	mov	bx,ch1emmpage
	mov	dx,emmhandle
	mov	ah,44h
	int	67h

	mov	es,soundbuffer
	mov	ds,emmpageframe
	mov	di,soundbufferwrite
	mov	si,ch0offset
	mov	bx,ch1offset
	add	bx,16384
	mov	cx,2048
	mov	dh,128
	mov	dl,255

	call	domix
	call	restoreems
	
	and	di,16384-1
	mov	soundbufferwrite,di

	mov	si,ch0offset
	add	si,2048
           	and	si,16384-1
	mov	ch0offset,si
	cmp	si,0
	jnz	notbothendofpage0
	inc	ch0emmpage
notbothendofpage0:	mov	si,ch1offset
	add	si,2048
           	and	si,16384-1
	mov	ch1offset,si
	cmp	si,0
	jnz	notbothendofpage1
	inc	ch1emmpage
notbothendofpage1:	ret

	endp



Saveems	proc	near

	mov	ah,4eh
	mov	al,0
	mov	es,soundbuffer
	mov	di,16384+2048-256
	int	67h
	ret

	endp


Restoreems	proc	near

	push	si di
	mov	ah,4eh
	mov	al,1
	mov	ds,soundbuffer
	mov	si,16384+2048-256
	int	67h
	pop	di si
	ret

	endp



Domix	proc	near

	cmp	volume,0
	jnz	lowvolumemix

slow:	lodsb
	mov	ah,[bx]
	inc	bx       
	cmp	al,dh
	jnc	toplot

botlot:	cmp	ah,dh
	jnc	nodistort
  	add	al,ah
	js	botok
	xor	al,al
	stosb
    	loop	slow	
     	jmp	doneit
botok:	xor	al,dh
   	stosb
    	loop	slow	
     	jmp	doneit

toplot:	cmp	ah,dh
	jc	nodistort
  	add	al,ah
	jns	topok
	mov	al,dl
	stosb
    	loop	slow	
     	jmp	doneit
topok:	xor	al,dh
   	stosb
    	loop	slow	
     	jmp	doneit
	
nodistort:	add	al,ah
	xor	al,dh
	stosb
	loop	slow
	jmp	doneit


lowvolumemix:	lodsb
	push	bx
	mov	bh,volume
	add	bh,63
	mov	bl,al
	mov	al,[es:bx]
	pop	bx

	mov	ah,[bx]
	inc	bx       
	cmp	al,dh
	jnc	toplotv

botlotv:	cmp	ah,dh
	jnc	nodistortv
  	add	al,ah
	js	botokv
	xor	al,al
	stosb
    	loop	lowvolumemix	
     	jmp	doneit
botokv:	xor	al,dh
   	stosb
    	loop	lowvolumemix	
     	jmp	doneit

toplotv:	cmp	ah,dh
	jc	nodistortv
  	add	al,ah
	jns	topokv
	mov	al,dl
	stosb
    	loop	lowvolumemix	
     	jmp	doneit
topokv:	xor	al,dh
   	stosb
    	loop	lowvolumemix	
     	jmp	doneit
	
nodistortv:	add	al,ah
	xor	al,dh
	stosb
	loop	lowvolumemix
doneit:	ret


	endp







Dmaend	proc	near
	
	cli
	push	ax cx dx
	call	startdmablock
	mov	dx,DSP_status
	in	al,dx
	mov	al,20h
	out	20h,al
	pop	dx cx ax
	iret

	endp
















Startdmablock	proc	near

	mov	al,sounddmachannel	;cx=length
	or	al,4	;bx=offset
	out	0ah,al
	xor	al,al
	out	0ch,al
	
	mov	al,48h
	or	al,sounddmachannel
	out	0bh,al

	mov	cx,soundbufferad
	xor	dh,dh
	mov	dl,sounddmachannel
	shl	dl,1
	mov	al,cl
	out	dx,al
	mov	al,ch
	out	dx,al

	mov	dl,dmaaddress
	mov	al,soundbufferpage	;hardware page
	out	dx,al

	mov	dl,sounddmachannel
	shl	dl,1
	inc	dl
	mov	cx,16384-1
	mov	al,cl
	out	dx,al
	mov	al,ch
	out	dx,al

	mov	al,sounddmachannel
	out	0ah,al	;dmac programmed
	
    	mov	dx,DSP_write
notclear1:	in	al,dx
	or	al,al
	js	notclear1
	mov	al,14h
	out	dx,al
notclear2:	in	al,dx
	or	al,al
	js	notclear2
	mov	al,cl
	out	dx,al
notclear3:	in	al,dx
	or	al,al
	js	notclear3
	mov	al,ch
	out	dx,al

	ret


	endp















SetupPIT	proc	near

	mov	ah,35h
	mov	al,8
	int	21h
	mov	oldint8seg,es		; Save es:bx to temp memory
	mov	oldint8add,bx
	push	cs
	pop	ds
	mov	dx,offset cs:PITinterupt
	mov	ah,25h
	mov	al,8
	int	21h		; Set to new
	
	mov	al,34h
	out	43h,al
	mov	al,0h
	out	40h,al
	mov	al,0dah
	out	40h,al
	ret

	endp






Getridofpit	proc	near

	cmp	oldint8seg,-1
	jz   	noresetPIT
	mov	dx,oldint8add
	mov	ax,oldint8seg		
	mov	ds,ax			
	mov	ah,25h
	mov	al,8
	int	21h
	mov	al,34h
	out	43h,al
	mov	al,0
	out	40h,al
	mov	al,0
	out	40h,al
noresetPIT:	ret

	endp






PITinterupt	proc	near

	cli
	push	ax dx cx
	
	xor	dh,dh
	mov	dl,sounddmachannel
	shl	dl,1
	in	al,dx
	mov	cl,al
	in	al,dx
	mov	ch,al
	sub	cx,soundbufferad
	mov	ax,soundbufferwrite
	sub	ax,cx
	and	ax,3fffh
	sti
	cmp	ax,8192
	jnc	mustgo
	cmp	ax,2048
	jnc	nopitflip
	
mustgo:	push	bx si di es ds
	call	makenextblock
	pop	ds es di si bx

nopitflip:	cli
	mov	al,20h
	out	20h,al
	pop	cx dx ax
	iret

	endp