/* k 0069	21/10/90	*/
/* k 0079	14/11/90	*/
/* k 0159 	20/04/91	*/
/* k 0160	04/05/91	*/
/* k 0182	17/06/91	*/
/* k 0184       18/06/91        */
/* k 0191	02/07/91	*/
/* k 0207	06/09/91	*//* betbut */
/* k 0209	07/09/91	*//* move betbut to panal */
/* k 0222	18/09/91	*//* Separation */
/* k 0224       23/09/91        *//* ebss */
/* k 0230       09/10/91        *//* assemble some data */
/* k 0232	11/10/91	*/
#include <ebss.h>               /* @0224 */
#if ((OS_TYPE==3) || (OS_TYPE==4))
extern unsigned int adaptIO;
#else
/* @0209 */
#endif
#include <pconio.h>
void movecursor(),c_dec();
void pwindow(xx1,yy1,xx2,yy2)	/* @0182 */
short xx1;	/* @0182 */
short yy1;	/* @0182 */
short xx2;	/* @0182 */
short yy2;	/* @0182 */
  {
   px1=xx1;
   py1=yy1;
   px2=xx2;
   py2=yy2;
   pcurx=xx1;
   pcury=yy1;
   pxw=px2-px1+1;
   pyw=py2-py1+1;
#if (OS_TYPE==4)     /* @0184 */
  begpt=videobuf;
#else
   begpt=(char *)0xb8000000;
#endif
   begpt+=((py1-1)*160+(px1-1)*2);
  }
void pclrscr()
  {
   short i,j;
   char *pt;
   pt=begpt;
   for (i=0;i<pyw;i++)
    {
     for (j=0;j<pxw;j++)
      {
       *pt=' ';
       pt++;
       *pt=pattr;
       pt++;
      }
     pt+=160-2*pxw;
    }
   pcurx=px1;
   pcury=py1;
   curpt=begpt;
   movecursor(pcurx,pcury);
  }
void ptextattr(c)	/* @0182 */
char c;	/* @0182 */
  {
   pattr=c;
  }
void ptextbackground(c)		/* @0182 */
unsigned char c; /* @0182 */    /* @0191 */
  {
   pattr&=143;
   pattr|=c*16;
  }
void ptextcolor(c)		/* @0182 */
char c;		/* @0182 */
  {
   pattr&=240;
   pattr|=c;
  }
#if (OS_TYPE==3)

int position ( ) {
unsigned int x,y;
 outp (baseport,0x0f);
 x=inp(baseport+1);
 outp (baseport,0x0e);
 y=inp(baseport+1);
 return ( (y << 8) + x );
}

void mygotoxy (unsigned int posit) {
unsigned char b1=posit & 0xFF;
unsigned char b2=posit >> 8;
 outp (baseport,0x0f);
 outp (baseport+1,b1);
 outp (baseport,0x0e);
 outp (baseport+1,b2);
}

short pwherex()
 {
unsigned int x,y;
 outp (baseport,0x0f);
 x=inp(baseport+1);
 outp (baseport,0x0e);
 y=inp(baseport+1);
 x=(y << 8) + x;
 return (x%80+2-px1);
}

short pwherey()
 {
unsigned int x,y;
 outp (baseport,0x0f);
 x=inp(baseport+1);
 outp (baseport,0x0e);
 y=inp(baseport+1);
 x=(y <<8) + x;
 return (x%80+2-px1);
}


void movecursor(short x,short y) /* physical coordinates */
 {
   mygotoxy (80*y + x - 81);
   curpt=(char *)0xb8000000;
   curpt+=((y-1)*160+(x-1)*2);
 }
#elif (OS_TYPE==4)
#define IN_ON_PORT	1
#define OUT_ON_PORT	0
int position ( ) {
struct port_io_arg depoz;
 depoz.args[0].dir=OUT_ON_PORT;
 depoz.args[0].port=baseport;
 depoz.args[0].data=0x0f;
 depoz.args[1].dir=IN_ON_PORT;
 depoz.args[1].port=baseport+1;
 depoz.args[2].dir=OUT_ON_PORT;
 depoz.args[2].port=baseport;
 depoz.args[2].data=0x0e;
 depoz.args[3].dir=IN_ON_PORT;
 depoz.args[3].port=baseport+1;

 ioctl (0,adaptIO,&depoz);
 return (((unsigned)depoz.args[3].data << 8) + (unsigned)depoz.args[1].data );
}

