/*********************************************************************

   Device name     : DM54/74S475, DM54/74S474; DM54/74S475A,
                     DM54/74S474A; DM54/74S474B
                     SIGNETICS  82S140  82S141
   Fuse enable pin : E1_, E2_
   Vccp            : NS  10.5 V,  SIGNETICS  8.75 V
   Data out volt   : NS  10.5 V,  SIGNETICS  17.5 V
   Delay time      : 10 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
    NS
    SIGNETICS
  ;
  Devices
    D74S474
    D74S475
    D82S140
    D82S141
  ;
  chips = A7, A6, A5, A4, A3, A2, A1, A0, Q0, Q1, Q2, GND,
	  Q3, Q4, Q5, Q6, Q7, E4, E3, E2_, E1_, NC, A8, Vcc:Vccp
  ;
  $v1 = Vccp;
  $v2 = Q;
  $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..7], @W;
  @Out  A[0..8], @W;
  @Out  E4,	 @W;
  @Out  E3,	 @W;
  @Out  E2_,     @W;
  @Out  E1_,     @W;
  @Out  NC,	 @W;
  @Out  Vcc,	 @W;
  
  @Out  Q[0..7], @L;
  @Out  A[0..8], @L;
  @Out  E1_,	 @L;
  @Out  E2_,	 @L;
  @Out  E[3..4], @H;
  @Out  Vcc,	 @L;
  @Out  NC,      @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;
  tmp = tmp2 = 0;
  init(5.0, 5.0, 5.0);
  @Out  Q[0..7],  @Z;
  for(i=S_LWHB;i<=E_LWHB; i++)
  {
     @Display  68, 9, i;
     @Out  A8,   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  E3,  @H;
       @Out  E4,  @H;
       @Out  E1_, @L;
       @Out  E2_, @L;

       @In   Q[0..7], tmp;
       if( option == 1 )      @Load tmp;
       else if(option == 4)   {
           @Store tmp2;
           @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 )     Failf( 0, 0, i, j, tmp );
       }
       @Inc  SPLIT;
     }
  }
}
/* ---------------------------------------------------------------------- */
int  program()
{
  int   failcnt, i;
  int   j, k;
  int   tmp, tmp2;
  int   saddr, eaddr;

  failcnt = 0;
  tmp = tmp2 = 0;   
  @Set  B_HADDR, B_LADDR;
  if( MFG == NS )    init( 10.5, 10.5, 5.0 );
  else		     init( 8.75, 17.5, 5.0 );
  for( i=S_LWHB;i<=E_LWHB;i++)
  {
       @Display  68, 9, i;
       @Out    A8,      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  E3 ,  @H;
	  @Out  E4,   @H;
	  @Out  E1_,  @L;
	  @Out  E2_,  @L;
	  @Store  tmp;
	  for(k=0; k<8;k++)
	  {
	     if( (tmp & (1 << k) ) > 0)
	     {
		@Out   E2_,  @H;
		@Out   E1_,  @H;
		@Out   Vccp, @O;
		@Out   Vcc,  @F;
		@Out   Q[k], @O;
	   	@Out   E2_,  @L;
		@Out   E1_,  @L;
		@Delay 10,   @Us;
		@Out   E1_,  @H;
		@Out   E2_,  @H;
		@Out   Q[k], @F;
		@Out  Vcc,   @O;
		@Out  Vccp,  @F;
                @Out  Q[0..7], @L;
                @Out  E2_,   @L;
                @Out  E1_,   @L;
	     }
	  }
	  @Out  Q[0..7],  @Z;
	  @Out  E3,	  @H;
	  @Out  E4,       @H;
	  @Out  E2_,	  @L;
	  @Out  E1_, 	  @L;
	  @In   Q[0..7], tmp2;
	  @Out  Q[0..7],  @W;
	  if( tmp != tmp2 )
	  {
	      if( failcnt > LOP )	Failf( 0, 0, i, j, tmp2);
	      else		failcnt ++;
	      j -= 1;
	  }
	  else	  
          {    failcnt = 0;
	       @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!";
  }
} 
/* ---------------------------- End of program --------------------------- */




