#include<stdio.h>
#include<math.h>

#define  stdprn NULL

/* ---------------------------------------------------------------*/
#define RI_IN "Voltabl1.txt"
#define P_IN  "Pwrtabl1.txt"
#define PWR_OUT "p003.bin"
#define VOLT_OUT "v003.bin"
/* ---------------------------------------------------------------*/

	unsigned long int samp;
	int a,b,c,d,Adr,r,i,p;
	FILE *RI, *P, *U16, *U17;
	int r1, i1, p1,
	    r2, i2, p2;
	char ir,rp ;

void main()
{
  fprintf(stdout,"    PA and IA LOOK-UP combiner      \n") ;
  fprintf(stdout,"It takes in PA and IA lookup files (TXT format),      \n") ;
  fprintf(stdout,"and produces U16 and U17 files \n") ;
  fprintf(stdout," Input files : PWRtabl1.txt, VOLTabl1.txt \n") ;
  fprintf(stdout,"Output files : P003.bin, V003.bin \n") ;

  RI = fopen(RI_IN,"rt");
  P  = fopen(P_IN,"rt");

  U16 = fopen(PWR_OUT, "wb");
  U17 = fopen(VOLT_OUT, "wb");

  samp=1;                     /* Generate the LT values for */
  for(d=0;d<=15;d++)         /* all 16 Tables, for all combination of the */
    for(c=0;c<=15;c++)          /*   EXP part, */
      for(b=0;b<=15;b++)        /*   Imaginary part, */
	for(a=0;a<=15;a++)      /*  and Real parts. */
	{      fscanf(RI,"%d %d %d",&r1,&i1) ;
	       fscanf(P,"%d",&p1) ;

	       r2 = r1 & 0x1f;     /* 000x xxxx ; R,I 5-bits each */
	       i2 = i1 & 0x1f;
	       p2 = p1 & 0x3f;     /* 00xx xxxx ; pwr 6-bits wide */

	       /*  iiii irrr   rrpp pppp
		   ---------   ---------
		       |           |
		      U17         U16
		      (ir)        (rp)
	       */

	       ir = ((i2 << 3)&0xf8) | ((r2>>2)&0x7);
	       rp = ((r2 << 6)&0xc0) | p2 ;


		fwrite(&ir,1,1,U17);
		fwrite(&rp,1,1,U16);

		/* ------------------below codes are just for monitoring */
			 if(fmod(samp,1024)==0)
			    printf("Finished combining %6ld samples\n",samp);
			 samp++;
	  }
  printf("\nSaved all values\nExiting\n\n");
  fcloseall();
} /* End of Program          */








