/* PVformat.c  */
#define NOTE_1 "Generates PVFORMAT.BIN,8IP-U29,35 PROM content.19Feb98 TP/JR"

/* This Eprom is used in both Power and Voltage (R&I) 8IP card last
   stages as a Output format Translator Lookup Tables.

  It is a copied and modified version of PFORMAT2.C. In this version
  the Diag mode Format tables (ie. format selection # 5,6,7) are replaced
  with the Sign Magnitude format tables. However, the Formats-0 to 4 are
  retained in their respective places, as it was, in the case of PFORMAT2.BIN.
  The Sign-Magnitude Version Tables can be used by the back-end machines,
  which connects to the PA mode outputs.


Seleted		Nature of						Select while
Format(Bank)	output							# of dishes are
------------    ---------						---------------
  0 (obs)	[Integer of Input/1] <<3 positions. 			1  to 4
  1 (obs)	[Integer of (Input/2 + 0.5)] <<3 positions.		5  to 8
  2 (obs)	[Integer of (Input/4 + 0.5)] <<3 positions.		9  to 16
  3 (obs)	[Integer of (Input/8 + 0.5)] <<3 positions.		17 to 32

  4 (obs/diag)	All 11 bits of Input.

  5 (PA obs)	Sign-Magn. of( [Integer of Input/1] <<3 positions )           1  to 8
  6 (PA obs)    Sign-Magn. of([Integer of (Input/2 + 0.5)] <<3 positions )    9  to 16
  7 (PA obs)    Sign-Magn. of([Integer of (Input/4 + 0.5)] <<3 positions)     17 to 32

 Input to the PROM

      MSB                            LSB
	* * *    * * * * * * * * * * *
	 \ /      \                 /
	F2..F0         D10..D0
*/

/* --------------------------------------------------------------------- */

#include "gac.h"
#include <stdio.h>
#define PromFile "PVFORMAT.BIN"
#define PromText "PVFORMAT.TXT"
#define PromSize 0x3fff        /* 16k X 16 */

void main(int argc, char *argv[])
{	char  c0, c1;
	unsigned int data,format, d1, d2, Magnitude,SignMagnitude,Signmask;
	FILE *out, *txt;

	clrscr();
	fprintf(stdout,"\nRunning : %s\n\n%s \n\n\n", argv[0], NOTE_1);
	printf("Creating %s ...\n", PromFile);

	out = fopen(PromFile,"wb");
	txt = fopen(PromText,"wt");

	fprintf(PromText,"\nRunning : %s\n\n%s \n\n\n", argv[0], NOTE_1);
    /* Prom Address[0..10] = d[0..10] */

    for(format=0; format<=7; format++)
	for(d1=0; d1<=2047; d1++)
       {
	   /* Left shifting by 3 is for aligning MSBs */

	   if(format==0)
		data = d1 << 3;

	   if(format==1)
		data = (int( d1/2 + 0.5)) << 3;

	   if(format==2)
		data = (int(d1/4 + 0.5)) << 3;

	   if(format==3)
		data = (int(d1/8 + 0.5)) << 3;

	   if(format==4)
		data = d1;

   /* --------------------------From here, sign magnitude o/p formats used */
     /* This format (format 5) will be selected, when the # of  dishes doesnot
      exceed Eight. Under this condition, the 5-bit o/p from the Voltage-
      lookup, grows only by another 3-bits, to make an overall growth of
      8-bits,including the sign bit. Now the sign of the number will have to
      be decided by looking at the numbers present MSB, which is the 8th bit.
      In otherwards the 8th bit place is the Currently, expected Sign bit place.
      Hence  000 1000 0000b (or) 080hex is used as a Mask/to-detect the sign
      of the data.
   */
	   SignMask = 0x080;
	   if(format==5)
	   {  if ((d1&SignMask)==SignMask)      /* if the number is -ve */
		magnitude = (~d1+1) ;   /* find absolute magnitude, and  */
		/* set the signbit as 1, to indicate the Negative magnitude */
		SignMagnitude = magnitude | SignMask ;
		data = SignMagnitude << 3;  /* align the MSB to 11th bit */
	      else
		data = d1 << 3;         /* same as Format#0 */
	   }

   /* This format (format 6) will be selected, when the # of  dishes are
      9 and above and upto 16. Under this condition, the o/p from the
      Voltage-lookup, grows to 9-bits,including the sign bit. The 9th bit
      is the Currently, expected Sign bit place.

      Hence  001 0000 0000b (or) 100hex is used as a Mask/to-detect the sign
      of the data.
   */
	   SignMask = 0x100;
	   if(format==6)
	   {  if ((d1&SignMask)==SignMask)      /* if the number is -ve */
		magnitude = (~d1+1) ;   /* find absolute magnitude, and  */
		magnitude = int((magnitude/2 + 0.5)) ; /* divide by 2 and*/
		/* set the signbit as 1, to indicate the Negative magnitude */
		SignMagnitude = magnitude | SignMask ;
		data = SignMagnitude << 3;  /* align the MSB to 11th bit */
	      else
		data = (int(d1/2 + 0.5)) << 3;  /* same as format#1 */
	      }

   /* This format (format 7) will be selected, when the # of  dishes are
      17 and above and upto 32. Under this condition, the o/p from the
      Voltage-lookup, grows to 10-bits,including the sign bit. The 10th bit
      is the Currently, expected Sign bit place.

      Hence  010 0000 0000b (or) 200hex is used as a Mask/to-detect the sign
      of the data.
   */
	   SignMask = 0x200 ;
	   if(format==7)
	   {  if ((d1&SignMask)==SignMask)      /* if the number is -ve */
		magnitude = (~d1+1) ;   /* find absolute magnitude, and  */
		magnitude = int((magnitude/4 + 0.5)) ; /* divide by 4 and*/
		/* set the signbit as 1, to indicate the Negative magnitude */
		SignMagnitude = magnitude | SignMask ;
		data = SignMagnitude << 3;  /* align the MSB to 11th bit */
	      else
		data = (int(d1/4 + 0.5)) << 3; /* same as format#2 */
	      }

    fwrite(&data,sizeof(data),1,out);
    fprintf(txt,"%2x  %2x  %2x  \n",format,d1,data);

       }


    /* Next two fwrite are used to write the CONTROL WORDS at 4000 Hex location */
    /* Control Word : X CS2 CS1 CS0  X X X X X  X X X X  X X X OE  */
    /* Control Word 0x0080 programs CS2,CS1,CS0, OE as active LOW  */

	c0 = 0x08; c1 = 0x00;
	fwrite(&c0,1,1,out);   	/* Write LS 8 control bits */
	fwrite(&c1,1,1,out);	/* Write MS 8 control bits */
	fclose(out);
}   /* ---------------------------------------------------End of program -- */
