/* F276-11B.C                           07th JAN.98        */
/* F276-11A.C  				19th OCT.97    JR  */
/* Program for testing the 8IP Card.  In this data generation scheme
   the output is as follows in different banks of the PROM
   (2exp3 = 8banks):

Format(Bank)
 0		Output <- Input(Address)
 1		Output <- Input(Address) reversed - only lower 10 bits
			  (To make it compatible for both the groups of
			   output.  The 11th bit is taken out as it is.)
 2		Output <- 16Mhz (LSB rate)
 3		Output <- 8Mhz (LSB+1 rate)
 4-8            All ZEROs

 Input to the PROM

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

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

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

int reversebits(int i);

void main()
{	char  d, c0, c1;
	int data[PromSize];
	int i, j, k, f1, d1, d2;

	FILE *out, *txt;

	clrscr();
	printf("Creating %s ...\n", PromFile);

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

    /* Prom Address[0..10] = d[0..10] */

    /* Initialize the data[] values */
    for(i=0; i<PromSize; i++)
	data[i] = 0x0;

    i = 0;
    for(f1=0; f1<=(PromSize/2048); f1++)
	for(d1=0; d1<=2047; d1++)
       {
//	 j = (f1*2048)+d1;  /* address computation */
//	 k = (f1<<11)|d1;

	   if(f1==0)
		data[i] = d1;

	   if(f1==1)
		data[i] = ((d2 = reversebits(d1)) | (d1&0x0400));

	   if(f1==2)
	      {	d2 = d1 & 0x0001;
		data[i] = d2 | d2<<1 | d2<<2 | d2<<3 | d2<<4 | d2<<5 | d2<<6 |
			  d2<<7 | d2<<8 | d2<<9 | d2<<10;
	      }

	   if(f1==3)
	      {	d2 = d1 & 0x0002;
		data[i] = d2 | d2<<1 | d2<<2 | d2<<3 | d2<<4 | d2<<5 | d2<<6 |
			  d2<<7 | d2<<8 | d2<<9 | d2>>1;
	      }

	   else
		data[i] = 0x0000;     /* Load all with ZEROs */

    fwrite(&data[i],1,1,out);
    fwrite(&data[i],1,1,out);
    fprintf(txt,"%4d  %4x  %2x %2x  %2x = %2x  \n",i,j,k,f1,d1,data[i]);

	   i++;   /* Address computation for data[i] */
}


    /* 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);
}

int reversebits(int i)
{	int rev,dat;
	dat = (i & 0x03ff);
	rev = ((dat & 0x0200)>>9)|((dat & 0x0001)<<9) | ((dat & 0x0100)>>7)|((dat & 0x0002)<<7)|
	      ((dat & 0x0080)>>5)|((dat & 0x0004)<<5) | ((dat & 0x0040)>>3)|((dat & 0x0008)<<3)|
	      ((dat & 0x0020)>>1)|((dat & 0x0010)<<1);
	return(rev);
}
