/* ****************************************************************************
 *    File Name : 2804.D  (EEPROM : Electrical Eraser PROM)
 *    Pin num	: 24-pin  (A0..A8	D0..D7)
 *    Memory Size :  2K(1FF)
 *    Last Modify Date : 91. 3. 15
 *
 *----------------------------------------------------------------------------- 
 *    Vcc=5.0  			Dur=100us
 *-----------------------------------------------------------------------------
 *    Device :    ATMEL		28C04E/F
 *		  MICROCHIP	28C04AF
 *----------------------------------------------------------------------------- 
 *    Vcc=5.0  			Dur=500us 	
 *----------------------------------------------------------------------------- 
 *    Device :	  ATMEL	 	*28HC04		28C04  
 *		 *GI		28C04A	
 *		  MICROCHIP	2804		28C04A	
 *		  SEEQ		2804A(max=10ms)	2804
 *----------------------------------------------------------------------------- 
 *    Vcc    : 5.0  		Dur:10ms(min) 
 *----------------------------------------------------------------------------- 
 *		  XICOR		2804A		2804AMB
 *		  EXEL		*28C04		2804A(min=10ms)
 * ************************************************************************** */
declaration
   dataunit : byte;
   functions
      Program
      Read
      Verify
      Blank_Check
      Data_Compare
   ;
   Manufacturers
      ATMEL		/* 0 */
      EXEL		/* 1 */
      GI                /* 2 */
      MICROCHIP         /* 3 */
      SEEQ              /* 4 */
      XICOR             /* 5 */
   ;
   Devices
      D28C04E		
      D28C04F
      D28C04AF
      D28HC04           
      D28C04 
      D28C04A           
      D2804A      
      D2804AMB
      D2804
   ;
   chips = A7, A6, A5, A4, A3, A2, A1, A0, D0, D1, D2, GND,
	   D3, D4, D5, D6, D7, CE, NC1, OE, WE, NC2, A8, Vcc;
   $v2 = OE;	   
   $v3 = Vcc;
endd

int durstat [] = { 2 , 1, 0, 2, 0, 1};
int durtbl  [] = { 500, 10000};

/* -----------------------------------------------------------------------
 * 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 (vol3)
voltage vol3;
{
   @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..8] , @W;       /*   Initial Direction      */
   @Out CE       , @W;       /*         Of               */
   @Out WE	 , @W;	     /*        Port		 */
   @Out OE       , @W;       /* ------------------------ */
   @Out Vcc	 , @W;
   @Out __GANG__[0..3] , @W;

   @Out CE	  , @H;
   @Out	OE	  , @H;
   @Out WE	  , @H;
   @Out Vcc	  , @L;
   @Out __GANG__[0..3], @H;
   
   @Out Vcc      , @O;       /* Vcc Enable */
   @Delay 100	 , @us;
}
/* -----------------------------------------------------------------------
 * InitProcess () ---> Initial Buffer | Voltage source
 * ----------------------------------------------------------------------- */
int InitProcess ()
{
int	stat, dur;

   init ( 5.0 );
   
   if (FUNCTION == Program)
   {
      stat = durstat [MFG];
      if (stat != 2)     dur = durtbl [stat];
      else
      {
         if (MFG == ATMEL)
         {
            if (DEVICE == D28C04E || DEVICE == D28C04F)     dur = 100;
            else 			                    dur = 500;
         }		/* end if: (MFG == ATMEL)  */
         else if (MFG == MICROCHIP)
         {
            if (DEVICE == D28C04AF)               dur = 100;
            else 		                  dur = 500;
         }		/*  end if : (MFG == MICROCHIP) */
      }         	/*  end if : (stat != 2)	*/
   }			/*  END IF : (FUNCTION == Program)	*/         
   
   return (dur);
}
/* ---------------------------------------------------------------------
 * read () ---> read processing
 * --------------------------------------------------------------------- */
int read (row)
int	row;
{
int  hwhb, hwlb, lwhb, lwlb;
int  sll , ell, slh, elh; 
int  tmp, tmp2;

   sll = ell = slh = elh = tmp = tmp2=0;
   hwhb = hwlb = lwlb = lwhb = 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=0 /* S_HWHB */ ; hwhb<=0 /* E_HWHB */; hwhb++ )
   {
      @Display 64, 9, hwhb;
      for ( hwlb=0; hwlb<=0; 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..8], 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 */
	       
	       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 == Blank_Check)
	       {
		  if (tmp != 0xFF /* Blank Char  */)
		     Failf (hwhb, hwlb, lwhb, lwlb, 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;
	       }
	       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 (Dur)
int	Dur;
{
int  hwhb, hwlb, lwhb, lwlb;
int  tmp, tmp2, failcnt;
int  sll, ell, slh, elh;

   @Set  B_HADDR, B_LADDR;
   @Inc  __B_START__;
   
   sll = ell = slh = elh = 0;
   lwlb = lwhb = hwhb = hwlb = 0;
   tmp = tmp2 = failcnt = 0;

   @Display 26, 6, "Programming...";   
   /* -------- Initial Process to Program device   --------- */
   @Out OE        , @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..8], 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 OE      , @H;
                  if (GANG == 0)        @Out CE            , @L;
                  else                  @Out __GANG__[GANG], @L;
                  @Out WE      , @L;
                  @Out D [0..7], tmp;                             
                  
                  if (GANG == 0)        @Out CE            , @H;
                  else                  @Out __GANG__[GANG], @H;
                  @Out WE      , @H;
                  @Out OE      , @L;
                  
                  @Delay  Dur  , @us;   /*  write cycle time delay */
                  
                  /* -------- Verify --------- */
                  @Out D [0..7], @Z;
                  if (GANG == 0)        @Out CE            , @L;
                  else                  @Out __GANG__[GANG], @L;
                  @Out OE      , @L;
                  @In  D [0..7], tmp2;
                  @Out OE      , @H;
                  
                  if (tmp != tmp2)      
                  {
                  	Failf (hwhb, hwlb, lwhb, lwlb, tmp2);       
                  }
                  
                  if (GANG == 0)        @Out CE            , @H;
                  else                  @Out __GANG__[GANG], @H;
                  
                  @Out D [0..7], @W;
                  
                  @Inc SPLIT;             
               }
           }
       }
   }
   @Display 26, 6, "Program OK!   ";
   FUNCTION = Verify;
   read (7);		  
}
/* -----------------------------------------------------------------------
 * main () ---> Control main
 * ----------------------------------------------------------------------- */
int main ()
{
int	dur;

   dur = InitProcess();
   
   if      ( FUNCTION == Program ) program(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 --------------------------- */ 