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

#define ESC	0x1B 

BYTE    rxbuffer[50];                     
INT     Box[16];
DWORD	OutLength[16] = { 0L };
DWORD	InLength[16] = { 0L };
DWORD	QueLength[16] = { 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> <card>\n\n\
	<baud> :        110, 150, 300, 600, 1200, 2400, 4800, 9600, \n\
                        19200, 38400, 57600, 115200, 50, 75, 100, 200\n\n\
        <card> :        1 - 4 piece card\n\n\
";

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

   INT    i,j,tt=0;
   INT    maxcard=0;
   CHAR   txbuf[100];
   WORD   b;
   WORD   Cflag,Iflag;
   WORD   card,model=0;
   WORD   temp,len,len1;
   WORD   aa,start=1;
   WORD   value,showport;
   WORD   key,noblock=0;
   WORD   qvalue;
   int    c;
  

     if (argc < 2 || argc > 3)
     {
	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=PciPortMaxCard();

     
     if (argc == 3)
     {      
     aa = ((int)argv[2][0])-48;
     if ( aa > maxcard || aa == 0 )
     {
           printf("You choose wrong card number");
           exit(0);
     }  
     else    
       maxcard=start=aa;
     } 


     b=i & 0x000F;
 
     Cflag =  b | CHR8 | NOPARITY | STOP1 ;
     Iflag =  0;
     
     
     if( PciPortInstall() != TRUE ) 
     {
   	printf("PCIPORT driver was not installed! Please load the driver first!\n");
       	exit(0);
     }
     
          
     for( card = start ; card <= maxcard ; card++ )
     {
        model=PciPortMaxPort(card); 
                      
        for( i = 0 ; i < model ; i++ )
        {
           
            temp=PciPortOpen( card, i, Cflag, Iflag);
            if(temp != 0)
            {
               printf("Port Open Fail\n"); 
               PciPortResetDoq(card,i);
               PciPortClose( card, i);
               exit(0);
            }
            else
               printf("port open OK!!\n");   
        }
     }


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

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

		if (key == 0)
		{  
 		   goto go;
 		}

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


go: 
     value=model=0;
     for(card = start ; card <= maxcard ; card++)
     {

         if(card != 1) 
            value+=model;       /*show block*/
        
         model=PciPortMaxPort(card);

         noblock=0;
         if((16-value < model) || (value > 16)){         /*show max 16 block*/
          noblock=1;
         }
          
          for(j=0;j<model;j++)
          { 
             if(noblock==0)
              {
              showport = j + value;
              box_putb(showport, "     ");
              box_putb(showport, Baud[b]);

              boxo_put(showport, OutLength[showport]);
              boxi_put(showport, InLength[showport]);
               }	

              if (PciPortDoqSpace(card,j) > 40)
              {        
                 
                   sprintf(txbuf,"RAYON-PCIPORT-1234567890-abcdefghijklmno");

                   len=PciPortWrite(card,j,(BYTE far *)txbuf,40);

                   if(len!=-1 && noblock==0)           
                   {
                    OutLength[showport] += len;
                    boxo_put(showport, OutLength[showport]);
                    }          
                    
                    
              }              
     
                 if(PciPortDiqCount(card,j)>40)
                 {  
                      len=PciPortRead(card,j,(BYTE far *)rxbuffer,40);
                      
                      if(len!=-1 && noblock==0)  
                      {
                      InLength[showport] += len;
                      boxi_put(showport, InLength[showport]);
                      }

                       
                 }

                                   
          }          	
          
      }
       
 }

     for(card=start;card<=maxcard;card++)
     {
          model=PciPortMaxPort(card);
          for(i=0;i<model;i++)
          {
             PciPortResetDoq(card,i);     
             if(PciPortClose(card,i)==-1)
               printf("port close fail!");	
          }     
     }

  
}    

