-
Notifications
You must be signed in to change notification settings - Fork 1
/
PLAYMOD.ASM
2812 lines (2440 loc) · 68.1 KB
/
PLAYMOD.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
; ****************************************************************************
; playmod.asm (for MSDOS)
; ----------------------------------------------------------------------------
; PLAYMOD.COM ! VIA VT8237 MOD PLAYER & VGA DEMO program by Erdogan TAN
;
; 13/02/2017
;
; [ Last Modification: 15/02/2017 ]
;
; Derived from source code of 'PLAY.EXE' (TINYPLAY) by Carlos Hasan (1993)
; PLAY.EXE: PLAY.ASM, MODLOAD.ASM, MODPLAY.ASM, SB.ASM
;
; Modified from 'TINYPLAY.COM' ('tinyplay.asm') source code by Erdogan Tan
; (13/02/2017)
;
; Derived from source code of 'PLAYER.COM' ('PLAYER.ASM') by Erdogan Tan
; (08/12/2016)
; Assembler: NASM 2.11
; ----------------------------------------------------------------------------
; nasm playmod.asm -l playmod.lst -o PLAYMOD.COM
; ****************************************************************************
; Tiny MOD Player v0.1b by Carlos Hasan.
; July 14th, 1993.
;=============================================================================
; PLAYER.ASM
;=============================================================================
; Audio controller, codec & PCI functions are derived from '.wav file player
; for DOS' source code by Jeff Leyda (PLAYER.EXE), Sep 02, 2002 .
[BITS 16]
[org 100h]
Start:
call DetectVT8233 ; Detect the VT8233 Audio Device
GetFileName: ; Parse the Command line...
mov si, 80h
mov bl, [si]
xor bh, bh
inc bx
mov byte [si+bx], 0 ; make AsciiZ filename.
inc si
ScanName:
lodsb
test al, al
je pmsg_2017
cmp al, 20h
je short ScanName ; scan start of name.
mov di, si
dec di
ScanPeriod:
lodsb
cmp al, '.' ; if period NOT found,
je short PrintMesg ; then add a .MOD extension.
test al, al
jnz short ScanPeriod
dec si
SetExt:
;mov byte [si+0], '.'
;mov byte [si+1], 'M'
;mov byte [si+2], 'O'
;mov byte [si+3], 'D'
mov dword [si], '.MOD'
mov byte [si+4], 0
PrintMesg: mov ax, 0900h ; Prints the Credits Text.
;lea dx, [Credits]
mov dx, Credits
int 21h
LoadMod:
; es:di = Filename address
push es
push di
call LoadModule ; Load the MODule...
cmp word [ErrorInfo], 0 ; any error loading?
je short init_codec
mov ax, 0900h ; yes, print error and Exit.
;lea dx, [ErrorMesg]
mov dx, ErrorMesg
int 21h
jmp Exit
init_codec:
; init AC97 codec
_a1:
mov al, VIA_ACLINK_CTRL ; AC link interface control (41h)
call pciRegRead8
mov al, VIA_ACLINK_STAT ; AC Link interface status (40h)
call pciRegRead8
;movzx eax, dl
;test al, VIA_ACLINK_C00_READY ; 1 ; primary codec ready ?
;jnz short _a2
call reset_codec
jnc short _a2 ; EAX = 1
;test al, VIA_ACLINK_C00_READY
;jnz short _a2
_codec_err:
push cs
pop ds
mov dx, CodecErrMsg
mov ah, 9
int 21h
jmp Exit
CodecErrMsg db "Codec Error #"
ErrNo db "0 !", CR,LF,"$"
_a2:
; eax = 1
call codec_io_w16 ; w32
;call detect_codec
call channel_reset
call write_ac97_dev_info
; setup the Codec (actually mixer registers)
call codecConfig ; unmute codec, set rates.
jnc short PlayNow
add al, '0'
mov [ErrNo], al
jmp short _codec_err
PlayNow:
mov ax, BdlBuffer
mov [BDL_BUFFER], ax
mov ax, DmaBuffer ; DmaBuffer (4096 bytes) buff addr
mov [DMA_BUFFER1], ax ; 2048 byte half buffer 1
add ax, BUFFERSIZE ; code/current segment
mov [DMA_BUFFER2], ax ; 2048 byte half buffer 2
mov word [MixSpeed], 22050 ; Mixing at 22.050 kHz
call StartPlaying
mov ax, 0013h ; Set Mode 320x200x256
int 10h
mov cx, 128 ; Make a lookup table
xor bx, bx ; for fastest pixel
mov dx, 320*(100-64) ; addressing.
MakeOfs:
mov [RowOfs+bx], dx
mov [RowOfs+bx+2], dx
add dx, 320
add bx, 4
loop MakeOfs
; Note: Normally IRQ 0 calls the ModPlay Polling at 18.2Hz thru
; the software interrupt 1Ch. If the IRQ 0 is disabled, then
; the INT 1Ch MUST BE CALLED at least MixSpeed/1024 times per
; second, or the module will sound "looped".
; Because we need better sync with the ModPlayer to draw the scope,
; the polling is called from my routine, and then the irq 0 must be
; disabled. The [DmaBuffer] points to the current buffer of 8-bit
; samples played by the Sound Blaster. Note that some samples are
; discarded in the next code, just for fun!
;in al, 21h ; disable irq 0!
;or al, 00000001b
;out 21h, al
call ModPlay ; 13/02/2017
;in al, 21h ; enable irq 0!
;and al, 11111110b
;out 21h, al
mov ax, 0003h ; Set Text Mode 80x25x16
int 10h
call StopPlaying ; STOP!
Exit:
;call FreeModule ; Free MODule core.
error_exit:
mov ax, 4C00h ; Bye!
int 21h
here:
jmp short here
pmsg_2017:
mov ax, 0900h ; Prints the Credits Text.
;lea dx, [msg_2017]
mov dx, msg_2017
int 21h
jmp short Exit
DetectVT8233:
; 13/02/2017
mov eax, (VT8233_DID << 16) + VIA_VID
call pciFindDevice
jnc short _1
; couldn't find the audio device!
push cs
pop ds
mov dx, noDevMsg
mov ah, 9
int 21h
jmp short error_exit
noDevMsg db "Error: Unable to find VIA VT8233 based audio device!",CR,LF,"$"
_1:
; 12/11/2016
; Erdogan Tan - 8/11/2016
; References: Kolibrios - vt823x.asm (2016)
; VIA VT8235 V-Link South Bridge (VT8235-VIA.PDF)(2002)
; lowlevel.eu - AC97 (2016)
; .wav player for DOS by Jeff Leyda (2002) -this file-
; Linux kernel - via82xx.c (2016)
; eax = BUS/DEV/FN
; 00000000BBBBBBBBDDDDDFFF00000000
; edx = DEV/VENDOR
; DDDDDDDDDDDDDDDDVVVVVVVVVVVVVVVV
mov [bus_dev_fn], eax
mov [dev_vendor], edx
; init controller
mov al, PCI_CMD_REG ; command register (04h)
call pciRegRead32
; eax = BUS/DEV/FN/REG
; edx = STATUS/COMMAND
; SSSSSSSSSSSSSSSSCCCCCCCCCCCCCCCC
mov [stats_cmd], edx
mov al, PCI_IO_BASE ; IO base address register (10h)
call pciRegRead32
and dx, 0FFC0h ; IO_ADDR_MASK (0FFFE) ?
mov [ac97_io_base], dx
mov al, AC97_INT_LINE ; Interrupt line register (3Ch)
call pciRegRead32
and edx, 0FFh
mov [ac97_int_ln_reg], dl
; 28/11/2016
mov bx, 1
mov cx, dx
shl bx, cl
;not bx
in al, 0A1h ; irq 8-15
mov ah, al
in al, 21h ; irq 0-7
;and ax, bx ; unmask
btr ax, dx ; unmask
out 21h, al ; enable interrupt (if irq <= 7)
mov al, ah
out 0A1h, al ; enable interrupt (if irq > 7)
;not bx
mov dx, 4D1h ;8259 ELCR1
in al, dx
mov ah, al
mov dx, 4D0h
in al, dx
;or ax, bx
bts ax, cx
mov dx, 4D0h
out dx, al ;set level-triggered mode
mov al, ah
mov dx, 4D1h
out dx, al ;set level-triggered mode
; 24/11/2016 - Erdogan Tan
mov bx, cx
;mov bx, dx
mov bl, [bx+irq_int]
shl bx, 2 ; * 4
; set up interrupt vector
; 30/11/2016
push es
xor ax, ax
mov es, ax
mov word [es:bx], ac97_int_handler
mov ax, cs
mov [es:bx+2], ax
pop es
retn
ac97_int_handler:
push ax
push dx
push cx
push bx
push si
push di
; 15/02/2017
push ds
mov dx, cs
mov ds, dx
; 28/11/2016
cmp byte [uLVI], 1
jnb short _busy
mov byte [uLVI], 1
mov byte [irq_status], 0
mov dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_STATUS
call ctrl_io_r8
test al, VIA_REG_STAT_ACTIVE
jz short _ih0
and al, VIA_REG_STAT_EOL + VIA_REG_STAT_FLAG + VIA_REG_STAT_STOPPED
mov [irq_status], al
jz short _ih0
; 28/11/2016 - Erdogan Tan
call tuneLoop
_ih0:
mov byte [uLVI], 0
_p_i_retn:
mov al, 20h
test byte [ac97_int_ln_reg], 8
jz short _ih_1
out 0A0h, al ; 20h ; EOI
_ih_1:
out 20h, al ; 20h ; EOI
_ih_2:
pop ds ; 15/02/2017
pop di
pop si
pop bx
pop cx
pop dx
pop ax
iret
_busy:
; 28/11/2016 - Erdogan Tan
mov al, [irq_status] ;; ack ;;
mov dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_STATUS
call ctrl_io_w8
jmp short _ih0
;=============================================================================
; PCI.ASM
;=============================================================================
; EQUATES
;constants of stuff that seem hard to remember at times.
TRUE EQU 1
FALSE EQU 0
ENABLED EQU 1
DISABLED EQU 0
BIT0 EQU 1
BIT1 EQU 2
BIT2 EQU 4
BIT3 EQU 8
BIT4 EQU 10h
BIT5 EQU 20h
BIT6 EQU 40h
BIT7 EQU 80h
BIT8 EQU 100h
BIT9 EQU 200h
BIT10 EQU 400h
BIT11 EQU 800h
BIT12 EQU 1000h
BIT13 EQU 2000h
BIT14 EQU 4000h
BIT15 EQU 8000h
BIT16 EQU 10000h
BIT17 EQU 20000h
BIT18 EQU 40000h
BIT19 EQU 80000h
BIT20 EQU 100000h
BIT21 EQU 200000h
BIT22 EQU 400000h
BIT23 EQU 800000h
BIT24 EQU 1000000h
BIT25 EQU 2000000h
BIT26 EQU 4000000h
BIT27 EQU 8000000h
BIT28 EQU 10000000h
BIT29 EQU 20000000h
BIT30 EQU 40000000h
BIT31 EQU 80000000h
;special characters
NUL EQU 0
NULL EQU 0
BELL EQU 07
BS EQU 08
TAB EQU 09
LF EQU 10
CR EQU 13
ESCAPE EQU 27 ;ESC is a reserved word....
;file stuff
READONLY EQU BIT0
HIDDEN EQU BIT1
SYSTEM EQU BIT2
VOLUME EQU BIT3 ;ignored for file access
DIRECTORY EQU BIT4 ;must be 0 for file access
ARCHIVE EQU BIT5
SHAREABLE EQU BIT7 ;for novell networks
OPEN EQU 2 ; open existing file
CREATE EQU 1 ; create new file
; PCI equates
; PCI function address (PFA)
; bit 31 = 1
; bit 23:16 = bus number (0-255)
; bit 15:11 = device number (0-31)
; bit 10:8 = function number (0-7)
; bit 7:0 = register number (0-255)
IO_ADDR_MASK EQU 0FFFEh ; mask off bit 0 for reading BARs
PCI_INDEX_PORT EQU 0CF8h
PCI_DATA_PORT EQU 0CFCh
PCI32 EQU BIT31 ; bitflag to signal 32bit access
PCI16 EQU BIT30 ; bitflag for 16bit access
PCI_FN0 EQU 0 << 8
PCI_FN1 EQU 1 << 8
PCI_FN2 EQU 2 << 8
PCI_FN3 EQU 3 << 8
PCI_FN4 EQU 4 << 8
PCI_FN5 EQU 5 << 8
PCI_FN6 EQU 6 << 8
PCI_FN7 EQU 7 << 8
PCI_CMD_REG EQU 04h ; reg 04, command reg
IO_ENA EQU BIT0 ; i/o decode enable
MEM_ENA EQU BIT1 ; memory decode enable
BM_ENA EQU BIT2 ; bus master enable
; CODE
; PCI device register reader/writers.
; NASM version: Erdogan Tan (29/11/2016)
;===============================================================
; 8/16/32bit PCI reader
;
; Entry: EAX=PCI Bus/Device/fn/register number
; BIT30 set if 32 bit access requested
; BIT29 set if 16 bit access requested
; otherwise defaults to 8bit read
;
; Exit: DL,DX,EDX register data depending on requested read size
;
; Note: this routine is meant to be called via pciRegRead8, pciRegread16,
; or pciRegRead32, listed below.
;
; Note2: don't attempt to read 32bits of data from a non dword aligned reg
; number. Likewise, don't do 16bit reads from non word aligned reg #
;
pciRegRead:
push ebx
push cx
mov ebx, eax ; save eax, dh
mov cl, dh
and eax, (~PCI32)+PCI16 ; clear out data size request
or eax, BIT31 ; make a PCI access request
and al, ~3 ; NOT 3 ; force index to be dword
mov dx, PCI_INDEX_PORT
out dx, eax ; write PCI selector
mov dx, PCI_DATA_PORT
mov al, bl
and al, 3 ; figure out which port to
add dl, al ; read to
in eax, dx ; do 32bit read
test ebx, PCI32
jz short _pregr1
mov edx, eax ; return 32bits of data
_pregr1:
mov dx, ax ; return 16bits of data
test ebx, PCI32+PCI16
jnz short _pregr2
mov dh, cl ; restore dh for 8 bit read
_pregr2:
mov eax, ebx ; restore eax
and eax, (~PCI32)+PCI16 ; clear out data size request
pop cx
pop ebx
retn
pciRegRead8:
and eax, (~PCI16)+PCI32 ; set up 8 bit read size
jmp short pciRegRead ; call generic PCI access
pciRegRead16:
and eax, (~PCI16)+PCI32 ; set up 16 bit read size
or eax, PCI16 ; call generic PCI access
jmp short pciRegRead
pciRegRead32:
and eax, (~PCI16)+PCI32 ; set up 32 bit read size
or eax, PCI32 ; call generic PCI access
jmp short pciRegRead
;===============================================================
; 8/16/32bit PCI writer
;
; Entry: EAX=PCI Bus/Device/fn/register number
; BIT31 set if 32 bit access requested
; BIT30 set if 16 bit access requested
; otherwise defaults to 8bit read
; DL/DX/EDX data to write depending on size
;
;
; note: this routine is meant to be called via pciRegWrite8, pciRegWrite16,
; or pciRegWrite32 as detailed below.
;
; Note2: don't attempt to write 32bits of data from a non dword aligned reg
; number. Likewise, don't do 16bit writes from non word aligned reg #
;
pciRegWrite:
push ebx
push cx
mov ebx, eax ; save eax, dx
mov cx, dx
or eax, BIT31 ; make a PCI access request
and eax, ~PCI16 ; NOT PCI16 ; clear out data size request
and al, ~3 ; NOT 3 ; force index to be dword
mov dx, PCI_INDEX_PORT
out dx, eax ; write PCI selector
mov dx, PCI_DATA_PORT
mov al, bl
and al, 3 ; figure out which port to
add dl, al ; write to
mov eax, edx ; put data into eax
mov ax, cx
out dx, al
test ebx, PCI16+PCI32 ; only 8bit access? bail
jz short _pregw1
out dx, ax ; write 16 bit value
test ebx, PCI16 ; 16bit requested? bail
jnz short _pregw1
out dx, eax ; write full 32bit
_pregw1:
mov eax, ebx ; restore eax
and eax, (~PCI32)+PCI16 ; clear out data size request
mov dx, cx ; restore dx
pop cx
pop ebx
ret
pciRegWrite8:
and eax, (~PCI16)+PCI32 ; set up 8 bit write size
jmp short pciRegWrite ; call generic PCI access
pciRegWrite16:
and eax, (~PCI16)+PCI32 ; set up 16 bit write size
or eax, PCI16 ; call generic PCI access
jmp short pciRegWrite
pciRegWrite32:
and eax, (~PCI16)+PCI32 ; set up 32 bit write size
or eax, PCI32 ; call generic PCI access
jmp short pciRegWrite
;===============================================================
; PCIFindDevice: scan through PCI space looking for a device+vendor ID
;
; Entry: EAX=Device+Vendor ID
;
; Exit: EAX=PCI address if device found
; EDX=Device+Vendor ID
; CY clear if found, set if not found. EAX invalid if CY set.
;
; [old stackless] Destroys: ebx, esi, edi, cl
;
pciFindDevice:
;push cx
push eax
;push esi
;push edi
mov esi, eax ; save off vend+device ID
mov edi, (80000000h - 100h) ; start with bus 0, dev 0 func 0
nextPCIdevice:
add edi, 100h
cmp edi, 80FFF800h ; scanned all devices?
stc
je short PCIScanExit ; not found
mov eax, edi ; read PCI registers
call pciRegRead32
cmp edx, esi ; found device?
jne short nextPCIdevice
clc
PCIScanExit:
pushf
mov eax, BIT31
not eax
and eax, edi ; return only bus/dev/fn #
popf
;pop edi
;pop esi
pop edx
;pop cx
retn
;=============================================================================
; CODEC.ASM
;=============================================================================
; EQUATES
;Codec registers.
;
;Not all codecs are created equal. Refer to the spec for your specific codec.
;
;All registers are 16bits wide. Access to codec registers over the AC97 link
;is defined by the OEM.
;
;Secondary codec's are accessed by ORing in BIT7 of all register accesses.
;
; each codec/mixer register is 16bits
CODEC_RESET_REG equ 00 ; reset codec
CODEC_MASTER_VOL_REG equ 02 ; master volume
CODEC_HP_VOL_REG equ 04 ; headphone volume
CODEC_MASTER_MONO_VOL_REG equ 06 ; master mono volume
CODEC_MASTER_TONE_REG equ 08 ; master tone (R+L)
CODEC_PCBEEP_VOL_REG equ 0ah ; PC beep volume
CODEC_PHONE_VOL_REG equ 0bh ; phone volume
CODEC_MIC_VOL_REG equ 0eh ; MIC volume
CODEC_LINE_IN_VOL_REG equ 10h ; line input volume
CODEC_CD_VOL_REG equ 12h ; CD volume
CODEC_VID_VOL_REG equ 14h ; video volume
CODEC_AUX_VOL_REG equ 16h ; aux volume
CODEC_PCM_OUT_REG equ 18h ; PCM output volume
CODEC_RECORD_SELECT_REG equ 1ah ; record select input
CODEC_RECORD_VOL_REG equ 1ch ; record volume
CODEC_RECORD_MIC_VOL_REG equ 1eh ; record mic volume
CODEC_GP_REG equ 20h ; general purpose
CODEC_3D_CONTROL_REG equ 22h ; 3D control
; 24h is reserved
CODEC_POWER_CTRL_REG equ 26h ; powerdown control
CODEC_EXT_AUDIO_REG equ 28h ; extended audio
CODEC_EXT_AUDIO_CTRL_REG equ 2ah ; extended audio control
CODEC_PCM_FRONT_DACRATE_REG equ 2ch ; PCM out sample rate
CODEC_PCM_SURND_DACRATE_REG equ 2eh ; surround sound sample rate
CODEC_PCM_LFE_DACRATE_REG equ 30h ; LFE sample rate
CODEC_LR_ADCRATE_REG equ 32h ; PCM in sample rate
CODEC_MIC_ADCRATE_REG equ 34h ; mic in sample rate
; registers 36-7a are reserved on the ICH
CODEC_VENDORID1_REG equ 7ch ; codec vendor ID 1
CODEC_VENDORID2_REG equ 7eh ; codec vendor ID 2
; Mixer registers 0 through 51h reside in the ICH and are not forwarded over
; the AC97 link to the codec, which I think is a little weird. Looks like
; the ICH makes it so you don't need a fully functional codec to play audio?
;
; whenever 2 codecs are present in the system, use BIT7 to access the 2nd
; set of registers, ie 80h-feh
PRIMARY_CODEC equ 0 ; 0-7F for primary codec
SECONDARY_CODEC equ BIT7 ; 80-8f registers for 2ndary
SAMPLE_RATE_441khz equ 44100 ; 44.1Khz (cd quality) rate
; each buffer descriptor BAR holds a pointer which has entries to the buffer
; contents of the .WAV file we're going to play. Each entry is 8 bytes long
; (more on that later) and can contain 32 entries total, so each BAR is
; 256 bytes in length, thus:
BDL_SIZE equ 32*8 ; Buffer Descriptor List size
INDEX_MASK equ 31 ; indexes must be 0-31
;
; Buffer Descriptors List
; As stated earlier, each buffer descriptor list is a set of (up to) 32
; descriptors, each 8 bytes in length. Bytes 0-3 of a descriptor entry point
; to a chunk of memory to either play from or record to. Bytes 4-7 of an
; entry describe various control things detailed below.
;
; Buffer pointers must always be aligned on a Dword boundry.
;
;
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.
;
;VIA VT8233 (VT8235) AC97 Codec equates
;(edited by Erdogan Tan, 7/11/2016)
; PCI stuff
VIA_VID equ 1106h ; VIA's PCI vendor ID
VT8233_DID equ 3059h ; VT8233 (VT8235) device ID
PCI_IO_BASE equ 10h
AC97_INT_LINE equ 3Ch
VIA_ACLINK_CTRL equ 41h
VIA_ACLINK_STAT equ 40h
VIA_ACLINK_C00_READY equ 01h ; primary codec ready
VIA_REG_AC97 equ 80h ; dword
VIA_ACLINK_CTRL_ENABLE equ 80h ; 0: disable, 1: enable
VIA_ACLINK_CTRL_RESET equ 40h ; 0: assert, 1: de-assert
VIA_ACLINK_CTRL_SYNC equ 20h ; 0: release SYNC, 1: force SYNC hi
VIA_ACLINK_CTRL_VRA equ 08h ; 0: disable VRA, 1: enable VRA
VIA_ACLINK_CTRL_PCM equ 04h ; 0: disable PCM, 1: enable PCM
VIA_ACLINK_CTRL_INIT equ (VIA_ACLINK_CTRL_ENABLE + \
VIA_ACLINK_CTRL_RESET + \
VIA_ACLINK_CTRL_PCM + \
VIA_ACLINK_CTRL_VRA)
CODEC_AUX_VOL equ 04h
VIA_REG_AC97_BUSY equ 01000000h ;(1<<24)
VIA_REG_AC97_CMD_SHIFT equ 10h ; 16
VIA_REG_AC97_PRIMARY_VALID equ 02000000h ;(1<<25)
VIA_REG_AC97_READ equ 00800000h ;(1<<23)
VIA_REG_AC97_CODEC_ID_SHIFT equ 1Eh ; 30
VIA_REG_AC97_CODEC_ID_PRIMARY equ 0
VIA_REG_AC97_DATA_SHIFT equ 0
VIADEV_PLAYBACK equ 0
VIA_REG_OFFSET_STATUS equ 0 ;; byte - channel status
VIA_REG_OFFSET_CONTROL equ 01h ;; byte - channel control
VIA_REG_CTRL_START equ 80h ;; WO
VIA_REG_CTRL_TERMINATE equ 40h ;; WO
VIA_REG_CTRL_PAUSE equ 08h ;; RW
VIA_REG_CTRL_RESET equ 01h ;; RW - probably reset? undocumented
VIA_REG_OFFSET_STOP_IDX equ 08h ;; dword - stop index, channel type, sample rate
VIA8233_REG_TYPE_16BIT equ 200000h ;; RW
VIA8233_REG_TYPE_STEREO equ 100000h ;; RW
VIA_REG_OFFSET_CURR_INDEX equ 0Fh ;; byte - channel current index (for via8233 only)
VIA_REG_OFFSET_TABLE_PTR equ 04h ;; dword - channel table pointer
VIA_REG_OFFSET_CURR_PTR equ 04h ;; dword - channel current pointer
VIA_REG_OFS_PLAYBACK_VOLUME_L equ 02h ;; byte
VIA_REG_OFS_PLAYBACK_VOLUME_R equ 03h ;; byte
VIA_REG_CTRL_AUTOSTART equ 20h
VIA_REG_CTRL_INT_EOL equ 02h
VIA_REG_CTRL_INT_FLAG equ 01h
VIA_REG_CTRL_INT equ (VIA_REG_CTRL_INT_FLAG + \
VIA_REG_CTRL_INT_EOL + \
VIA_REG_CTRL_AUTOSTART)
; 24/11/2016
VIA_REG_STAT_STOPPED equ 04h ;; RWC
VIA_REG_STAT_EOL equ 02h ;; RWC
VIA_REG_STAT_FLAG equ 01h ;; RWC
VIA_REG_STAT_ACTIVE equ 80h ;; RO
; 28/11/2016
VIA_REG_STAT_LAST equ 40h ;; RO
VIA_REG_STAT_TRIGGER_QUEUED equ 08h ;; RO
VIA_REF_CTRL_INT_STOP equ 04h ; Interrupt on Current Index = Stop Index
; and End of Block
; CODE
; codec configuration code. Not much here really.
; NASM version: Erdogan Tan (29/11/2016)
; enable codec, unmute stuff, set output rate to 44.1
; entry: ax = desired sample rate
;
codecConfig:
; 15/11/2016
; 14/11/2016
; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, 'setup_codec', codec.inc)
; 14/11/2016 - Erdogan Tan
; (Ref: Mpxplay, PDSoft, Attila Padar, SC_VIA82.C)
;;; call channel_reset
mov byte [err_num], 0
mov eax, 0202h
mov edx, CODEC_MASTER_VOL_REG ; 02h ; Line Out
call codec_write
;jc cconfig_error
mov byte [err_num], 1
mov eax, 0202h
mov edx, CODEC_PCM_OUT_REG ; 18h ; Wave Output (Stereo)
call codec_write
;jc cconfig_error
mov byte [err_num], 2
;xor eax, eax
mov eax, 0202h
mov edx, CODEC_AUX_VOL ; 04h ; CODEC_HP_VOL_REG ; HeadPhone
call codec_write
;jc cconfig_error
mov byte [err_num], 3
mov ax, 08h
mov edx, 0Ch ; AC97_PHONE_VOL ; TAD Input (Mono)
call codec_write
;jc short cconfig_error
mov byte [err_num], 4
mov ax, 0808h
mov edx, CODEC_LINE_IN_VOL_REG ; 10h ; Line Input (Stereo)
call codec_write
;jc short cconfig_error
mov byte [err_num], 5
mov ax, 0808h
mov edx, CODEC_CD_VOL_REG ; 12h ; CR Input (Stereo)
call codec_write
;jc short cconfig_error
mov byte [err_num], 6
mov ax, 0808h
mov edx, CODEC_AUX_VOL_REG ; 16h ; Aux Input (Stereo)
call codec_write
;jc short cconfig_error
mov byte [err_num], 7
; Extended Audio Status (2Ah)
mov eax, CODEC_EXT_AUDIO_CTRL_REG ; 2Ah
call codec_read
and eax, 0FFFFh - 2 ; clear DRA (BIT1)
;or eax, 1 ; set VRA (BIT0)
or eax, 5 ; VRA (BIT0) & S/PDIF (BIT2) ; 14/11/2016
mov edx, CODEC_EXT_AUDIO_CTRL_REG
call codec_write
;jc short cconfig_error
mov byte [err_num], 8
set_sample_rate:
xor eax, eax
mov ax, [sample_rate]
mov edx, CODEC_PCM_FRONT_DACRATE_REG ; 2Ch ; PCM Front DAC Rate
;call codec_write
;retn
jmp codec_write
cconfig_error:
mov al, [err_num]
retn
reset_codec:
; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, vt823x.asm)
mov eax, [bus_dev_fn]
mov al, VIA_ACLINK_CTRL
mov dl, VIA_ACLINK_CTRL_ENABLE + VIA_ACLINK_CTRL_RESET + VIA_ACLINK_CTRL_SYNC
call pciRegWrite8
call delay_100ms ; wait 100 ms
_rc_cold:
call cold_reset
jnc short _reset_codec_ok
xor eax, eax ; timeout error
retn
_reset_codec_ok:
xor eax, eax
;mov al, VIA_ACLINK_C00_READY ; 1
inc al
retn
cold_reset:
; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, vt823x.asm)
;mov eax, [bus_dev_fn]
;mov al, VIA_ACLINK_CTRL
xor dl, dl ; 0
call pciRegWrite8
call delay_100ms ; wait 100 ms
;; ACLink on, deassert ACLink reset, VSR, SGD data out
;; note - FM data out has trouble with non VRA codecs !!
;mov eax, [bus_dev_fn]
;mov al, VIA_ACLINK_CTRL
mov dl, VIA_ACLINK_CTRL_INIT
call pciRegWrite8
mov ecx, 16 ; total 2s
_crst_wait:
push ecx
;mov eax, [bus_dev_fn]
mov al, VIA_ACLINK_STAT
call pciRegRead8
test dl, VIA_ACLINK_C00_READY
jnz short _crst_ok
call delay_100ms
pop ecx
dec ecx
jnz short _crst_wait
_crst_fail:
stc
retn
_crst_ok:
pop ecx
; are these necessary ? - 14/11/2016 - Erdogan Tan
;mov eax, [bus_dev_fn]
;mov al, VIA_ACLINK_CTRL
call pciRegRead8
;mov eax, [bus_dev_fn]
;mov al, VIA_ACLINK_STAT
call pciRegRead8
movzx eax, dl
and al, VIA_ACLINK_C00_READY ; 1
jz short _crst_fail
retn
delay_100ms:
; wait 100 ms
mov ecx, 25
_delay_x_ms:
push ecx
call delay1_4ms
pop ecx
loop _delay_x_ms
retn
codec_io_w16: ;w32
mov dx, [ac97_io_base]