 /* *********************************************************************
 *    File Name : CY7C251.D
 *    Last Modify Date : 91. 2. 27 
 *
 *    Device 	: CYPRESS 7C251/254 (Power Switched Reprogrammable PROM)
 *    Pin num	: 28-pin   (A0..A13	D0..D7)
 *    Size	: 128K (3FFF)
 *    Vpp	: 12.5		Vcc=5.0		Dur=200us	
 * ********************************************************************** */
declaration
   dataunit : byte;
   functions
      Program
      Read
      Verify
      Blank_Check
      Data_Compare
      Auto      
   ;
   Manufacturers
      CYPRESS
   ;
   Devices
     D7C251
     D7C254
   ;
   chips = A9, A8, A7, A6, A5, A4, A3, A2, A1, A0, D0, D1, D2, GND,
	   D3, D4, D5, D6, D7, CS4, CS3, CS2, CS1, A13, A12, A11, A10, Vcc;
   $v2 = CS2;
   $v3 = Vcc;
endd

/* -----------------------------------------------------------------------
 * Failf () ---> Fail address setting
 * ----------------------------------------------------------------------- */
int Failf (hwhb, hwlb, lwhb, lwlb, tmp)
int hwhb, hwlb, lwhb, lwlb, tmp;
{
int high, low;

   high = (hwhb << 8) | hwlb;
   low  = (lwhb << 8) | lwlb;

   @Fail high, low, tmp;
}
/* ----------------------------------------------------------------------
 * init () ---> Initial processing
 * ----------------------------------------------------------------------- */
int     init (vol2, vol3)
voltage vol2, vol3;
{
   @Vout 2	 , vol2;            /* Voltage Source #2 */
   @Vout 3	 , vol3;            /* Voltage Source #3 */
   @Out GND      , @G;       /* Ground output */
   @Delay 10	 , @ms;

   @Out D [0..7] , @W;   
   @Out A [0..7] , @W;       
   @Out A [8..13], @W; 
   @Out Vcc	 , @W;

   @Out CS1	 , @W;	/*  PGM	  */
   @Out CS2	 , @W;	/*  Vpp   */
   @Out CS3	 , @W;
   @Out CS4	 , @W;
   @Out Vcc	 , @L;
                    
   @Out Vcc      , @O;       /* Vcc Enable */
   @Delay 100	 , @us;
}
/* -----------------------------------------------------------------------
 * InitProcess () ---> Initial Buffer | Voltage source
 * ----------------------------------------------------------------------- */
int InitProcess ()
{
   if (FUNCTION != Program) init ( 5.0 , 5.0 );
   else                     init ( 12.5, 5.0 );
}
/* -----------------------------------------------------------------------
 * read () ---> read processing
 * ----------------------------------------------------------------------- */
int read (row)
int	row;
{
int  hwhb, hwlb, lwhb, lwlb;
int  sll, ell, slh, elh;
int  tmp, tmp2;

   lwlb = lwhb = hwhb = hwlb = 0;
   sll = ell = elh = elh = tmp= tmp2 = 0;
   
   @Set	B_HADDR, B_LADDR;   
   @Inc	__B_START__;
   
   @Out D [0..7], @Z;	/* Data Input  */
   
   if (FUNCTION == Read ) 
   {
      @Display 26, row, "Reading...";
      @CheckSumON;
   }
   else if (FUNCTION == Verify)		@Display 26, row, "Verifying...";
   else if (FUNCTION == Blank_Check)	@Display 26, row, "Blank Checking...";
   else 			        @Display 26, row, "Data Comparing...";
   
   @Out D [0..7], @Z;	/*  Data High Impedance */ 
   @Out CS3	, @H;
   @Out CS1	, @L;   
   @Out CS2	, @L;
   @Out CS4	, @L;   

   for ( hwhb=0 /* S_HWHB */ ; hwhb<=0 /* E_HWHB */; hwhb++ )
   {
      @Display 64, 9, hwhb;
      for ( hwlb=0 /* S_HWLB */; hwlb<=0 /* E_HWLB */; hwlb++ )
      {
         @Display 66, 9, hwlb;
         if (hwlb == S_HWLB) slh = S_LWHB;
         else		     slh = 0;
         if (hwlb == E_HWLB) elh = E_LWHB;
         else                elh = 0xFF;	 
         
         for ( lwhb=slh; lwhb <=elh; lwhb++)
         {
            @Display 68, 9, lwhb;
	    @Out A [8..13], lwhb;   /* Output High word address */
	    
	    if (lwhb == S_LWHB) sll = S_LWLB;
            else                sll = 0;
            if (lwhb == E_LWHB) ell = E_LWLB;
            else                ell = 0xFF;

	    for ( lwlb=sll; lwlb<=ell; lwlb++)
	    {
	       @Out A [0..7], lwlb;    /* Low  byte address output */
	       @In  D [0..7], tmp;
	       @Out CS3	, @L;
   	       @Out CS1	, @H;   
	       @Out CS2	, @H;
	       @Out CS4	, @H;	       
	       @Out Vcc	, @F;
	       
	       @Out CS3	, @H;
   	       @Out CS1	, @L;   
	       @Out CS2	, @L;
	       @Out CS4	, @L;
	       @Out Vcc	, @O;

	       if (FUNCTION == Read)		  @Load tmp;
	       else if (FUNCTION == Blank_Check)
	       {
		  if (tmp != 0x00) Failf (hwhb, hwlb, lwhb, lwlb, tmp);
	       }
	       else if (FUNCTION == Verify)
	       {
                  @Store tmp2;
                  if (tmp != tmp2) Failf (hwhb, hwlb, lwhb, lwlb, tmp);
               }
               if (FUNCTION == Data_Compare)
	       {
	          @Store tmp2;
	          @Compare tmp, tmp2;
	       }	       
	       @Inc SPLIT;
            }
	 }
      }
   }
   if 	   (FUNCTION == Read)		@Display 26, row, "Read OK!   ";
   else if (FUNCTION == Verify)		@Display 26, row, "Verification OK!";
   else if (FUNCTION == Blank_Check)	@Display 26, row, "Blank Check OK!  ";
   else 			        @Display 26, row, "Data Comparison OK!";
}
/* -----------------------------------------------------------------------
 * program () ---> Control main
 * ----------------------------------------------------------------------- */
