     1                                  ; ****************************************************************************
     2                                  ; wavplay.s (for TRDOS 386)
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; WAVPLAY.PRG ! VIA VT8237R (VT8233) .WAV PLAYER program by Erdogan TAN
     5                                  ;
     6                                  ; 17/03/2017
     7                                  ;
     8                                  ; [ Last Modification: 25/08/2020 ]
     9                                  ;
    10                                  ; Modified from PLAYWAV.PRG .wav player program by Erdogan Tan, 10/03/2017 
    11                                  ;
    12                                  ; Derived from source code of 'PLAYER.COM' ('PLAYER.ASM') by Erdogan Tan
    13                                  ;	      (18/02/2017) 
    14                                  ; Assembler: NASM version 2.11
    15                                  ;	     nasm wavplay.asm -l wavplay.txt -o WAVPLAY.PRG	
    16                                  ; ----------------------------------------------------------------------------
    17                                  ; Derived from '.wav file player for DOS' Jeff Leyda, Sep 02, 2002
    18                                  
    19                                  ; 01/03/2017
    20                                  ; 16/10/2016
    21                                  ; 29/04/2016
    22                                  ; TRDOS 386 system calls (temporary list!)
    23                                  _ver 	equ 0
    24                                  _exit 	equ 1
    25                                  _fork 	equ 2
    26                                  _read 	equ 3
    27                                  _write	equ 4
    28                                  _open	equ 5
    29                                  _close 	equ 6
    30                                  _wait 	equ 7
    31                                  _creat 	equ 8
    32                                  _link 	equ 9
    33                                  _unlink	equ 10
    34                                  _exec	equ 11
    35                                  _chdir	equ 12
    36                                  _time 	equ 13
    37                                  _mkdir 	equ 14
    38                                  _chmod	equ 15
    39                                  _chown	equ 16
    40                                  _break	equ 17
    41                                  _stat	equ 18
    42                                  _seek	equ 19
    43                                  _tell 	equ 20
    44                                  _mount	equ 21
    45                                  _umount	equ 22
    46                                  _setuid	equ 23
    47                                  _getuid	equ 24
    48                                  _stime	equ 25
    49                                  _quit	equ 26	
    50                                  _intr	equ 27
    51                                  _fstat	equ 28
    52                                  _emt 	equ 29
    53                                  _mdate 	equ 30
    54                                  _video 	equ 31
    55                                  _audio	equ 32
    56                                  _timer	equ 33
    57                                  _sleep	equ 34
    58                                  _msg    equ 35
    59                                  _geterr	equ 36
    60                                  _fpsave	equ 37
    61                                  _pri	equ 38
    62                                  _rele	equ 39
    63                                  _fff	equ 40
    64                                  _fnf	equ 41
    65                                  _alloc	equ 42
    66                                  _dalloc equ 43
    67                                  _calbac equ 44
    68                                  
    69                                  %macro sys 1-4
    70                                      ; 29/04/2016 - TRDOS 386 (TRDOS v2.0)	
    71                                      ; 03/09/2015	
    72                                      ; 13/04/2015
    73                                      ; Retro UNIX 386 v1 system call.	
    74                                      %if %0 >= 2   
    75                                          mov ebx, %2
    76                                          %if %0 >= 3    
    77                                              mov ecx, %3
    78                                              %if %0 = 4
    79                                                 mov edx, %4   
    80                                              %endif
    81                                          %endif
    82                                      %endif
    83                                      mov eax, %1
    84                                      ;int 30h
    85                                      int 40h ; TRDOS 386 (TRDOS v2.0)	   
    86                                  %endmacro
    87                                  
    88                                  ; TRDOS 386 (and Retro UNIX 386 v1) system call format:
    89                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    90                                  
    91                                  
    92                                  [BITS 32]
    93                                  
    94                                  [ORG 0] 
    95                                  
    96                                  _STARTUP:
    97                                  	; Prints the Credits Text.
    98                                  	sys	_msg, Credits, 255, 0Bh
    98                              <1> 
    98                              <1> 
    98                              <1> 
    98                              <1> 
    98                              <1>  %if %0 >= 2
    98 00000000 BB[370A0000]        <1>  mov ebx, %2
    98                              <1>  %if %0 >= 3
    98 00000005 B9FF000000          <1>  mov ecx, %3
    98                              <1>  %if %0 = 4
    98 0000000A BA0B000000          <1>  mov edx, %4
    98                              <1>  %endif
    98                              <1>  %endif
    98                              <1>  %endif
    98 0000000F B823000000          <1>  mov eax, %1
    98                              <1> 
    98 00000014 CD40                <1>  int 40h
    99                                  
   100                                  	; clear bss
   101 00000016 B9[00000200]            	mov	ecx, EOF
   102 0000001B BF[A10B0000]            	mov	edi, bss_start
   103 00000020 29F9                    	sub	ecx, edi
   104 00000022 D1E9                    	shr	ecx, 1
   105 00000024 31C0                    	xor	eax, eax
   106 00000026 F366AB                  	rep	stosw
   107                                  
   108 00000029 E8CB010000              	call    DetectVT8233	; Detect the VT8233 Audio Device
   109                                  GetFileName:  
   110 0000002E 89E6                    	mov	esi, esp
   111 00000030 AD                      	lodsd
   112 00000031 83F802                  	cmp	eax, 2 ; two arguments 
   113                                  	       ; (program file name & mod file name)
   114 00000034 0F828F010000            	jb	pmsg_usage ; nothing to do
   115                                  
   116 0000003A AD                      	lodsd ; program file name address 
   117 0000003B AD                      	lodsd ; mod file name address (file to be read)
   118 0000003C 89C6                    	mov	esi, eax
   119 0000003E BF[D80B0000]            	mov	edi, wav_file_name
   120                                  ScanName:       
   121 00000043 AC                      	lodsb
   122 00000044 84C0                    	test	al, al
   123 00000046 0F847D010000            	je	pmsg_usage
   124 0000004C 3C20                    	cmp	al, 20h
   125 0000004E 74F3                    	je	short ScanName	; scan start of name.
   126 00000050 AA                      	stosb
   127 00000051 B4FF                    	mov	ah, 0FFh
   128                                  a_0:	
   129 00000053 FEC4                    	inc	ah
   130                                  a_1:
   131 00000055 AC                      	lodsb
   132 00000056 AA                      	stosb
   133 00000057 3C2E                    	cmp	al, '.'
   134 00000059 74F8                    	je	short a_0	
   135 0000005B 20C0                    	and	al, al
   136 0000005D 75F6                    	jnz	short a_1
   137                                  
   138 0000005F 08E4                    	or	ah, ah		 ; if period NOT found,
   139 00000061 750B                    	jnz	short init_codec ; then add a .WAV extension.
   140                                  SetExt:
   141 00000063 4F                      	dec	edi
   142 00000064 C7072E574156            	mov	dword [edi], '.WAV'
   143 0000006A C6470400                	mov	byte [edi+4], 0
   144                                  
   145                                  init_codec:
   146                                  	; init AC97 codec
   147                                  
   148                                  	; 19/06/2017
   149                                  	; 05/06/2017
   150                                  	; 19/03/2017
   151 0000006E A1[AC0B0000]            	mov	eax, [bus_dev_fn]
   152 00000073 B041                    	mov	al, VIA_ACLINK_CTRL  ; AC link interface control (41h)
   153 00000075 E8D1020000              	call	pciRegRead8
   154                                  
   155                                  	;mov	eax, [bus_dev_fn]
   156 0000007A B040                    	mov	al, VIA_ACLINK_STAT  ; AC Link interface status (40h)
   157 0000007C E8CA020000              	call	pciRegRead8
   158                                  	
   159 00000081 0FB6C2                  	movzx	eax, dl
   160 00000084 2401                    	and	al, VIA_ACLINK_C00_READY  ; 1 ; primary codec ready ?
   161 00000086 7522                    	jnz	short a_2
   162                                  
   163 00000088 E8EE030000              	call	reset_codec
   164 0000008D 731B                    	jnc	short a_2 ; EAX = 1
   165                                  
   166                                  	;test	al, VIA_ACLINK_C00_READY 	
   167                                          ;jnz     short a_2
   168                                  
   169                                  _codec_err:
   170                                  	sys	_msg, CodecErrMsg, 255, 0Fh
   170                              <1> 
   170                              <1> 
   170                              <1> 
   170                              <1> 
   170                              <1>  %if %0 >= 2
   170 0000008F BB[090A0000]        <1>  mov ebx, %2
   170                              <1>  %if %0 >= 3
   170 00000094 B9FF000000          <1>  mov ecx, %3
   170                              <1>  %if %0 = 4
   170 00000099 BA0F000000          <1>  mov edx, %4
   170                              <1>  %endif
   170                              <1>  %endif
   170                              <1>  %endif
   170 0000009E B823000000          <1>  mov eax, %1
   170                              <1> 
   170 000000A3 CD40                <1>  int 40h
   171 000000A5 E916010000                      jmp     Exit
   172                                  
   173                                  a_2:
   174                                  	; eax = 1
   175 000000AA E836040000              	call	codec_io_w16 ; w32
   176                                  	
   177                                  	;call	detect_codec
   178                                  
   179 000000AF E8F2040000              	call	channel_reset
   180                                  
   181 000000B4 E898070000              	call	write_ac97_dev_info 
   182                                  
   183                                  a_3:
   184                                  	; 03/08/2020
   185                                  	; 14/10/2017
   186                                  	; SETUP INTERRUPT CALLBACK SERVICE
   187                                  	; 05/03/2017
   188                                  	;mov	bl, [ac97_int_ln_reg] ; IRQ number
   189                                  	;mov	bh, 2 ; Link IRQ to user for callback service
   190                                  	;mov	edx, ac97_int_handler
   191                                  	;;xor	ecx, ecx
   192                                  	;sys	_calbac
   193                                  	;jc	error_exit
   194                                  
   195                                  	; 03/08/2020
   196                                  	; 24/06/2017
   197                                  	;; 23/06/2017
   198 000000B9 8A1D[A90B0000]          	mov	bl, [ac97_int_ln_reg] ; IRQ number
   199 000000BF B701                    	mov	bh, 1 ; Link IRQ to user for signal response byte
   200 000000C1 88F9                    	mov	cl, bh ; 1
   201 000000C3 BA[A50B0000]            	mov	edx, srb
   202                                  	sys	_calbac
   202                              <1> 
   202                              <1> 
   202                              <1> 
   202                              <1> 
   202                              <1>  %if %0 >= 2
   202                              <1>  mov ebx, %2
   202                              <1>  %if %0 >= 3
   202                              <1>  mov ecx, %3
   202                              <1>  %if %0 = 4
   202                              <1>  mov edx, %4
   202                              <1>  %endif
   202                              <1>  %endif
   202                              <1>  %endif
   202 000000C8 B82C000000          <1>  mov eax, %1
   202                              <1> 
   202 000000CD CD40                <1>  int 40h
   203 000000CF 0F820C010000            	jc	error_exit ; 03/08/2020
   204                                  
   205                                  	; DIRECT CGA (TEXT MODE) MEMORY ACCESS
   206                                  	; bl = 0, bh = 04h
   207                                  	; Direct access/map to CGA (Text) memory (0B8000h)
   208                                  
   209                                  	sys	_video, 0400h
   209                              <1> 
   209                              <1> 
   209                              <1> 
   209                              <1> 
   209                              <1>  %if %0 >= 2
   209 000000D5 BB00040000          <1>  mov ebx, %2
   209                              <1>  %if %0 >= 3
   209                              <1>  mov ecx, %3
   209                              <1>  %if %0 = 4
   209                              <1>  mov edx, %4
   209                              <1>  %endif
   209                              <1>  %endif
   209                              <1>  %endif
   209 000000DA B81F000000          <1>  mov eax, %1
   209                              <1> 
   209 000000DF CD40                <1>  int 40h
   210 000000E1 3D00800B00              	cmp	eax, 0B8000h
   211 000000E6 0F85F5000000            	jne	error_exit
   212                                  
   213                                  ; open the file
   214                                          ; open existing file
   215 000000EC E869010000                      call    openFile ; no error? ok.
   216 000000F1 731B                            jnc     short _gsr
   217                                  
   218                                  ; file not found!
   219                                  	sys	_msg, noFileErrMsg, 255, 0Fh
   219                              <1> 
   219                              <1> 
   219                              <1> 
   219                              <1> 
   219                              <1>  %if %0 >= 2
   219 000000F3 BB[8E0A0000]        <1>  mov ebx, %2
   219                              <1>  %if %0 >= 3
   219 000000F8 B9FF000000          <1>  mov ecx, %3
   219                              <1>  %if %0 = 4
   219 000000FD BA0F000000          <1>  mov edx, %4
   219                              <1>  %endif
   219                              <1>  %endif
   219                              <1>  %endif
   219 00000102 B823000000          <1>  mov eax, %1
   219                              <1> 
   219 00000107 CD40                <1>  int 40h
   220 00000109 E9B2000000                      jmp     Exit
   221                                  
   222                                  _gsr:  
   223 0000010E E877010000                     	call    getSampleRate		; read the sample rate
   224                                                                          ; pass it onto codec.
   225 00000113 0F82A7000000            	jc	Exit
   226                                  
   227 00000119 66A3[BA0B0000]          	mov	[sample_rate], ax
   228 0000011F 880D[B80B0000]          	mov	[stmo], cl
   229 00000125 8815[B90B0000]          	mov	[bps], dl
   230                                  
   231                                  	; 24/06/2017
   232                                  ; setup the Codec (actually mixer registers) 
   233 0000012B E8DD020000                      call    codecConfig            ; unmute codec, set rates.
   234                                  	;jc	_codec_err
   235                                  
   236                                  PlayNow: 
   237                                  	; DIRECT MEMORY ACCESS (for Audio Controller)
   238                                  	; ebx = BDL buffer address (virtual, user)
   239                                  	; ecx = buffer size (in bytes)
   240                                  	; edx = upper limit = 0 = no limit
   241                                  
   242                                  	sys	_alloc, BdlBuffer, 4096, 0 
   242                              <1> 
   242                              <1> 
   242                              <1> 
   242                              <1> 
   242                              <1>  %if %0 >= 2
   242 00000130 BB[00100000]        <1>  mov ebx, %2
   242                              <1>  %if %0 >= 3
   242 00000135 B900100000          <1>  mov ecx, %3
   242                              <1>  %if %0 = 4
   242 0000013A BA00000000          <1>  mov edx, %4
   242                              <1>  %endif
   242                              <1>  %endif
   242                              <1>  %endif
   242 0000013F B82A000000          <1>  mov eax, %1
   242                              <1> 
   242 00000144 CD40                <1>  int 40h
   243 00000146 0F8295000000            	jc	error_exit
   244                                  
   245 0000014C A3[280C0000]            	mov	[BDL_phy_buff], eax	; physical address
   246                                  					; of the buffer
   247                                  					; (which is needed
   248                                  					; for Audio controller)
   249                                  
   250                                  	; DIRECT MEMORY ACCESS (for Audio Controller)
   251                                  	; ebx = DMA buffer address (virtual, user)
   252                                  	; ecx = buffer size (in bytes)
   253                                  	; edx = upper limit = 0 = no limit
   254                                  
   255                                  	sys	_alloc, DmaBuffer, 65536, 0 
   255                              <1> 
   255                              <1> 
   255                              <1> 
   255                              <1> 
   255                              <1>  %if %0 >= 2
   255 00000151 BB[00000100]        <1>  mov ebx, %2
   255                              <1>  %if %0 >= 3
   255 00000156 B900000100          <1>  mov ecx, %3
   255                              <1>  %if %0 = 4
   255 0000015B BA00000000          <1>  mov edx, %4
   255                              <1>  %endif
   255                              <1>  %endif
   255                              <1>  %endif
   255 00000160 B82A000000          <1>  mov eax, %1
   255                              <1> 
   255 00000165 CD40                <1>  int 40h
   256 00000167 7278                    	jc	short error_exit
   257                                  
   258 00000169 A3[2C0C0000]            	mov	[DMA_phy_buff], eax	; physical address
   259                                  					; of the buffer
   260                                  					; (which is needed
   261                                  					; for Audio controller)
   262                                  ;
   263                                  ; position file pointer to start in actual wav data
   264                                  ; MUCH improvement should really be done here to check if sample size is
   265                                  ; supported, make sure there are 2 channels, etc.  
   266                                  ;
   267                                          ;mov     ah, 42h
   268                                          ;mov     al, 0	; from start of file
   269                                          ;mov     bx, [FileHandle]
   270                                          ;xor     cx, cx
   271                                          ;mov     dx, 44	; jump past .wav/riff header
   272                                          ;int     21h
   273                                  
   274                                  	sys	_seek, [FileHandle], 44, 0
   274                              <1> 
   274                              <1> 
   274                              <1> 
   274                              <1> 
   274                              <1>  %if %0 >= 2
   274 0000016E 8B1D[C70A0000]      <1>  mov ebx, %2
   274                              <1>  %if %0 >= 3
   274 00000174 B92C000000          <1>  mov ecx, %3
   274                              <1>  %if %0 = 4
   274 00000179 BA00000000          <1>  mov edx, %4
   274                              <1>  %endif
   274                              <1>  %endif
   274                              <1>  %endif
   274 0000017E B813000000          <1>  mov eax, %1
   274                              <1> 
   274 00000183 CD40                <1>  int 40h
   275                                  
   276                                  ; 14/10/2017
   277                                  
   278                                  ; play the .wav file.  Most of the good stuff is in here.
   279                                  
   280 00000185 E89B040000                      call    PlayWav
   281                                  
   282                                  ; close the .wav file and exit.
   283                                  
   284 0000018A E8E4000000                      call    closeFile
   285                                  
   286                                  StopPlaying:
   287                                  	; 14/10/2017
   288                                  	; 24/06/2017
   289 0000018F 8A1D[A90B0000]          	mov	bl, [ac97_int_ln_reg] ; Audio IRQ number
   290 00000195 28FF                    	sub	bh, bh ; 0 = Unlink IRQ from user
   291                                  	sys	_calbac 
   291                              <1> 
   291                              <1> 
   291                              <1> 
   291                              <1> 
   291                              <1>  %if %0 >= 2
   291                              <1>  mov ebx, %2
   291                              <1>  %if %0 >= 3
   291                              <1>  mov ecx, %3
   291                              <1>  %if %0 = 4
   291                              <1>  mov edx, %4
   291                              <1>  %endif
   291                              <1>  %endif
   291                              <1>  %endif
   291 00000197 B82C000000          <1>  mov eax, %1
   291                              <1> 
   291 0000019C CD40                <1>  int 40h
   292                                  
   293                                  	; Deallocate BDL buffer (not necessary just before exit!)
   294                                  	sys	_dalloc, BdlBuffer, 4096
   294                              <1> 
   294                              <1> 
   294                              <1> 
   294                              <1> 
   294                              <1>  %if %0 >= 2
   294 0000019E BB[00100000]        <1>  mov ebx, %2
   294                              <1>  %if %0 >= 3
   294 000001A3 B900100000          <1>  mov ecx, %3
   294                              <1>  %if %0 = 4
   294                              <1>  mov edx, %4
   294                              <1>  %endif
   294                              <1>  %endif
   294                              <1>  %endif
   294 000001A8 B82B000000          <1>  mov eax, %1
   294                              <1> 
   294 000001AD CD40                <1>  int 40h
   295                                  	; Deallocate DMA buffer (not necessary just before exit!)
   296                                  	sys	_dalloc, DmaBuffer, 65536  ; 14/03/2017
   296                              <1> 
   296                              <1> 
   296                              <1> 
   296                              <1> 
   296                              <1>  %if %0 >= 2
   296 000001AF BB[00000100]        <1>  mov ebx, %2
   296                              <1>  %if %0 >= 3
   296 000001B4 B900000100          <1>  mov ecx, %3
   296                              <1>  %if %0 = 4
   296                              <1>  mov edx, %4
   296                              <1>  %endif
   296                              <1>  %endif
   296                              <1>  %endif
   296 000001B9 B82B000000          <1>  mov eax, %1
   296                              <1> 
   296 000001BE CD40                <1>  int 40h
   297                                  Exit:           
   298                                  	sys	_exit	; Bye!
   298                              <1> 
   298                              <1> 
   298                              <1> 
   298                              <1> 
   298                              <1>  %if %0 >= 2
   298                              <1>  mov ebx, %2
   298                              <1>  %if %0 >= 3
   298                              <1>  mov ecx, %3
   298                              <1>  %if %0 = 4
   298                              <1>  mov edx, %4
   298                              <1>  %endif
   298                              <1>  %endif
   298                              <1>  %endif
   298 000001C0 B801000000          <1>  mov eax, %1
   298                              <1> 
   298 000001C5 CD40                <1>  int 40h
   299                                  here:
   300 000001C7 EBFE                    	jmp	short here
   301                                  
   302                                  pmsg_usage:
   303                                  	sys	_msg, msg_usage, 255, 0Bh
   303                              <1> 
   303                              <1> 
   303                              <1> 
   303                              <1> 
   303                              <1>  %if %0 >= 2
   303 000001C9 BB[190A0000]        <1>  mov ebx, %2
   303                              <1>  %if %0 >= 3
   303 000001CE B9FF000000          <1>  mov ecx, %3
   303                              <1>  %if %0 = 4
   303 000001D3 BA0B000000          <1>  mov edx, %4
   303                              <1>  %endif
   303                              <1>  %endif
   303                              <1>  %endif
   303 000001D8 B823000000          <1>  mov eax, %1
   303                              <1> 
   303 000001DD CD40                <1>  int 40h
   304 000001DF EBDF                    	jmp	short Exit
   305                                  
   306                                  error_exit:
   307                                  	sys	_msg, trdos386_err_msg, 255, 0Eh
   307                              <1> 
   307                              <1> 
   307                              <1> 
   307                              <1> 
   307                              <1>  %if %0 >= 2
   307 000001E1 BB[A70A0000]        <1>  mov ebx, %2
   307                              <1>  %if %0 >= 3
   307 000001E6 B9FF000000          <1>  mov ecx, %3
   307                              <1>  %if %0 = 4
   307 000001EB BA0E000000          <1>  mov edx, %4
   307                              <1>  %endif
   307                              <1>  %endif
   307                              <1>  %endif
   307 000001F0 B823000000          <1>  mov eax, %1
   307                              <1> 
   307 000001F5 CD40                <1>  int 40h
   308 000001F7 EBC7                    	jmp	short Exit
   309                                  
   310                                  DetectVT8233:
   311 000001F9 B806115930              	mov     eax, (VT8233_DID << 16) + VIA_VID
   312 000001FE E8DC010000                      call    pciFindDevice
   313 00000203 7318                            jnc     short _1
   314                                  
   315                                  ; couldn't find the audio device!
   316                                  	sys	_msg, noDevMsg, 255, 0Fh
   316                              <1> 
   316                              <1> 
   316                              <1> 
   316                              <1> 
   316                              <1>  %if %0 >= 2
   316 00000205 BB[D2090000]        <1>  mov ebx, %2
   316                              <1>  %if %0 >= 3
   316 0000020A B9FF000000          <1>  mov ecx, %3
   316                              <1>  %if %0 = 4
   316 0000020F BA0F000000          <1>  mov edx, %4
   316                              <1>  %endif
   316                              <1>  %endif
   316                              <1>  %endif
   316 00000214 B823000000          <1>  mov eax, %1
   316                              <1> 
   316 00000219 CD40                <1>  int 40h
   317 0000021B EBA3                            jmp     short Exit
   318                                  
   319                                  _1:
   320                                  	; 19/06/2017
   321                                  	; 05/03/2017 (TRDOS 386)
   322                                  	; 12/11/2016
   323                                  	; Erdogan Tan - 8/11/2016
   324                                  	; References: Kolibrios - vt823x.asm (2016)
   325                                  	;	      VIA VT8235 V-Link South Bridge (VT8235-VIA.PDF)(2002)
   326                                  	;	      lowlevel.eu - AC97 (2016)
   327                                  	;	      .wav player for DOS by Jeff Leyda (2002) -this file-
   328                                  	;	      Linux kernel - via82xx.c (2016)
   329                                  
   330                                  	; eax = BUS/DEV/FN
   331                                  	;	00000000BBBBBBBBDDDDDFFF00000000
   332                                  	; edx = DEV/VENDOR
   333                                  	;	DDDDDDDDDDDDDDDDVVVVVVVVVVVVVVVV
   334                                  
   335 0000021D A3[AC0B0000]            	mov	[bus_dev_fn], eax
   336 00000222 8915[B00B0000]          	mov	[dev_vendor], edx
   337                                  
   338                                  	; init controller
   339 00000228 B004                    	mov	al, PCI_CMD_REG ; command register (04h)
   340 0000022A E82F010000              	call	pciRegRead32
   341                                  
   342                                  	; eax = BUS/DEV/FN/REG
   343                                  	; edx = STATUS/COMMAND
   344                                  	; 	SSSSSSSSSSSSSSSSCCCCCCCCCCCCCCCC
   345 0000022F 8915[B40B0000]          	mov	[stats_cmd], edx
   346                                  
   347 00000235 B010                    	mov	al, PCI_IO_BASE ; IO base address register (10h)
   348 00000237 E822010000              	call	pciRegRead32
   349                                  
   350 0000023C 6683E2C0                	and     dx, 0FFC0h	; IO_ADDR_MASK (0FFFE) ?
   351 00000240 668915[AA0B0000]                mov     [ac97_io_base], dx
   352                                  
   353 00000247 B03C                    	mov	al, AC97_INT_LINE ; Interrupt line register (3Ch)
   354                                  	;call	pciRegRead32
   355 00000249 E8FD000000              	call	pciRegRead8
   356                                  
   357                                  	;and 	edx, 0FFh
   358 0000024E 6681E2FF00              	and	dx, 0FFh
   359 00000253 8815[A90B0000]            	mov     [ac97_int_ln_reg], dl
   360                                  
   361 00000259 C3                      	retn
   362                                  
   363                                  ;open or create file
   364                                  ;
   365                                  ;input: ds:dx-->filename (asciiz)
   366                                  ;       al=file Mode (create or open)
   367                                  ;output: none  cs:[FileHandle] filled
   368                                  ;
   369                                  openFile:
   370                                  	;;push	eax
   371                                  	;;push	ecx
   372                                  	;mov	ah, 3Bh	; start with a mode
   373                                  	;add	ah, al	; add in create or open mode
   374                                  	;xor	cx, cx
   375                                  	;int	21h
   376                                  	;jc	short _of1
   377                                  	;;mov	[cs:FileHandle], ax
   378                                  
   379                                  	sys	_open, wav_file_name, 0
   379                              <1> 
   379                              <1> 
   379                              <1> 
   379                              <1> 
   379                              <1>  %if %0 >= 2
   379 0000025A BB[D80B0000]        <1>  mov ebx, %2
   379                              <1>  %if %0 >= 3
   379 0000025F B900000000          <1>  mov ecx, %3
   379                              <1>  %if %0 = 4
   379                              <1>  mov edx, %4
   379                              <1>  %endif
   379                              <1>  %endif
   379                              <1>  %endif
   379 00000264 B805000000          <1>  mov eax, %1
   379                              <1> 
   379 00000269 CD40                <1>  int 40h
   380 0000026B 7205                    	jc	short _of1
   381                                  
   382 0000026D A3[C70A0000]            	mov	[FileHandle], eax
   383                                  _of1:
   384                                  	;;pop	ecx
   385                                  	;;pop	eax
   386 00000272 C3                      	retn
   387                                  
   388                                  ; close the currently open file
   389                                  ; input: none, uses cs:[FileHandle]
   390                                  closeFile:
   391                                  	;push	eax
   392                                  	;push	ebx
   393 00000273 833D[C70A0000]FF        	cmp	dword [FileHandle], -1
   394 0000027A 740D                    	je	short _cf1
   395                                  	;mov    bx, [FileHandle]  
   396                                  	;mov    ax, 3E00h
   397                                          ;int    21h              ;close file
   398                                  
   399                                  	sys	_close, [FileHandle]
   399                              <1> 
   399                              <1> 
   399                              <1> 
   399                              <1> 
   399                              <1>  %if %0 >= 2
   399 0000027C 8B1D[C70A0000]      <1>  mov ebx, %2
   399                              <1>  %if %0 >= 3
   399                              <1>  mov ecx, %3
   399                              <1>  %if %0 = 4
   399                              <1>  mov edx, %4
   399                              <1>  %endif
   399                              <1>  %endif
   399                              <1>  %endif
   399 00000282 B806000000          <1>  mov eax, %1
   399                              <1> 
   399 00000287 CD40                <1>  int 40h
   400                                  _cf1:
   401                                  	;pop	ebx
   402                                  	;pop	eax
   403 00000289 C3                      	retn
   404                                  
   405                                  getSampleRate:
   406                                  	
   407                                  ; reads the sample rate from the .wav file.
   408                                  ; entry: none - assumes file is already open
   409                                  ; exit: ax = sample rate (11025, 22050, 44100, 48000)
   410                                  ;	cx = number of channels (mono=1, stereo=2)
   411                                  ;	dx = bits per sample (8, 16)
   412                                  
   413 0000028A 53                      	push    ebx
   414                                  
   415                                          ;mov	ah, 42h
   416                                          ;mov	al, 0	; from start of file
   417                                          ;mov	bx, [FileHandle]
   418                                          ;xor	cx, cx
   419                                          ;mov	dx, 08h	; "WAVE"
   420                                          ;int	21h
   421                                  	
   422                                  	sys	_seek, [FileHandle], 8, 0
   422                              <1> 
   422                              <1> 
   422                              <1> 
   422                              <1> 
   422                              <1>  %if %0 >= 2
   422 0000028B 8B1D[C70A0000]      <1>  mov ebx, %2
   422                              <1>  %if %0 >= 3
   422 00000291 B908000000          <1>  mov ecx, %3
   422                              <1>  %if %0 = 4
   422 00000296 BA00000000          <1>  mov edx, %4
   422                              <1>  %endif
   422                              <1>  %endif
   422                              <1>  %endif
   422 0000029B B813000000          <1>  mov eax, %1
   422                              <1> 
   422 000002A0 CD40                <1>  int 40h
   423                                  
   424                                          ;mov	dx, smpRBuff
   425                                          ;mov	cx, 28	; 28 bytes
   426                                  	;mov	ah, 3fh
   427                                          ;int	21h
   428                                  
   429                                  	sys	_read, [FileHandle], smpRBuff, 28
   429                              <1> 
   429                              <1> 
   429                              <1> 
   429                              <1> 
   429                              <1>  %if %0 >= 2
   429 000002A2 8B1D[C70A0000]      <1>  mov ebx, %2
   429                              <1>  %if %0 >= 3
   429 000002A8 B9[BC0B0000]        <1>  mov ecx, %3
   429                              <1>  %if %0 = 4
   429 000002AD BA1C000000          <1>  mov edx, %4
   429                              <1>  %endif
   429                              <1>  %endif
   429                              <1>  %endif
   429 000002B2 B803000000          <1>  mov eax, %1
   429                              <1> 
   429 000002B7 CD40                <1>  int 40h
   430                                  
   431 000002B9 813D[BC0B0000]5741-     	cmp	dword [smpRBuff], 'WAVE'
   431 000002C1 5645               
   432 000002C3 7520                    	jne	short gsr_stc
   433                                  
   434 000002C5 66833D[C80B0000]01      	cmp	word [smpRBuff+12], 1	; Offset 20, must be 1 (= PCM)
   435 000002CD 7516                    	jne	short gsr_stc
   436                                  
   437 000002CF 668B0D[CA0B0000]        	mov	cx, [smpRBuff+14]	; return num of channels in CX
   438 000002D6 66A1[CC0B0000]                  mov     ax, [smpRBuff+16]	; return sample rate in AX
   439 000002DC 668B15[D60B0000]        	mov	dx, [smpRBuff+26]	; return bits per sample value in DX
   440                                  gsr_retn:
   441 000002E3 5B                              pop     ebx
   442 000002E4 C3                              retn
   443                                  gsr_stc:
   444 000002E5 F9                      	stc
   445 000002E6 EBFB                    	jmp	short gsr_retn
   446                                  
   447                                  	; 25/08/2020
   448                                  ;ac97_int_handler:
   449                                  ;	; 03/08/2020
   450                                  ;	; 30/07/2020
   451                                  ;	; Interrupt Handler for VIA VT8237R Audio Controller
   452                                  ;	;(Derived from TRDOS 386 kernel, 'audio.s', 14/10/2017)
   453                                  ;	; 29/07/2020
   454                                  ;	; 15/10/2017
   455                                  ;	; 14/10/2017 
   456                                  ;	; 09/10/2017, 10/10/2017, 12/10/2017
   457                                  ;	; 13/06/2017
   458                                  ;	; 21/04/2017 (TRDOS 386 kernel, 'audio.s')
   459                                  ;	; 24/03/2017 - 'PLAYER.COM' ('player.asm') 
   460                                  ;
   461                                  ;	; 30/07/2020
   462                                  ;	; we are in CALLBACK service
   463                                  ;	; (So, we can not use direct I/O interrupt)
   464                                  ;	; (We can set sometings without using direct I/O, int 34h)
   465                                  ;
   466                                  ;	; 15/10/2017
   467                                  ;	mov	byte [srb], 1
   468                                  ;
   469                                  ;	; 03/08/2020
   470                                  ;	mov 	al, [audio_flag]
   471                                  ;	add	al, '1'
   472                                  ;	mov	ah, 4Eh
   473                                  ;	mov 	[0B8000h], ax ; Display current buffer number
   474                                  ;
   475                                  ;	; 03/08/2020
   476                                  ;	xor	byte [audio_flag], 1
   477                                  ;
   478                                  ;	sys	_rele ; return from callback service 
   479                                  ;	; we must not come here !
   480                                  ;	sys	_exit
   481                                  
   482                                  ;=============================================================================
   483                                  ;               PCI.ASM
   484                                  ;=============================================================================
   485                                  
   486                                  ; EQUATES
   487                                  
   488                                  ;constants of stuff that seem hard to remember at times.
   489                                  
   490                                  TRUE  EQU 1
   491                                  FALSE EQU 0
   492                                  
   493                                  ENABLED  EQU 1
   494                                  DISABLED EQU 0
   495                                  
   496                                  BIT0  EQU 1
   497                                  BIT1  EQU 2
   498                                  BIT2  EQU 4
   499                                  BIT3  EQU 8
   500                                  BIT4  EQU 10h
   501                                  BIT5  EQU 20h
   502                                  BIT6  EQU 40h
   503                                  BIT7  EQU 80h
   504                                  BIT8  EQU 100h
   505                                  BIT9  EQU 200h
   506                                  BIT10 EQU 400h
   507                                  BIT11 EQU 800h
   508                                  BIT12 EQU 1000h
   509                                  BIT13 EQU 2000h
   510                                  BIT14 EQU 4000h
   511                                  BIT15 EQU 8000h
   512                                  BIT16 EQU 10000h
   513                                  BIT17 EQU 20000h
   514                                  BIT18 EQU 40000h
   515                                  BIT19 EQU 80000h
   516                                  BIT20 EQU 100000h
   517                                  BIT21 EQU 200000h
   518                                  BIT22 EQU 400000h
   519                                  BIT23 EQU 800000h
   520                                  BIT24 EQU 1000000h
   521                                  BIT25 EQU 2000000h
   522                                  BIT26 EQU 4000000h
   523                                  BIT27 EQU 8000000h
   524                                  BIT28 EQU 10000000h
   525                                  BIT29 EQU 20000000h
   526                                  BIT30 EQU 40000000h
   527                                  BIT31 EQU 80000000h
   528                                  NOT_BIT31 EQU 7FFFFFFFh ; 19/03/2017
   529                                  
   530                                  ;special characters
   531                                  NUL     EQU 0
   532                                  NULL    EQU 0
   533                                  BELL    EQU 07
   534                                  BS      EQU 08
   535                                  TAB     EQU 09
   536                                  LF      EQU 10
   537                                  CR      EQU 13
   538                                  ESCAPE  EQU 27           ;ESC is a reserved word....
   539                                  
   540                                  ; PCI equates
   541                                  ; PCI function address (PFA)
   542                                  ; bit 31 = 1
   543                                  ; bit 23:16 = bus number     (0-255)
   544                                  ; bit 15:11 = device number  (0-31)
   545                                  ; bit 10:8 = function number (0-7)
   546                                  ; bit 7:0 = register number  (0-255)
   547                                  
   548                                  IO_ADDR_MASK    EQU     0FFFEh	; mask off bit 0 for reading BARs
   549                                  PCI_INDEX_PORT  EQU     0CF8h
   550                                  PCI_DATA_PORT   EQU     0CFCh
   551                                  PCI32           EQU     BIT31	; bitflag to signal 32bit access
   552                                  PCI16           EQU     BIT30	; bitflag for 16bit access
   553                                  NOT_PCI32_PCI16	EQU	03FFFFFFFh ; NOT BIT31+BIT30 ; 19/03/2017
   554                                  
   555                                  PCI_FN0         EQU     0 << 8
   556                                  PCI_FN1         EQU     1 << 8
   557                                  PCI_FN2         EQU     2 << 8
   558                                  PCI_FN3         EQU     3 << 8
   559                                  PCI_FN4         EQU     4 << 8
   560                                  PCI_FN5         EQU     5 << 8
   561                                  PCI_FN6         EQU     6 << 8
   562                                  PCI_FN7         EQU     7 << 8
   563                                  
   564                                  PCI_CMD_REG	EQU	04h	; reg 04, command reg
   565                                   IO_ENA		EQU	BIT0	; i/o decode enable
   566                                   MEM_ENA	EQU	BIT1	; memory decode enable
   567                                   BM_ENA		EQU     BIT2	; bus master enable
   568                                  
   569                                  ; CODE
   570                                  
   571                                  ; PCI device register reader/writers.
   572                                  
   573                                  ; 19/03/2017
   574                                  ; 05/03/2017 (TRDOS 386, INT 34h, IOCTL interrupt modifications)
   575                                  ; NASM version: Erdogan Tan (29/11/2016)
   576                                  
   577                                  ;===============================================================
   578                                  ; 8/16/32bit PCI reader
   579                                  ;
   580                                  ; Entry: EAX=PCI Bus/Device/fn/register number
   581                                  ;           BIT30 set if 32 bit access requested
   582                                  ;           BIT29 set if 16 bit access requested
   583                                  ;           otherwise defaults to 8bit read
   584                                  ;
   585                                  ; Exit:  DL,DX,EDX register data depending on requested read size
   586                                  ;
   587                                  ; Note: this routine is meant to be called via pciRegRead8, pciRegread16,
   588                                  ;	or pciRegRead32, listed below.
   589                                  ;
   590                                  ; Note2: don't attempt to read 32bits of data from a non dword aligned reg
   591                                  ;	 number.  Likewise, don't do 16bit reads from non word aligned reg #
   592                                  ; 
   593                                  pciRegRead:
   594 000002E8 53                      	push	ebx
   595 000002E9 51                      	push	ecx
   596 000002EA 89C3                            mov     ebx, eax		; save eax, dh
   597 000002EC 88F1                            mov     cl, dh
   598                                  
   599 000002EE 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; clear out data size request
   600 000002F3 0D00000080                      or      eax, BIT31		; make a PCI access request
   601 000002F8 24FC                            and     al, ~3 ; NOT 3		; force index to be dword
   602                                  
   603 000002FA 66BAF80C                        mov     dx, PCI_INDEX_PORT
   604                                          ;out	dx, eax			; write PCI selector
   605 000002FE 53                      	push	ebx
   606 000002FF 89C3                    	mov	ebx, eax ; Data dword		
   607 00000301 B405                    	mov	ah, 5	; outd (32 bit write)
   608 00000303 CD34                    	int	34h	
   609 00000305 5B                      	pop	ebx
   610                                  
   611 00000306 66BAFC0C                        mov     dx, PCI_DATA_PORT
   612 0000030A 88D8                            mov     al, bl
   613 0000030C 2403                            and     al, 3			; figure out which port to
   614 0000030E 00C2                            add     dl, al			; read to
   615                                  
   616                                  	; 19/03/2017
   617 00000310 B404                    	mov	ah, 4  ; ind
   618 00000312 F7C300000080            	test    ebx, PCI32
   619 00000318 750C                            jnz     short _pregr0
   620 0000031A D0EC                    	shr	ah, 1  ; ah = 2 ; inw
   621 0000031C F7C300000040            	test    ebx, PCI16
   622 00000322 7502                            jnz     short _pregr0
   623 00000324 28E4                    	sub	ah, ah ; ah = 0 ; inb
   624                                  _pregr0:	
   625 00000326 CD34                    	int	34h
   626                                  
   627 00000328 F7C300000080            	test    ebx, PCI32
   628 0000032E 7404                            jz      short _pregr1
   629                                  
   630 00000330 89C2                            mov     edx, eax		; return 32bits of data
   631 00000332 EB0D                    	jmp	short _pregr2
   632                                  _pregr1:
   633 00000334 6689C2                  	mov	dx, ax			; return 16bits of data
   634 00000337 F7C300000040                    test    ebx, PCI16
   635 0000033D 7502                            jnz     short _pregr2
   636 0000033F 88CE                            mov     dh, cl			; restore dh for 8 bit read
   637                                  _pregr2:
   638 00000341 89D8                            mov     eax, ebx		; restore eax
   639 00000343 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; clear out data size request
   640 00000348 59                      	pop	ecx
   641 00000349 5B                      	pop	ebx
   642 0000034A C3                      	retn
   643                                  
   644                                  pciRegRead8:
   645 0000034B 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; set up 8 bit read size
   646 00000350 EB96                            jmp     short pciRegRead	; call generic PCI access
   647                                  
   648                                  pciRegRead16:
   649 00000352 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; set up 16 bit read size
   650 00000357 0D00000040                      or      eax, PCI16		; call generic PCI access
   651 0000035C EB8A                            jmp     short pciRegRead
   652                                  
   653                                  pciRegRead32:
   654 0000035E 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; set up 32 bit read size
   655 00000363 0D00000080                      or      eax, PCI32		; call generic PCI access
   656 00000368 E97BFFFFFF                      jmp     pciRegRead
   657                                  
   658                                  
   659                                  ; 23/03/2017
   660                                  ; 19/03/2017
   661                                  ;===============================================================
   662                                  ; 8/16/32bit PCI writer
   663                                  ;
   664                                  ; Entry: EAX=PCI Bus/Device/fn/register number
   665                                  ;           BIT31 set if 32 bit access requested
   666                                  ;           BIT30 set if 16 bit access requested
   667                                  ;           otherwise defaults to 8bit read
   668                                  ;        DL/DX/EDX data to write depending on size
   669                                  ;
   670                                  ;
   671                                  ; note: this routine is meant to be called via pciRegWrite8, pciRegWrite16,
   672                                  ; 	or pciRegWrite32 as detailed below.
   673                                  ;
   674                                  ; Note2: don't attempt to write 32bits of data from a non dword aligned reg
   675                                  ;	 number.  Likewise, don't do 16bit writes from non word aligned reg #
   676                                  ;
   677                                  pciRegWrite:
   678 0000036D 53                      	push	ebx
   679 0000036E 51                      	push	ecx
   680 0000036F 89C3                            mov     ebx, eax		; save eax, edx
   681 00000371 89D1                            mov     ecx, edx
   682 00000373 25FFFFFF3F              	and     eax, NOT_PCI32_PCI16	; clear out data size request
   683 00000378 0D00000080                      or      eax, BIT31		; make a PCI access request
   684 0000037D 24FC                            and     al, ~3 ; NOT 3		; force index to be dword
   685                                  
   686 0000037F 66BAF80C                        mov     dx, PCI_INDEX_PORT
   687                                          ;out	dx, eax			; write PCI selector
   688 00000383 53                      	push	ebx
   689 00000384 89C3                    	mov	ebx, eax ; Data dword		
   690 00000386 B405                    	mov	ah, 5	; outd (32 bit write)
   691 00000388 CD34                    	int	34h	
   692 0000038A 8B1C24                  	mov	ebx, [esp]
   693                                  
   694 0000038D 66BAFC0C                        mov     dx, PCI_DATA_PORT
   695 00000391 88D8                            mov     al, bl
   696 00000393 2403                            and     al, 3			; figure out which port to
   697 00000395 00C2                            add     dl, al			; write to
   698                                  
   699                                  	; 19/03/2017
   700 00000397 F7C3000000C0            	test    ebx, PCI32+PCI16
   701 0000039D 7506                            jnz     short _pregw0
   702 0000039F B401                    	mov	ah, 1
   703 000003A1 88C8                    	mov	al, cl 			; put data into al
   704                                  	;int	34h
   705 000003A3 EB0C                    	jmp	short _pregw2
   706                                  _pregw0:
   707                                  	;mov	ah, 5  ; outd
   708 000003A5 F7C300000080            	test    ebx, PCI32
   709 000003AB 7502                            jnz     short _pregw1
   710 000003AD B403                    	mov	ah, 3
   711                                  _pregw1:
   712 000003AF 89CB                    	mov	ebx, ecx		; put data into ebx 		
   713                                  _pregw2:
   714 000003B1 CD34                    	int	34h
   715                                  	;
   716 000003B3 5B                      	pop	ebx
   717 000003B4 89D8                            mov     eax, ebx		; restore eax
   718 000003B6 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; clear out data size request
   719 000003BB 89CA                            mov     edx, ecx		; restore dx
   720 000003BD 59                      	pop	ecx
   721 000003BE 5B                      	pop	ebx
   722 000003BF C3                      	retn
   723                                  
   724                                  pciRegWrite8:
   725 000003C0 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; set up 8 bit write size
   726 000003C5 EBA6                            jmp	short pciRegWrite	; call generic PCI access
   727                                  
   728                                  pciRegWrite16:
   729 000003C7 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; set up 16 bit write size
   730 000003CC 0D00000040                      or      eax, PCI16		; call generic PCI access
   731 000003D1 EB9A                            jmp	short pciRegWrite
   732                                  
   733                                  pciRegWrite32:
   734 000003D3 25FFFFFF3F                      and     eax, NOT_PCI32_PCI16	; set up 32 bit write size
   735 000003D8 0D00000080                      or      eax, PCI32		; call generic PCI access
   736 000003DD EB8E                            jmp	pciRegWrite
   737                                  
   738                                  ;===============================================================
   739                                  ; PCIFindDevice: scan through PCI space looking for a device+vendor ID
   740                                  ;
   741                                  ; Entry: EAX=Device+Vendor ID
   742                                  ;
   743                                  ;  Exit: EAX=PCI address if device found
   744                                  ;	 EDX=Device+Vendor ID
   745                                  ;        CY clear if found, set if not found. EAX invalid if CY set.
   746                                  ;
   747                                  ; [old stackless] Destroys: ebx, esi, edi, cl
   748                                  ;
   749                                  pciFindDevice:
   750                                  	;push	ecx
   751 000003DF 50                      	push	eax
   752                                  	;push	esi
   753                                  	;push	edi
   754                                  
   755 000003E0 89C6                            mov     esi, eax                ; save off vend+device ID
   756 000003E2 BF00FFFF7F                      mov     edi, (80000000h - 100h) ; start with bus 0, dev 0 func 0
   757                                  
   758                                  nextPCIdevice:
   759 000003E7 81C700010000                    add     edi, 100h
   760 000003ED 81FF00F8FF80                    cmp     edi, 80FFF800h		; scanned all devices?
   761 000003F3 F9                              stc
   762 000003F4 740C                            je      short PCIScanExit       ; not found
   763                                  
   764 000003F6 89F8                            mov     eax, edi                ; read PCI registers
   765 000003F8 E861FFFFFF                      call    pciRegRead32
   766 000003FD 39F2                            cmp     edx, esi                ; found device?
   767 000003FF 75E6                            jne     short nextPCIdevice
   768 00000401 F8                              clc
   769                                  
   770                                  PCIScanExit:
   771 00000402 9C                      	pushf
   772 00000403 B8FFFFFF7F              	mov	eax, NOT_BIT31 	; 19/03/2017
   773 00000408 21F8                    	and	eax, edi	; return only bus/dev/fn #
   774 0000040A 9D                      	popf
   775                                  
   776                                  	;pop	edi
   777                                  	;pop	esi
   778 0000040B 5A                      	pop	edx
   779                                  	;pop	ecx
   780 0000040C C3                      	retn
   781                                  
   782                                  ;=============================================================================
   783                                  ;               CODEC.ASM
   784                                  ;=============================================================================
   785                                  
   786                                  ; EQUATES
   787                                  
   788                                  ;Codec registers.
   789                                  ;
   790                                  ;Not all codecs are created equal. Refer to the spec for your specific codec.
   791                                  ;
   792                                  ;All registers are 16bits wide.  Access to codec registers over the AC97 link
   793                                  ;is defined by the OEM.  
   794                                  ;
   795                                  ;Secondary codec's are accessed by ORing in BIT7 of all register accesses.
   796                                  ;
   797                                  
   798                                  ; each codec/mixer register is 16bits
   799                                  
   800                                  CODEC_RESET_REG                 equ     00      ; reset codec
   801                                  CODEC_MASTER_VOL_REG            equ     02      ; master volume
   802                                  CODEC_HP_VOL_REG                equ     04      ; headphone volume
   803                                  CODEC_MASTER_MONO_VOL_REG       equ     06      ; master mono volume
   804                                  CODEC_MASTER_TONE_REG           equ     08      ; master tone (R+L)
   805                                  CODEC_PCBEEP_VOL_REG            equ     0ah     ; PC beep volume
   806                                  CODEC_PHONE_VOL_REG             equ     0bh     ; phone volume
   807                                  CODEC_MIC_VOL_REG               equ     0eh     ; MIC volume
   808                                  CODEC_LINE_IN_VOL_REG           equ     10h     ; line input volume
   809                                  CODEC_CD_VOL_REG                equ     12h     ; CD volume
   810                                  CODEC_VID_VOL_REG               equ     14h     ; video volume
   811                                  CODEC_AUX_VOL_REG               equ     16h     ; aux volume
   812                                  CODEC_PCM_OUT_REG               equ     18h     ; PCM output volume
   813                                  CODEC_RECORD_SELECT_REG         equ     1ah     ; record select input
   814                                  CODEC_RECORD_VOL_REG            equ     1ch     ; record volume
   815                                  CODEC_RECORD_MIC_VOL_REG        equ     1eh     ; record mic volume
   816                                  CODEC_GP_REG                    equ     20h     ; general purpose
   817                                  CODEC_3D_CONTROL_REG            equ     22h     ; 3D control
   818                                  ; 24h is reserved
   819                                  CODEC_POWER_CTRL_REG            equ     26h     ; powerdown control
   820                                  CODEC_EXT_AUDIO_REG             equ     28h     ; extended audio
   821                                  CODEC_EXT_AUDIO_CTRL_REG        equ     2ah     ; extended audio control
   822                                  CODEC_PCM_FRONT_DACRATE_REG     equ     2ch     ; PCM out sample rate
   823                                  CODEC_PCM_SURND_DACRATE_REG     equ     2eh     ; surround sound sample rate
   824                                  CODEC_PCM_LFE_DACRATE_REG       equ     30h     ; LFE sample rate
   825                                  CODEC_LR_ADCRATE_REG            equ     32h     ; PCM in sample rate
   826                                  CODEC_MIC_ADCRATE_REG           equ     34h     ; mic in sample rate
   827                                  
   828                                  ; 30/07/2020
   829                                  CODEC_MISC_CRTL_BITS_REG	equ	76h	; misc control bits ; AD1980
   830                                  ;	
   831                                  CODEC_VENDOR_ID1		equ	7Ch	; REALTEK: 414Ch, ADI: 4144h	
   832                                  CODEC_VENDOR_ID2		equ	7Eh	; REALTEK: 4760h, ADI: 5370h
   833                                  
   834                                  
   835                                  ; Mixer registers 0 through 51h reside in the ICH and are not forwarded over
   836                                  ; the AC97 link to the codec, which I think is a little weird.  Looks like
   837                                  ; the ICH makes it so you don't need a fully functional codec to play audio?
   838                                  ;
   839                                  ; whenever 2 codecs are present in the system, use BIT7 to access the 2nd
   840                                  ; set of registers, ie 80h-feh
   841                                  
   842                                  PRIMARY_CODEC		equ     0       ; 0-7F for primary codec
   843                                  SECONDARY_CODEC		equ     BIT7    ; 80-8f registers for 2ndary
   844                                  
   845                                  SAMPLE_RATE_441khz	equ     44100   ; 44.1Khz (cd quality) rate
   846                                  
   847                                  ; each buffer descriptor BAR holds a pointer which has entries to the buffer
   848                                  ; contents of the .WAV file we're going to play.  Each entry is 8 bytes long
   849                                  ; (more on that later) and can contain 32 entries total, so each BAR is
   850                                  ; 256 bytes in length, thus:
   851                                  
   852                                  BDL_SIZE                equ     32*8    ; Buffer Descriptor List size
   853                                  INDEX_MASK              equ     31      ; indexes must be 0-31
   854                                  
   855                                  ;
   856                                  ; Buffer Descriptors List
   857                                  ; As stated earlier, each buffer descriptor list is a set of (up to) 32 
   858                                  ; descriptors, each 8 bytes in length.  Bytes 0-3 of a descriptor entry point
   859                                  ; to a chunk of memory to either play from or record to.  Bytes 4-7 of an
   860                                  ; entry describe various control things detailed below.
   861                                  ; 
   862                                  ; Buffer pointers must always be aligned on a Dword boundry.
   863                                  ;
   864                                  ;
   865                                  
   866                                  IOC                     equ     BIT31	; Fire an interrupt whenever this
   867                                                                          ; buffer is complete.
   868                                  
   869                                  BUP                     equ     BIT30	; Buffer Underrun Policy.
   870                                                                          ; if this buffer is the last buffer
   871                                                                          ; in a playback, fill the remaining
   872                                                                          ; samples with 0 (silence) or not.
   873                                                                          ; It's a good idea to set this to 1
   874                                                                          ; for the last buffer in playback,
   875                                                                          ; otherwise you're likely to get a lot
   876                                                                          ; of noise at the end of the sound.
   877                                  
   878                                  ;
   879                                  ; Bits 15:0 contain the length of the buffer, in number of samples, which
   880                                  ; are 16 bits each, coupled in left and right pairs, or 32bits each.
   881                                  ; Luckily for us, that's the same format as .wav files.
   882                                  ;
   883                                  ; A value of FFFF is 65536 samples.  Running at 44.1Khz, that's just about
   884                                  ; 1.5 seconds of sample time.  FFFF * 32bits is 1FFFFh bytes or 128k of data.
   885                                  ;
   886                                  ; A value of 0 in these bits means play no samples.
   887                                  ;
   888                                  
   889                                  ;VIA VT8233 (VT8235) AC97 Codec equates 
   890                                  ;(edited by Erdogan Tan, 7/11/2016)
   891                                  
   892                                  ; PCI stuff
   893                                  
   894                                  VIA_VID		equ 1106h	; VIA's PCI vendor ID
   895                                  VT8233_DID      equ 3059h	; VT8233 (VT8235) device ID
   896                                  		
   897                                  PCI_IO_BASE          equ 10h
   898                                  AC97_INT_LINE        equ 3Ch
   899                                  VIA_ACLINK_CTRL      equ 41h
   900                                  VIA_ACLINK_STAT      equ 40h
   901                                  VIA_ACLINK_C00_READY equ 01h ; primary codec ready
   902                                  	
   903                                  VIA_REG_AC97	     equ 80h ; dword
   904                                  
   905                                  VIA_ACLINK_CTRL_ENABLE	equ   80h ; 0: disable, 1: enable
   906                                  VIA_ACLINK_CTRL_RESET	equ   40h ; 0: assert, 1: de-assert
   907                                  VIA_ACLINK_CTRL_SYNC	equ   20h ; 0: release SYNC, 1: force SYNC hi
   908                                  VIA_ACLINK_CTRL_VRA	equ   08h ; 0: disable VRA, 1: enable VRA
   909                                  VIA_ACLINK_CTRL_PCM	equ   04h ; 0: disable PCM, 1: enable PCM
   913                                  VIA_ACLINK_CTRL_INIT	equ  (VIA_ACLINK_CTRL_ENABLE +                               VIA_ACLINK_CTRL_RESET +                               VIA_ACLINK_CTRL_PCM +                               VIA_ACLINK_CTRL_VRA)
   914                                  
   915                                  CODEC_AUX_VOL		equ   04h
   916                                  VIA_REG_AC97_BUSY	equ   01000000h ;(1<<24) 
   917                                  VIA_REG_AC97_CMD_SHIFT	equ   10h ; 16
   918                                  VIA_REG_AC97_PRIMARY_VALID equ 02000000h ;(1<<25)
   919                                  VIA_REG_AC97_READ	equ   00800000h ;(1<<23)
   920                                  VIA_REG_AC97_CODEC_ID_SHIFT   equ  1Eh ; 30
   921                                  VIA_REG_AC97_CODEC_ID_PRIMARY equ  0
   922                                  VIA_REG_AC97_DATA_SHIFT equ   0
   923                                  VIADEV_PLAYBACK         equ   0
   924                                  VIA_REG_OFFSET_STATUS   equ   0    ;; byte - channel status
   925                                  VIA_REG_OFFSET_CONTROL  equ   01h  ;; byte - channel control
   926                                  VIA_REG_CTRL_START	equ   80h  ;; WO
   927                                  VIA_REG_CTRL_TERMINATE  equ   40h  ;; WO
   928                                  VIA_REG_CTRL_PAUSE      equ   08h  ;; RW
   929                                  VIA_REG_CTRL_RESET      equ   01h  ;; RW - probably reset? undocumented
   930                                  VIA_REG_OFFSET_STOP_IDX equ   08h  ;; dword - stop index, channel type, sample rate
   931                                  VIA8233_REG_TYPE_16BIT  equ   200000h ;; RW
   932                                  VIA8233_REG_TYPE_STEREO equ   100000h ;; RW
   933                                  VIA_REG_OFFSET_CURR_INDEX equ 0Fh ;; byte - channel current index (for via8233 only)
   934                                  VIA_REG_OFFSET_TABLE_PTR equ  04h  ;; dword - channel table pointer
   935                                  VIA_REG_OFFSET_CURR_PTR equ   04h  ;; dword - channel current pointer
   936                                  VIA_REG_OFS_PLAYBACK_VOLUME_L equ  02h ;; byte
   937                                  VIA_REG_OFS_PLAYBACK_VOLUME_R equ  03h ;; byte
   938                                  VIA_REG_CTRL_AUTOSTART	equ   20h
   939                                  VIA_REG_CTRL_INT_EOL	equ   02h
   940                                  VIA_REG_CTRL_INT_FLAG	equ   01h
   943                                  VIA_REG_CTRL_INT	equ  (VIA_REG_CTRL_INT_FLAG +                               VIA_REG_CTRL_INT_EOL +                               VIA_REG_CTRL_AUTOSTART)
   944                                  
   945                                  VIA_REG_STAT_STOP_IDX	equ   10h    ;; RO ; 30/07/2020
   946                                  				     ; current index = stop index
   947                                  ; 24/11/2016
   948                                  VIA_REG_STAT_STOPPED	equ   04h    ;; RWC
   949                                  VIA_REG_STAT_EOL	equ   02h    ;; RWC
   950                                  VIA_REG_STAT_FLAG	equ   01h    ;; RWC
   951                                  VIA_REG_STAT_ACTIVE	equ   80h    ;; RO
   952                                  ; 28/11/2016
   953                                  VIA_REG_STAT_LAST	equ   40h    ;; RO
   954                                  VIA_REG_STAT_TRIGGER_QUEUED equ 08h  ;; RO
   955                                  VIA_REF_CTRL_INT_STOP	equ   04h  ; Interrupt on Current Index = Stop Index
   956                                  		   ; and End of Block
   957                                  
   958                                  ; 14/03/2017
   959                                  VIA_REG_OFFSET_CURR_COUNT equ 0Ch ;; dword - channel current count, index
   960                                  
   961                                  ; CODE
   962                                  
   963                                  ; codec configuration code. Not much here really.
   964                                  ; NASM version: Erdogan Tan (29/11/2016)
   965                                  
   966                                  ; enable codec, unmute stuff, set output rate to 44.1
   967                                  ; entry: ax = desired sample rate
   968                                  ;
   969                                  codecConfig:
   970                                  	; 14/10/2017
   971                                  	; 24/06/2017
   972                                  	; 19/06/2017, 23/06/2017
   973                                  	; 14/11/2016, 15/11/2016
   974                                  	; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, 'setup_codec', codec.inc)
   975                                  
   976                                  	;mov	eax, 0202h
   977 0000040D B804040000              	mov	eax, 0404h ; 23/06/2017
   978                                  	;mov	[audio_master_volume], ax ; 14/10/2017
   979 00000412 A2[300C0000]            	mov	[audio_master_volume], al ; 14/10/2017
   980 00000417 BA02000000              	mov	edx, CODEC_MASTER_VOL_REG ; 02h ; Line Out
   981 0000041C E84A010000              	call	codec_write
   982                                  	;jc	cconfig_error
   983                                  
   984 00000421 B802020000              	mov     eax, 0202h
   985 00000426 BA18000000              	mov	edx, CODEC_PCM_OUT_REG ; 18h ; Wave Output (Stereo)
   986 0000042B E83B010000              	call	codec_write
   987                                  	;jc	cconfig_error
   988                                        
   989 00000430 B802020000              	mov	eax, 0202h
   990 00000435 BA04000000              	mov	edx, CODEC_AUX_VOL ; 04h ; CODEC_HP_VOL_REG ; HeadPhone
   991 0000043A E82C010000              	call	codec_write
   992                                  	;jc	cconfig_error
   993                                  
   994 0000043F B808800000                      mov     eax, 8008h ; Mute
   995 00000444 BA0C000000                      mov	edx, 0Ch  ; AC97_PHONE_VOL ; TAD Input (Mono)
   996 00000449 E81D010000              	call	codec_write
   997                                  	;jc	short cconfig_error
   998                                  
   999 0000044E B808080000                      mov     eax, 0808h
  1000 00000453 BA10000000                      mov	edx, CODEC_LINE_IN_VOL_REG ; 10h ; Line Input (Stereo)	
  1001 00000458 E80E010000              	call	codec_write
  1002                                  	;jc	short cconfig_error
  1003                                  
  1004 0000045D B808080000              	mov     eax, 0808h
  1005 00000462 BA12000000                      mov	edx, CODEC_CD_VOL_REG ; 12h ; CR Input (Stereo)
  1006 00000467 E8FF000000              	call	codec_write
  1007                                  	;jc	short cconfig_error
  1008                                  
  1009 0000046C B808080000              	mov     eax, 0808h
  1010 00000471 BA16000000                      mov	edx, CODEC_AUX_VOL_REG ; 16h ; Aux Input (Stereo)
  1011                                  ;	call	codec_write
  1012                                  ;	;jc	short cconfig_error
  1013                                  
  1014                                  ;	; Extended Audio Status (2Ah)
  1015                                  ;	mov	eax, CODEC_EXT_AUDIO_CTRL_REG ; 2Ah 
  1016                                  ;	call	codec_read
  1017                                  ;       and     eax, 0FFFFh - 2 ; clear DRA (BIT1)
  1018                                  ;       ;or     eax, 1		; set VRA (BIT0)
  1019                                  ;	or	eax, 5  	; VRA (BIT0) & S/PDIF (BIT2) ; 14/11/2016
  1020                                  ;	mov	edx, CODEC_EXT_AUDIO_CTRL_REG
  1021                                  ;	call	codec_write
  1022                                  ;	;jc	short cconfig_error
  1023                                  ;
  1024                                  ;	; 24/06/2017
  1025                                  ;set_sample_rate:
  1026                                  ;       movzx	eax, word [sample_rate]
  1027                                  ;	mov	edx, CODEC_PCM_FRONT_DACRATE_REG ; 2Ch ; PCM Front DAC Rate
  1028                                  ;       ;call	codec_write
  1029                                  ;	;retn
  1030 00000476 E9F0000000              	jmp	codec_write
  1031                                  
  1032                                  ;cconfig_error:
  1033                                  ;	retn
  1034                                  
  1035                                  reset_codec:
  1036                                  	; 23/03/2017
  1037                                  	; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, vt823x.asm)
  1038 0000047B A1[AC0B0000]            	mov	eax, [bus_dev_fn]
  1039 00000480 B041                     	mov	al, VIA_ACLINK_CTRL
  1040 00000482 B2E0                           	mov	dl, VIA_ACLINK_CTRL_ENABLE + VIA_ACLINK_CTRL_RESET + VIA_ACLINK_CTRL_SYNC
  1041 00000484 E837FFFFFF              	call	pciRegWrite8
  1042                                  
  1043 00000489 E882030000              	call	delay_100ms 	; wait 100 ms
  1044                                  _rc_cold:
  1045 0000048E E822000000                      call    cold_reset
  1046 00000493 7303                            jnc     short _reset_codec_ok
  1047                                  
  1048 00000495 31C0                            xor     eax, eax         ; timeout error
  1049 00000497 C3                              retn
  1050                                  
  1051                                  _reset_codec_ok:
  1052                                  	; 30/07/2020
  1053                                  	; also reset codec by using index control register 0 of AD1980 or ALC655
  1054                                  	; (to fix line out -2 channels audio playing- problem on AD1980 codec)  
  1055                                  
  1056 00000498 29C0                    	sub	eax, eax
  1057 0000049A BA00000000              	mov	edx, CODEC_RESET_REG ; 00h ; Reset register
  1058 0000049F E8C7000000              	call	codec_write
  1059                                  
  1060 000004A4 29C0                    	sub	eax, eax
  1061 000004A6 BA76000000              	mov	edx, CODEC_MISC_CRTL_BITS_REG ; 76h ; Misc Ctrl Bits ; AD1980
  1062 000004AB E8BB000000              	call	codec_write
  1063                                  	;
  1064                                  
  1065 000004B0 31C0                            xor     eax, eax
  1066                                          ;mov	al, VIA_ACLINK_C00_READY ; 1
  1067 000004B2 FEC0                            inc	al
  1068 000004B4 C3                      	retn
  1069                                  
  1070                                  cold_reset:
  1071                                  	; 23/03/2017
  1072                                  	; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, vt823x.asm)
  1073                                  	;mov	eax, [bus_dev_fn]
  1074                                  	;mov	al, VIA_ACLINK_CTRL
  1075 000004B5 30D2                    	xor	dl, dl ; 0
  1076 000004B7 E804FFFFFF              	call	pciRegWrite8
  1077                                  
  1078 000004BC E84F030000              	call	delay_100ms 	; wait 100 ms
  1079                                  
  1080                                  	;; ACLink on, deassert ACLink reset, VSR, SGD data out
  1081                                          ;; note - FM data out has trouble with non VRA codecs !!
  1082                                          
  1083                                  	;mov	eax, [bus_dev_fn]
  1084                                  	;mov	al, VIA_ACLINK_CTRL
  1085 000004C1 B2CC                    	mov	dl, VIA_ACLINK_CTRL_INIT
  1086 000004C3 E8F8FEFFFF              	call	pciRegWrite8
  1087                                  
  1088 000004C8 B910000000              	mov	ecx, 16	; total 2s
  1089                                  
  1090                                  _crst_wait:
  1091                                  	;mov	eax, [bus_dev_fn]
  1092 000004CD B040                    	mov	al, VIA_ACLINK_STAT
  1093 000004CF E877FEFFFF              	call	pciRegRead8	
  1094                                  
  1095 000004D4 F6C201                          test    dl, VIA_ACLINK_C00_READY
  1096 000004D7 750B                            jnz     short _crst_ok
  1097                                  
  1098 000004D9 51                      	push	ecx
  1099 000004DA E831030000              	call	delay_100ms
  1100 000004DF 59                      	pop	ecx
  1101                                  
  1102 000004E0 49                              dec     ecx
  1103 000004E1 75EA                            jnz     short _crst_wait
  1104                                  
  1105                                  _crst_fail:
  1106 000004E3 F9                              stc
  1107                                  _crst_ok:
  1108 000004E4 C3                      	retn
  1109                                  
  1110                                  codec_io_w16: ;w32
  1111 000004E5 668B15[AA0B0000]                mov	dx, [ac97_io_base]
  1112 000004EC 6681C28000                      add     dx, VIA_REG_AC97
  1113                                  	;out	dx, eax
  1114                                  	; 05/03/2017
  1115 000004F1 53                      	push	ebx
  1116 000004F2 89C3                    	mov	ebx, eax
  1117 000004F4 B405                    	mov	ah, 5 ; outd
  1118 000004F6 CD34                    	int	34h
  1119 000004F8 5B                      	pop	ebx
  1120 000004F9 C3                              retn
  1121                                  
  1122                                  codec_io_r16: ;r32
  1123 000004FA 668B15[AA0B0000]                mov     dx, [ac97_io_base]
  1124 00000501 6681C28000                      add     dx, VIA_REG_AC97
  1125                                          ;in	eax, dx
  1126                                  	; 05/03/2017
  1127 00000506 B404                    	mov	ah, 4 ; ind
  1128 00000508 CD34                    	int	34h
  1129 0000050A C3                              retn
  1130                                  
  1131                                  ctrl_io_w8:
  1132 0000050B 660315[AA0B0000]                add     dx, [ac97_io_base]
  1133                                          ;out	dx, al
  1134                                  	; 05/03/2017
  1135 00000512 B401                    	mov	ah, 1 ; outb
  1136 00000514 CD34                    	int	34h
  1137 00000516 C3                              retn
  1138                                  
  1139                                  ctrl_io_r8:
  1140 00000517 660315[AA0B0000]                add     dx, [ac97_io_base]
  1141                                          ;in	al, dx
  1142                                  	; 05/03/2017
  1143 0000051E B400                    	mov	ah, 0 ; inb
  1144 00000520 CD34                    	int	34h
  1145 00000522 C3                              retn
  1146                                  
  1147                                  ctrl_io_w32:
  1148 00000523 660315[AA0B0000]                add     dx, [ac97_io_base]
  1149                                          ;out	dx, eax
  1150                                  	; 05/03/2017
  1151 0000052A 53                      	push	ebx
  1152 0000052B 89C3                    	mov	ebx, eax
  1153 0000052D B405                    	mov	ah, 5 ; outd
  1154 0000052F CD34                    	int	34h
  1155 00000531 5B                      	pop	ebx
  1156 00000532 C3                              retn
  1157                                  
  1158                                  ctrl_io_r32:
  1159 00000533 660315[AA0B0000]                add	dx, [ac97_io_base]
  1160                                  	;in	eax, dx
  1161                                  	; 05/03/2017
  1162 0000053A B404                    	mov	ah, 4 ; ind
  1163 0000053C CD34                    	int	34h
  1164 0000053E C3                              retn
  1165                                  
  1166                                  codec_read:
  1167                                  	; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, vt823x.asm)
  1168                                          ; Use only primary codec.
  1169                                          ; eax = register
  1170 0000053F C1E010                          shl     eax, VIA_REG_AC97_CMD_SHIFT
  1171 00000542 0D00008002                      or      eax, VIA_REG_AC97_PRIMARY_VALID + VIA_REG_AC97_READ
  1172                                  
  1173 00000547 E899FFFFFF              	call    codec_io_w16
  1174                                  
  1175                                        	; codec_valid
  1176 0000054C E831000000              	call	codec_check_ready
  1177 00000551 7301                            jnc	short _cr_ok
  1178                                  
  1179 00000553 C3                      	retn
  1180                                  
  1181                                  _cr_ok:
  1182                                  	; wait 25 ms
  1183 00000554 B950000000              	mov	ecx, 80
  1184                                  _cr_wloop:
  1185 00000559 E8BF020000              	call	delay1_4ms
  1186 0000055E E2F9                    	loop	_cr_wloop
  1187                                  
  1188 00000560 E895FFFFFF                      call    codec_io_r16
  1189 00000565 25FFFF0000                      and     eax, 0FFFFh
  1190 0000056A C3                              retn
  1191                                  
  1192                                  codec_write:
  1193                                  	; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, vt823x.asm)
  1194                                          ; Use only primary codec.
  1195                                          
  1196                                  	; eax = data (volume)
  1197                                  	; edx = register (mixer register)
  1198                                  	
  1199 0000056B C1E210                  	shl     edx, VIA_REG_AC97_CMD_SHIFT
  1200                                  
  1201 0000056E C1E000                          shl     eax, VIA_REG_AC97_DATA_SHIFT ; shl eax, 0
  1202 00000571 09C2                            or      edx, eax
  1203                                  
  1204 00000573 B800000000                      mov     eax, VIA_REG_AC97_CODEC_ID_PRIMARY
  1205 00000578 C1E01E                          shl     eax, VIA_REG_AC97_CODEC_ID_SHIFT
  1206 0000057B 09D0                            or      eax, edx
  1207                                  
  1208 0000057D E863FFFFFF                      call    codec_io_w16
  1209                                          ;mov    [codec.regs+esi], ax
  1210                                  
  1211                                          ;call	codec_check_ready
  1212                                         	;retn
  1213                                  	;jmp	short _codec_check_ready	
  1214                                  
  1215                                  codec_check_ready:
  1216                                  	; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, vt823x.asm)
  1217                                  
  1218                                  _codec_check_ready:
  1219 00000582 B914000000              	mov	ecx, 20	; total 2s
  1220                                  _ccr_wait:
  1221 00000587 51                      	push	ecx
  1222                                  
  1223 00000588 E86DFFFFFF                      call    codec_io_r16
  1224 0000058D A900000001                      test    eax, VIA_REG_AC97_BUSY
  1225 00000592 740B                            jz      short _ccr_ok
  1226                                  
  1227 00000594 E877020000              	call	delay_100ms
  1228                                  
  1229 00000599 59                      	pop	ecx
  1230                                  
  1231 0000059A 49                      	dec     ecx
  1232 0000059B 75EA                            jnz     short _ccr_wait
  1233                                  
  1234 0000059D F9                              stc
  1235 0000059E C3                              retn
  1236                                  
  1237                                  _ccr_ok:
  1238 0000059F 59                      	pop	ecx
  1239 000005A0 25FFFF0000              	and     eax, 0FFFFh
  1240 000005A5 C3                              retn
  1241                                  
  1242                                  channel_reset:
  1243                                  	; 24/06/2017
  1244                                  	; 29/05/2017
  1245                                  	; 23/03/2017
  1246                                  	; 14/11/2016 - Erdogan Tan
  1247                                  	; 12/11/2016 - Erdogan Tan (Ref: KolibriOS, vt823x.asm)
  1248 000005A6 BA01000000                      mov	edx, VIA_REG_OFFSET_CONTROL
  1249                                          ;mov	eax, VIA_REG_CTRL_PAUSE + VIA_REG_CTRL_TERMINATE + VIA_REG_CTRL_RESET
  1250 000005AB B848000000                      mov	eax, VIA_REG_CTRL_PAUSE + VIA_REG_CTRL_TERMINATE ; 24/06/2017        
  1251 000005B0 E856FFFFFF              	call    ctrl_io_w8
  1252                                  
  1253                                          ;mov	edx, VIA_REG_OFFSET_CONTROL
  1254                                          ;call   ctrl_io_r8
  1255                                  
  1256 000005B5 B9A0000000              	mov	ecx, 160 ; 200 (50 ms)	
  1257                                  _ch_rst_wait:
  1258 000005BA E85E020000              	call	delay1_4ms
  1259 000005BF 49                      	dec	ecx
  1260 000005C0 75F8                    	jnz	short _ch_rst_wait     
  1261                                  
  1262                                          ; disable interrupts
  1263 000005C2 BA01000000                      mov	edx, VIA_REG_OFFSET_CONTROL
  1264 000005C7 31C0                            xor     eax, eax
  1265 000005C9 E83DFFFFFF                      call    ctrl_io_w8
  1266                                  
  1267                                          ; clear interrupts
  1268 000005CE BA00000000                      mov	edx, VIA_REG_OFFSET_STATUS
  1269 000005D3 B803000000              	mov	eax, 3
  1270 000005D8 E82EFFFFFF                      call	ctrl_io_w8
  1271                                  
  1272                                  	;mov	edx, VIA_REG_OFFSET_CURR_PTR
  1273                                  	;xor	eax, eax
  1274                                  	;call	ctrl_io_w32
  1275                                  
  1276 000005DD C3                              retn
  1277                                  
  1278                                  loadFromFile:
  1279                                  	; 17/03/2017
  1280                                  	; edi = buffer address
  1281                                  	; edx = buffer size
  1282                                  	; 10/03/2017
  1283                                          ;push	eax
  1284                                          ;push	ecx
  1285                                          ;push	edx
  1286                                  	;push	ebx
  1287 000005DE F605[A40B0000]01                test    byte [eof_flag], ENDOFFILE	; have we already read the
  1288 000005E5 F9                              stc			; last of the file?
  1289 000005E6 7531                            jnz     short endLFF
  1290                                  	;clc
  1291                                  	; load file into memory
  1292                                  	sys 	_read, [FileHandle], edi
  1292                              <1> 
  1292                              <1> 
  1292                              <1> 
  1292                              <1> 
  1292                              <1>  %if %0 >= 2
  1292 000005E8 8B1D[C70A0000]      <1>  mov ebx, %2
  1292                              <1>  %if %0 >= 3
  1292 000005EE 89F9                <1>  mov ecx, %3
  1292                              <1>  %if %0 = 4
  1292                              <1>  mov edx, %4
  1292                              <1>  %endif
  1292                              <1>  %endif
  1292                              <1>  %endif
  1292 000005F0 B803000000          <1>  mov eax, %1
  1292                              <1> 
  1292 000005F5 CD40                <1>  int 40h
  1293 000005F7 89D1                    	mov	ecx, edx
  1294 000005F9 720A                    	jc	short padfill ; error !
  1295 000005FB 21C0                    	and	eax, eax
  1296 000005FD 7406                    	jz	short padfill
  1297 000005FF 29C1                    	sub	ecx, eax
  1298 00000601 7416                    	jz	short endLFF
  1299 00000603 01C7                    	add	edi, eax  
  1300                                  padfill:
  1301 00000605 803D[B90B0000]10        	cmp 	byte [bps], 16
  1302 0000060C 740C                    	je	short _7
  1303                                  	; Minimum Value = 0
  1304 0000060E 30C0                            xor     al, al
  1305 00000610 F3AA                    	rep	stosb
  1306                                  _6:
  1307                                          ;clc			; don't exit with CY yet.
  1308 00000612 800D[A40B0000]01                or	byte [eof_flag], ENDOFFILE	; end of file flag
  1309                                  endLFF:
  1310                                  	;pop	ebx
  1311                                  	;pop	edx
  1312                                          ;pop	ecx
  1313                                          ;pop	eax
  1314 00000619 C3                              retn
  1315                                  _7:
  1316                                  	; Minimum value = 8000h (-32768)
  1317 0000061A D1E9                    	shr	ecx, 1 
  1318 0000061C 66B80080                	mov	ax, 8000h ; -32768
  1319 00000620 F366AB                  	rep	stosw
  1320 00000623 EBED                    	jmp	short _6
  1321                                  
  1322                                  ;=============================================================================
  1323                                  ;               VIA_WAV.ASM
  1324                                  ;=============================================================================
  1325                                  
  1326                                  ; DOS based .WAV player using AC'97 and codec interface.
  1327                                  ; ---------------------------------------------------------------
  1328                                  ; VIA VT8233 Modification & NASM version: Erdogan Tan (29/11/2016)
  1329                                  ; Last Update: 08/12/2016 (by Erdogan Tan)
  1330                                  
  1331                                  ; player internal variables and other equates.
  1332                                  BUFFERSIZE      equ     32768	; 32K half buffer size. ; 14/03/2017
  1333                                  ENDOFFILE       equ     BIT0	; flag for knowing end of file
  1334                                  
  1335                                  ;===========================================================================
  1336                                  ; entry: none.  File is already open and [filehandle] filled.
  1337                                  ; exit:  not until the song is finished or the user aborts.
  1338                                  ;
  1339                                  	; 14/10/2017
  1340                                  	; 17/03/2017
  1341                                  PlayWav:
  1342                                         ; load 32768 bytes into half buffer 1
  1343                                  
  1344 00000625 BF[00000100]            	mov     edi, DmaBuffer
  1345 0000062A BA00800000              	mov	edx, BUFFERSIZE
  1346 0000062F E8AAFFFFFF              	call	loadFromFile
  1347                                  
  1348                                  	; 30/07/2020
  1349 00000634 F605[A40B0000]01        	test    byte [eof_flag], ENDOFFILE  ; end of file
  1350 0000063B 7518                    	jnz	short _8 ; yes
  1351                                  			 ; bypass filling dma half buffer 2
  1352                                  
  1353 0000063D C605[A80B0000]01        	mov	byte [audio_flag], 1
  1354                                  
  1355                                  	; load 32768 bytes into half buffer 2
  1356                                  
  1357 00000644 BF[00000100]            	mov	edi, DmaBuffer
  1358 00000649 BA00800000              	mov	edx, BUFFERSIZE
  1359 0000064E 01D7                    	add	edi, edx
  1360 00000650 E889FFFFFF              	call	loadFromFile
  1361                                  
  1362                                  _8:
  1363                                  
  1364                                  ; write last valid index to 31 to start with.
  1365                                  ; The Last Valid Index register tells the DMA engine when to stop playing.
  1366                                  ; 
  1367                                  ; As we progress through the song we change the last valid index to always be
  1368                                  ; something other than the index we're currently playing.  
  1369                                  ;
  1370                                          ;;mov   al, 1
  1371                                          ;mov	al, 31
  1372                                  	;call   setLastValidIndex
  1373                                  
  1374                                  ; create Buffer Descriptor List
  1375                                  ;
  1376                                  ; A buffer descriptor list is a list of pointers and control bits that the
  1377                                  ; DMA engine uses to know where to get the .wav data and how to play it.
  1378                                  ;
  1379                                  ; I set it up to use only 2 buffers of .wav data, and whenever 1 buffer is
  1380                                  ; playing, I refresh the other one with good data.
  1381                                  ;
  1382                                  ;
  1383                                  ; For the control bits, you can specify that the DMA engine fire an interrupt
  1384                                  ; after a buffer has been processed, but I poll the current index register
  1385                                  ; to know when it's safe to update the other buffer.
  1386                                  ;
  1387                                  ; I set the BUP bit, which tells the DMA engine to just play 0's (silence)
  1388                                  ; if it ever runs out of data to play.  Good for safety.
  1389                                  ;
  1390                                  	; 05/03/2017 (32 bit buffer addresses)
  1391                                  
  1392                                  	; 14/02/2017
  1393 00000655 BF[00100000]                    mov     edi, BdlBuffer		; get BDL address
  1394                                  	; ecx <= 32768 ; 29/07/2020
  1395 0000065A 66B91000                        mov     cx, 32 / 2		; make 32 entries in BDL
  1396                                  _0:
  1397                                  
  1398                                  ; set buffer descriptor 0 to start of data file in memory
  1399                                  
  1400 0000065E A1[2C0C0000]                    mov	eax, [DMA_phy_buff]	; Physical address of DMA buffer
  1401 00000663 AB                              stosd				; store dmabuffer1 address
  1402                                  
  1403 00000664 89C2                    	mov	edx, eax ; 05/03/2017
  1404                                  
  1405                                  ;
  1406                                  ; set length to 32k samples. 1 sample is 16bits or 2bytes.
  1407                                  ; Set control (bits 31:16) to BUP, bits 15:0=number of samples.
  1408                                  ; 
  1409                                  
  1410                                  ; VIA VT8235.PDF: (Page 110) (Erdogan Tan, 29/11/2016)
  1411                                  	;
  1412                                  	; 	Audio SGD Table Format
  1413                                  	;	-------------------------------
  1414                                  	;	63   62    61-56    55-32  31-0
  1415                                  	;	--   --   --------  -----  ----
  1416                                  	;	EOL FLAG -reserved- Base   Base
  1417                                  	;		    	    Count  Address
  1418                                  	;		            [23:0] [31:0]
  1419                                  	;	EOL: End Of Link. 
  1420                                  	;	     1 indicates this block is the last of the link.
  1421                                  	;	     If the channel Interrupt on EOL bit is set, then
  1422                                  	;	     an interrupt is generated at the end of the transfer.
  1423                                  	;
  1424                                  	;	FLAG: Block Flag. If set, transfer pauses at the end of this
  1425                                  	;	      block. If the channel Interrupt on FLAG bit is set,
  1426                                  	;	      then an interrupt is generated at the end of this block.
  1427                                  
  1428                                  	FLAG	EQU BIT30
  1429                                  	EOL	EQU BIT31
  1430                                  
  1431                                  	; 08/12/2016 - Erdogan Tan
  1432 00000666 B800800000              	mov	eax, BUFFERSIZE ; DMA half buffer size ; 30/07/2020
  1433 0000066B 01C2                    	add	edx, eax ; 05/03/2017
  1434 0000066D 0D00000040              	or	eax, FLAG
  1435                                  	;or	eax, EOL
  1436 00000672 AB                      	stosd
  1437                                  
  1438                                  ; 2nd buffer:
  1439                                  
  1440 00000673 89D0                            mov	eax, edx ; Physical address of the 2nd half of DMA buffer	
  1441 00000675 AB                      	stosd		 ; store dmabuffer2 address
  1442                                  
  1443                                  ; set length to 64k (32k of two 16 bit samples)
  1444                                  ; Set control (bits 31:16) to BUP, bits 15:0=number of samples
  1445                                  ; 
  1446                                  	; 08/12/2016 - Erdogan Tan
  1447                                  	;mov	eax, BUFFERSIZE
  1448                                  	;or	eax, EOL
  1449                                  	; 29/07/2020
  1450                                  	;or	eax, FLAG
  1451 00000676 B800800040              	mov	eax, BUFFERSIZE | FLAG
  1452 0000067B AB                      	stosd
  1453                                  
  1454 0000067C E2E0                            loop    _0
  1455                                  
  1456                                  	; 30/07/2020
  1457 0000067E 814FFC00000080          	or	dword [edi-4], EOL
  1458                                  
  1459                                  ;
  1460                                  ; tell the DMA engine where to find our list of Buffer Descriptors.
  1461                                  ; this 32bit value is a flat mode memory offset (ie no segment:offset)
  1462                                  ;
  1463                                  ; write buffer descriptor list address
  1464                                  ;
  1465                                  	; Extended Audio Status (2Ah)
  1466 00000685 B82A000000              	mov	eax, CODEC_EXT_AUDIO_CTRL_REG ; 2Ah 
  1467 0000068A E8B0FEFFFF              	call	codec_read
  1468                                  	;and     eax, 0FFFFh - 2	; clear DRA (BIT1)
  1469                                  	; 03/08/2020
  1470 0000068F 25FDC7FFFF              	and	eax, ~3802h  ; modification for AD1980 
  1471                                  	;;or	eax, 1		; set VRA (BIT0)
  1472                                  	;or	eax, 5  	; VRA (BIT0) & S/PDIF (BIT2) ; 14/11/2016
  1473 00000694 0C05                    	or	al, 5 ; 03/08/2020
  1474 00000696 BA2A000000              	mov	edx, CODEC_EXT_AUDIO_CTRL_REG
  1475 0000069B E8CBFEFFFF              	call	codec_write
  1476                                  	;jc	short cconfig_error
  1477                                  
  1478                                  set_sample_rate:
  1479                                  	;movzx	eax, word [audio_freq]
  1480 000006A0 66A1[BA0B0000]          	mov	ax, [audio_freq]
  1481 000006A6 BA2C000000              	mov	edx, CODEC_PCM_FRONT_DACRATE_REG ; 2Ch ; PCM Front DAC Rate
  1482 000006AB E8BBFEFFFF              	call	codec_write
  1483                                  
  1484                                  	; 14/10/2017
  1485 000006B0 A1[280C0000]                    mov	eax, [BDL_phy_buff] ; Physical address of the BDL
  1486                                  	  
  1487                                  	; 12/11/2016 - Erdogan Tan 
  1488                                  	; (Ref: KolibriOS, vt823x.asm, 'create_primary_buff')
  1489 000006B5 BA04000000              	mov	edx, VIADEV_PLAYBACK + VIA_REG_OFFSET_TABLE_PTR
  1490 000006BA E864FEFFFF                      call	ctrl_io_w32
  1491                                  
  1492                                  	;call	codec_check_ready
  1493                                  
  1494 000006BF 66BA0200                  	mov	dx, VIADEV_PLAYBACK + VIA_REG_OFS_PLAYBACK_VOLUME_L
  1495                                          ;mov	eax, 2	; 31
  1496                                  	; 30/07/2020
  1497                                  	;mov	al, 31
  1498                                          ;sub	al, [audio_master_volume_l]
  1499 000006C3 A0[300C0000]            	mov	al, [audio_master_volume]  ; 14/10/2017
  1500 000006C8 E83EFEFFFF              	call	ctrl_io_w8
  1501                                  
  1502                                  	;call	codec_check_ready
  1503                                  
  1504 000006CD 66BA0300                        mov     dx, VIADEV_PLAYBACK + VIA_REG_OFS_PLAYBACK_VOLUME_R
  1505                                          ;mov	ax, 2	; 31
  1506                                  	; 30/07/2020
  1507                                  	;mov	al, 31
  1508                                          ;sub	al, [audio_master_volume_r]
  1509 000006D1 A0[300C0000]            	mov	al, [audio_master_volume]  ;14/10/2017
  1510 000006D6 E830FEFFFF              	call    ctrl_io_w8
  1511                                  
  1512                                  	;call	codec_check_ready
  1513                                  ;
  1514                                  ;
  1515                                  ; All set. Let's play some music.
  1516                                  ;
  1517                                  ;
  1518                                         	;mov    dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_STOP_IDX
  1519                                          ;mov    ax, VIA8233_REG_TYPE_16BIT or VIA8233_REG_TYPE_STEREO or 0xfffff or 0xff000000
  1520                                          ;call   ctrl_io_w32
  1521                                  
  1522                                  	;call	codec_check_ready
  1523                                  
  1524                                  	; 08/12/2016
  1525                                  	; 07/10/2016
  1526                                          ;mov    al, 1	
  1527                                  	; 29/07/2020
  1528                                  	;mov	al, 31
  1529 000006DB B0FF                    	mov	al, 0FFh
  1530 000006DD E823000000              	call    set_VT8233_LastValidIndex
  1531                                  
  1532                                  	; 25/08/2020
  1533                                  	;mov	byte [audio_play_cmd], 1 ; play command (do not stop) !
  1534                                  	
  1535                                  	; 14/10/2017
  1536 000006E2 A0[A80B0000]            	mov 	al, [audio_flag]
  1537 000006E7 0431                    	add	al, '1'
  1538 000006E9 B44E                    	mov	ah, 4Eh
  1539 000006EB 66A300800B00            	mov 	[0B8000h], ax ; Display current buffer number
  1540                                  
  1541                                  vt8233_play: ; continue to play
  1542                                  	; 22/04/2017
  1543                                          ;mov	al, VIA_REG_CTRL_INT
  1544                                         	;or	al, VIA_REG_CTRL_START
  1545                                          ;;mov	al, VIA_REG_CTRL_AUTOSTART + VIA_REG_CTRL_START
  1546                                  	; 29/07/2020
  1547 000006F1 B0A1                    	mov	al, VIA_REG_CTRL_AUTOSTART + VIA_REG_CTRL_START + VIA_REG_CTRL_INT_FLAG
  1548 000006F3 66BA0100                	mov     dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_CONTROL
  1549 000006F7 E80FFEFFFF                      call    ctrl_io_w8
  1550                                  	;call	codec_check_ready
  1551                                  	;retn
  1552                                  	;jmp	codec_check_ready
  1553                                  
  1554                                  	; 14/10/2017
  1555 000006FC C605[310C0000]1B        	mov	byte [volume_level], 1Fh-04h ; initial value
  1556                                  
  1557 00000703 EB43                    	jmp	short p_loop ; 14/10/2017
  1558                                  
  1559                                  ;input AL = index # to stop on
  1560                                  set_VT8233_LastValidIndex:
  1561                                  	; 29/07/2020
  1562                                  	; 10/06/2017
  1563                                  	; 21/04/2017 (TRDOS 386 kernel, 'audio.s')
  1564                                  	; 24/03/2017 - 'PLAYER.COM' ('via_wav.asm' - 29/11/2016) 
  1565                                  	; 19/11/2016
  1566                                  	; 14/11/2016 - Erdogan Tan (Ref: VIA VT8235.PDF, Page 110)
  1567                                  	; 12/11/2016 - Erdogan Tan
  1568                                  	; (Ref: KolibriOS, vt823x.asm, 'create_primary_buff')
  1569                                  	;push	edx
  1570                                  	;push	ax
  1571 00000705 50                      	push	eax ; 29/07/2020
  1572                                  	;push	ecx
  1573 00000706 0FB705[BA0B0000]        	movzx	eax, word [audio_freq] ; Hertz
  1574 0000070D BA00001000              	mov	edx, 100000h ; 2^20 = 1048576
  1575 00000712 F7E2                    	mul	edx
  1576 00000714 B980BB0000              	mov	ecx, 48000	
  1577 00000719 F7F1                    	div	ecx
  1578                                  	;and	eax, 0FFFFFh
  1579                                  	;pop	ecx
  1580                                  	;pop	dx 
  1581 0000071B 5A                      	pop	edx ; 29/07/2020
  1582 0000071C C1E218                  	shl	edx, 24  ; STOP Index Setting: Bit 24 to 31
  1583 0000071F 09D0                    	or	eax, edx
  1584                                  	; 19/11/2016
  1585 00000721 803D[B90B0000]10        	cmp	byte [audio_bps], 16
  1586 00000728 7505                    	jne	short sLVI_1
  1587 0000072A 0D00002000              	or	eax, VIA8233_REG_TYPE_16BIT
  1588                                  sLVI_1:
  1589 0000072F 803D[B80B0000]02        	cmp	byte [audio_stmo], 2
  1590 00000736 7505                    	jne	short sLVI_2
  1591 00000738 0D00001000              	or	eax, VIA8233_REG_TYPE_STEREO
  1592                                  sLVI_2:
  1593 0000073D BA08000000              	mov     edx, VIADEV_PLAYBACK + VIA_REG_OFFSET_STOP_IDX
  1594 00000742 E8DCFDFFFF                      call    ctrl_io_w32
  1595                                  	;call	codec_check_ready
  1596                                  	;pop	edx
  1597 00000747 C3                      	retn
  1598                                  
  1599                                  	; 03/08/2020
  1600                                  	; 30/07/2020
  1601                                  	; 15/10/2017
  1602                                  	; 14/10/2017
  1603                                  	; 21/04/2017
  1604                                  	; 17/03/2017
  1605                                  	; 05/03/2017 (TRDOS 386)
  1606                                  	; 14/02/2017
  1607                                  	; 13/02/2017
  1608                                  	; 08/12/2016
  1609                                  	; 28/11/2016
  1610                                  p_loop:
  1611                                  	; 15/10/2017
  1612 00000748 803D[A50B0000]00        	cmp	byte [srb], 0
  1613 0000074F 7628                    	jna	short q_loop
  1614                                  
  1615 00000751 C605[A50B0000]00        	mov	byte [srb], 0
  1616                                  
  1617                                  	; 03/08/2020
  1618                                  	;; 01/08/2020
  1619                                  	;mov	al, '0'
  1620                                  	;mov	ah, 4Eh
  1621                                  	;mov 	[0B8000h], ax ; Display current buffer number
  1622                                  
  1623                                  	; 30/07/2020
  1624 00000758 E8E1000000              	call	ac97_int_ack
  1625                                  	
  1626 0000075D BF[00000100]            	mov	edi, DmaBuffer
  1627 00000762 BA00800000              	mov	edx, BUFFERSIZE ; DMA half buffer size
  1628                                  
  1629 00000767 803D[A80B0000]00        	cmp	byte [audio_flag], 0
  1630 0000076E 7602                    	jna	short p_load_buffer
  1631                                  
  1632 00000770 01D7                    	add	edi, edx  ; 2nd half of DMA buffer
  1633                                  
  1634                                  p_load_buffer:
  1635 00000772 E867FEFFFF              	call	loadFromFile
  1636 00000777 7212                    	jc	short p_return  ; EOF or read error. 
  1637                                  
  1638                                  q_loop:
  1639                                  	; 15/10/2017
  1640 00000779 B401                    	mov     ah, 1		; any key pressed?
  1641 0000077B CD32                    	int     32h		; no, Loop.
  1642 0000077D 7411                    	jz	short r_loop
  1643                                  
  1644 0000077F B400                    	mov     ah, 0		; flush key buffer...
  1645 00000781 CD32                    	int     32h
  1646                                  
  1647                                  	; 14/10/2017
  1648                                  	; 09/10/2017 (playmod5.s)
  1649 00000783 3C2B                    	cmp	al, '+' ; increase sound volume
  1650 00000785 743B                    	je	short inc_volume_level
  1651 00000787 3C2D                    	cmp	al, '-'
  1652 00000789 745A                    	je	short dec_volume_level
  1653                                  
  1654                                  p_return:
  1655                                  	; 25/08/2020
  1656                                  	;mov	byte [audio_play_cmd], 0
  1657                                  
  1658                                  	; 23/06/2017
  1659                                  _exit_:
  1660                                  	; 24/06/2017
  1661                                  	; finished with song, stop everything
  1662                                  	;mov	al, VIA_REG_CTRL_INT
  1663                                  	;or	al, VIA_REG_CTRL_TERMINATE
  1664                                  	;mov	dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_CONTROL
  1665                                  	;call	ctrl_io_w8
  1666                                  
  1667 0000078B E916FEFFFF              	jmp	channel_reset
  1668                                  
  1669                                  r_loop:
  1670                                  	; 30/07/2020
  1671 00000790 66FF0D[320C0000]        	dec	word [counter]
  1672 00000797 66A1[320C0000]          	mov	ax, [counter]
  1673 0000079D 663DFF7F                	cmp	ax, 32767
  1674 000007A1 770B                    	ja	short t_loop
  1675 000007A3 6609C0                  	or	ax, ax
  1676 000007A6 7402                    	jz	short s_loop
  1677 000007A8 EB9E                    	jmp	p_loop
  1678                                  s_loop:
  1679 000007AA B020                    	mov	al, ' '
  1680 000007AC EB0A                    	jmp	short v_loop
  1681                                  t_loop:
  1682 000007AE 6683F8FF                	cmp	ax, 65535
  1683 000007B2 7402                    	je	short u_loop
  1684 000007B4 EB92                    	jmp	p_loop
  1685                                  u_loop:
  1686 000007B6 B02E                    	mov	al, '.'
  1687                                  v_loop:
  1688 000007B8 B40E                    	mov	ah, 0Eh
  1689 000007BA 66A302800B00            	mov	[0B8002h], ax
  1690 000007C0 EB86                    	jmp	p_loop
  1691                                  	
  1692                                  	; 09/10/2017 (playmod5.s)
  1693                                  	; 24/06/2017 (wavplay2.s)
  1694                                  inc_volume_level:
  1695 000007C2 8A0D[310C0000]          	mov	cl, [volume_level]
  1696 000007C8 80F91F                  	cmp	cl, 1Fh ; 31
  1697                                  	;jnb	short p_loop ; 15/10/2017
  1698 000007CB 0F8377FFFFFF            	jnb	p_loop ; 30/07/2020
  1699 000007D1 FEC1                    	inc	cl
  1700                                  change_volume_level:
  1701 000007D3 880D[310C0000]          	mov	[volume_level], cl
  1702                                  	; 30/07/2020
  1703 000007D9 88CD                    	mov	ch, cl ; same volume level for L & R
  1704                                  	; Set Master Volume Level
  1705 000007DB E818000000              	call	set_master_volume_level ; 14/10/2017
  1706 000007E0 E963FFFFFF              	jmp	p_loop ; 15/10/2017
  1707                                  dec_volume_level:
  1708 000007E5 8A0D[310C0000]          	mov	cl, [volume_level]
  1709 000007EB 80F901                  	cmp	cl, 1 ; 1
  1710 000007EE 0F8654FFFFFF            	jna	p_loop ; 15/10/2017
  1711 000007F4 FEC9                    	dec	cl
  1712 000007F6 EBDB                    	jmp	short change_volume_level
  1713                                  
  1714                                  set_master_volume_level:
  1715                                  vt8233_volume:
  1716                                  	; 30/07/2020
  1717                                  	; 14/10/2017
  1718                                  	; set VT8237R (vt8233) sound volume level
  1719                                  	; 24/04/2017 (TRDOS 386 kernel, 'audio.s')
  1720                                  	; 22/04/2017
  1721                                  	; bl = component (0 = master/playback/lineout volume)
  1722                                  	; cl = left channel volume level (0 to 31)
  1723                                  	; ch = right channel volume level (0 to 31)
  1724                                  
  1725                                  	;or	bl, bl
  1726                                  	;jnz	short vt8233_vol_1 ; temporary !
  1727 000007F8 66B81F1F                	mov	ax, 1F1Fh ; 31,31
  1728                                  	;cmp	cl, al
  1729                                  	;ja	short vt8233_vol_1 ; temporary !
  1730                                  	;cmp	ch, ah
  1731                                  	;ja	short vt8233_vol_1 ; temporary !
  1732                                  	;mov	[audio_master_volume], cx
  1733                                  
  1734                                  	; 30/07/2020
  1735 000007FC 6629C8                  	sub	ax, cx
  1736 000007FF 66A3[300C0000]          	mov	[audio_master_volume], ax
  1737                                  	;
  1738 00000805 BA02000000              	mov	edx, CODEC_MASTER_VOL_REG ; 02h ; Line Out
  1739 0000080A E85CFDFFFF              	call	codec_write
  1740                                  vt8233_vol_1:
  1741 0000080F C3                      	retn
  1742                                  
  1743                                  delay_100ms:
  1744                                  	; wait 100 ms
  1745                                  	;mov	ecx, 400  ; 400*0.25ms  ; 29/05/2017
  1746 00000810 B964000000              	mov	ecx, 100  ; 23/06/2017
  1747                                  _delay_x_ms:
  1748 00000815 E803000000              	call	delay1_4ms
  1749 0000081A E2F9                            loop	_delay_x_ms
  1750 0000081C C3                      	retn
  1751                                  
  1752                                  ;       delay1_4ms - Delay for 1/4 millisecond.
  1753                                  ;	    1mS = 1000us
  1754                                  ;       Entry:
  1755                                  ;         None
  1756                                  ;       Exit:
  1757                                  ;	  None
  1758                                  ;
  1759                                  ;       Modified:
  1760                                  ;         None
  1761                                  ;
  1762                                  PORTB		EQU	061h
  1763                                  REFRESH_STATUS	EQU	010h	; Refresh signal status
  1764                                  
  1765                                  	; 29/05/2017
  1766                                  	; 05/03/2017 (TRDOS 386)
  1767                                  delay1_4ms:
  1768 0000081D 50                              push    eax 
  1769 0000081E 51                              push    ecx
  1770                                          ;mov	cl, 16		; close enough.
  1771 0000081F B10C                    	mov	cl, 12 ; + INT 34h delay	
  1772                                  
  1773                                  	;in	al, PORTB
  1774                                  	
  1775 00000821 66BA6100                	mov	dx, PORTB
  1776 00000825 28E4                    	sub	ah, ah ; 0 ; inb
  1777 00000827 CD34                    	int	34h
  1778                                  	
  1779 00000829 2410                    	and	al, REFRESH_STATUS
  1780 0000082B 88C5                    	mov	ch, al		; Start toggle state
  1781                                  _d4ms1:	
  1782                                  	;in	al, PORTB	; Read system control port
  1783                                  	
  1784                                  	;mov	ah, 0 ; inb
  1785                                  	;mov	dx, PORTB
  1786 0000082D CD34                    	int	34h
  1787                                  	
  1788 0000082F 2410                    	and	al, REFRESH_STATUS ; Refresh toggles 15.085 microseconds
  1789 00000831 38C5                    	cmp	ch, al
  1790 00000833 74F8                    	je	short _d4ms1	; Wait for state change
  1791                                  
  1792 00000835 88C5                    	mov	ch, al		; Update with new state
  1793 00000837 FEC9                    	dec	cl
  1794 00000839 75F2                    	jnz	short _d4ms1
  1795                                  
  1796 0000083B 59                              pop     ecx
  1797 0000083C 58                              pop     eax
  1798 0000083D C3                              retn
  1799                                  
  1800                                  ac97_int_ack:
  1801                                  	; 30/07/2020
  1802                                  	; Interrupt Handler for VIA VT8237R Audio Controller
  1803                                  	;(Derived from TRDOS 386 kernel, 'audio.s', 14/10/2017)
  1804                                  	; Note: I have moved following code here because
  1805                                  	; callback service can not use int 35h (direct I/O) interrupt
  1806                                  	; (we must check VT8233 interrupt status and
  1807                                  	; clear/ack interrupt after callback service while [srb] = 1)
  1808                                  
  1809                                  	; 30/07/2020
  1810                                  	; mov	byte [srb], 1
  1811                                  
  1812                                  	; [srb] = 1
  1813                                  
  1814                                  	; 29/07/2020
  1815                                  	; 15/10/2017
  1816                                  	; 14/10/2017 
  1817                                  	; 09/10/2017, 10/10/2017, 12/10/2017
  1818                                  	; 13/06/2017
  1819                                  	; 21/04/2017 (TRDOS 386 kernel, 'audio.s')
  1820                                  	; 24/03/2017 - 'PLAYER.COM' ('player.asm') 
  1821                                  
  1822                                  	;push	eax ; * must be saved !
  1823                                  	;push	edx
  1824                                  	;push	ecx
  1825                                  	;push	ebx ; * must be saved !
  1826                                  	;push	esi
  1827                                  	;push	edi
  1828                                  
  1829                                  	; 14/10/2017
  1830                                  	;mov	byte [srb], 1	
  1831                                  
  1832                                  	;cmp	byte [audio_busy], 1
  1833                                  	;jnb	short _ih0 ; 09/10/2017
  1834                                  
  1835                                  	;mov	byte [audio_flag_eol], 0
  1836                                  
  1837                                  	; 25/08/2020
  1838                                  	;mov	dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_STATUS
  1839                                          ;call	ctrl_io_r8
  1840                                  	;
  1841                                  	;test	al, VIA_REG_STAT_ACTIVE
  1842                                          ;;jz	short _ih0 ; 09/10/2017
  1843                                  	;jz	short _ih1 ; 25/08/2020
  1844                                  	;
  1845                                          ;and	al, VIA_REG_STAT_EOL + VIA_REG_STAT_FLAG + VIA_REG_STAT_STOPPED
  1846                                  	;mov	[audio_flag_eol], al
  1847                                          ;;jz	short _ih0 ; 09/10/2017
  1848                                  	;jz	short _ih1 ; 25/08/2020
  1849                                  
  1850                                  	; 09/10/2017
  1851                                  	;mov	byte [audio_busy], 1
  1852                                  
  1853                                  	;cmp	byte [audio_play_cmd], 1
  1854                                  	;jnb	short _ih1 ; 10/10/2017
  1855                                  	;
  1856                                  	;call	channel_reset
  1857                                  
  1858                                  	; 25/08/2020
  1859                                  _ih0:
  1860                                  	; 09/10/2017
  1861                                  	;mov	al, [audio_flag_eol]   ;; ack ;;
  1862 0000083E B007                    	mov	al, 7 ; 25/08/2020
  1863 00000840 66BA0000                	mov     dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_STATUS
  1864 00000844 E8C2FCFFFF              	call    ctrl_io_w8
  1865                                  	;jmp	short _ih3
  1866                                  
  1867                                  	;; 25/08/2020
  1868                                  	;cmp	byte [audio_play_cmd], 1
  1869                                  	;jnb	short _ih1
  1870                                  	;
  1871                                  	;call	channel reset
  1872                                  	;
  1873                                  	;stc
  1874                                  	;retn
  1875                                  
  1876                                  _ih1:
  1877                                  ;vt8233_tuneLoop:
  1878                                  ;	;mov	al, [audio_flag_eol]   ;; ack ;;
  1879                                  ;	mov	al, 7 ; 25/08/2020
  1880                                  ;	mov     dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_STATUS
  1881                                  ;	call    ctrl_io_w8
  1882                                  
  1883                                  	; 29/07/2020
  1884                                  	; 12/10/2017
  1885                                  	;mov	byte [audio_flag], 0 ; Reset
  1886                                  
  1887                                  	; 10/10/2017
  1888                                  	; 09/10/2017
  1889                                  	;test	byte [audio_flag_eol], VIA_REG_STAT_FLAG
  1890                                  	;jz	short _ih2 ; EOL
  1891                                  
  1892                                  	; 29/07/2020
  1893                                  	; 14/10/2017
  1894                                  	;test	byte [audio_flag_eol], VIA_REG_STAT_EOL
  1895                                  	;jnz	short _ih2 ; EOL
  1896                                  	;		   ; (Half Buffer 2 has been completed 
  1897                                  	;		   ; and Half Buffer 1 will be played.)
  1898                                  	
  1899                                  	; FLAG  
  1900                                  	; (Half Buffer 1 has been completed 
  1901                                  	;  and Half Buffer 2 will be played.)
  1902                                  
  1903                                  	; 14/10/2017
  1904                                  	;; (Continue to play.)
  1905                                  	;mov	al, VIA_REG_CTRL_INT
  1906                                         	;or	al, VIA_REG_CTRL_START
  1907                                         	;mov	dx, VIADEV_PLAYBACK + VIA_REG_OFFSET_CONTROL
  1908                                          ;call	ctrl_io_w8
  1909                                  	; 12/10/2017
  1910                                  	;mov	byte [audio_flag], 1 
  1911                                  	; 29/07/2020
  1912                                  	;inc	byte [audio_flag] ; = 1
  1913                                  _ih2: 
  1914                                  	; Update half buffer 2 while playing half buffer 1 (FLAG)
  1915                                  	; Update half buffer 1 while playing half buffer 2 (EOL)
  1916                                  	
  1917                                  	; 30/07/2020
  1918                                  	;mov 	al, [audio_flag]
  1919                                  	;add	al, '1'
  1920                                  	;mov	ah, 4Eh
  1921                                  	;mov 	[0B8000h], ax ; Display current buffer number
  1922                                  
  1923                                  	; 25/08/2020
  1924                                  	; switch flag value ;
  1925 00000849 8035[A80B0000]01        	xor	byte [audio_flag], 1 ; 10/10/2017 
  1926                                  
  1927                                  	; 12/10/2017
  1928                                  	; [audio_flag] = 0 : Playing dma half buffer 2 (just after FLAG)
  1929                                  			   ; Next buffer (to update) is dma half buff 1
  1930                                  	; 	       = 1 : Playing dma half buffer 1 (just after EOL)
  1931                                  			   ; Next buffer (to update) is dma half buff 2
  1932                                  	; 30/07/2020
  1933                                  	; 15/10/2017
  1934                                  	;mov	byte [srb], 1
  1935                                  _ih3:	
  1936                                  	; 28/05/2017
  1937                                  	;mov	byte [audio_busy], 0 ; 09/10/2017
  1938                                  	;
  1939                                  	;pop	edi
  1940                                  	;pop	esi
  1941                                  	;pop	ebx ; * must be restored !
  1942                                  	;pop	ecx
  1943                                  	;pop	edx
  1944                                  	;pop	eax ; * must be restored !
  1945                                  
  1946                                  _ih4:	; 14/10/2017
  1947                                  	;sys	_rele ; return from callback service 
  1948                                  	;; we must not come here !
  1949                                  	;sys	_exit
  1950                                  
  1951                                  	; 30/07/2020
  1952 00000850 C3                      	retn
  1953                                  
  1954                                  ; ---------------------------------------------------------------------
  1955                                  
  1956                                  	; 05/03/2017 (TRDOS 386)
  1957                                  	; 13/11/2016 - Erdogan Tan
  1958                                  write_ac97_dev_info:
  1959                                  	; BUS/DEV/FN
  1960                                  	;	00000000BBBBBBBBDDDDDFFF00000000
  1961                                  	; DEV/VENDOR
  1962                                  	;	DDDDDDDDDDDDDDDDVVVVVVVVVVVVVVVV
  1963                                  
  1964 00000851 8B35[B00B0000]          	mov	esi, [dev_vendor]
  1965 00000857 6689F0                  	mov	ax, si
  1966 0000085A 0FB6D8                  	movzx	ebx, al
  1967 0000085D 88DA                    	mov	dl, bl
  1968 0000085F 80E30F                  	and	bl, 0Fh
  1969 00000862 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  1970 00000868 A2[0E0B0000]            	mov	[msgVendorId+3], al
  1971 0000086D 88D3                    	mov	bl, dl
  1972 0000086F C0EB04                  	shr	bl, 4
  1973 00000872 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  1974 00000878 A2[0D0B0000]            	mov	[msgVendorId+2], al
  1975 0000087D 88E3                    	mov	bl, ah
  1976 0000087F 88DA                    	mov	dl, bl
  1977 00000881 80E30F                  	and	bl, 0Fh
  1978 00000884 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  1979 0000088A A2[0C0B0000]            	mov	[msgVendorId+1], al
  1980 0000088F 88D3                    	mov	bl, dl
  1981 00000891 C0EB04                  	shr	bl, 4
  1982 00000894 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  1983 0000089A A2[0B0B0000]            	mov	[msgVendorId], al
  1984 0000089F C1EE10                  	shr	esi, 16
  1985 000008A2 6689F0                  	mov	ax, si
  1986 000008A5 88C3                    	mov	bl, al
  1987 000008A7 88DA                    	mov	dl, bl
  1988 000008A9 80E30F                  	and	bl, 0Fh
  1989 000008AC 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  1990 000008B2 A2[1F0B0000]            	mov	[msgDevId+3], al
  1991 000008B7 88D3                    	mov	bl, dl
  1992 000008B9 C0EB04                  	shr	bl, 4
  1993 000008BC 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  1994 000008C2 A2[1E0B0000]            	mov	[msgDevId+2], al
  1995 000008C7 88E3                    	mov	bl, ah
  1996 000008C9 88DA                    	mov	dl, bl
  1997 000008CB 80E30F                  	and	bl, 0Fh
  1998 000008CE 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  1999 000008D4 A2[1D0B0000]            	mov	[msgDevId+1], al
  2000 000008D9 88D3                    	mov	bl, dl
  2001 000008DB C0EB04                  	shr	bl, 4
  2002 000008DE 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2003 000008E4 A2[1C0B0000]            	mov	[msgDevId], al
  2004                                  
  2005 000008E9 8B35[AC0B0000]          	mov	esi, [bus_dev_fn]
  2006 000008EF C1EE08                  	shr	esi, 8
  2007 000008F2 6689F0                  	mov	ax, si
  2008 000008F5 88C3                    	mov	bl, al
  2009 000008F7 88DA                    	mov	dl, bl
  2010 000008F9 80E307                  	and	bl, 7 ; bit 0,1,2
  2011 000008FC 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2012 00000902 A2[430B0000]            	mov	[msgFncNo+1], al
  2013 00000907 88D3                    	mov	bl, dl
  2014 00000909 C0EB03                  	shr	bl, 3
  2015 0000090C 88DA                    	mov	dl, bl
  2016 0000090E 80E30F                  	and	bl, 0Fh
  2017 00000911 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2018 00000917 A2[350B0000]            	mov	[msgDevNo+1], al
  2019 0000091C 88D3                    	mov	bl, dl
  2020 0000091E C0EB04                  	shr	bl, 4
  2021 00000921 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2022 00000927 A2[340B0000]            	mov	[msgDevNo], al
  2023 0000092C 88E3                    	mov	bl, ah
  2024 0000092E 88DA                    	mov	dl, bl
  2025 00000930 80E30F                  	and	bl, 0Fh
  2026 00000933 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2027 00000939 A2[290B0000]            	mov	[msgBusNo+1], al
  2028 0000093E 88D3                    	mov	bl, dl
  2029 00000940 C0EB04                  	shr	bl, 4
  2030 00000943 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2031 00000949 A2[280B0000]            	mov	[msgBusNo], al
  2032                                  
  2033 0000094E 66A1[AA0B0000]          	mov	ax, [ac97_io_base]
  2034 00000954 88C3                    	mov	bl, al
  2035 00000956 88DA                    	mov	dl, bl
  2036 00000958 80E30F                  	and	bl, 0Fh
  2037 0000095B 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2038 00000961 A2[5C0B0000]            	mov	[msgIOBaseAddr+3], al
  2039 00000966 88D3                    	mov	bl, dl
  2040 00000968 C0EB04                  	shr	bl, 4
  2041 0000096B 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2042 00000971 A2[5B0B0000]            	mov	[msgIOBaseAddr+2], al
  2043 00000976 88E3                    	mov	bl, ah
  2044 00000978 88DA                    	mov	dl, bl
  2045 0000097A 80E30F                  	and	bl, 0Fh
  2046 0000097D 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2047 00000983 A2[5A0B0000]            	mov	[msgIOBaseAddr+1], al
  2048 00000988 88D3                    	mov	bl, dl
  2049 0000098A C0EB04                  	shr	bl, 4
  2050 0000098D 8A83[CB0A0000]          	mov	al, [ebx+hex_chars]
  2051 00000993 A2[590B0000]            	mov	[msgIOBaseAddr], al
  2052                                  
  2053                                  	; 24/11/2016
  2054 00000998 30E4                    	xor	ah, ah
  2055 0000099A A0[A90B0000]            	mov	al, [ac97_int_ln_reg]
  2056 0000099F B10A                    	mov	cl, 10
  2057 000009A1 F6F1                    	div	cl
  2058 000009A3 660105[640B0000]        	add	[msgIRQ], ax
  2059 000009AA 20C0                    	and	al, al
  2060 000009AC 750D                    	jnz	short _pmi
  2061 000009AE A0[650B0000]            	mov	al, [msgIRQ+1]
  2062 000009B3 B420                    	mov	ah, ' '
  2063 000009B5 66A3[640B0000]          	mov	[msgIRQ], ax
  2064                                  _pmi:
  2065                                  	; EBX = Message address
  2066                                  	; ECX = Max. message length (or stop on ZERO character)
  2067                                  	;	(1 to 255)
  2068                                  	; DL  = Message color (07h = light gray, 0Fh = white) 
  2069                                       	sys 	_msg, msgAC97Info, 255, 07h
  2069                              <1> 
  2069                              <1> 
  2069                              <1> 
  2069                              <1> 
  2069                              <1>  %if %0 >= 2
  2069 000009BB BB[DC0A0000]        <1>  mov ebx, %2
  2069                              <1>  %if %0 >= 3
  2069 000009C0 B9FF000000          <1>  mov ecx, %3
  2069                              <1>  %if %0 = 4
  2069 000009C5 BA07000000          <1>  mov edx, %4
  2069                              <1>  %endif
  2069                              <1>  %endif
  2069                              <1>  %endif
  2069 000009CA B823000000          <1>  mov eax, %1
  2069                              <1> 
  2069 000009CF CD40                <1>  int 40h
  2070 000009D1 C3                              retn
  2071                                  
  2072                                  ;=============================================================================
  2073                                  ;               preinitialized data
  2074                                  ;=============================================================================
  2075                                  
  2076                                  noDevMsg:
  2077 000009D2 4572726F723A20556E-     	db "Error: Unable to find VIA VT8233 based audio device!",CR,LF,0
  2077 000009DB 61626C6520746F2066-
  2077 000009E4 696E64205649412056-
  2077 000009ED 543832333320626173-
  2077 000009F6 656420617564696F20-
  2077 000009FF 646576696365210D0A-
  2077 00000A08 00                 
  2078                                  
  2079                                  CodecErrMsg:
  2080 00000A09 436F64656320457272-     	db	"Codec Error !", CR,LF,0
  2080 00000A12 6F7220210D0A00     
  2081                                  
  2082                                  msg_usage:
  2083 00000A19 75736167653A207761-     	db	'usage: wavplay filename.wav',10,13,0
  2083 00000A22 76706C61792066696C-
  2083 00000A2B 656E616D652E776176-
  2083 00000A34 0A0D00             
  2084                                  Credits:
  2085 00000A37 54696E792057415620-     	db	'Tiny WAV Player for TRDOS 386 by Erdogan Tan. '
  2085 00000A40 506C6179657220666F-
  2085 00000A49 72205452444F532033-
  2085 00000A52 383620627920457264-
  2085 00000A5B 6F67616E2054616E2E-
  2085 00000A64 20                 
  2086 00000A65 417567757374203230-     	db	'August 2020.',10,13,0
  2086 00000A6E 32302E0A0D00       
  2087 00000A74 31352F31302F323031-     	db	'15/10/2017', 10,13,0
  2087 00000A7D 370A0D00           
  2088 00000A81 32352F30382F323032-     	db	'25/08/2020', 10,13,0 
  2088 00000A8A 300A0D00           
  2089                                  
  2090                                  noFileErrMsg:
  2091 00000A8E 4572726F723A206669-     	db	'Error: file not found.',10,13,0
  2091 00000A97 6C65206E6F7420666F-
  2091 00000AA0 756E642E0A0D00     
  2092                                  
  2093                                  trdos386_err_msg:
  2094 00000AA7 5452444F5320333836-     	db	'TRDOS 386 System call error !',10,13,0
  2094 00000AB0 2053797374656D2063-
  2094 00000AB9 616C6C206572726F72-
  2094 00000AC2 20210A0D00         
  2095                                  
  2096                                  FileHandle:	
  2097 00000AC7 FFFFFFFF                	dd	-1
  2098                                  
  2099                                  ; 13/11/2016
  2100 00000ACB 303132333435363738-     hex_chars:	db "0123456789ABCDEF", 0
  2100 00000AD4 3941424344454600   
  2101 00000ADC 414339372041756469-     msgAC97Info:	db "AC97 Audio Controller & Codec Info", 0Dh, 0Ah 
  2101 00000AE5 6F20436F6E74726F6C-
  2101 00000AEE 6C6572202620436F64-
  2101 00000AF7 656320496E666F0D0A 
  2102 00000B00 56656E646F72204944-     		db "Vendor ID: "
  2102 00000B09 3A20               
  2103 00000B0B 303030306820446576-     msgVendorId:	db "0000h Device ID: "
  2103 00000B14 6963652049443A20   
  2104 00000B1C 30303030680D0A          msgDevId:	db "0000h", 0Dh, 0Ah
  2105 00000B23 4275733A20              		db "Bus: "
  2106 00000B28 303068204465766963-     msgBusNo:	db "00h Device: "
  2106 00000B31 653A20             
  2107 00000B34 3030682046756E6374-     msgDevNo:	db "00h Function: "
  2107 00000B3D 696F6E3A20         
  2108 00000B42 303068                  msgFncNo:	db "00h"
  2109 00000B45 0D0A                    		db 0Dh, 0Ah
  2110 00000B47 492F4F204261736520-     		db "I/O Base Address: "
  2110 00000B50 416464726573733A20 
  2111 00000B59 303030306820495251-     msgIOBaseAddr:	db "0000h IRQ: "
  2111 00000B62 3A20               
  2112 00000B64 3030                    msgIRQ:		dw 3030h
  2113 00000B66 0D0A00                  		db 0Dh, 0Ah, 0
  2114 00000B69 53616D706C65205261-     msgSampleRate:	db "Sample Rate: "
  2114 00000B72 74653A20           
  2115 00000B76 303030303020487A20-     msgHertz:	db "00000 Hz ", 0
  2115 00000B7F 00                 
  2116 00000B80 3820626974732000        msg8Bits:	db "8 bits ", 0
  2117 00000B88 4D6F6E6F0D0A00          msgMono:	db "Mono", 0Dh, 0Ah, 0
  2118 00000B8F 313620626974732024      msg16Bits:	db "16 bits ", "$" 
  2119 00000B98 53746572656F0D0A00      msgStereo:	db "Stereo", 0Dh, 0Ah, 0
  2120                                  
  2121                                  ;; 13/11/2016 - Erdogan Tan (Ref: KolibriOS, codec.inc)
  2122                                  ;codec_id:	   dd 0
  2123                                  ;codec_chip_id:	   dd 0
  2124                                  ;codec_vendor_ids: dw 0
  2125                                  ;codec_chip_ids:   dw 0
  2126                                  
  2127                                  ;dword_str:	dd 30303030h, 30303030h
  2128                                  ;	 	db 'h', 0Dh, 0Ah, 0
  2129                                  
  2130                                  ;=============================================================================
  2131                                  ;        	uninitialized data
  2132                                  ;=============================================================================
  2133                                  
  2134                                  bss_start:
  2135                                  
  2136                                  ABSOLUTE bss_start
  2137                                  
  2138 00000BA1 <res 00000003>          alignb 4
  2139                                  
  2140 00000BA4 <res 00000001>          eof_flag:	resb 1
  2141 00000BA5 <res 00000001>          srb:		resb 1
  2142 00000BA6 <res 00000001>          audio_busy:	resb 1
  2143                                  ; 25/08/2020 
  2144                                  ;audio_play_cmd: resb 1
  2145                                  
  2146 00000BA7 <res 00000001>          audio_flag_eol:	resb 1
  2147 00000BA8 <res 00000001>          audio_flag:	resb 1
  2148                                  
  2149 00000BA9 <res 00000001>          ac97_int_ln_reg: resb 1 
  2150 00000BAA <res 00000002>          ac97_io_base:	resw 1
  2151                                  
  2152 00000BAC <res 00000004>          bus_dev_fn:	resd 1
  2153 00000BB0 <res 00000004>          dev_vendor:	resd 1
  2154 00000BB4 <res 00000004>          stats_cmd:	resd 1
  2155                                  
  2156                                  ; 14/10/2017 (audio_stmo, audio_bps, audio_freq)
  2157                                  stmo:
  2158 00000BB8 <res 00000001>          audio_stmo: 	resb 1 ; stereo or mono  
  2159                                  bps:
  2160 00000BB9 <res 00000001>          audio_bps: 	resb 1 ; bits per sample (16)
  2161                                  sample_rate:
  2162 00000BBA <res 00000002>          audio_freq:	resw 1
  2163                                  
  2164 00000BBC <res 0000001C>          smpRBuff:	resw 14 
  2165                                  
  2166                                  wav_file_name:
  2167 00000BD8 <res 00000050>          		resb 80
  2168                                  alignb 4
  2169                                  
  2170 00000C28 <res 00000004>          BDL_phy_buff:	resd 1
  2171 00000C2C <res 00000004>          DMA_phy_buff:	resd 1
  2172                                  
  2173                                  ; 14/10/2017
  2174                                  audio_master_volume:
  2175                                  ;audio_master_volume_l: resb 1 ; sound volume (lineout) left channel
  2176                                  ;audio_master_volume_r: resb 1 ; sound volume (lineout) right channel
  2177 00000C30 <res 00000001>          		resb 1
  2178                                  volume_level:
  2179 00000C31 <res 00000001>          		resb 1
  2180                                  ; 30/07/2020
  2181 00000C32 <res 00000002>          counter:	resw 1
  2182                                  
  2183 00000C34 <res 000003CC>          alignb 4096
  2184                                  
  2185 00001000 <res 00001000>          BdlBuffer:	resb 4096 ; BDL_SIZE (round up to 1 page)
  2186                                  
  2187 00002000 <res 0000E000>          alignb 65536
  2188 00010000 <res 00010000>          DmaBuffer:	resb 65536 ; 2 * BUFFERSIZE
  2189                                  EOF:
