-
Notifications
You must be signed in to change notification settings - Fork 1
/
ich_wav3.asm
4242 lines (3839 loc) · 91.7 KB
/
ich_wav3.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
; 19/05/2024
; 18/11/2023
; 17/11/2023
; 16/11/2023
; 15/11/2023
; 14/11/2023
; 13/11/2023
; 03/11/2023 - 11/11/2023
; DOS based .WAV player using AC'97 and codec interface.
; ---------------------------------------------------------------
; NASM version: Erdogan Tan (29/11/2016)
; Last Update: 17/02/2017 (by Erdogan Tan)
; AC97 interrupt version - 09/11/2023 - Erdogan Tan
; sample rate conversion version - 13/11/2023 - Erdogan Tan
; ICHWAV.ASM
; player internal variables and other equates.
BUFFERSIZE equ 64 * 1024 ; 64k file buffer size.
ENDOFFILE equ BIT0 ; flag for knowing end of file
;===========================================================================
; entry: none. File is already open and [filehandle] filled.
; exit: not until the song is finished or the user aborts.
;
playWav:
; 13/11/2023
cmp byte [VRA], 1
jb short chk_sample_rate
playwav_48_khz:
mov word [loadfromwavfile], loadFromFile
;mov word [loadsize], 0 ; 65536
mov word [buffersize], 32768 ; samples
jmp playWav_vra
chk_sample_rate:
; set conversion parameters
; (for 8, 11.025, 16, 22.050, 24, 32 kHZ)
mov ax, [sample_rate]
cmp ax, 48000
je short playwav_48_khz
chk_22khz:
cmp ax, 22050
jne short chk_11khz
cmp byte [bps], 8
jna short chk_22khz_1
mov bx, load_22khz_stereo_16_bit
cmp byte [stmo], 1
jne short chk_22khz_2
mov bx, load_22khz_mono_16_bit
jmp short chk_22khz_2
chk_22khz_1:
mov bx, load_22khz_stereo_8_bit
cmp byte [stmo], 1
jne short chk_22khz_2
mov bx, load_22khz_mono_8_bit
chk_22khz_2:
mov ax, 7514 ; (442*17)
mov dx, 37
mov cx, 17
jmp set_sizes
chk_11khz:
cmp ax, 11025
jne short chk_44khz
cmp byte [bps], 8
jna short chk_11khz_1
mov bx, load_11khz_stereo_16_bit
cmp byte [stmo], 1
jne short chk_11khz_2
mov bx, load_11khz_mono_16_bit
jmp short chk_11khz_2
chk_11khz_1:
mov bx, load_11khz_stereo_8_bit
cmp byte [stmo], 1
jne short chk_11khz_2
mov bx, load_11khz_mono_8_bit
chk_11khz_2:
mov ax, 3757 ; (221*17)
mov dx, 74
mov cx, 17
jmp set_sizes
chk_44khz:
cmp ax, 44100
jne short chk_16khz
cmp byte [bps], 8
jna short chk_44khz_1
mov bx, load_44khz_stereo_16_bit
cmp byte [stmo], 1
jne short chk_44khz_2
mov bx, load_44khz_mono_16_bit
jmp short chk_44khz_2
chk_44khz_1:
mov bx, load_44khz_stereo_8_bit
cmp byte [stmo], 1
jne short chk_44khz_2
mov bx, load_44khz_mono_8_bit
chk_44khz_2:
;mov ax, 15065 ; (655*23)
; 18/11/2023 ((file size + bss + stack) <= 64KB)
mov ax, 14076 ; (612 *23)
mov dx, 25
mov cx, 23
jmp set_sizes
chk_16khz:
cmp ax, 16000
jne short chk_8khz
cmp byte [bps], 8
jna short chk_16khz_1
mov bx, load_16khz_stereo_16_bit
cmp byte [stmo], 1
jne short chk_16khz_2
mov bx, load_16khz_mono_16_bit
jmp short chk_16khz_2
chk_16khz_1:
mov bx, load_16khz_stereo_8_bit
cmp byte [stmo], 1
jne short chk_16khz_2
mov bx, load_16khz_mono_8_bit
chk_16khz_2:
mov ax, 5461
mov dx, 3
mov cx, 1
jmp set_sizes
chk_8khz:
cmp ax, 8000
jne short chk_24khz
cmp byte [bps], 8
jna short chk_8khz_1
mov bx, load_8khz_stereo_16_bit
cmp byte [stmo], 1
jne short chk_8khz_2
mov bx, load_8khz_mono_16_bit
jmp short chk_8khz_2
chk_8khz_1:
mov bx, load_8khz_stereo_8_bit
cmp byte [stmo], 1
jne short chk_8khz_2
mov bx, load_8khz_mono_8_bit
chk_8khz_2:
mov ax, 2730
mov dx, 6
mov cx, 1
jmp short set_sizes
chk_24khz:
cmp ax, 24000
jne short chk_32khz
cmp byte [bps], 8
jna short chk_24khz_1
mov bx, load_24khz_stereo_16_bit
cmp byte [stmo], 1
jne short chk_24khz_2
mov bx, load_24khz_mono_16_bit
jmp short chk_24khz_2
chk_24khz_1:
mov bx, load_24khz_stereo_8_bit
cmp byte [stmo], 1
jne short chk_24khz_2
mov bx, load_24khz_mono_8_bit
chk_24khz_2:
mov ax, 8192
mov dx, 2
mov cx, 1
jmp short set_sizes
chk_32khz:
cmp ax, 32000
jne short vra_needed
cmp byte [bps], 8
jna short chk_32khz_1
mov bx, load_32khz_stereo_16_bit
cmp byte [stmo], 1
jne short chk_32khz_2
mov bx, load_32khz_mono_16_bit
jmp short chk_32khz_2
chk_32khz_1:
mov bx, load_32khz_stereo_8_bit
cmp byte [stmo], 1
jne short chk_32khz_2
mov bx, load_32khz_mono_8_bit
chk_32khz_2:
mov ax, 10922
mov dx, 3
mov cx, 2
;jmp short set_sizes
set_sizes:
cmp byte [stmo], 1
je short ss_1
shl ax, 1
ss_1:
cmp byte [bps], 8
jna short ss_2
; 16 bit samples
shl ax, 1
ss_2:
mov [loadsize], ax
mul dx
;cmp cx, 1
;je short ss_3
;ss_3:
div cx
mov cl, [fbs_shift]
shl ax, cl
shr ax, 1 ; buffer size is 16 bit sample count
mov [buffersize], ax
mov [loadfromwavfile], bx
jmp short playWav_vra
vra_needed:
; 13/11/2023
pop ax ; discard return address to the caller
mov dx, msg_no_vra
jmp vra_err
; 13/11/2023
loadfromwavfile:
dw loadFromFile
loadsize: ; read from wav file
dw 0
buffersize: ; write to DMA buffer
dd 32768 ; 16 bit samples (not bytes)
playWav_vra:
; 07/11/2023
; clear buffer 2
;push es
;mov ax, [WAV_BUFFER2]
;mov es, ax
;sub ax, ax
;mov di, ax ; 17/02/2017
;mov cx, (BUFFERSIZE/2)
;rep stosw
;pop es
; load 64k into buffer 1
mov ax, [WAV_BUFFER1]
;call loadFromFile
; 13/11/2023
call word [loadfromwavfile]
; and 64k into buffer 2
mov ax, [WAV_BUFFER2]
;call loadFromFile
; 13/11/2023
call word [loadfromwavfile]
; register reset the DMA engine. This may cause a pop noise on the output
; lines when the device is reset. Prolly a better idea to mute output, then
; reset.
; 11/11/2023
;mov dx, [NABMBAR]
;add dx, PO_CR_REG ; set pointer to Ctrl reg
;mov al, RR ; set reset
;out dx, al ; self clearing bit
; write last valid index to 31 to start with.
; The Last Valid Index register tells the DMA engine when to stop playing.
;
; As we progress through the song we change the last valid index to always be
; something other than the index we're currently playing.
;
; 08/11/2023
;; 07/11/2023
;mov al, 1
;;mov al, 31
;call setLastValidIndex
; create Buffer Descriptor List
;
; A buffer descriptor list is a list of pointers and control bits that the
; DMA engine uses to know where to get the .wav data and how to play it.
;
; I set it up to use only 2 buffers of .wav data, and whenever 1 buffer is
; playing, I refresh the other one with good data.
;
;
; For the control bits, you can specify that the DMA engine fire an interrupt
; after a buffer has been processed, but I poll the current index register
; to know when it's safe to update the other buffer.
;
; I set the BUP bit, which tells the DMA engine to just play 0's (silence)
; if it ever runs out of data to play. Good for safety.
;
push es
mov ax, [BDL_BUFFER] ; get segment # for BDL
mov es, ax
mov cx, 32 / 2 ; make 32 entries in BDL
xor di, di
_0:
; set buffer descriptor 0 to start of data file in memory
movzx eax, word [WAV_BUFFER1]
shl eax, 4 ; convert seg:off ->0:offset
stosd ; store pointer to wavbuffer1
;
; set length to 32k samples. 1 sample is 16bits or 2bytes.
; Set control (bits 31:16) to BUP, bits 15:0=number of samples.
;
; 17/02/2017 (Erdogan Tan)
; Intel® 82801AA (ICH) & Intel® 82801AB (ICH0) I/O Controller Hub AC’97
; Programmer’s Reference Manual
; 2.2.1 Buffer Descriptor List (on Page 13)
;
; Generic Form of Buffer Descriptor
; ---------------------------------
; 63 62 61-48 47-32 31-0
; --- --- -------- ------- -----
; IOC BUP -reserved- Buffer Buffer
; Length Pointer
; [15:0] [31:0]
;
; IOC: Interrupt On Completion.
; 1 = Enabled.
; When this is set, it means that the controller should
; issue an interrupt upon completion of this buffer.
; It should also set the IOC bit in the status register
; 0 = Disabled
;
; BUP: Buffer Underrun Policy.
; 0 = When this buffer is complete,
; if the next buffer is not yet ready
; (i.e., the last valid buffer has been processed),
; then continue to transmit the last valid sample.
; 1 = When this buffer is complete,
; if this is the last valid buffer, transmit zeros after
; this buffer has been processed completely.
; This bit typically is set only if this is the last
; buffer in the current stream.
;
; [31:0]: Buffer pointer. This field points to the location of
; the data buffer. Since samples can be as wide as one
; word, the buffer must be aligned with word boundaries,
; to prevent samples from straddling DWord boundaries.
;
; [15:0]: Buffer Length: This is the length of the data buffer,
; in number of samples. The controller uses this data
; to determine the length of the buffer, in bytes.
; "0" indicates no sample to process.
; ICH2AC97.INC
; IOC equ BIT31 ; Fire an interrupt whenever this
; buffer is complete.
; BUP equ BIT30 ; Buffer Underrun Policy.
; if this buffer is the last buffer
; in a playback, fill the remaining
; samples with 0 (silence) or not.
; It's a good idea to set this to 1
; for the last buffer in playback,
; otherwise you're likely to get a lot
; of noise at the end of the sound.
;
; Bits 15:0 contain the length of the buffer, in number of samples, which
; are 16 bits each, coupled in left and right pairs, or 32bits each.
; Luckily for us, that's the same format as .wav files.
;
; A value of FFFF is 65536 samples. Running at 44.1Khz, that's just about
; 1.5 seconds of sample time. FFFF * 32bits is 1FFFFh bytes or 128k of data.
;
; A value of 0 in these bits means play no samples.
;
; ICHWAV.ASM
; 19/05/2024
;mov eax, BUFFERSIZE / 2 ; size of buffer (32K) in (16bit) words
; 13/11/2023
mov eax, [buffersize]
; 09/11/2023
or eax, IOC + BUP
; 06/11/2023
;or eax, BUP
stosd
; 2nd buffer:
movzx eax, word [WAV_BUFFER2]
shl eax, 4 ; convert seg:off ->0:offset
stosd ; store pointer to wavbuffer2
; set length to 64k (32k of two 16 bit samples)
; Set control (bits 31:16) to BUP, bits 15:0=number of samples
;
;mov eax, BUFFERSIZE / 2
; 13/11/2023
mov eax, [buffersize]
; 09/11/2023
or eax, IOC + BUP
; 06/11/2023
;or eax, BUP
stosd
loop _0
pop es
;
; tell the DMA engine where to find our list of Buffer Descriptors.
; this 32bit value is a flat mode memory offset (ie no segment:offset)
;
; write NABMBAR+10h with offset of buffer descriptor list
;
movzx eax, word [BDL_BUFFER]
shl eax, 4 ; convert seg:off to 0:off
mov dx, [NABMBAR]
add dx, PO_BDBAR_REG ; set pointer to BDL
out dx, eax ; write to AC97 controller
; 19/05/2024
call delay1_4ms
;
; All set. Let's play some music.
;
; 10/11/2023
; 08/11/2023
; 07/11/2023
; 08/12/2016
; 07/10/2016
;mov al, 1
mov al, 31
mov [LVI], al ; 10/11/2023
call setLastValidIndex
; 06/11/2023 (not neccessary)
;; 05/11/2023
;; reset current index
;mov al, 0
;call setCurrentIndex
; 06/11/2023
;mov byte [tLoop], 1 ; 30/11/2016
; 19/05/2024
call delay1_4ms
; 17/02/2017
mov dx, [NABMBAR]
add dx, PO_CR_REG ; PCM out Control Register
; 09/11/2023
mov al, IOCE + RPBM ; Enable 'Interrupt On Completion' + run
; ; (LVBI interrupt will not be enabled)
; 06/11/2023 (TUNELOOP version, without interrupt)
;mov al, RPBM
out dx, al ; Start bus master operation.
; 09/11/2023
mov byte [b_indicator], '?'
; 19/05/2024
; 06/11/2023
call delay1_4ms
call delay1_4ms
call delay1_4ms
call delay1_4ms
; 10/11/2023
;mov byte [tloop], 1
; while DMA engine is running, examine current index and wait until it hits 1
; as soon as it's 1, we need to refresh the data in wavbuffer1 with another
; 64k. Likewise when it's playing buffer 2, refresh buffer 1 and repeat.
; 09/11/2023
; 06/11/2023
%if 1
; 08/12/2016
; 28/11/2016
p_loop:
call check4keyboardstop ; keyboard halt?
jnc short r_loop ; no ; 09/11/2023
; 09/11/2023
;or byte [flags], ENDOFFILE
;mov byte [b_indicator], '0'
x_loop:
mov al, '0' ; 19/05/2024
q_loop:
;mov al, '0'
call tL0
; 04/11/2023
; finished with song, stop everything
call ac97_stop
; 11/11/2023
irq_restore:
; restore previous interrupt vector and interrupt_status
cli
in al, 0A1h ; irq 8-15
mov al, [IRQ_status+1]
out 0A1h, al
in al, 021h ; irq 0-7
mov al, [IRQ_status]
out 21h, al
; ...
push es
xor ax, ax
mov es, ax
mov ax, [IRQ_vector]
mov [es:bx], ax
mov ax, [IRQ_vector+2]
mov [es:bx+2], ax
pop es
sti
retn
r_loop:
; 19/05/2024
; 10/11/2023
nop
nop
nop
; 11/11/2023
mov al, '0'
cmp byte [tloop], 0
je short p_loop
jl short q_loop
dec byte [tloop] ; 0
cmp byte [b_indicator], '1'
jne short s_loop
; load buffer 1
mov ax, [WAV_BUFFER1]
u_loop:
;call loadFromFile
; 13/11/2023
call [loadfromwavfile]
;jnc short v_loop
;mov al, '0'
;jmp short q_loop
; 19/05/2024
jc short x_loop
v_loop:
; 09/11/2023 - Erdogan Tan
; (print buffer number on top left of the screen)
mov al, [b_indicator]
call tL0
jmp short p_loop
s_loop:
; load buffer 2
mov ax, [WAV_BUFFER2]
jmp short u_loop
%endif
; while DMA engine is running, examine current index and wait until it hits 1
; as soon as it's 1, we need to refresh the data in wavbuffer1 with another
; 64k. Likewise when it's playing buffer 2, refresh buffer 1 and repeat.
; 11/11/2023
; 10/11/2023
; 09/11/2023
; 08/11/2023
; 07/11/2023
%if 1
tuneLoop:
; 11/11/2023
; 10/11/2023
; 09/11/2023
; 08/11/2023
; 06/11/2023
;mov al, '1'
;call tL0
tL1:
; 10/11/2023
;mov byte [tloop], 1
; 11/11/2023
;call updateLVI ; set LVI != CIV
;jz short tL4
; 11/11/2023
mov byte [tloop], 1
call getCurrentIndex
; 10/11/2023
cmp al, [LVI]
jne short tL2
test byte [flags], ENDOFFILE
;jnz short tL2
jnz short tL4
dec ax
and al, 1Fh
call setLastValidIndex
xchg al, [LVI]
tL2:
test al, BIT0
jz short tL3
mov byte [b_indicator], '1'
retn
tL3:
mov byte [b_indicator], '2'
retn
tL4:
; 11/11/2023
mov byte [tloop], -1
stc
retn
; 06/11/2023
tL0:
; 08/11/2023
; 05/11/2023
; 17/02/2017 - Buffer switch test (temporary)
; 06/11/2023
; al = buffer indicator ('1', '2' or '0' -stop- )
push ds
;push bx
mov bx, 0B800h ; video display page segment
mov ds, bx
sub bx, bx ; 0
mov ah, 4Eh
mov [bx], ax ; show current play buffer (1, 2)
;pop bx
pop ds
retn
%endif
; 08/11/2023
; 07/11/2023
%if 0
tuneLoop:
; 07/11/2023
;mov dx, NABMBAR
;add dx, PO_CIV_REG ; 14h
tL1:
call updateLVI ; set LVI != CIV
call check4keyboardstop
jc short _exit_
call getCurrentIndex
test al, BIT0
jz short tL1 ; loop if buffer 2 is not playing
; load buffer 1
mov ax, [WAV_BUFFER1]
call loadFromFile
jc short _exit_ ; end of file
tL2:
call updateLVI
call check4keyboardstop
jc short _exit_
call getCurrentIndex
test al, BIT0
jnz short tL2 ; loop if buffer 1 is not playing
; load buffer 2
mov ax, [WAV_BUFFER2]
call loadFromFile
jnc short tuneLoop
_exit_:
mov dx, [NABMBAR]
add dx, PO_CR_REG ; PCM out Control Register
mov al, 0
out dx, al ; stop player
retn
%endif
; load data from file in 32k chunks. Would be nice to load 1 64k chunk,
; but in DOS you can only load FFFF bytes at a time.
;
; entry: ax = segment to load data to
; exit: CY set if end of file reached.
; note: last file buffer is padded with 0's to avoid pops at the end of song.
; assumes file is already open. uses [filehandle]
; 07/11/2023
%if 0
loadFromFile:
; 07/11/2023
; 06/11/2023
; 17/02/2017
;push ax
;push cx
;push dx
;push bx
;push es
;push ds
test byte [flags], ENDOFFILE ; have we already read the
;stc ; last of the file?
; 05/11/2023
;jnz endLFF
jz short lff_12 ; 06/11/2023
; 06/11/2023
;;mov byte [tLoop], 0
;jmp endLFF
stc
retn
lff_12: ; 06/11/2023
mov [fbs_seg], ax ; save buffer segment
xor dx, dx
lff_0:
mov [fbs_off], dx ; buffer offset
mov bx, (BUFFERSIZE / 2) ; 32k chunk
mov cl, [fbs_shift]
and cl, cl
jz short lff_1 ; stereo, 16 bit
; fbs_shift =
; 2 for mono and 8 bit sample (multiplier = 4)
; 1 for mono or 8 bit sample (multiplier = 2)
shr bx, cl ; 32K / multiplier
mov ax, cs
mov dx, temp_buffer ; temporary buffer for wav data
lff_1:
; 17/02/2017 (stereo/mono, 8bit/16bit corrections)
; load file into memory
mov cx, bx
mov bx, [filehandle]
mov ds, ax
mov ah, 3fh
int 21h
mov bx, cs
mov ds, bx
jc short lff_9 ; error !
and ax, ax
jz short lff_10
mov bl, [fbs_shift] ; shift count
or bl, bl
jz short lff_7 ; 16 bit stereo samples
push es
; 06/11/2023
;push di
;push si
mov di, [fbs_off]
mov si, [fbs_seg] ; buffer segment
mov es, si
mov si, temp_buffer ; temporary buffer address
mov cl, [bps] ; bits per sample (8 or 16)
cmp cl, 8
jne short lff_4 ; 16 bit samples
; 8 bit samples
mov cx, ax ; byte count
dec bl ; shift count, 1 = stereo, 2 = mono
jz short lff_3 ; 8 bit, stereo
lff_2:
; mono & 8 bit
lodsb
shl ax, 8 ; convert 8 bit sample to 16 bit sample
stosw ; left channel
stosw ; right channel
loop lff_2
jmp short lff_6
lff_3:
; stereo & 8 bit
lodsb
shl ax, 8 ; convert 8 bit sample to 16 bit sample
stosw
loop lff_3
jmp short lff_6
lff_4:
; 16 bit mono samples
mov cx, ax ; word count
lff_5:
lodsw
stosw ; left channel
stosw ; right channel
loop lff_5
lff_6:
mov ax, di ; save next buffer offset/position
; 06/11/2023
;pop si
;pop di
pop es
;lff_7:
; 06/11/2023
and ax, ax
jz short endLFF ; end of 2nd half
mov cx, (BUFFERSIZE / 2)
lff_7:
cmp ax, cx
je short endLFF ; 06/11/2023
;jb short lff_11
;xor cx, cx
;sub cx, ax
;neg cx
jmp short lff_11
lff_8:
; 07/11/2023
cmp word [fbs_off], (BUFFERSIZE / 2) ; 32768
jnb short endLFF
mov dx, ax ; buffer offset
mov ax, [fbs_seg] ; buffer segment
jmp lff_0
lff_9:
; 07/11/2023
;; 06/11/2023 (temporary)
;mov al, '!' ; error
;call tL0
xor ax, ax
lff_10:
; 07/11/2023
;mov cx, (BUFFERSIZE / 2)
lff_11:
call padfill ; blank pad the remainder
;clc ; don't exit with CY yet.
or byte [flags], ENDOFFILE ; end of file flag
endLFF:
;pop ds
;pop es
; 06/11/2023
;pop bx
;pop dx
;pop cx
;pop ax
retn
%endif
; 08/11/2023
; 07/11/2023
%if 1
loadFromFile:
; 07/11/2023
test byte [flags], ENDOFFILE ; have we already read the
; last of the file?
jz short lff_0 ; no
stc
retn
lff_0:
; 08/11/2023
mov bp, ax ; save buffer segment
mov cl, [fbs_shift]
and cl, cl
jz short lff_1 ; stereo, 16 bit
mov di, BUFFERSIZE - 1 ; 65535
; fbs_shift =
; 2 for mono and 8 bit sample (multiplier = 4)
; 1 for mono or 8 bit sample (multiplier = 2)
shr di, cl
inc di ; 16384 for 8 bit and mono
; 32768 for 8 bit or mono
mov ax, cs
mov dx, temp_buffer ; temporary buffer for wav data
; 17/02/2017 (stereo/mono, 8bit/16bit corrections)
; load file into memory
mov cx, di
mov bx, [filehandle]
mov ds, ax
mov ah, 3Fh
int 21h
mov bx, cs
mov ds, bx
jc short lff_4 ; error !
; 08/11/2023
xor dx, dx ; 0
and ax, ax
jz short lff_3
mov bl, [fbs_shift]
push es
mov di, dx ; 0 ; [fbs_off]
;mov bp, [fbs_seg] ; buffer segment
mov es, bp
mov si, temp_buffer ; temporary buffer address
mov cx, ax ; byte count
cmp byte [bps], 8 ; bits per sample (8 or 16)
jne short lff_7 ; 16 bit samples
; 8 bit samples
dec bl ; shift count, 1 = stereo, 2 = mono
jz short lff_6 ; 8 bit, stereo
lff_5:
; mono & 8 bit
lodsb
sub al, 80h ; 08/11/2023
shl ax, 8 ; convert 8 bit sample to 16 bit sample
stosw ; left channel
stosw ; right channel
loop lff_5
jmp short lff_9
lff_6:
; stereo & 8 bit
lodsb
sub al, 80h ; 08/11/2023
shl ax, 8 ; convert 8 bit sample to 16 bit sample
stosw
loop lff_6
jmp short lff_9
lff_7:
shr cx, 1 ; word count
lff_8:
lodsw
stosw ; left channel
stosw ; right channel
loop lff_8
lff_9:
pop es
or di, di
jz short endLFF ; 64KB ok
mov ax, di ; [fbs_off]
dec ax
mov cx, BUFFERSIZE - 1 ; 65535
jmp short lff_3
lff_4:
; 08/11/2023
mov al, '!' ; error
call tL0
xor ax, ax
jmp short lff_3
lff_1:
;mov bp, ax ; save buffer segment
xor dx, dx
; load file into memory
mov cx, (BUFFERSIZE / 2) ; 32k chunk
mov bx, [filehandle]
mov ds, ax
mov ah, 3Fh
int 21h
mov di, cs
mov ds, di
; 07/11/2023
jc short lff_4 ; error !
cmp ax, cx
jne short lff_3
lff_2:
; 08/11/2023
add dx, ax
;mov cx, (BUFFERSIZE / 2) ; 32k chunk
;mov bx, [filehandle]
mov ds, bp
mov ah, 3Fh
int 21h
;mov di, cs
mov ds, di
jc short lff_4 ; error !
cmp ax, cx
je short endLFF
lff_3:
call padfill ; blank pad the remainder
;clc ; don't exit with CY yet.
or byte [flags], ENDOFFILE ; end of file flag
endLFF:
retn
%endif
; entry ds:ax points to last byte in file
; cx=target size
; note: must do byte size fill
; destroys bx, cx
;
padfill:
; 07/11/2023
; 06/11/2023