void mygotoxy (unsigned int posit) {
struct port_io_arg depoz;
unsigned char b1=posit & 0xFF;
unsigned char b2=posit >> 8;
 depoz.args[0].dir=OUT_ON_PORT;
 depoz.args[0].port=baseport;
 depoz.args[0].data=0x0f;
 depoz.args[1].dir=OUT_ON_PORT;
 depoz.args[1].port=baseport+1;
 depoz.args[1].data=b1;
 depoz.args[2].dir=OUT_ON_PORT;
 depoz.args[2].port=baseport;
 depoz.args[2].data=0x0e;
 depoz.args[3].dir=OUT_ON_PORT;
 depoz.args[3].port=baseport+1;
 depoz.args[3].data=b2;
 ioctl (0,adaptIO,&depoz);
}

short pwherex()
 {
 int x;
struct port_io_arg depoz;
 depoz.args[0].dir=OUT_ON_PORT;
 depoz.args[0].port=baseport;
 depoz.args[0].data=0x0f;
 depoz.args[1].dir=IN_ON_PORT;
 depoz.args[1].port=baseport+1;
 depoz.args[2].dir=OUT_ON_PORT;
 depoz.args[2].port=baseport;
 depoz.args[2].data=0x0e;
 depoz.args[3].dir=IN_ON_PORT;
 depoz.args[3].port=baseport+1;

 ioctl (0,adaptIO,&depoz);
 x=((unsigned)depoz.args[3].data << 8) + (unsigned)depoz.args[1].data;
  
 return (x%80+2-px1);
}
short pwherey()
 {
 int x;
 struct port_io_arg depoz;
 depoz.args[0].dir=OUT_ON_PORT;
 depoz.args[0].port=baseport;
 depoz.args[0].data=0x0f;
 depoz.args[1].dir=IN_ON_PORT;
 depoz.args[1].port=baseport+1;
 depoz.args[2].dir=OUT_ON_PORT;
 depoz.args[2].port=baseport;
 depoz.args[2].data=0x0e;
 depoz.args[3].dir=IN_ON_PORT;
 depoz.args[3].port=baseport+1;

 ioctl (0,adaptIO,&depoz);
 x=((unsigned)depoz.args[3].data <<8) + (unsigned)depoz.args[1].data;

 return (x/80+2-py1);
}
void movecursor(short x,short y) /* physical coordinates */
 {
   mygotoxy (y*80 + x - 81);
  curpt=videobuf;
   curpt+=((y-1)*160+(x-1)*2);
 }
#else

#if ((OVL_TYPE==1)&&(RESIDENT!=1))
 short pwherex()
 {
   reg.h.ah=3;
   reg.h.bh=0;
   Res_int86(0x10,&reg,&reg);
   return (reg.h.dl+2-px1);
 }
short pwherey()
 {
   reg.h.ah=3;
   reg.h.bh=0;
   Res_int86(0x10,&reg,&reg);
   return (reg.h.dh+2-py1);
 }
#else
 short pwherex()
 {
   reg.h.ah=3;
   reg.h.bh=0;
   int86(0x10,&reg,&reg);
   return (reg.h.dl+2-px1);
 }
short pwherey()
 {
   reg.h.ah=3;
   reg.h.bh=0;
   int86(0x10,&reg,&reg);
   return (reg.h.dh+2-py1);
 }
#endif
#if ((OVL_TYPE==1)&&(RESIDENT!=1))
void mygotoxy (unsigned int posit) {
unsigned char b1=posit & 0xFF;
unsigned char b2=posit >> 8;
 Res_outportb (baseport,0x0f);
 Res_outportb (baseport+1,b1);
 Res_outportb (baseport,0x0e);
 Res_outportb (baseport+1,b2);
}
#else
void mygotoxy (unsigned int posit) {
unsigned char b1=posit & 0xFF;
unsigned char b2=posit >> 8;
 outportb (baseport,0x0f);	/* @0231 */
 outportb (baseport+1,b1);
 outportb (baseport,0x0e);
 outportb (baseport+1,b2);
}
#endif

#if ((OVL_TYPE==1)&&(RESIDENT!=1))
void movecursor(short x,short y) /* physical coordinates */
 {
  reg.h.ah=2;
  reg.h.bh=0;
  reg.h.dh=y-1;
  reg.h.dl=x-1;
  Res_int86(0x10,&reg,&reg);
/*   mygotoxy (80*y + x - 81);  */
   curpt=(char *)0xb8000000;
   curpt+=((y-1)*160+(x-1)*2);
 }
