#include <stdio.h>
#include <ctype.h>

unsigned int offset, attr ;
char *str, data ;

#define  crt_putc(data,attr,row,col)  { offset = (row)*160 + (col)*2;\
         mem_putc(data,attr,offset); }
#define  crt_puts(str,attr,row,col)  { offset = (row)*160 + (col)*2;\
         mem_puts(str,attr,offset); }

extern clear_screen();
extern set_cursor();

box(left_up_row,left_up_col,right_down_row,right_down_col,label,type)
char *label ;
{
	int i, j, cv, ch ;
	cv = left_up_row ;
	ch = left_up_col ;

	if (type==1) crt_putc(218,0x07,cv,ch++);
	if (type==2) crt_putc(201,0x07,cv,ch++);
	for ( i = left_up_col+1; i < right_down_col; i++)
	{	if (type==1) crt_putc(196,0x07,cv,ch++) ;
		if (type==2) crt_putc(205,0x07,cv,ch++) ;
	}
	if (type==1) crt_putc(191,0x07,cv,ch++) ;
	if (type==2) crt_putc(187,0x07,cv,ch++) ;
	cv++ ;
	for ( j = left_up_row+1; j < right_down_row; j++)
	{
		if (type==1)
		{	crt_putc(179,0x07,cv,left_up_col) ;
                	crt_putc(179,0x07,cv++,right_down_col) ;
		}
		if (type==2)
		{	crt_putc(186,0x07,cv,left_up_col) ;
			crt_putc(186,0x07,cv++,right_down_col) ;
		}
	}
	cv = right_down_row ;
	ch = left_up_col ;
	if (type==1) crt_putc(192,0x07,cv,ch++) ;
	if (type==2) crt_putc(200,0x07,cv,ch++) ;
	for ( i = left_up_col+1; i < right_down_col; i++)
	{	if (type==1) crt_putc(196,0x07,cv,ch++) ;
		if (type==2) crt_putc(205,0x07,cv,ch++) ;
	}
	if (type==1) crt_putc(217,0x07,cv,ch++) ;
	if (type==2) crt_putc(188,0x07,cv,ch++) ;
/*	sprintf(label,"port [%d]",1);*/
	i = left_up_col+(right_down_col-left_up_col+1)/2-strlen(label)/2 ;
	crt_puts(label,0x0E,left_up_row,i) ;
}

int	ROWD[8] = {8,8,8,8,19,19,19,19};
int	COLD[8] = {3,23,43,63,3,23,43,63};
int	ROWS[8] = {10,10,10,10,21,21,21,21};
int	COLS[8] = {3,23,43,63,3,23,43,63};
int	ROWI[8] = {3,3,3,3,14,14,14,14};
int	COLI[8] = {10,30,50,70,10,30,50,70};
int	ROWO[8] = {4,4,4,4,15,15,15,15};
int	COLO[8] = {10,30,50,70,10,30,50,70};
int	ROWB[8] = {6,6,6,6,17,17,17,17};
int	COLB[8] = {10,30,50,70,10,30,50,70};
int     ROWL[8] = {2,2,2,2,13,13,13,13};
int     COLL[8] = {6,26,46,66,6,26,46,66};
char	buffer[25];

box_puts(box,a,b)
int	box;
int     a;
int     b;
{
        if(b==1)
          sprintf(buffer,"[%02d] rx data is OK",a);
        else
          sprintf(buffer,"[%02d] rx data is Wrong",a);          
	crt_puts(buffer,0x70,ROWS[box],COLS[box]-2) ;
}

box_putd(box,a)
int	box;
int     a;
{
    int b,d,c;

        b=(a & 0x04) > 2;
        d=(a & 0x02) > 1;
        c= a & 0x01;
        sprintf(buffer," B[%d]  D[%d]  C[%d] ",b,d,c);
	crt_puts(buffer,0x70,ROWD[box],COLD[box]-2) ;
}

