/******************************************************************
   FUJITSU         : MB 7121E, MB 7121H, MB 7121L
	             MB 7122E, MB 7122H, MB 7122L, MB 7122Y
   Fuse enable pin : CE1_
   Vccp            =  7.0 V
   Data out volt   = 20.0 V
   Enable volt     = 20.0 V
   Delay time      = 11   Us
   Date		   : 11. 28. 1990
   Last Modify date:  2. 26. 1991
   Author	   : E. Y. Lee
*****************************************************************/
declaration
  dataunit : byte;
  functions
    Program
    Read
    Verify
    Blank_check
    Data_compare
    Auto
  ;
  Manufacturers
    FUJITSU
  ;
  Devices
    D7121
    D7122
  ;
  chips = A6, A5, A4, A3, A0, A1, A2, CE2_, GND,
	  CE1_, Q3, Q2, Q1, Q0, A9, A8, A7, Vcc
  ;
  $v2 = Q, CE1_;
  $v3 = Vcc;
endd
/* ----------------------------------------------------------------------  */
int  init( vol1, vol2, vol3 )
voltage vol1, vol2, vol3;
{
  @Vout  1, vol1;
  @Vout  2, vol2;
  @Vout  3, vol3;
  @Out  GND,  @G;
  @Delay 10,  @Ms;

  @Out  Q[0..3], @Z;
  @Out  A[0..9], @W;
  @Out  CE1_,    @W;
  @Out  CE2_,    @W;
  @Out  Vcc,	 @W;
  
  @Out  A[0..9], @L;
  @Out  CE1_,	 @L;
  @Out  CE2_,	 @L;
  @Out  Vcc,	 @L;
  @Out  Vcc,     @O;
  @Delay 100,	 @Us;
}
/* ---------------------------------------------------------------------- */
int  Failf ( hwhb, hwlb, lwhb, lwlb, data )
int  hwhb, hwlb, lwhb, lwlb, data;
{
  int  high, low;

  high = ( hwhb << 8) | hwlb;
  low  = ( lwhb << 8) | lwlb;
  @Fail  high, low, data;
}
/* ---------------------------------------------------------------------- */
int  read(option)
int  option;
{
  int  i, j;
  int  tmp, tmp2;
  int  saddr, eaddr;

  @Set  B_HADDR,  B_LADDR;
  init(5.0, 5.0, 5.0);
  tmp = tmp2 = 0;

  for(i=S_LWHB;i<=E_LWHB;i++)
  {
    @Display 68, 9, i;
    @Out   A[8..9], i;
    if( i == S_LWHB )  saddr = S_LWLB;
    else		saddr = 0;
    if( i == E_LWHB )  eaddr = E_LWLB;
    else		eaddr = 0xff;    
    for(j=saddr; j<=eaddr;j++)
    {
       @Out  A[0..7], j;
       @Out  CE1_, @L;
       @Out  CE2_, @L;

       @In   Q[0..3], tmp;
       tmp = tmp & 0x0f;
       if( option == 1 )      @Load tmp;
       else if(option == 4 )  {
         @Store  tmp2;
         tmp2 = tmp2 & 0x0f;
         @Compare tmp, tmp2;
       }
       else if(option == 3 )
       {
	   if( tmp != 0)      Failf(0, 0, i, j, tmp );
       }
       else if(option == 2 )
       {
	   @Store  tmp2;
	   if( tmp != (tmp2 & 0x0f) )     Failf( 0, 0, i, j, tmp );
       }
       @Inc  SPLIT;
    }
  }
}
/* ---------------------------------------------------------------------- */
int  program()
{
  int   failcnt, i, j, k, m;
  int   tmp, tmp2, tmp3;
  int   saddr, eaddr;

  failcnt = 0;
  tmp = tmp2 = tmp3 = 0;
  @Set  B_HADDR,  B_LADDR;
  init( 20.0, 20.0, 7.0 );
  for(i=S_LWHB;i<=E_LWHB;i++)
  {
       @Display  68, 9, i;
       @Out    A[8..9], i;
       if( i == S_LWHB )  saddr = S_LWLB;
       else		saddr = 0;
       if( i == E_LWHB )  eaddr = E_LWLB;
       else		eaddr = 0xff;       
       for(j=saddr;j<=eaddr;j++)
       {
	  @Out  A[0..7], j;
	  @Out  CE1_,  @L;
	  @Out  CE2_,  @L;
	  @Store  tmp;
	  tmp = tmp & 0x0f;

	  for(k=0; k<4;k++)
	  {
	     if( (tmp & (1 << k) ) > 0)
	     {
		@Out  CE1_,      @L;
		@Out  CE2_,      @L;
		@In   Q[0..3], tmp2;
		tmp2 = tmp2 & 0x0f;
		if( (tmp2 & (1 << k) ) == 0)
		{
		  for(m=0;m<1;m++)
		  {
		     @Out  Q[0..3], @W;
		     @Out  Q[0..3], @L;
		     @Out    CE1_,  @O;
		     @Delay     4,  @Us;
		     @Out  Q[k],    @O;
		     @Delay  10, @Us;
		     @Out  Q[k],    @F;
		     @Delay     2,  @Us;
		     @Out    CE1_,  @F;
		     @Out    CE1_,  @L;

		     @Delay  10,    @Us;
		     @Out  Q[0..3], @Z; 	
		     @In   Q[0..3], tmp3;
		     tmp3 = tmp3 & 0x0f;
		     tmp3 = tmp3 & (1 << k) ;
		     if( tmp3 == 0 )
		     {
			m -= 1;
			failcnt++;
			if( failcnt >= LOP )     Failf ( 0, 0, i, j, tmp3 );
		     }
		     else     failcnt = 0;
		     @Delay  2,  @Us;
		  } /* for m */
	       }  /*  if  */
	     }  /*  if  */
	  } /* for k */
	  @Inc  SPLIT;
       }  /* for j */
    }  /* for i */
}  
/* --------------------------------------------------------------------- */ 
int  main()
{
  if( FUNCTION == Read)	     {
  	@Display 26, 6, "Reading...";
  	read(1);
  	@Display 26, 6, "Read OK!  ";
  	@CheckSumOn;
  }
  else if( FUNCTION == Program)	 {
  	@Display 26, 6, "Programming...";
  	program();
  	@Display 26, 6, "Program OK!   ";
  	@Display 26, 7, "Verifying...";
  	read(2);
  	@Display 26, 7, "Verification OK!";
  }  	
  else if( FUNCTION == Verify)	 {
  	@Display 26, 6, "Verifying...";
  	read(2);
  	@Display 26, 6, "Verification OK!";
  }
  else if( FUNCTION == Blank_check)  {
  	@Display 26, 6, "Blank checking...";
  	read(3);
  	@Display 26, 6, "Blank check OK!  ";
  }
  else if( FUNCTION == Data_compare)  {
  	@Display 26, 6, "Data comparing...";
  	read(4);
  	@Display 26, 6, "Data comparison OK!";
  }
  else if( FUNCTION == Auto)     {
	@Display 26, 6, "Blank checking...";  
  	read(3);
  	@Display 26, 6, "Blank check OK!  ";
  	@Display 26, 7, "Programming...";
  	program();
  	@Display 26, 7, "Program OK!   ";
  	@Display 26, 8, "Verifying...";
  	read(2);
  	@Display 26, 8, "Verification OK!";
  }
} 
/* --------------------------------------------------------------------- */  