/***************************************************************************
*	NAME:  COMSTUFF.C
**	COPYRIGHT:
**	"Copyright (c) 1992, by FORTE
**
**       "This software is furnished under a license and may be used,
**       copied, or disclosed only in accordance with the terms of such
**       license and with the inclusion of the above copyright notice.
**       This software or any other copies thereof may not be provided or
**       otherwise made available to any other person. No title to and
**       ownership of the software is hereby transfered."
****************************************************************************
*  CREATION DATE: 11/18/92
*--------------------------------------------------------------------------*
*     VERSION	DATE	   NAME		DESCRIPTION
*>	1.0	05/01/93		Original
***************************************************************************/

#include <dos.h>
#include <conio.h>

#include "forte.h"
#include "extern.h"
#include "extern16.h"
#include "utility.h"

unsigned char
xmit_busy(port)
int port;
{
unsigned char status;

status = inp(port + 5);
return(!(status & 0x60));
}

unsigned char
data_ready(port)
int port;
{
unsigned char status;

status = inp(port + 5);

return(status & 0x01);
}

void
OutChar(port,data)
int port;
unsigned char data;
{
while(xmit_busy(port))
	;
outp(port + 0,data);
}

unsigned char 
InChar(port)
int port;
{
unsigned char data;

while (!(data_ready(port)))
	;
data = inp(port+0);
return(data);
}

void
init_port(port)
int port;		/* 0 = com1, 1 = com2 */
{

outp(port+3,0x80);
outp(port+1,00);
outp(port+0,0x06);
outp(port+3,0x03);
outp(port+1,00);

while(data_ready(port))
	(void)InChar(port);
}

void
read_joystick(x1,y1,x2,y2,butts)
unsigned int *x1;
unsigned int *y1;
unsigned int *x2;
unsigned int *y2;
unsigned int *butts;
{
union REGS regs;
#ifndef WATCOMC
regs.h.ah = 0x84;
regs.x.dx = 1;

int86(0x15,&regs,&regs);

*x1 = regs.x.ax;
*y1 = regs.x.bx;
*x2 = regs.x.cx;
*y2 = regs.x.dx;

/* Now read buttons ... */
regs.h.ah = 0x84;
regs.x.dx = 0;

int86(0x15,&regs,&regs);
*butts = regs.x.ax;
#else
regs.h.ah = 0x84;
regs.x.edx = 1;

int86(0x15,&regs,&regs);

*x1 = regs.x.eax;
*y1 = regs.x.ebx;
*x2 = regs.x.ecx;
*y2 = regs.x.edx;

/* Now read buttons ... */
regs.h.ah = 0x84;
regs.x.edx = 0;

int86(0x15,&regs,&regs);
*butts = regs.x.eax;
#endif
}