boxi_put(box,val)
int	box;
unsigned long	val;
{
	sprintf(buffer,"%08lX",val);
	crt_puts(buffer,0x2F,ROWI[box],COLI[box]) ;
}

boxo_put(box,val)
int	box;
unsigned long	val;
{
	sprintf(buffer,"%08lX",val);
	crt_puts(buffer,0x1F,ROWO[box],COLO[box]) ;
}

box_putb(box,buf)
int	box;
char	*buf;
{
	crt_puts(buf,0x3F, ROWB[box], COLB[box]); 
}

box_putl(box,buf)
int	box;
char	*buf;
{
	crt_puts(buf,0x0E, ROWL[box], COLL[box]); 
}


ShowForm(n)
int	n;
{
	char	buf[100];
	char    buff1[10],buff2[10],buff3[10],buff4[10];    
	char    buff5[10],buff6[10],buff7[10],buff8[10];    
	
	if ( !video_mode() ) {
		printf("This demo program only support 80X25 text mode.\n");
		exit(0);
	}
	clear_screen() ;
	set_cursor(30,0);	/* set cursor invisable */
    sprintf(buf, "****************************  P640N DEMO PROGRAM  *****************************");
	crt_puts(buf, 0x4f, 0, 0);
	sprintf(buff1,"");

	box( 2, 0, 12, 19, buff1, 2);
	crt_puts("INPUT  :          ",0x2F,3,1);
	crt_puts("OUTPUT :          ",0x1F,4,1);
	crt_puts("BAUD   :          ",0x3F,6,1);
	crt_puts("                  ",0x70,10,1);
	
	box( 2, 20, 12, 39, buff1, 2);
	crt_puts("INPUT  :          ",0x2F,3,21);
	crt_puts("OUTPUT :          ",0x1F,4,21);
	crt_puts("BAUD   :          ",0x3F,6,21);
	crt_puts("                  ",0x70,10,21);
	
	
	box( 2, 40, 12, 59, buff1, 2);
	crt_puts("INPUT  :          ",0x2F,3,41);
	crt_puts("OUTPUT :          ",0x1F,4,41);
	crt_puts("BAUD   :          ",0x3F,6,41);
	crt_puts("                  ",0x70,10,41);
	
	box( 2, 60, 12, 79, buff1, 2);
	crt_puts("INPUT  :          ",0x2F,3,61);
	crt_puts("OUTPUT :          ",0x1F,4,61);
	crt_puts("BAUD   :          ",0x3F,6,61);
	crt_puts("                  ",0x70,10,61);
	
	box( 13, 0, 23, 19, buff1, 2);
	crt_puts("INPUT  :          ",0x2F,14,1);
	crt_puts("OUTPUT :          ",0x1F,15,1);
	crt_puts("BAUD   :          ",0x3F,17,1);
	crt_puts("                  ",0x70,21,1);
	
	box( 13, 20, 23, 39, buff1, 2);
	crt_puts("INPUT  :          ",0x2F,14,21);
	crt_puts("OUTPUT :          ",0x1F,15,21);
	crt_puts("BAUD   :          ",0x3F,17,21);
	crt_puts("                  ",0x70,21,21);
	
	box( 13, 40, 23, 59, buff1, 2);
	crt_puts("INPUT  :          ",0x2F,14,41);
	crt_puts("OUTPUT :          ",0x1F,15,41);
	crt_puts("BAUD   :          ",0x3F,17,41);
	crt_puts("                  ",0x70,21,41);
	
	box( 13, 60, 23, 79, buff1, 2);
	crt_puts("INPUT  :          ",0x2F,14,61);
	crt_puts("OUTPUT :          ",0x1F,15,61);
	crt_puts("BAUD   :          ",0x3F,17,61);
        crt_puts("                  ",0x70,21,61);

crt_puts("                                                       RAYON TECHNOLOGY CO.,LTD.",0x4F,24,1);
 
set_cursor(0,0);	/* set cursor invisiable */
}