#else
void movecursor(short x,short y) /* physical coordinates */
 {
  reg.h.ah=2;
  reg.h.bh=0;
  reg.h.dh=y-1;
  reg.h.dl=x-1;
  int86(0x10,&reg,&reg);
/*   mygotoxy (80*y + x - 81);  */
   curpt=(char *)0xb8000000;
   curpt+=((y-1)*160+(x-1)*2);
 }
#endif
#endif             /* @0184 */
void pgetputtext(p,xx1,yy1,xx2,yy2,buf)		/* @0182 */
short p;	/* @0182 */
short xx1;	/* @0182 */
short yy1;	/* @0182 */
short xx2;	/* @0182 */
short yy2;	/* @0182 */
char *buf;	/* @0182 */
 {
  char *pt;
  short i,j;
#if (OS_TYPE==4)          /* @0184 */
  pt=videobuf;            /* @0184 */
#else                     /* @0184 */
  pt=(char *)0xb8000000;
#endif                    /* @0184 */
  pt+=((yy1-1)*160+(xx1-1)*2);
  for (i=0;i<yy2-yy1+1;i++)
   {
    for (j=0;j<xx2-xx1+1;j++)
      {
	if (p==0)     /* gettext */
	  *buf=*pt;
	else	      /* puttext */
	  *pt=*buf;
	buf++;
	pt++;
	if (p==0)
	  *buf=*pt;
	else
	  *pt=*buf;
	buf++;
	pt++;
      }
    pt+=(160-2*(xx2-xx1+1));
   }
 }
void pgotoxy(x,y)	/* @0182 */
short x;	/* @0182 */
short y;	/* @0182 */
 {
  pcurx=x+px1-1;
  pcury=y+py1-1;
  movecursor(pcurx,pcury);
 }
void pputch(c)		/* @0182 */
char c;		/* @0182 */
 {
  char *pt;
  pt=curpt;
  if (c==0x0d)			/* @0079 */
    pcurx=px1;			/* @0079 */
  else				/* @0079 */
    if (c==0x0a)		/* @0079 */
      if (pcury==py2)		/* @0079 */
	pcury=py1;		/* @0079 */
      else 			/* @0079 */
	pcury++;		/* @0079 */
    else			/* @0079 */
      {				/* @0079 */
	*pt=c;
	pt++;
	*pt=pattr;
	if (pcurx==px2)
	  {
	    pcurx=px1;
	    if (pcury==py2)
	      pcury=py1;
	    else
	      pcury++;
	  }
	else
	  pcurx++;
      }				/* @0079 */
  movecursor(pcurx,pcury);
 }
void pcprintf(s,dec)	/* @0159 */	/* @0182 */
char *s;	/* @0182 */
unsigned short dec;	/* @0182 */
 {					/* 1 */
  unsigned  short len,min;		/* @0159 *//* @0160 */
  while (*s != '\0')
   {                                    /* 2 */
    if (*s!='%')
      pputch(*s);
    else
      {                                 /* 3 */
       len=0;
       min=0;		/* @0160 */
       s++;
       if (*s=='%')
	   pputch('%');
       else
	{                               /* 4 */
	 if (*s>48 && *s<54)
	  {
	   len=*s-48;
	   s++;
	  }
	 if (*s=='.')		/* @0160 */
	   {			/* @0160 */
	     s++;		/* @0160 */
	     if (*s>48 && *s<54)/* @0160 */
	       {		/* @0160 */
		 min=*s-48;	/* @0160 */
		 s++;		/* @0160 */
	       }		/* @0160 */
	     else		/* @0160 */
	       ;		/* @0160 */
	   }			/* @0160 */
	 if (*s=='u' || *s=='d')/* @0160 */
	     c_dec(dec,len,min);/* @0160 */
	 else ;
	}                               /* 4 */
      }
     s++;
    }
 }
void c_dec(de,le,mi)	/* @0159 *//* @0160 *//* @0182 */
unsigned short de;	/* @0182 */
unsigned short le;	/* @0182 */
unsigned mi;		/* @0182 */
 {
  char d[5];
  unsigned short i,new,old;	/* @0159 */
  old=de;
  for (i=0;i<5;i++)
   {
    new=old/10;
    d[i]=old-new*10;
    old=new;
   }
  for (i=5;i>0;i--)            /* @0159 */
   if (d[i-1]==0 && i>mi)              /* @0159 *//* @0160 */
     d[i-1]=' ';               /* @0159 */
   else
     break;
  if (le==0)
   le=i;					/* @0160 */
  for (i=le;i>0;i--)   /* @0159 */
    if (d[i-1]==' ')   /* @0159 */
      pputch(32);      /* @0159 */
    else               /* @0159 */
      pputch(d[i-1]+48);/* @0159 */
 }