int program (row, kind)
int	row, kind;
{
int  hwhb, hwlb, lwhb, lwlb;
int  sll, ell, slh, elh;
int  tmp , tmp2, failcnt;

   @Set	B_HADDR, B_LADDR;
   @Inc	__B_START__;
   
   slh = elh = sll = ell = failcnt = tmp = tmp2= 0;
   lwlb = lwhb = hwhb = hwlb = 0;

   @Out CS2	, @O;
   @Out CS1	, @H;
   @Out CS3	, @H;	  
   
   @Display 26, row, "Programming...";      
   
   for ( hwhb=0 /* S_HWHB */; hwhb<=0 /* E_HWHB */; hwhb++ )
   {
       @Display 64, 9, hwhb;
       for ( hwlb=0 /* S_HWLB */; hwlb<=0 /* E_HWLB */; hwlb++ )
       {
           if (hwlb == S_HWLB) slh = S_LWHB;
           else		     slh = 0;
           if (hwlb == E_HWLB) elh = E_LWHB;
           else                elh = 0xFF;	 
         
           for ( lwhb=slh; lwhb <=elh; lwhb++)
           {
               @Display 68, 9, lwhb;
	       @Out A [8..13], lwhb;   /* Output High word address */
	    
	       if (lwhb == S_LWHB) sll = S_LWLB;
               else                sll = 0;
               if (lwhb == E_LWHB) ell = E_LWLB;
               else                ell = 0xFF;

 	       for ( lwlb=sll; lwlb<=ell; lwlb++)
	       {
		  @Store tmp;
		  @Out A [0..7], lwlb;
		  @Out D [0..7], tmp;
		  @Out CS1     , @L;
		  @Delay 200   , @us;
		  @Out CS1     , @H;

	          @Out D [0..7], @Z;
		  @Out CS3     , @L;
		  @In  D [0..7], tmp2;
		  @Out CS3     , @H;
		  
		  @Out D [0..7], @W;
		  
		  if ( tmp != tmp2 )
                  {
		     if (failcnt > 10 ) Failf (hwhb, hwlb, lwhb, lwlb, tmp2);
		     else
		     {
			++ failcnt;
			lwlb--;
		     }
                  }
                  else
                  {
                     failcnt = 0;
                     @Inc SPLIT;
                  }
               }
            }
       }
   }
   @Out	CS2,	@F;
   @Display 26, row, "Program OK!   ";
   if (kind == 0)	row = 7;
   else			row = 8;
   FUNCTION = Verify;
   @Vout  2, 5.0;
   read (row);   
}

/* -----------------------------------------------------------------------
 * main () ---> Control main
 * ----------------------------------------------------------------------- */
int main ()
{
   InitProcess();

   if ( FUNCTION == Auto )
   {
	FUNCTION = Blank_Check;
	read (6);

	FUNCTION = Program;
	InitProcess ();
	program(7, 1);
   }
   else if ( FUNCTION == Program ) program(6, 0);
   else 			   read (6);
}
/* ---------------------------- End of program --------------------------- */ 