     1                                  ; ****************************************************************************
     2                                  ; wavplay2.s (for TRDOS 386)
     3                                  ; ----------------------------------------------------------------------------
     4                                  ; WAVPLAY2.PRG ! VIA VT8237 .WAV PLAYER program by Erdogan TAN
     5                                  ;
     6                                  ; 17/03/2017
     7                                  ;
     8                                  ; [ Last Modification: 23/04/2017 ]
     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                                  ; CODE
    20                                  
    21                                  ; 01/03/2017
    22                                  ; 16/10/2016
    23                                  ; 29/04/2016
    24                                  ; TRDOS 386 system calls (temporary list!)
    25                                  _ver 	equ 0
    26                                  _exit 	equ 1
    27                                  _fork 	equ 2
    28                                  _read 	equ 3
    29                                  _write	equ 4
    30                                  _open	equ 5
    31                                  _close 	equ 6
    32                                  _wait 	equ 7
    33                                  _creat 	equ 8
    34                                  _link 	equ 9
    35                                  _unlink	equ 10
    36                                  _exec	equ 11
    37                                  _chdir	equ 12
    38                                  _time 	equ 13
    39                                  _mkdir 	equ 14
    40                                  _chmod	equ 15
    41                                  _chown	equ 16
    42                                  _break	equ 17
    43                                  _stat	equ 18
    44                                  _seek	equ 19
    45                                  _tell 	equ 20
    46                                  _mount	equ 21
    47                                  _umount	equ 22
    48                                  _setuid	equ 23
    49                                  _getuid	equ 24
    50                                  _stime	equ 25
    51                                  _quit	equ 26	
    52                                  _intr	equ 27
    53                                  _fstat	equ 28
    54                                  _emt 	equ 29
    55                                  _mdate 	equ 30
    56                                  _video 	equ 31
    57                                  _audio	equ 32
    58                                  _timer	equ 33
    59                                  _sleep	equ 34
    60                                  _msg    equ 35
    61                                  _geterr	equ 36
    62                                  _fpsave	equ 37
    63                                  _pri	equ 38
    64                                  _rele	equ 39
    65                                  _fff	equ 40
    66                                  _fnf	equ 41
    67                                  _alloc	equ 42
    68                                  _dalloc equ 43
    69                                  _calbac equ 44
    70                                  
    71                                  %macro sys 1-4
    72                                      ; 29/04/2016 - TRDOS 386 (TRDOS v2.0)	
    73                                      ; 03/09/2015	
    74                                      ; 13/04/2015
    75                                      ; Retro UNIX 386 v1 system call.	
    76                                      %if %0 >= 2   
    77                                          mov ebx, %2
    78                                          %if %0 >= 3    
    79                                              mov ecx, %3
    80                                              %if %0 = 4
    81                                                 mov edx, %4   
    82                                              %endif
    83                                          %endif
    84                                      %endif
    85                                      mov eax, %1
    86                                      ;int 30h
    87                                      int 40h ; TRDOS 386 (TRDOS v2.0)	   
    88                                  %endmacro
    89                                  
    90                                  ; TRDOS 386 (and Retro UNIX 386 v1) system call format:
    91                                  ; sys systemcall (eax) <arg1 (ebx)>, <arg2 (ecx)>, <arg3 (edx)>
    92                                  
    93                                  BUFFERSIZE      equ     32768	; audio buffer size 
    94                                  ENDOFFILE       equ     1	; flag for knowing end of file
    95                                  
    96                                  [BITS 32]
    97                                  
    98                                  [ORG 0] 
    99                                  
   100                                  _STARTUP:
   101                                  	; Prints the Credits Text.
   102                                  	sys	_msg, Credits, 255, 0Bh
   102                              <1> 
   102                              <1> 
   102                              <1> 
   102                              <1> 
   102                              <1>  %if %0 >= 2
   102 00000000 BB[79030000]        <1>  mov ebx, %2
   102                              <1>  %if %0 >= 3
   102 00000005 B9FF000000          <1>  mov ecx, %3
   102                              <1>  %if %0 = 4
   102 0000000A BA0B000000          <1>  mov edx, %4
   102                              <1>  %endif
   102                              <1>  %endif
   102                              <1>  %endif
   102 0000000F B823000000          <1>  mov eax, %1
   102                              <1> 
   102 00000014 CD40                <1>  int 40h
   103                                  
   104                                  	; clear bss
   105 00000016 B9[E8040000]            	mov	ecx, bss_end
   106 0000001B BF[73040000]            	mov	edi, bss_start
   107 00000020 29F9                    	sub	ecx, edi
   108 00000022 D1E9                    	shr	ecx, 1
   109 00000024 31C0                    	xor	eax, eax
   110 00000026 F366AB                  	rep	stosw
   111                                  
   112                                  	; Detect (& Enable) VT8233 Audio Device
   113 00000029 E86A010000              	call    DetectVT8233 
   114 0000002E 731B                    	jnc     short _1
   115                                  
   116                                  ; couldn't find the audio device!
   117                                  	sys	_msg, noDevMsg, 255, 0Fh
   117                              <1> 
   117                              <1> 
   117                              <1> 
   117                              <1> 
   117                              <1>  %if %0 >= 2
   117 00000030 BB[03040000]        <1>  mov ebx, %2
   117                              <1>  %if %0 >= 3
   117 00000035 B9FF000000          <1>  mov ecx, %3
   117                              <1>  %if %0 = 4
   117 0000003A BA0F000000          <1>  mov edx, %4
   117                              <1>  %endif
   117                              <1>  %endif
   117                              <1>  %endif
   117 0000003F B823000000          <1>  mov eax, %1
   117                              <1> 
   117 00000044 CD40                <1>  int 40h
   118 00000046 E927010000                      jmp     Exit
   119                                  _1:
   120                                  	; Allocate Audio Buffer (for user)
   121                                  	sys	_audio, 200h, BUFFERSIZE, audio_buffer
   121                              <1> 
   121                              <1> 
   121                              <1> 
   121                              <1> 
   121                              <1>  %if %0 >= 2
   121 0000004B BB00020000          <1>  mov ebx, %2
   121                              <1>  %if %0 >= 3
   121 00000050 B900800000          <1>  mov ecx, %3
   121                              <1>  %if %0 = 4
   121 00000055 BA[00100000]        <1>  mov edx, %4
   121                              <1>  %endif
   121                              <1>  %endif
   121                              <1>  %endif
   121 0000005A B820000000          <1>  mov eax, %1
   121                              <1> 
   121 0000005F CD40                <1>  int 40h
   122 00000061 731B                    	jnc	short _2
   123                                  error_exit:
   124                                  	sys	_msg, trdos386_err_msg, 255, 0Eh
   124                              <1> 
   124                              <1> 
   124                              <1> 
   124                              <1> 
   124                              <1>  %if %0 >= 2
   124 00000063 BB[53040000]        <1>  mov ebx, %2
   124                              <1>  %if %0 >= 3
   124 00000068 B9FF000000          <1>  mov ecx, %3
   124                              <1>  %if %0 = 4
   124 0000006D BA0E000000          <1>  mov edx, %4
   124                              <1>  %endif
   124                              <1>  %endif
   124                              <1>  %endif
   124 00000072 B823000000          <1>  mov eax, %1
   124                              <1> 
   124 00000077 CD40                <1>  int 40h
   125 00000079 E9F4000000              	jmp	Exit
   126                                  _2:
   127                                  	; DIRECT CGA (TEXT MODE) MEMORY ACCESS
   128                                  	; bl = 0, bh = 4
   129                                  	; Direct access/map to CGA (Text) memory (0B8000h)
   130                                  
   131                                  	sys	_video, 0400h
   131                              <1> 
   131                              <1> 
   131                              <1> 
   131                              <1> 
   131                              <1>  %if %0 >= 2
   131 0000007E BB00040000          <1>  mov ebx, %2
   131                              <1>  %if %0 >= 3
   131                              <1>  mov ecx, %3
   131                              <1>  %if %0 = 4
   131                              <1>  mov edx, %4
   131                              <1>  %endif
   131                              <1>  %endif
   131                              <1>  %endif
   131 00000083 B81F000000          <1>  mov eax, %1
   131                              <1> 
   131 00000088 CD40                <1>  int 40h
   132 0000008A 3D00800B00              	cmp	eax, 0B8000h
   133 0000008F 75D2                    	jne	short error_exit
   134                                  
   135                                  	; Initialize Audio Device
   136                                  	sys	_audio, 301h, 0, audio_int_handler 
   136                              <1> 
   136                              <1> 
   136                              <1> 
   136                              <1> 
   136                              <1>  %if %0 >= 2
   136 00000091 BB01030000          <1>  mov ebx, %2
   136                              <1>  %if %0 >= 3
   136 00000096 B900000000          <1>  mov ecx, %3
   136                              <1>  %if %0 = 4
   136 0000009B BA[3D020000]        <1>  mov edx, %4
   136                              <1>  %endif
   136                              <1>  %endif
   136                              <1>  %endif
   136 000000A0 B820000000          <1>  mov eax, %1
   136                              <1> 
   136 000000A5 CD40                <1>  int 40h
   137 000000A7 72BA                    	jc	short error_exit
   138                                  
   139                                  GetFileName:  
   140 000000A9 89E6                    	mov	esi, esp
   141 000000AB AD                      	lodsd
   142 000000AC 83F802                  	cmp	eax, 2 ; two arguments 
   143                                  	       ; (program file name & mod file name)
   144 000000AF 0F82CB000000            	jb	pmsg_usage ; nothing to do
   145                                  
   146 000000B5 AD                      	lodsd ; program file name address 
   147 000000B6 AD                      	lodsd ; mod file name address (file to be read)
   148 000000B7 89C6                    	mov	esi, eax
   149 000000B9 BF[98040000]            	mov	edi, wav_file_name
   150                                  ScanName:       
   151 000000BE AC                      	lodsb
   152 000000BF 84C0                    	test	al, al
   153 000000C1 0F84B9000000            	je	pmsg_usage
   154 000000C7 3C20                    	cmp	al, 20h
   155 000000C9 74F3                    	je	short ScanName	; scan start of name.
   156 000000CB AA                      	stosb
   157 000000CC B4FF                    	mov	ah, 0FFh
   158                                  a_0:	
   159 000000CE FEC4                    	inc	ah
   160                                  a_1:
   161 000000D0 AC                      	lodsb
   162 000000D1 AA                      	stosb
   163 000000D2 3C2E                    	cmp	al, '.'
   164 000000D4 74F8                    	je	short a_0	
   165 000000D6 20C0                    	and	al, al
   166 000000D8 75F6                    	jnz	short a_1
   167                                  
   168 000000DA 08E4                    	or	ah, ah		; if period NOT found,
   169 000000DC 750B                    	jnz	short _3 	; then add a .WAV extension.
   170                                  SetExt:
   171 000000DE 4F                      	dec	edi
   172 000000DF C7072E574156            	mov	dword [edi], '.WAV'
   173 000000E5 C6470400                	mov	byte [edi+4], 0
   174                                  
   175                                  _3:
   176 000000E9 E870020000              	call	write_ac97_dev_info 
   177                                  
   178                                  ; open the file
   179                                          ; open existing file
   180 000000EE E8B2000000                      call    openFile ; no error? ok.
   181 000000F3 7318                            jnc     short _gsr
   182                                  
   183                                  ; file not found!
   184                                  	sys	_msg, noFileErrMsg, 255, 0Fh
   184                              <1> 
   184                              <1> 
   184                              <1> 
   184                              <1> 
   184                              <1>  %if %0 >= 2
   184 000000F5 BB[3A040000]        <1>  mov ebx, %2
   184                              <1>  %if %0 >= 3
   184 000000FA B9FF000000          <1>  mov ecx, %3
   184                              <1>  %if %0 = 4
   184 000000FF BA0F000000          <1>  mov edx, %4
   184                              <1>  %endif
   184                              <1>  %endif
   184                              <1>  %endif
   184 00000104 B823000000          <1>  mov eax, %1
   184                              <1> 
   184 00000109 CD40                <1>  int 40h
   185 0000010B EB65                            jmp     short Exit
   186                                  
   187                                  _gsr:  
   188 0000010D E8CD000000                     	call    getSampleRate		; read the sample rate
   189                                                                          ; pass it onto codec.
   190 00000112 725E                    	jc	Exit
   191                                  
   192 00000114 66A3[76040000]          	mov	[sample_rate], ax
   193 0000011A 880D[74040000]          	mov	[stmo], cl
   194 00000120 8815[75040000]          	mov	[bps], dl
   195                                  	
   196                                  PlayNow: 
   197                                  ;
   198                                  ; position file pointer to start in actual wav data
   199                                  ; MUCH improvement should really be done here to check if sample size is
   200                                  ; supported, make sure there are 2 channels, etc.  
   201                                  ;
   202                                          ;mov     ah, 42h
   203                                          ;mov     al, 0	; from start of file
   204                                          ;mov     bx, [FileHandle]
   205                                          ;xor     cx, cx
   206                                          ;mov     dx, 44	; jump past .wav/riff header
   207                                          ;int     21h
   208                                  
   209                                  	sys	_seek, [FileHandle], 44, 0
   209                              <1> 
   209                              <1> 
   209                              <1> 
   209                              <1> 
   209                              <1>  %if %0 >= 2
   209 00000126 8B1D[75030000]      <1>  mov ebx, %2
   209                              <1>  %if %0 >= 3
   209 0000012C B92C000000          <1>  mov ecx, %3
   209                              <1>  %if %0 = 4
   209 00000131 BA00000000          <1>  mov edx, %4
   209                              <1>  %endif
   209                              <1>  %endif
   209                              <1>  %endif
   209 00000136 B813000000          <1>  mov eax, %1
   209                              <1> 
   209 0000013B CD40                <1>  int 40h
   210                                  
   211                                  ; play the .wav file.  Most of the good stuff is in here.
   212                                  
   213 0000013D E88A010000                      call    PlayWav
   214                                  
   215                                  ; close the .wav file and exit.
   216                                  
   217                                  StopPlaying:
   218                                  	; Stop Playing
   219                                  	sys	_audio, 0700h
   219                              <1> 
   219                              <1> 
   219                              <1> 
   219                              <1> 
   219                              <1>  %if %0 >= 2
   219 00000142 BB00070000          <1>  mov ebx, %2
   219                              <1>  %if %0 >= 3
   219                              <1>  mov ecx, %3
   219                              <1>  %if %0 = 4
   219                              <1>  mov edx, %4
   219                              <1>  %endif
   219                              <1>  %endif
   219                              <1>  %endif
   219 00000147 B820000000          <1>  mov eax, %1
   219                              <1> 
   219 0000014C CD40                <1>  int 40h
   220                                  	; Cancel callback service (for user)
   221                                  	sys	_audio, 0900h
   221                              <1> 
   221                              <1> 
   221                              <1> 
   221                              <1> 
   221                              <1>  %if %0 >= 2
   221 0000014E BB00090000          <1>  mov ebx, %2
   221                              <1>  %if %0 >= 3
   221                              <1>  mov ecx, %3
   221                              <1>  %if %0 = 4
   221                              <1>  mov edx, %4
   221                              <1>  %endif
   221                              <1>  %endif
   221                              <1>  %endif
   221 00000153 B820000000          <1>  mov eax, %1
   221                              <1> 
   221 00000158 CD40                <1>  int 40h
   222                                  	; Deallocate Audio Buffer (for user)
   223                                  	sys	_audio, 1000h
   223                              <1> 
   223                              <1> 
   223                              <1> 
   223                              <1> 
   223                              <1>  %if %0 >= 2
   223 0000015A BB00100000          <1>  mov ebx, %2
   223                              <1>  %if %0 >= 3
   223                              <1>  mov ecx, %3
   223                              <1>  %if %0 = 4
   223                              <1>  mov edx, %4
   223                              <1>  %endif
   223                              <1>  %endif
   223                              <1>  %endif
   223 0000015F B820000000          <1>  mov eax, %1
   223                              <1> 
   223 00000164 CD40                <1>  int 40h
   224                                  	; Disable Audio Device
   225                                  	sys	_audio, 1200h
   225                              <1> 
   225                              <1> 
   225                              <1> 
   225                              <1> 
   225                              <1>  %if %0 >= 2
   225 00000166 BB00120000          <1>  mov ebx, %2
   225                              <1>  %if %0 >= 3
   225                              <1>  mov ecx, %3
   225                              <1>  %if %0 = 4
   225                              <1>  mov edx, %4
   225                              <1>  %endif
   225                              <1>  %endif
   225                              <1>  %endif
   225 0000016B B820000000          <1>  mov eax, %1
   225                              <1> 
   225 00000170 CD40                <1>  int 40h
   226                                  Exit:  
   227 00000172 E847000000                      call    closeFile
   228                                           
   229                                  	sys	_exit	; Bye!
   229                              <1> 
   229                              <1> 
   229                              <1> 
   229                              <1> 
   229                              <1>  %if %0 >= 2
   229                              <1>  mov ebx, %2
   229                              <1>  %if %0 >= 3
   229                              <1>  mov ecx, %3
   229                              <1>  %if %0 = 4
   229                              <1>  mov edx, %4
   229                              <1>  %endif
   229                              <1>  %endif
   229                              <1>  %endif
   229 00000177 B801000000          <1>  mov eax, %1
   229                              <1> 
   229 0000017C CD40                <1>  int 40h
   230                                  here:
   231 0000017E EBFE                    	jmp	short here
   232                                  
   233                                  pmsg_usage:
   234                                  	sys	_msg, msg_usage, 255, 0Bh
   234                              <1> 
   234                              <1> 
   234                              <1> 
   234                              <1> 
   234                              <1>  %if %0 >= 2
   234 00000180 BB[E5030000]        <1>  mov ebx, %2
   234                              <1>  %if %0 >= 3
   234 00000185 B9FF000000          <1>  mov ecx, %3
   234                              <1>  %if %0 = 4
   234 0000018A BA0B000000          <1>  mov edx, %4
   234                              <1>  %endif
   234                              <1>  %endif
   234                              <1>  %endif
   234 0000018F B823000000          <1>  mov eax, %1
   234                              <1> 
   234 00000194 CD40                <1>  int 40h
   235 00000196 EBDA                    	jmp	short Exit
   236                                  
   237                                  DetectVT8233:
   238                                  	; Detect (BH=1) VT8233 (BL=3) Audio Controller
   239                                          sys	_audio, 103h
   239                              <1> 
   239                              <1> 
   239                              <1> 
   239                              <1> 
   239                              <1>  %if %0 >= 2
   239 00000198 BB03010000          <1>  mov ebx, %2
   239                              <1>  %if %0 >= 3
   239                              <1>  mov ecx, %3
   239                              <1>  %if %0 = 4
   239                              <1>  mov edx, %4
   239                              <1>  %endif
   239                              <1>  %endif
   239                              <1>  %endif
   239 0000019D B820000000          <1>  mov eax, %1
   239                              <1> 
   239 000001A2 CD40                <1>  int 40h
   240 000001A4 C3                      	retn
   241                                  
   242                                  ;open or create file
   243                                  ;
   244                                  ;input: ds:dx-->filename (asciiz)
   245                                  ;       al=file Mode (create or open)
   246                                  ;output: none  cs:[FileHandle] filled
   247                                  ;
   248                                  openFile:
   249                                  	;mov	ah, 3Bh	; start with a mode
   250                                  	;add	ah, al	; add in create or open mode
   251                                  	;xor	cx, cx
   252                                  	;int	21h
   253                                  	;jc	short _of1
   254                                  	;;mov	[cs:FileHandle], ax
   255                                  
   256                                  	sys	_open, wav_file_name, 0
   256                              <1> 
   256                              <1> 
   256                              <1> 
   256                              <1> 
   256                              <1>  %if %0 >= 2
   256 000001A5 BB[98040000]        <1>  mov ebx, %2
   256                              <1>  %if %0 >= 3
   256 000001AA B900000000          <1>  mov ecx, %3
   256                              <1>  %if %0 = 4
   256                              <1>  mov edx, %4
   256                              <1>  %endif
   256                              <1>  %endif
   256                              <1>  %endif
   256 000001AF B805000000          <1>  mov eax, %1
   256                              <1> 
   256 000001B4 CD40                <1>  int 40h
   257 000001B6 7205                    	jc	short _of1
   258                                  
   259 000001B8 A3[75030000]            	mov	[FileHandle], eax
   260                                  _of1:
   261 000001BD C3                      	retn
   262                                  
   263                                  ; close the currently open file
   264                                  ; input: none, uses cs:[FileHandle]
   265                                  closeFile:
   266 000001BE 833D[75030000]FF        	cmp	dword [FileHandle], -1
   267 000001C5 7417                    	je	short _cf1
   268                                  	;mov    bx, [FileHandle]  
   269                                  	;mov    ax, 3E00h
   270                                          ;int    21h              ;close file
   271                                  
   272                                  	sys	_close, [FileHandle]
   272                              <1> 
   272                              <1> 
   272                              <1> 
   272                              <1> 
   272                              <1>  %if %0 >= 2
   272 000001C7 8B1D[75030000]      <1>  mov ebx, %2
   272                              <1>  %if %0 >= 3
   272                              <1>  mov ecx, %3
   272                              <1>  %if %0 = 4
   272                              <1>  mov edx, %4
   272                              <1>  %endif
   272                              <1>  %endif
   272                              <1>  %endif
   272 000001CD B806000000          <1>  mov eax, %1
   272                              <1> 
   272 000001D2 CD40                <1>  int 40h
   273 000001D4 C705[75030000]FFFF-     	mov 	dword [FileHandle], -1
   273 000001DC FFFF               
   274                                  _cf1:
   275 000001DE C3                      	retn
   276                                  
   277                                  getSampleRate:
   278                                  	
   279                                  ; reads the sample rate from the .wav file.
   280                                  ; entry: none - assumes file is already open
   281                                  ; exit: ax = sample rate (11025, 22050, 44100, 48000)
   282                                  ;	cx = number of channels (mono=1, stereo=2)
   283                                  ;	dx = bits per sample (8, 16)
   284                                  
   285 000001DF 53                      	push    ebx
   286                                  
   287                                          ;mov	ah, 42h
   288                                          ;mov	al, 0	; from start of file
   289                                          ;mov	bx, [FileHandle]
   290                                          ;xor	cx, cx
   291                                          ;mov	dx, 08h	; "WAVE"
   292                                          ;int	21h
   293                                  	
   294                                  	sys	_seek, [FileHandle], 8, 0
   294                              <1> 
   294                              <1> 
   294                              <1> 
   294                              <1> 
   294                              <1>  %if %0 >= 2
   294 000001E0 8B1D[75030000]      <1>  mov ebx, %2
   294                              <1>  %if %0 >= 3
   294 000001E6 B908000000          <1>  mov ecx, %3
   294                              <1>  %if %0 = 4
   294 000001EB BA00000000          <1>  mov edx, %4
   294                              <1>  %endif
   294                              <1>  %endif
   294                              <1>  %endif
   294 000001F0 B813000000          <1>  mov eax, %1
   294                              <1> 
   294 000001F5 CD40                <1>  int 40h
   295                                  
   296                                          ;mov	dx, smpRBuff
   297                                          ;mov	cx, 28	; 28 bytes
   298                                  	;mov	ah, 3fh
   299                                          ;int	21h
   300                                  
   301                                  	sys	_read, [FileHandle], smpRBuff, 28
   301                              <1> 
   301                              <1> 
   301                              <1> 
   301                              <1> 
   301                              <1>  %if %0 >= 2
   301 000001F7 8B1D[75030000]      <1>  mov ebx, %2
   301                              <1>  %if %0 >= 3
   301 000001FD B9[7C040000]        <1>  mov ecx, %3
   301                              <1>  %if %0 = 4
   301 00000202 BA1C000000          <1>  mov edx, %4
   301                              <1>  %endif
   301                              <1>  %endif
   301                              <1>  %endif
   301 00000207 B803000000          <1>  mov eax, %1
   301                              <1> 
   301 0000020C CD40                <1>  int 40h
   302                                  
   303 0000020E 813D[7C040000]5741-     	cmp	dword [smpRBuff], 'WAVE'
   303 00000216 5645               
   304 00000218 7520                    	jne	short gsr_stc
   305                                  
   306 0000021A 66833D[88040000]01      	cmp	word [smpRBuff+12], 1	; Offset 20, must be 1 (= PCM)
   307 00000222 7516                    	jne	short gsr_stc
   308                                  
   309 00000224 668B0D[8A040000]        	mov	cx, [smpRBuff+14]	; return num of channels in CX
   310 0000022B 66A1[8C040000]                  mov     ax, [smpRBuff+16]	; return sample rate in AX
   311 00000231 668B15[96040000]        	mov	dx, [smpRBuff+26]	; return bits per sample value in DX
   312                                  gsr_retn:
   313 00000238 5B                              pop     ebx
   314 00000239 C3                              retn
   315                                  gsr_stc:
   316 0000023A F9                      	stc
   317 0000023B EBFB                    	jmp	short gsr_retn
   318                                  
   319                                  audio_int_handler:
   320 0000023D C605[7B040000]01        	mov	byte [srb], 1 ; interrupt (or signal response byte)
   321                                  
   322 00000244 803D[78040000]01        	cmp	byte [cbs_busy], 1
   323 0000024B 732A                    	jnb	short _callback_bsy_retn
   324                                  
   325 0000024D C605[78040000]01        	mov	byte [cbs_busy], 1
   326                                  
   327 00000254 A0[79040000]            	mov	al, [half_buff]
   328                                  
   329 00000259 3C01                    	cmp	al, 1
   330 0000025B 7213                    	jb	short _callback_retn
   331                                  
   332 0000025D 8035[79040000]03        	xor	byte [half_buff], 3 ; 2->1, 1->2
   333                                  
   334 00000264 BB00800B00              	mov	ebx, 0B8000h ; video display page address
   335 00000269 B44E                    	mov	ah, 4Eh
   336 0000026B 0431                    	add	al, '1'
   337 0000026D 668903                  	mov	[ebx], ax ; show playing buffer (1, 2)
   338                                  _callback_retn:
   339 00000270 C605[78040000]00        	mov	byte [cbs_busy], 0
   340                                  _callback_bsy_retn:
   341                                  	sys	_rele ; return from callback service 
   341                              <1> 
   341                              <1> 
   341                              <1> 
   341                              <1> 
   341                              <1>  %if %0 >= 2
   341                              <1>  mov ebx, %2
   341                              <1>  %if %0 >= 3
   341                              <1>  mov ecx, %3
   341                              <1>  %if %0 = 4
   341                              <1>  mov edx, %4
   341                              <1>  %endif
   341                              <1>  %endif
   341                              <1>  %endif
   341 00000277 B827000000          <1>  mov eax, %1
   341                              <1> 
   341 0000027C CD40                <1>  int 40h
   342                                  	; we must not come here !
   343                                  	sys	_exit
   343                              <1> 
   343                              <1> 
   343                              <1> 
   343                              <1> 
   343                              <1>  %if %0 >= 2
   343                              <1>  mov ebx, %2
   343                              <1>  %if %0 >= 3
   343                              <1>  mov ecx, %3
   343                              <1>  %if %0 = 4
   343                              <1>  mov edx, %4
   343                              <1>  %endif
   343                              <1>  %endif
   343                              <1>  %endif
   343 0000027E B801000000          <1>  mov eax, %1
   343                              <1> 
   343 00000283 CD40                <1>  int 40h
   344                                  	
   345                                  loadFromFile:
   346                                  	; 17/03/2017
   347                                  	; edi = buffer address
   348                                  	; edx = buffer size
   349                                  	; 10/03/2017
   350                                          ;push	eax
   351                                          ;push	ecx
   352                                          ;push	edx
   353                                  	;push	ebx
   354 00000285 F605[7A040000]01                test    byte [flags], ENDOFFILE	; have we already read the
   355 0000028C F9                              stc			; last of the file?
   356 0000028D 7531                            jnz     short endLFF
   357                                  	;clc
   358                                  	; load file into memory
   359                                  	sys 	_read, [FileHandle], edi
   359                              <1> 
   359                              <1> 
   359                              <1> 
   359                              <1> 
   359                              <1>  %if %0 >= 2
   359 0000028F 8B1D[75030000]      <1>  mov ebx, %2
   359                              <1>  %if %0 >= 3
   359 00000295 89F9                <1>  mov ecx, %3
   359                              <1>  %if %0 = 4
   359                              <1>  mov edx, %4
   359                              <1>  %endif
   359                              <1>  %endif
   359                              <1>  %endif
   359 00000297 B803000000          <1>  mov eax, %1
   359                              <1> 
   359 0000029C CD40                <1>  int 40h
   360 0000029E 89D1                    	mov	ecx, edx
   361 000002A0 720A                    	jc	short padfill ; error !
   362 000002A2 21C0                    	and	eax, eax
   363 000002A4 7406                    	jz	short padfill
   364 000002A6 29C1                    	sub	ecx, eax
   365 000002A8 7416                    	jz	short endLFF
   366 000002AA 01C7                    	add	edi, eax  
   367                                  padfill:
   368 000002AC 803D[75040000]10        	cmp 	byte [bps], 16
   369 000002B3 740C                    	je	short _5
   370                                  	; Minimum Value = 0
   371 000002B5 30C0                            xor     al, al
   372 000002B7 F3AA                    	rep	stosb
   373                                  _4:
   374                                          ;clc			; don't exit with CY yet.
   375 000002B9 800D[7A040000]01                or	byte [flags], ENDOFFILE	; end of file flag
   376                                  endLFF:
   377                                  	;pop	ebx
   378                                  	;pop	edx
   379                                          ;pop	ecx
   380                                          ;pop	eax
   381 000002C0 C3                              retn
   382                                  _5:
   383                                  	; Minimum value = 8000h (-32768)
   384 000002C1 D1E9                    	shr	ecx, 1 
   385 000002C3 66B80080                	mov	ax, 8000h ; -32768
   386 000002C7 F366AB                  	rep	stosw
   387 000002CA EBED                    	jmp	short _4
   388                                  
   389                                  PlayWav:
   390                                         ; load 32768 bytes into audio buffer
   391 000002CC BF[00100000]            	mov     edi, audio_buffer
   392 000002D1 BA00800000              	mov	edx, BUFFERSIZE
   393 000002D6 E8AAFFFFFF              	call	loadFromFile
   394 000002DB 0F8282FDFFFF            	jc	error_exit
   395 000002E1 C605[79040000]01        	mov	byte [half_buff], 1 ; Buffer 1
   396                                  
   397                                  	; Set Master Volume Level
   398                                  	sys	_audio, 1100h, 1D1Dh
   398                              <1> 
   398                              <1> 
   398                              <1> 
   398                              <1> 
   398                              <1>  %if %0 >= 2
   398 000002E8 BB00110000          <1>  mov ebx, %2
   398                              <1>  %if %0 >= 3
   398 000002ED B91D1D0000          <1>  mov ecx, %3
   398                              <1>  %if %0 = 4
   398                              <1>  mov edx, %4
   398                              <1>  %endif
   398                              <1>  %endif
   398                              <1>  %endif
   398 000002F2 B820000000          <1>  mov eax, %1
   398                              <1> 
   398 000002F7 CD40                <1>  int 40h
   399                                  	
   400                                  	; Start	to play
   401 000002F9 A0[75040000]            	mov	al, [bps]
   402 000002FE C0E804                  	shr	al, 4 ; 8 -> 0, 16 -> 1
   403 00000301 D0E0                    	shl	al, 1 ; 16 -> 2, 8 -> 0
   404 00000303 8A1D[74040000]          	mov	bl, [stmo]
   405 00000309 FECB                    	dec	bl
   406 0000030B 08C3                    	or	bl, al
   407 0000030D 668B0D[76040000]        	mov	cx, [sample_rate] 
   408 00000314 B704                    	mov	bh, 4 ; start to play	
   409                                  	sys	_audio
   409                              <1> 
   409                              <1> 
   409                              <1> 
   409                              <1> 
   409                              <1>  %if %0 >= 2
   409                              <1>  mov ebx, %2
   409                              <1>  %if %0 >= 3
   409                              <1>  mov ecx, %3
   409                              <1>  %if %0 = 4
   409                              <1>  mov edx, %4
   409                              <1>  %endif
   409                              <1>  %endif
   409                              <1>  %endif
   409 00000316 B820000000          <1>  mov eax, %1
   409                              <1> 
   409 0000031B CD40                <1>  int 40h
   410                                  
   411 0000031D BB00800B00              	mov	ebx, 0B8000h ; video display page address
   412 00000322 B44E                    	mov	ah, 4Eh
   413 00000324 0431                    	add	al, '1'
   414 00000326 668903                  	mov	[ebx], ax ; show playing buffer (1, 2)
   415                                  p_loop:
   416 00000329 B401                    	mov     ah, 1		; any key pressed?
   417 0000032B CD32                    	int     32h		; no, Loop.
   418 0000032D 740C                    	jz	short q_loop
   419                                  
   420 0000032F B400                    	mov     ah, 0		; flush key buffer...
   421 00000331 CD32                    	int     32h
   422                                  p_return:
   423 00000333 C605[79040000]00        	mov	byte [half_buff], 0
   424 0000033A C3                      	retn
   425                                  q_loop:
   426 0000033B 803D[7B040000]00        	cmp	byte [srb], 0
   427 00000342 76E5                    	jna	short p_loop
   428 00000344 BF[00100000]            	mov     edi, audio_buffer
   429 00000349 BA00800000              	mov	edx, BUFFERSIZE
   430 0000034E E832FFFFFF              	call    loadFromFile
   431 00000353 72DE                    	jc	short p_return
   432 00000355 C605[7B040000]00        	mov	byte [srb], 0
   433 0000035C EBCB                    	jmp	short p_loop
   434                                  
   435                                  write_ac97_dev_info:
   436                                  	; EBX = Message address
   437                                  	; ECX = Max. message length (or stop on ZERO character)
   438                                  	;	(1 to 255)
   439                                  	; DL  = Message color (07h = light gray, 0Fh = white) 
   440                                       	sys 	_msg, msgAC97Info, 255, 07h
   440                              <1> 
   440                              <1> 
   440                              <1> 
   440                              <1> 
   440                              <1>  %if %0 >= 2
   440 0000035E BB[C2030000]        <1>  mov ebx, %2
   440                              <1>  %if %0 >= 3
   440 00000363 B9FF000000          <1>  mov ecx, %3
   440                              <1>  %if %0 = 4
   440 00000368 BA07000000          <1>  mov edx, %4
   440                              <1>  %endif
   440                              <1>  %endif
   440                              <1>  %endif
   440 0000036D B823000000          <1>  mov eax, %1
   440                              <1> 
   440 00000372 CD40                <1>  int 40h
   441 00000374 C3                              retn
   442                                  
   443                                  ; DATA
   444                                  
   445                                  FileHandle:	
   446 00000375 FFFFFFFF                	dd	-1
   447                                  
   448                                  Credits:
   449 00000379 54696E792057415620-     	db	'Tiny WAV Player for TRDOS 386 by Erdogan Tan. '
   449 00000382 506C6179657220666F-
   449 0000038B 72205452444F532033-
   449 00000394 383620627920457264-
   449 0000039D 6F67616E2054616E2E-
   449 000003A6 20                 
   450 000003A7 417072696C20323031-     	db	'April 2017.',10,13,0
   450 000003B0 372E0A0D00         
   451 000003B5 32332F30342F323031-     	db	'23/04/2017', 10,13,0
   451 000003BE 370A0D00           
   452                                  
   453                                  msgAC97Info:
   454 000003C2 666F72205649412056-     	db 	'for VIA VT8233 Audio Controller.', 10,13,0
   454 000003CB 543832333320417564-
   454 000003D4 696F20436F6E74726F-
   454 000003DD 6C6C65722E0A0D00   
   455                                  
   456                                  msg_usage:
   457 000003E5 75736167653A207761-     	db	'usage: wavplay filename.wav',10,13,0
   457 000003EE 76706C61792066696C-
   457 000003F7 656E616D652E776176-
   457 00000400 0A0D00             
   458                                  
   459                                  noDevMsg:
   460 00000403 4572726F723A20556E-     	db	'Error: Unable to find VIA VT8233 based audio device!'
   460 0000040C 61626C6520746F2066-
   460 00000415 696E64205649412056-
   460 0000041E 543832333320626173-
   460 00000427 656420617564696F20-
   460 00000430 64657669636521     
   461 00000437 0A0D00                  	db	10,13,0
   462                                  
   463                                  noFileErrMsg:
   464 0000043A 4572726F723A206669-     	db	'Error: file not found.',10,13,0
   464 00000443 6C65206E6F7420666F-
   464 0000044C 756E642E0A0D00     
   465                                  
   466                                  trdos386_err_msg:
   467 00000453 5452444F5320333836-     	db	'TRDOS 386 System call error !',10,13,0
   467 0000045C 2053797374656D2063-
   467 00000465 616C6C206572726F72-
   467 0000046E 20210A0D00         
   468                                  
   469                                  EOF: 
   470                                  
   471                                  ; BSS
   472                                  
   473                                  bss_start:
   474                                  
   475                                  ABSOLUTE bss_start
   476                                  
   477 00000473 <res 00000001>          alignb 4
   478                                  
   479 00000474 <res 00000001>          stmo:		resb 1 ; stereo or mono (1=stereo) 
   480 00000475 <res 00000001>          bps:		resb 1 ; bits per sample (8,16)
   481 00000476 <res 00000002>          sample_rate:	resw 1 ; Sample Frequency (Hz)
   482                                  
   483 00000478 <res 00000001>          cbs_busy:	resb 1 
   484 00000479 <res 00000001>          half_buff:	resb 1
   485 0000047A <res 00000001>          flags:		resb 1
   486 0000047B <res 00000001>          srb:		resb 1	
   487                                  
   488 0000047C <res 0000001C>          smpRBuff:	resw 14 
   489                                  
   490                                  wav_file_name:
   491 00000498 <res 00000050>          		resb 80 ; wave file, path name (<= 80 bytes)
   492                                  bss_end:
   493 000004E8 <res 00000B18>          alignb 4096
   494 00001000 <res 00008000>          audio_buffer:	resb BUFFERSIZE ; DMA Buffer Size / 2  (32768)
