/* ****************************************************************************
 *    File Name : 2732.D
 *    Pin num 	: 24 pin
 *    Size 	: 512K (FFF)	(A0..A11	D0..D7)
 *    Last Modify Date : 91. 3. 20
 *
 *    |-------------- Vpp:25.0   Vcc:5.0   Dur:50ms -------------------------|
 *    Device 	:  AMD		*2732	*9732
 *		   FUJITSU	*2732
 *	           MITSUBISHI	*2732
 *		   NEC		*2732
 *	 	   NS		*2732	27C32
 *		   OKI		*2732
 *		   UMC		*2732
 *		   SGS		*27C32
 *		   SIGNETICS    27C32	(DON'T KNOW SPEC)
 *		   TI		2732
 *		   TOSHIBA	2732	2732D
 *		   UMC		2732
 *    |-------------- Vpp:25.0   Vcc:5.0   Dur:10ms -------------------------|
 *		   NS		27C32H	27C32H-45
 *    |-------------- Vpp:21.0   Vcc:5.0   Dur:50ms  -------------------------|
 *		   AMD		*2732A	*9732A
 *		   ATMEL	*2732A
 *		   FUJITSU	*2732A	     *27C32A
 *		   HITACHI	*462732A      462732	462732P
 *		   INTEL	2732A	2732
 *		   MITSUBISHI	*2732A
 *		   NEC		*2732A
 *		   OKI		*2732A
 *		   SGS		2732A
 *		   TOSHIBA	*2732A
 *		   TI	  	2732A	27P32A
 *		   RICHO	27C32
 *    |-------------- Vpp:12.75   Vcc:6.25   Dur:100us  ---------------------|
 *		   AMD		2732B  (12.5, 5.0, 1ms)
 *		   NS		27C32B
 *		   MICROCHIP    27C32A
 *		   SIGNETICS    27PC32
 *		   TI		27C32  	27PC32A	27PC32 (13.0+-0.25, 6.5+-0.25)
 * ************************************************************************** */
declaration
   dataunit : byte;
   functions
      Program
      Read
      Verify
      Blank_Check
      Data_Compare
      Auto
   ;
   Manufacturers
      AMD		/* 0 */
      ATMEL		/* 1 */
      FUJITSU		/* 2 */
      HITACHI		/* 3 */
      INTEL		/* 4 */
      MITSUBISHI	/* 5 */
      MICROCHIP		/* 6 */
      NEC		/* 7 */
      NS		/* 8 */
      OKI		/* 9 */
      SGS		/* 10 */
      SIGNETICS		/* 11 */
      TI		/* 12 */
      TOSHIBA		/* 13 */
      UMC		/* 14 */
      RICHO		/* 15 */
   ;
   Devices
      D2732		
      D2732D
      D27C32
      D9732
      D27C32H	    	
      D27C32H_45
      D2732A		
      D9732A
      D27C32A
      D27P32A
      D462732
      D462732A
      D462732P
      D27C32B		
      D27PC32
      D27PC32A
      D2732B
   ;
   chips = A7, A6, A5, A4, A3, A2, A1, A0, D0, D1, D2, GND,
	   D3, D4, D5, D6, D7, CE, A10, OE:Vpp, A11, A9, A8, Vcc;
   $v2 = Vpp;
   $v3 = Vcc;
endd

int durstat [] = { 3 , 0, 3, 0, 0, 1, 3, 3, 3, 3, 3, 3, 3, 0, 0 };
int volstat [] = { 3 , 1, 3, 1, 1, 2, 3, 3, 3, 3, 3, 3, 3, 1, 1 };
int durtbl  [] = { 50000, 100};

voltage vpptbl [] = { 25.0, 21.0, 12.75 };
voltage vcctbl [] = {  5.0,  5.0,  6.25 };
/* -----------------------------------------------------------------------
 * 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..11], @W;       /*   Initial Direction      */
   @Out CE       , @W;       /*         Of               */
   @Out OE       , @W;       /* ------------------------ */
   @Out Vcc	 , @W;
   @Out __GANG__[0..3], @W;

   @Out Vcc	 , @H;   
   @Out CE       , @H;       /* Chip enable  is High */
   @Out OE       , @H;       /* Out  enable  is High */
   @Out __GANG__[0..3], @H;

   @Out Vcc	 , @O;       /* Vcc Enable */
   @Delay 100	 , @us;
}
/* -----------------------------------------------------------------------
 * InitProcess () ---> Initial Buffer | Voltage source
 * ----------------------------------------------------------------------- */
