/* *****************************************************************************
 *    File Name : CY7C235.D	(Gang is NOT Supported !! )
 *    Last Modify Date : 91. 4. 2 
 *    Pin num	: 24-pin   (A0..A9	D0..D7) 
 *    Size	: 3FF
 *    LOP	: 4
 *
 *    Vpp	: 13.5		Vcc : 5.0	Dur : 100us
 *    Device 	: CYPRESS  7C235 (Registered PROM)
 * ************************************************************************** */
declaration
   dataunit : byte;
   functions
      Program
      Read
      Verify
/*      Blank_Check      */
      Data_Compare      
      Auto
   ;
   Manufacturers
      CYPRESS
   ;
   Devices
     D7C235
   ;
   chips = A7, 
           A6, 
           A5, 
           A4, 
           A3, 
           A2, 
           A1, 
           A0, 
           D0, 
           D1, 
           D2, 
          GND,
          
	   D3, D4, D5, D6, D7, CP:PGM, Es:VFY, INIT:Vpp, E, A9, A8, Vcc;
	   
   $v2 = Vpp;
   $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 1, vol2;
   @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..9] , @W;       /*                          */

   @Out PGM	 , @W;
   @Out VFY	 , @W;
   @Out CP	 , @W;
   @Out Es	 , @W;
   @Out INIT	 , @W;
   @Out E	 , @W;
   @Out Vcc 	 , @W;
   @Out Vpp	 , @W;
   
   @Out PGM	 , @H;
   @Out VFY	 , @H;
   @Out CP	 , @H;
   @Out Es	 , @H;
   @Out INIT	 , @H;
   @Out E	 , @H;
   @Out Vcc	 , @H;

   @Out Vcc      , @O;       /* Vcc Enable */
   @Delay 10	 , @Ms;
}
/* -----------------------------------------------------------------------
 * InitProcess () ---> Initial Buffer | Voltage source
 * ----------------------------------------------------------------------- */
int InitProcess ()
{
   if (FUNCTION != Program) init ( 5.0 , 5.0 );
   else                     init ( 13.5, 5.0 );
}
/* -----------------------------------------------------------------------
 * read () ---> read processing
 * ----------------------------------------------------------------------- */
int read (row)
int	row;
{
int  hwhb, hwlb, lwhb, lwlb;
int  sll, ell, slh , elh;
int  tmp, tmp2;

   @Set B_HADDR, B_LADDR;
   
   lwlb = lwhb = hwhb = hwlb = 0;
   sll = ell = slh = elh = tmp = tmp2 = 0;
   
   if (FUNCTION == Read ) 
   {
      @Display 26, row, "Reading...";
      @CheckSumON;
   }
   else if (FUNCTION == Verify)		@Display 26, row, "Verifying...";
   else 			        @Display 26, row, "Data Comparing...";   
   
   @Out D [0..7], @Z;				/* Data Input  */
   @Out Vpp, @H;	       
   @Out Es,  @L;
   @Out E,   @L;
   @Out CP,  @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..9], lwhb;   		/* Output High word address */
	    
	    if (hwlb == S_LWHB) sll = S_LWLB;
            else		sll = 0;
            if (hwlb == E_LWHB) ell = E_LWLB;
            else                ell = 0xFF;	 

	    for ( lwlb=sll; lwlb<=ell; lwlb++)
	    {
	    
	       @Out A [0..7], lwlb;    		/* Low  byte address output */
               @Out CP, @H;
               @Out CP, @L;
	       @In  D [0..7], tmp;

	       if (FUNCTION == Read)	  	@Load tmp;
	       else if (FUNCTION == Verify)
	       {
                  @Store tmp2;
                  if (tmp != tmp2) Failf (hwhb, hwlb, lwhb, lwlb, tmp);
               }
               else 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 			        @Display 26, row, "Data Comparison OK!";
}
/* -----------------------------------------------------------------------
 * blankchk () ---> read processing
 * ----------------------------------------------------------------------- */
int blankcheck ()
{
int  hwhb, hwlb, lwhb, lwlb;
int  sll, ell, slh , elh;
int  tmp, tmp2, i;

   @Set B_HADDR, B_LADDR;
   @Inc __B_START__;
   
   @Out D [0..7], @Z;	/* Data Input  */
   @Out PGM	, @O;
   
   lwlb = lwhb = hwhb = hwlb = BLKCHAR = 0;
   sll = ell = slh = elh = tmp =tmp2 = i = 0;
   
   @Display 26, 6, "Blank Checking...";
   
   for ( hwhb=0 /* S_HWHB */ ; hwhb<=0 /* E_HWHB */; hwhb++ )
   {
      @Display 64, 9, hwhb;
      for ( hwlb= S_HWLB; hwlb<=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..9], 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++)
	    {
	       for (i=0; i<=1; i++)
	       {
	       	    @Out A [0..7], lwlb;    /* Low  byte address output */
	       	    if (i == 0)	 
	       	    {
	       	    	BLKCHAR = 0xFF;
	       	    	@Out Es      , @L;
	       	    }
	            @Out INIT	 , @L;
	       	    @Out E       , @L;
	       	    @In  D [0..7], tmp;
	       
	       	    if (tmp != BLKCHAR && i == 1) 
	       	       Failf (lwlb, lwhb, hwlb, hwhb, tmp);
	       }
            }
	 }
      }
   }
   @Out PGM	, @F;
   @Display 26, 6, "Blank Check 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;

   lwlb = lwhb = hwhb = hwlb = 0;
   sll = ell = slh = elh = 0;
   failcnt = tmp = tmp2 = 0;
   
   @Set	B_HADDR, B_LADDR;
   @Inc __B_START__;
   
   @Display 26, row, "Programming...";      
   
   @Out Vpp	, @O;   
   
   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..9], 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 D [0..7], @W;
		  @Out A [0..7], lwlb;
		  @Out D [0..7], tmp;
		  @Out PGM     , @L;
		  @Delay 100   , @us;
		  @Out PGM     , @H;

		  /* --------- One Byte Verify  ---------- */
	          @Out D [0..7], @Z;
		  @Out VFY     , @L;
		  @Delay 10, @Us;
		  @In  D [0..7], tmp2;
		  @Out VFY     , @H;

		  
		  if ( tmp != tmp2 )
                  {
		     if (failcnt > 10 ) Failf (hwhb, hwlb, lwhb, lwlb, tmp2);
		     else
		     {
			++ failcnt;
			lwlb--;
		     }
                  }
                  else
                  {
  		    @Out D [0..7], @W;
		    @Out A [0..7], lwlb;
		    @Out D [0..7], tmp;
		    @Out PGM     , @L;
		    @Delay 2400*failcnt, @us;
		    @Out PGM     , @H;
                  
                    failcnt = 1;
                    @Inc SPLIT;
                  }
               }
            }
       }
   }
   @Out	Vpp,	@F;
   FUNCTION = Verify;
   InitProcess ();
   read (7);
}
/* -----------------------------------------------------------------------
 * main () ---> Control main
 * ----------------------------------------------------------------------- */
int main ()
{
   InitProcess();

   if ( FUNCTION == Auto )
   {
	blankcheck ();
	
	FUNCTION = Program;
	InitProcess ();
	program(7, 1);
   }
   else if ( FUNCTION == Program )    	program(6, 0);
/*   else	if ( FUNCTION == Blank_Check)	blankcheck ();*/
   else 			   	read (6);
}
/* ---------------------------- End of program --------------------------- */ 