#include<dos.h>
#include<conio.h>
#include<fcntl.h>
#include "pci.h"

#define ESC	0x1B 
#define F1      0x3B
#define F2      0x3C
#define F3      0x3D
#define F4      0x3E
#define F5      0x3F
#define F6      0x40
#define F7      0x41
#define F8      0x42

BYTE    rxbuffer[50];                     
INT     Box[16];
INT     Status[8];
DWORD	OutLength[64] = { 0L };
DWORD	InLength[64] = { 0L };
DWORD	QueLength[64] = { 0L };	

char    *Baud[16] = { "110", "150", "300", "600", "1200", "2400", "4800", "9600", "19200",
                     "38400", "57600", "115200", "50", "75", "100", "200"}; 


char *usage = "\n\
Argument Error!!!\n\
Usage :\n\
        C>DEMO <baud> \n\n\
	<baud> :        110, 150, 300, 600, 1200, 2400, 4800, 9600, \n\
                        19200, 38400, 57600, 115200, 50, 75, 100, 200\n\n\
";

void main(argc,argv)
INT	argc;
CHAR    *argv[];
{

   INT    i,j,tt=0;
   INT    maxcard=0;
   CHAR   txbuf[100];
   WORD	  stus;
   WORD   b;
   WORD   Cflag,Iflag;
   WORD   card,model=0;
   WORD   temp,len,len1;
   WORD   aa,start=1;
   WORD   value,showport=0;
   WORD   key;
   char   buff[10];
   int    c;
   int    te;
   int   count=0;
  

     if (argc > 2)
     {
	printf(usage);
	exit(0);
     }

     for (i = 0; i < 16; i++)
     {
         if (!strcmp(argv[1], Baud[i]))
		break;
     }
     
     if (i == 16)
     {
        printf(usage);
 	exit(0);
     }
     
     maxcard=PciIopMaxCard();


     b=i & 0x000F;
 
     Cflag =  b | CHR8 | NOPARITY | STOP1 ;
     Iflag =  0;
     

     
     if( PciIopInstall() != TRUE ) 
     {
   	printf("PCIIOP driver was not installed! Please load the driver first!\n");
       	exit(0);
     }
     
       Help();        
       
       model=PciIopMaxPort(1); 

        for( i = 0 ; i < model ; i++ )
        {
           
            temp=PciIopOpen( 1, i, Cflag, Iflag);
            if(temp != 0)
            {
               printf("Port Open Fail\n"); 
               PciIopResetDoq( 1, i);
               PciIopClose( 1, i);
               exit(0);
            }
            else
               printf("port open OK!!\n");   
        }

    ShowForm();                 /* Display boxes (in scr.c) */                 

for (;;)
{
		key = read_key();

		if (key == 0)
		{  
 		   goto go;
 		}
 		
 		if ( (key & 0x00FF) == 0 )
                {
                 key >>= 8;
                 if (key >= F1 && key <= F8)
                 {
                    if(key==F1)
                      showport=0;	
                      
                    if(key==F2)
                      showport=1;	
                    if(key==F3)
                      showport=2;	                      
                    if(key==F4)
                      showport=3;	                 
                    if(key==F5)
                      showport=4;	          
                    if(key==F6)
                      showport=5;	
                    if(key==F7)
                      showport=6;	
                    if(key==F8)
                      showport=7;
                      
                }      
          }

                key &= 0xFF;
		if (key == ESC)
                   break;
                
		key = toupper(key);


go: 
          
          for(i=0;i<model;i++)
          { 
          	for(te=0;te<8;te++)
          	{	
          		sprintf(buff," PORT %02d ",showport*8+te);
	                box_putb(te, "     ");
                        box_putb(te, Baud[b]);
                        box_putl(te, buff);
                        boxo_put(te, OutLength[showport*8+te]);
                        boxi_put(te, InLength[showport*8+te]);
                }        

                          if (PciIopDoqSpace(1,i) > 40)
                          {        

                               sprintf(txbuf,"RAYON-PCIPORT-1234567890-abcdefghijklmno");
				delay(5);
                               len=PciIopWrite(1,i,(BYTE far *)txbuf,40);

                               if(len!=-1)           
                                   OutLength[i] += len;
                           }              
     
     
     
                           if(PciIopDiqCount(1,i)>40)
                           {  
                                len=PciIopRead(1,i,(BYTE far *)rxbuffer,40);
                      
                                if(len!=-1)  
                                    InLength[i] += len;
                       
                           }
                

          }          	
          
}

          for(i=0;i<model;i++)
          {
             PciIopResetDoq(1,i);     
             if(PciIopClose(1,i)==-1)
               printf("port close fail!");	
          }     

  
}    

Help()
{
	clrscr();
	printf("\n");
	printf("\t**********************   P640N DEMO program   ******************\n\n");
	printf("\t\t[F1]    :    Show ports on box 1.\n");
	printf("\t\t[F2]    :    Show ports on box 2.\n");
	printf("\t\t[F3]    :    Show ports on box 3.\n");
	printf("\t\t[F4]    :    Show ports on box 4.\n");
	printf("\t\t[F5]    :    Show ports on box 5.\n");
	printf("\t\t[F6]    :    Show ports on box 6.\n");
	printf("\t\t[F7]    :    Show ports on box 7.\n");
	printf("\t\t[F8]    :    Show ports on box 8.\n\n");
	printf("\t\t[ESC]   :    Quit.\n\n");

	printf("\t\tPress a key to demo program ...\n");
	getch();
}