int InitProcess ()
{
int	stat, dur;

   if (FUNCTION != Program) init ( 5.0 , 5.0 );
   else
   {
      stat = volstat [MFG];
      if (stat != 3) 
      {
         init (vpptbl [stat], vcctbl [stat]);
         stat = durstat [MFG];
         dur = durtbl [stat];
      }
      else
      {
         if (MFG == AMD)
         {
            if (DEVICE == D2732 || DEVICE == D9732) 
            {
               init (25.0 , 5.0);
               dur = 50000;
            }
            else if (DEVICE == D2732A || DEVICE == D9732A)
            {
               init ( 21.0, 5.0 );
               dur = 50000;
            }
            else if (DEVICE == D2732B)
            {
               init (12.5, 5.0);
               dur = 1000;
            }
	 }	/*  END IF : MFG == AMD */            
         else if (MFG == FUJITSU)
         {
            if (DEVICE == D2732) 
            {
               init ( 25.0, 5.0 );
               dur = 50000;
            }
            else if (DEVICE == D2732A || DEVICE == D27C32A)
            {
               init (12.5 , 6.0 );
               dur = 1000;
            }
         }		/*  end if : (MFG == FUJITSU) */
         else if (MFG == MITSUBISHI)
         {
             if (DEVICE == D2732)
             {
             	init (25.0 , 5.0);
             	dur = 50000;
             }
             else if (DEVICE == D2732A)
             {
             	init (21.0 , 5.0 );
		dur = 50000;             	
             } 
         }		/*  end if : (MFG == MITSUBISHI) */
         else if (MFG == NS)
         {
             if (DEVICE == D27C32B)
             {
		init ( 12.75 , 6.25 );      
		dur = 100;		
             }
             else if (DEVICE == D2732 || DEVICE == D27C32)
             {
		init (25.0 , 5.0 );
		dur = 50000;             	             	
             }
             else 
             {
             	init ( 25.0, 5.0 );
             	dur = 10000;
             }
         }   		/*  end if : (MFG == NS)  */
         else if (MFG == OKI)
         {
             dur = 50000;
             
	     if (DEVICE == D2732)     	init ( 25.0, 5.0 );
	     else 		     	init ( 21.0, 5.0 );
         }   		/*  end if : (MFG == OKI)  */
         else if (MFG == SGS)
         {
             dur = 50000;
             if (DEVICE == D27C32)   	 init (25.0 , 5.0);
      	     else 	 	     	 init ( 21.0 , 5.0 );
         }		/*  end if : (MFG == SGS)	*/
         else if (MFG == SIGNETICS)
         {
             if (DEVICE == D27C32)
             {
             	init (25.0, 5.0);
             	dur = 50000;
             }
             else 
             {
             	init (12.75, 6.25);
             	dur = 100;
             }
         }		/* end if : (MFG == SIGNETICS)  */
         else if (MFG == TI)
         {
             if (DEVICE == D2732A || DEVICE == D27P32A)
             {
		init (21.0 , 5.0 );
		dur = 50000;
             }
             else 
             {
             	init ( 12.75, 6.25 );
             	dur = 100;
             }
         }		/*  end if : MFG == TI  	*/
         else if (MFG == TOSHIBA)
         {
             dur = 50000;
             if (DEVICE == D2732A)	init (21.0 , 5.0 );
             else 	             	init (25.0, 5.0 );
         }		/*  end if   : MFG == TOSHIBA 	*/
         else if (MFG == NEC)
         {
             dur = 50000;
             if (DEVICE == D2732)      init ( 25.0, 5.0 );
             else 	               init ( 21.0, 5.0 );
         }		/*  end if : MFG == NEC  */
      }			/*  end else : stat == 3 */         
   }			/*  end if : (FUNCTION != Program) */
   return (dur);
}
/* -----------------------------------------------------------------------
 * read () ---> read processing
 * ----------------------------------------------------------------------- */
