#include <stdio.h>

char *syntax= "Syntax: stat [-bXXX]\n";
char *pcmcom= "Requires that \"pcm.com\" or \"pcmfun.com\" be loaded.\n";
char *pcmnot= "error: no pcm driver is loaded.\n";
char *copyright= "stat - v(1.0) - Copyright Media Vision, Inc. 1992\n";
char *programmer= "Bart Crane";

main(int argc, char **argv)
{
	unsigned int boardaddress= 0;
	unsigned int pcmstatus;

	#if 0
	if (argc < 2)
		{
		fprintf(stderr, syntax);
		fprintf(stderr, pcmcom);
		return(1);
		}
	#endif

	if (argv[1][0] == '-' && argv[1][1] == 'b')
		{
		char *b= &argv[1][2];

		while (isxdigit(*b))
			{
			boardaddress*= 16;
			if (*b >= '0' && *b <= '9') boardaddress+= *b- '0';
			else
			if (*b >= 'A' && *b <= 'F') boardaddress+= *b- 'A'+ 10;
			else
			if (*b >= 'a' && *b <= 'f') boardaddress+= *b- 'a'+ 10;
			b++;
			}

			{
			int i= 1;

			while (i < argc- 1)
				{
				argv[i]= argv[i+ 1];
				i++;
				}
			argv[i]= NULL;
			}

		boardaddress<<= 4;
		}

	if (!checkforsig(boardaddress))
		{
		fprintf(stderr, pcmnot);
		return(2);
		}

	printf("PCM is ");

	_asm
		{
		push si
		mov si, 0Dh
		or ax, boardaddress
		int 94h
		pop si

		mov pcmstatus, ax
		}

	if (!pcmstatus)
		{
		printf("waiting.");
		}
	else
		{
		if (pcmstatus& 0x8000) printf("paused ");
		if (pcmstatus& 0x0001) printf("playing ");
		if (pcmstatus& 0x0002) printf("recording ");

		if (pcmstatus& 0x4000) printf("SBpaused ");
		if (pcmstatus& 0x0004) printf("SBPlaying ");
		if (pcmstatus& 0x0008) printf("SBrecording ");

		if (boardaddress) printf("at port %X", boardaddress>> 4);
		}

	putchar('\n');

	return(pcmstatus& 0x000F);
}

checkforsig(int boardaddress)
{
	int status;
	int int94isthere= 0;

	_asm
		{
		mov al, 94h
		mov ah, 35h					; GETDOSVECTOR
		int 21h

		mov ax, es
		or ax, bx
		mov int94isthere, ax

		mov bx, 107h				; offset f_sig in segment holding int 94h

		mov ax, es:[bx][0]
		xor ax, 4350h				; 'CP'
		jnz nodriver

		mov ax, es:[bx][2]
		xor ax, 2D4dh				; '-M'
		jnz nodriver

		mov ax, es:[bx][4]
		xor ax, 4853h				; 'HS'
		jnz nodriver

		mov ax, es:[bx][6]
		xor ax, 5241h				; 'RA'
		jnz nodriver

		mov ax, es:[bx][8]
		xor al, 4Bh					; 'K'
		jnz nodriver

		cbw							; ax= 0

		cmp boardaddress, 0		; board specified?
		jz nodriver				

		cmp int94isthere, 0		; 
		jz nodriver
		
		push si
		mov si, 8002h
		or si, boardaddress
		int 94h
		pop si
		or ax, ax					; ax= 0 if not at address, !0 if is
		mov ax, 0					; ax= 0
		jnz nodriver				; ax= 0 if driver
		dec ax						; ax= !0 if nodriver

		nodriver:					

		mov status, ax				; ax= !0 if nodriver, 0 if driver
		}

	return(!status);				// status= 0 if nodriver, !0 if driver
}


