/* ----------------------------------------------------------------P. S. O--
 *  FIRST UPDATE : 1. 23. 91  all ok !
 *  LIB Ver      : 1.00
 * -------------------------------------------------------------------------
 * Fuse_su		      : 2048     [32 * 64]
 * Internal Security Fuse     : 1
 * ------------------------------------------------------------------------- */
declaration
  dataunit : bit 
  ;
  functions
    Program  
    Read
    Verify
    Blank_Check
    Security
    Auto
  ;
  Manufacturers
    AMD
  ;
  Devices
    dAMPAL16L8
    dAMPAL16R4
    dAMPAL16R6
    dAMPAL16R8
    dAMPAL16L8_7
    dAMPAL16R4_7
    dAMPAL16R6_7
    dAMPAL16R8_7

  ;
                 /*  ΥΝΝΝΝΈΥΝΝΝΝΈ        */
  chips =PGM_V,  /* [³ 1     20 ³] VCC   */
            A0,  /* [³ 2     19 ³] D0    */
            A1,  /* [³ 3     18 ³] D1    */
            A2,  /* [³ 4     17 ³] D2    */
            A3,  /* [³ 5     16 ³] D3    */
            A4,  /* [³ 6     15 ³] D4    */
            A5,  /* [³ 7     14 ³] D5    */
            A6,  /* [³ 8     13 ³] D6    */
            A7,  /* [³ 9     12 ³] D7    */
            GND, /* [³ 10    11 ³] PGM   */
                 /*  ΤΝΝΝΝΝΝΝΝΝΝΎ        */
	   PGM, D7, D6, D5, D4, D3, D2, D1, D0, VCC;

  $v1 = PGM, PGM_V;
  $v2 = D, A3;
  $v3 = VCC;
endd

  int index, disp_flag;
  int caddr = 0;
/* -----------------------------------------------------------------------
 * Display processing
 * ----------------------------------------------------------------------- */
int disp(option, p)
int option, p;
{
  if(disp_flag == 1) return(p);
  if(p == 0) {
    if     (option == 'p') @Display 26, index, "Programming ...";    
    else if(option == 'r') @Display 26, index, "Reading ...";      
    else if(option == 'b') @Display 26, index, "Blank checking ...";
    else if(option == 'v') @Display 26, index, "Verifying ...   ";
    else if(option == 's') @Display 26, index, "Securing ...";
  }
  else {   
    if     (option == 'p') @Display 26, index, "Program OK!    ";
    else if(option == 'r') @Display 26, index, "Read OK!   ";
    else if(option == 'b') @Display 26, index, "Blank check OK!   ";
    else if(option == 'v') @Display 26, index, "Verification OK!";
    else if(option == 's') @Display 26, index, "Security OK!";
    index ++;
  }
}
/* -----------------------------------------------------------------------
 * init() ---> inital processing
 * ----------------------------------------------------------------------- */
int init(option)
int option;
{
  if(option == 's') {
    if(DEVICE > 3) @Vout 1, 15.00;
    else           @Vout 1, 18.00;
    @Vout 2,     11.00;  
  }
  else {
    @Vout 1,     11.00;
    if(DEVICE > 3) @Vout 2, 15.00;
    else           @Vout 2, 18.00;
  }
  @Vout 3,	5.20;
  @Out  GND, 	@G;
  @Delay 5, @Ms;

  @Out A[0..7], @W;
  @Out D[0..7], @Z;
  @Out PGM,	@W;
  @Out PGM_V,   @W;

  @Out A[0..7], @L;
  @Out PGM,     @L;
  @Out PGM_V,   @L;

  @Out VCC, 	@O;
  @Delay 1, @Ms;
}
/* -----------------------------------------------------------------------
 * Verify function
 * ----------------------------------------------------------------------- */
int verifyf(data, option)
int data, option;
{
  int tmp;

  if     (option == 'r') @Load data;		/* Read */
  else if(option == 'b') {			/* Blank check */
    if(data&1 != 0)      @Exit;
  }
  else if(option == 's') {			/* Blank check */
    if(data&1 != 1)      @Exit;
  }
  else if(option == 'v') {			/* Verify check */
    @Store tmp;
    if(tmp&1 != data&1)  @Exit;
  }
}
/* -----------------------------------------------------------------------
 * Read data from device
 * Blank check
 * Verify
 * ----------------------------------------------------------------------- */
int top(option)
int option;
{
  int 	i, j, k, addr,
  	pgm_d, read_d;

  disp(option, 0);
  init(option);

  pgm_d = read_d = 0;
  
  for(i=0; i<8; i++) {
   for(j=0; j<8; j++) {  
    for(k=0; k<32; k++) {
      addr = (i*8+j)*32+k;
      @Set 0, addr;
      @Store pgm_d;
      @Display 67, 9, addr, @d;       
      @Out A[0..2], j;				/* Product term addressing */
      @Out A[3..7], k;				/* Input line addressing */

      if(option == 'p' && pgm_d == 1) {
        @Out PGM_V,   @O;
        @Out D[i],    @O;
        @Out PGM,     @O;
        @Delay 20, @us;
        @Out D[i],    @F;        
        @Out PGM_V,   @F;
      }
      
      @Out PGM,     @O;			      	/* Vpp enable */
      @Out PGM_V,   @H;
      @In D[0..7], read_d;      
      @Out PGM_V,   @L;
      @Out PGM,     @F;
      
      read_d = ~(read_d >> i) & 1;      

      if(option == 'p') {if(read_d != pgm_d) @Exit;}
      else              verifyf(read_d, option);
    }   /* k */
   }    /* j */
  }     /* i */
  
  disp(option, 1);
}
/* -----------------------------------------------------------------------
 * Security device
 * ----------------------------------------------------------------------- */
int security_device()
{
  disp('s', 0);
  init('s');

  @Out A3, @O;
  @PinDelay PGM, @O, @F, 50;
  @Out A3, @F;

  disp_flag = 0;
  top('s');
  disp_flag = 1;
  disp('s', 1);
}
/* -----------------------------------------------------------------------
 * MAIN Porgram
 * ----------------------------------------------------------------------- */
int main()
{
  index = 6;
  disp_flag = 0;

  if(FUNCTION == Program) {
    top('p');					/* Program device */
    top('v');					/* Verify device */
    }
  else if(FUNCTION == Security) {
    security_device();                          /* Security device    */
    }
  else if(FUNCTION == Read) {           	/* Read device        */
    top('r');	
    @CheckSumOn;
  }
  else if(FUNCTION == Blank_Check) top('b'); 	/* Blank check device */
  else if(FUNCTION == Verify)      top('v'); 	/* Verify device      */
  else if(FUNCTION == Auto) {
    top('b');					/* Blank check        */
    top('p');					/* Program device     */
    top('v');					/* Verify device      */
    security_device();                          /* Security device    */
    }
}
/* ------------------------------- End of program ------------------------ */