int read (row)
int	row;
{
int  hwhb, hwlb, lwhb, lwlb;	/* eaddr : end address */
int  tmp, tmp2, smask, emask, slh, elh;

   lwlb = lwhb = hwhb = hwlb = 0;
   smask = emask = slh = 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...";
   
   for ( hwhb= S_HWHB ; hwhb<=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..11], lwhb;   /* Output High word address */
	    
	    if (lwhb == S_LWHB) smask = S_LWLB;
            else                smask = 0;
            if (lwhb == E_LWHB) emask = E_LWLB;
            else                emask = 0xFF;

	    for ( lwlb=smask ; lwlb<=emask; lwlb++)
	    {
	       @Out A [0..7], lwlb;    /* Low  byte address output */
	       
	       if (GANG == 0)	@Out CE      	   , @L;
	       else		@Out __GANG__[GANG], @L;
	       
	       @Out OE      , @L;
	       @In  D [0..7], tmp;
	       
	       if (GANG == 0)	@Out CE	    	   , @H;
	       else		@Out __GANG__[GANG], @H;
	       
	       @Out OE      , @H;
	       if      (FUNCTION == Read)          @Load tmp;
	       else if (FUNCTION == Data_Compare) 
	       {
	       	    @Store   tmp2;
	       	    @Compare tmp, tmp2;
	       }
	       else if (FUNCTION == Blank_Check)
	       {
		    if (tmp != 0xFF)	Failf(hwhb, hwlb, lwhb, lwlb, tmp);
	       }
	       else if (FUNCTION == Verify)
	       {
		   @Store tmp2;
		   if (tmp != tmp2)  Failf (hwhb, hwlb, lwhb, lwlb, tmp);
	       }
	       if (FUNCTION != Blank_Check)	@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, Dur)
int	row, kind, Dur;
{
int  hwhb, hwlb, lwhb, lwlb;
int  tmp , tmp2, smask, emask, slh, elh;
int  failcnt;

   failcnt = tmp = tmp2 = 0;
   lwlb = lwhb = hwhb = hwlb = smask = emask = slh = elh = 0;
   
   @Set B_HADDR, B_LADDR;
   @Inc	 __B_START__;
   @Display 26, row, "Programming...";

   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..11], lwhb;   /* Output High word address */
	       
       	       if (lwhb == S_LWHB) smask = S_LWLB;
               else                smask = 0;
               if (lwhb == E_LWHB) emask = E_LWLB;
               else                emask = 0xFF;	       

	       for ( lwlb=smask; lwlb<=emask; lwlb++)
	       {
		  @Store tmp;
		  
		  if (tmp == 0xFF)	
		  {
		      @Inc  SPLIT;
		      continue;
		  }
		  
		  @Out Vpp      , @L;
	          @Out A [0..7], lwlb;
		  @Out D [0..7], tmp;
		  @Out Vpp     , @O;
		  
		  if (GANG == 0)	@Out CE		   , @L;
		  else		  	@Out __GANG__[GANG], @L;
		  @Delay Dur   , @us;
		  if (GANG == 0)	@Out CE		   , @H;
		  else		  	@Out __GANG__[GANG], @H;
		  
		  @Out Vpp     , @F;
		  @Out Vpp     , @L;

		  /* -------- Verify ----------- */
                  @Out D [0..7], @Z;
                  
		  if (GANG == 0)	@Out CE		   , @L;
		  else		  	@Out __GANG__[GANG], @L;
		  
		  @In  D [0..7], tmp2;
		  if (GANG == 0)	@Out CE		   , @H;
		  else		  	@Out __GANG__[GANG], @H;
		  
		  @Out D [0..7], @W;
		  
		  if (tmp != tmp2)
                  {
		     if (failcnt > 25) 
		       	Failf (hwhb, hwlb, lwhb, lwlb, tmp2);
		     else
		     {
			++ failcnt;
			lwlb--;
		     }
                  }
		  else
		  {
                      failcnt = 0;
		      @Inc SPLIT;
		  }
               }
	   }
       }
   }
   @Display 26, row, "Program OK!   ";
   if (kind == 0)	row = 7;
   else			row = 8;
   FUNCTION = Verify;
   @Vout 2, 5.0;
   @Vout 3, 5.0;
   read (row);		/*  Verify  */   
}
/* -----------------------------------------------------------------------
 * main () ---> Control main
 * ----------------------------------------------------------------------- */
int main ()
{
int	dur;

   dur = InitProcess();

   if ( FUNCTION == Auto )
   {
	FUNCTION = Blank_Check;
	read (6);
	
	FUNCTION = Program;
	dur = InitProcess ();
	program (7,1, dur);
   }
   else if ( FUNCTION == Program ) program(6, 0, dur);
   else if ( FUNCTION == Read )
   {
   	if 	(GANG >= 1 && __B_START__ == 0);
   	else if (GANG == 3 && __B_START__ == 1);
   	else read (6);
   }    
   else 			   read (6);
}
/* ---------------------------- End of program --------------------------- */  