/* clkinit6.c   6 Jan '98 RRI.
		CV,GCK16,X4,X3,X2 positions shifted by 6-locations to
		account for the additional 6 pipeline stages in the
		8-input card.                              JR,TP      */  

/* clkinit5.c   18 may 96 khodad  Xinit4,Gated clock 16Mhz, ClockValid
				  relocated by +2 locations */
/* clkinit4.c   17 may 96 khodad  N_Rotate increased by 2 */
/*  ClkInit3.c  24 Mr 96  khodad */
/*  ClkInit1.c  Ver:1  - Dated : 28 Nov '95 */
/* *************************************************  */
/*              CY 7C245A        Clock and Inits      */
/* *************************************************  */

/* 24Mr96 Clock Cycle-Offset introduced to match with the FFT init and the
   FFT DC channel positions */
/* This program generates              */
/*      Clock Valid ( CV )	       */
/*      Gated Clock 16 MHz (GCK16)            */
/*      16 MHz Clock  (CK16)	       */
/*      Xinit4 ( X4 - for Pol.mtr. & search machine )*/
/*      Xinit3 ( X3 - for CDS)              */
/*      Xinit2 ( X2 - for 8 input card )    */
/*      Xinit1 ( X1 - for 4 input card )    */
/*      Reset  ( RST - for the Addr. counters) */

#define   N_Rotate 62     /* No of clocks before XINITs  */

#define   CVL  8  /* ( CV goes Low at 8th Location)  */
#define   CVH  12  /* ( CV back to High at 12th location )*/

#define   GCK16L  8         
#define   GCK16H  12        

#define  X4L 8    
#define  X4H 12   

#define  X3L 11
#define  X3H 12

#define  X2L 11
#define  X2H 12

#define  X1L 513
#define  X1H 514

#define  RSTL 514
#define  RSTH 515

#include "gac.h"

main()
{	unsigned char data[2050],rdata[2050],d ;
	int i,j;
	FILE *out;

   clrscr() ;
   out = fopen("signals6.bin","wb"); /*data file for CY7C245A */

   /* divide by 2, and gated clock generation */
   for(i=0;i<=2047;i++)
   {   data[i] = 0x0;
       data[i] = data[i] | ((i<<2) & 0x4 ) ; /* produces CK16 */
       if( (i<GCK16L) || (i>=GCK16H))
	   data[i] = data[i] | ((i<<1) & 0x2 ) ; /* produces GCK16 */
/*   printf("%1x  %1x  %1x  %1x  \n",(data[i]>>3)&0x1, (data[i]>>2)&0x1,(data[i]>>1)&0x1,data[i]&0x1 );*/
   }

    for(i=0;i<=2047;i++)
    {  data[i] = data[i] | 0xf9 ;

       if( (i>= CVL) && (i< CVH) )   /* fe = 1111 1110 */
	 data[i] = data[i] & 0xfe ; /* make CV Low  (bit 0 - LSB)*/

       if( (i>= X4L) && (i< X4H) )  /* f3 = 1111 0111 */
	 data[i] = data[i] & 0xf7 ; /*  make X4 Low ( bit 3 )*/

       if( (i>= X3L) && (i< X3H) )  /* ef = 1110 1111 */
	 data[i] = data[i] & 0xef ; /* make X3 Low ( bit 4 )*/

       if( (i>= X2L) && (i< X2H) )  /* df = 1101 1111 */
	 data[i] = data[i] & 0xdf ; /* make X2 Low ( bit 5 )*/

       if( (i>= X1L) && (i< X1H) )  /* bf = 1011 1111 */
	 data[i] = data[i] & 0xbf ; /* make X1 Low ( bit 6 )*/

       /* RST generation moved to the end */

    }
   /* The following 2 for loops does the INIT alignment with reference to */
   /* the FFT init (Master reset )                                        */
   /* In this case the FFT init arrives 64 Clocks ahead of the data Ch# 0 */
   /* and hence the sequencer need to be rotated for 64 Clocks            */
   /* Note: 'N_Rotate' tells  how many rotations are required, modify     */
   /* this value  suitably at the begining of this program. */

   for(i=0; i<=(2047-N_Rotate); i++)  /* moves the data by N_Rotate   */
      rdata[N_Rotate+i] = data[i] ;      /* locations                    */

   for(i=0; i<N_Rotate; i++)          /* copies the first N_Rotate locations */
     rdata[i] = data[ (515-(N_Rotate-1)) +i ] ; /* from 516-N_Rotate values */


   printf("Addr:RST X1 X2 X3 X4 C GC CV\n") ;
   for(i=0;i<=2047;i++)
    { /* sequencer addr counter reset generated here     */
       if( (i>= RSTL) && (i< RSTH) )   /* 7f = 0111 1111 */
	   rdata[i] = rdata[i] & 0x7f ; /* make X1 Low ( bit 7 - MSB )*/

      printf("%4i: %1x  %1x  %1x  %1x  ",i,(rdata[i]>>7)&0x1, (rdata[i]>>6)&0x1,(rdata[i]>>5)&0x1,(rdata[i]>>4)&0x1 );
      printf("%1x  %1x  %1x  %1x   : %2x\n",(rdata[i]>>3)&0x1, (rdata[i]>>2)&0x1,(rdata[i]>>1)&0x1,rdata[i]&0x1,rdata[i] );
      fwrite(&rdata[i],1,1,out);
    }


   /* The 2048th Location for this PROM is reserved  */
   /* for the  Init Byte                               */
   /* which is also the first location value               */
   rdata[2048]= rdata[0] ;
   fwrite(&rdata[2048],1,1,out);   /* Write Init Byte   */

   /* The 2049th the Location for this PROM is reserved  */
   /* for the  Control Byte                              */
   /* For this PROM the Control Word is 00 hex           */
   /* which makes the Output Enable asynchronous         */
   rdata[2049]= 0x00 ;
   fwrite(&rdata[2049],1,1,out);   /* Write Control Byte   */
   fclose(out) ;

   for(i=2048;i<=2049;i++)
   {
      printf("%4i: %1x  %1x  %1x  %1x  ",i,(rdata[i]>>7)&0x1, (rdata[i]>>6)&0x1,(rdata[i]>>5)&0x1,(rdata[i]>>4)&0x1 );
      printf("%1x  %1x  %1x  %1x   : %2x\n",(rdata[i]>>3)&0x1, (rdata[i]>>2)&0x1,(rdata[i]>>1)&0x1,rdata[i]&0x1,rdata[i] );
 /*   fwrite(&rdata[i],1,1,out);    */
   }

}     /* main end */


