/* --< 6 >---------------------------------------------------------P. S. O--
 *  FIRST UPDATE : Oct. 10. 90
 *                   1. 18. 91
 *                   1. 21. 91 check securiy ! (others ok ! )
 *  LIB Ver      : 1.00
 * -------------------------------------------------------------------------
 * Fuse_su		      : 5828     [44 * 132 others 20]
 * Internal Security Fuse     : 1        [Verify --> blank_check OK]
 * --------------------------------------------------------------------------
 * SYMBOL |                              |    MIN   TYP   MAX  |  UNIT
 * -------+------------------------------+---------------------+-------------
 * Vhh    | Control Extra HIGH Level     |  10.00 11.00 12.00  |   V
 * Vop    | Program voltage pins 14-23   |  14.00 15.00 16.00  |   V
 * Vccp   | Programming supply voltage   |   5.00  5.20  6.50  |   V
 * T op/dt| Rate of output voltage change|     20     -   250  | V/us
 * T 13/dt| Rate of fusing enable voltage|    100     -  1000  | V/us
 *        | change (pin13 Rising Edge)   |                     |
 * Tp     | Fusing first attempt         |     10    50   100  |   us
 *        | Subsequent attempt(MAX 8)    |      4     5   10   |   ms
 *------------------------------------------------------------------------ */
declaration
  dataunit : bit 
  ;
  functions
    Program  
    Read
    Verify
    Blank_Check
    Security
    Auto
  ;
  Manufacturers
    AMD
  ;
  Devices
    dAmPAL22V10
  ;
                /*  ΥΝΝΝΝΈΥΝΝΝΝΈ       */
  chips =  P1,  /* [³ 1     24 ³] VCC  */
	  CA0,  /* [³ 2     23 ³] D0   */
	  CA1,  /* [³ 3     22 ³] D1   */
	  CA2,  /* [³ 4     21 ³] D2   */
	  CA3,  /* [³ 5     20 ³] D3   */
	  IA0,  /* [³ 6     19 ³] D4   */
	  IA1,  /* [³ 7     18 ³] D5   */
	  IA2,  /* [³ 8     17 ³] D6   */
	  IA3,  /* [³ 9     16 ³] D7   */
	  IA4,  /* [³ 10    15 ³] D8   */
	  IA5,  /* [³ 11    14 ³] D9   */
	  GND,  /* [³ 12    13 ³] P13  */
                /*  ΤΝΝΝΝΝΝΝΝΝΝΎ       */
	  P13, D9, D8, D7, D6, D5, D4, D3, D2, D1, D0, VCC;

  $v1 = CA3, P1, P13;
  $v2 = D;
  $v3 = VCC;
endd

  int tg[10] = { 8,10,12,14,16,16,14,12,10,8 };

  int index     = 6;
  int disp_flag = 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()
{
  @Vout 1,    11.00;
  @Vout 2,    15.00;
  @Vout 3,     5.20;
  @Out GND, 	@G;
  @Delay 10, @Ms;

  @Out CA[0..3], @W;
  @Out IA[0..5], @W;
  @Out D[0..9],  @Z;
  @Out P1,       @W;
  @Out P13,      @W;

  @Out CA[0..3], @L;
  @Out IA[0..5], @L;
  @Out P1,       @L;
  @Out P13,      @L;

  @Out  VCC, 	 @O;
  @Delay 100, @Us;
}
/* -----------------------------------------------------------------------
 * 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 == 'v') {			/* Verify check */
    @Store tmp;
    if(tmp&1 != data&1)  @Exit;
  }
}
/* -----------------------------------------------------------------------
 * 1bit data process
 * ----------------------------------------------------------------------- */
int sub(i, j, d_pin, oe_flag, option, addr)
int  i,        /* Input line addressing                    */
     j,        /* Column addressing                        */
     d_pin,    /* Data communication pin                   */
     oe_flag,  /* CA5 enable flag  1-->enable, 0-->disable */
     addr,     /* Current address                          */
     option;   /* Read, Program..  or more                 */
{
  int pgm_d, read_d, limit;

  pgm_d = read_d = 0;
  
  @Display 67, 9, addr, @d;

  for(limit=0; limit<125; limit++) {
    @Out CA[0..3], j;				/* Column addressing */
    @Out IA[0..5], i;				/* Input line addressing */
    if(oe_flag == 1)  @Out CA3, @O;
    @Set 0, addr;
    @Store pgm_d;
    						/* Program or Security */
    if(((option == 'p') || (option == 's')) && (pgm_d == 1)) {
      @Out P1, @O;                              /* Pin 1 Vihh */
      @Out D[d_pin], @O;			/* Selected data pin Vop */
      @Out P13,      @O;
      @Delay 40, @Us;				/* Delay 50us */
      @Out D[d_pin], @F;			/* Selected data pin Z */
      @Out P1, @F;				/* Pin 1 off */
    }
    @Out P13, @O;				/* Pin 13 Vihh */
    @Out P1,  @H;				/* Verify clock */
    @Out P1,  @L;
    @In D[0..9], read_d;			/* Read data */
    @Out P13, @F;
    @Out CA3, @F;
    read_d = (~read_d >> d_pin) & 1;

    if(option == 'p') {				/* Program */
      if(pgm_d&1 == read_d&1) break;		/* Program sucess */
      if(limit == 124) @Exit;
    }
    else {
      verifyf(read_d, option);
      break;
    }
  }   /* end for */
}
/* -----------------------------------------------------------------------
 * Top program
 * ----------------------------------------------------------------------- */
int top(option)
int option;
{
  int i, j, k;
  int addr;

  disp(option, 0);           
  addr = 0;					/* Addrss Pointer */
  init();

  for(k=0; k<44; k++) {				/* AR array */
    sub(k, 3, 1, 1, option, addr++);
  }
    
  for(i=0; i<10; i++) {                         /* Normal & OE array */
    for(j=0; j<=tg[i]; j++) {
      for(k=0; k<44; k++) {
	if(j == 0)  sub(k,   0, i, 1, option, addr++);
	else        sub(k, j-1, i, 0, option, addr++);
      }
    }
  }

  for(k=0; k<44; k++) {				/* SP array */
    sub(k, 3, 8, 1, option, addr++);
  }

  for(k=0; k<10; k++) {				/* P & R array */
    sub(63, 1, k, 1, option, addr);
    sub(63, 2, k, 1, option, addr+1);
    addr += 2;
  }
  disp(option, 1);               
}
/* -----------------------------------------------------------------------
 * Security device
 * ----------------------------------------------------------------------- */
int security_device()
{
  disp('s', 0);           
  init();

  sub(63, 5, 1, 1, 's', 5828);
  disp('s', 1);             
}
/* -----------------------------------------------------------------------
 * MAIN Porgram
 * ----------------------------------------------------------------------- */
int main()
{
  index = 6;

  if     (FUNCTION == Program) {
    top('p');					/* Program device */
    top('v');					/* Verify device */
  }
  else if(FUNCTION == Security) {
    top('s');					/* 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      */
    top('s');		                        /* Security device    */
  }
}
/* ------------------------------- end of program ------------------------ */
 