/* --< 5 >---------------------------------------------------------P. S. O-
 *  FIRST UPDATE : dEC. 12. 1990
 *  LIB Ver      : 1.00
 *  Fuse_su      : 256   [32 * 8]
 *---------------------------------------------------------------------- */

declaration
  dataunit : bit 
  ;
  functions
    Program
    Read
    Verify
    Blank_Check
    Auto
  ;
  Manufacturers
    INTEL
  ;
  Devices
    d85C508
  ;
		/*  ΥΝΝΝΝΈΥΝΝΝΝΈ        */
  chips = VPP,  /* [³ 1     28 ³] VCC   */
           A2,  /* [³ 2     27 ³] A1    */
	   A3,  /* [³ 3     26 ³] A0    */
	   A4,  /* [³ 4     25 ³] Z7    */
	  VER,  /* [³ 5     24 ³] Z6    */
           G0,  /* [³ 6     23 ³] Z5    */
	  CLK,  /* [³ 7     22 ³] Z4    */
	DATIN,  /* [³ 8     21 ³] Z3    */
	  MUX,  /* [³ 9     20 ³] Z2    */
	   G1,  /* [³ 10    19 ³] Z1    */
	   G2,  /* [³ 11    18 ³] Z0    */
           G3,  /* [³ 12    17 ³] DATOUT*/
      	   G4,  /* [³ 13    16 ³] G5    */
	  GND,  /* [³ 14    15 ³] PGM   */
		/*  ΤΝΝΝΝΝΝΝΝΝΝΎ        */
	PGM, G5, DATOUT, Z0, Z1, Z2, Z3, Z4, Z5, Z6, Z7, A0, A1, VCC;
  $v2 = VPP;
  $v3 = VCC;
endd

int addr[32] = {0x1f, 0x1e, 0x1d, 0x1c, 0x17, 0x16, 0x15, 0x14, 0x0f, 0x0e,
                0x0d, 0x0c, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
                0x0b, 0x0a, 0x09, 0x08, 0x13, 0x12, 0x11, 0x10, 0x1b, 0x1a, 
                0x19, 0x18};

int index, disp_flag;
/* -----------------------------------------------------------------------
 * 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 == 'e') @Display 26, index, "Eraseing ...";
  }
  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!";
    else if(option == 'e') @Display 26, index, "Erase OK!   ";
    index ++;
  }
}
/* -----------------------------------------------------------------------
 * init() ---> inital processing
 * ----------------------------------------------------------------------- */
int init()
{
  @Vout 1,       5.00;    /* NULL */
  @Vout 2,       5.00;
  @Vout 3,       6.00;
  @Out  GND,     @G;
  @Delay 10, @Ms;
            
  @Out VCC,      @W;
  @Out VPP,      @W;
  @Out A[0..4],  @W;
  @Out VER,      @W;
  @Out CLK,      @W;
  @Out DATIN,    @W;
  @Out MUX,      @W;
  @Out PGM,      @W;
  @Out DATOUT,   @Z;
  @Out VCC,      @W;
  @Out G[0..5],  @W;
  @Out Z[0..7],  @Z;
  
  @Out VCC,      @L;
  @Out VPP,      @L;
  @Out A[0..4],  @L;
  @Out VER,      @L;
  @Out CLK,      @L;
  @Out DATIN,    @L;
  @Out MUX,      @L;
  @Out PGM,      @L;
  @Out VCC,      @L;
  @Out G[0..5],  @L;
  
  @Out VCC, 	 @O;
  @Delay 1, @Ms;
  @Raising VPP, 5.00, 12.50, 100;
  @Delay 100, @Us;  
}
int exit()
{
  @Falling VPP, 12.50, 5.00, 100;
  @Out VPP, @F;
  @Delay 100, @Us;
  @Out VCC, @F;
}
int clock()
{
  @Out CLK, @H;
  @Out CLK, @L;
}
/* -----------------------------------------------------------------------
 * Read data from device
 * Blank check
 * Verify
 * ----------------------------------------------------------------------- */
int verifyf(data, option)
int data, option;
{
   int tmp;

   if     (option == 'r') @Load data;		/* Read data	   */
   else if(option == 'b') {			/* Blank check     */
     if(data & 1 != 1)    @Exit;
   }
   else if(option == 'v') {			/* Verify check    */
     @Store tmp;
     if((tmp&1) != (data&1)) @Exit;
   }
   else if(option == 's') {
     if(data & 1 != 1)    @Exit;
   }   
}
/* -----------------------------------------------------------------------
 * Top program
 * ----------------------------------------------------------------------- */
int top(option)
int option;
{
  int 	i;
  int   read_d;

  disp(option, 0);
  read_d = 0;

  init();

  @Out VER, @H;
  @Out VER, @L;
  for(i=0; i<256; i++) {
    @In DATOUT, read_d;
    clock();
    @Set 0, 255-i;
    @Display 67, 9, i, @d;
    verifyf(read_d&1, option); 
  }

  exit();

  disp(option, 1);
}
/* -----------------------------------------------------------------------
 * program
 * ----------------------------------------------------------------------- */
int program_device()
{
  int 	i, j, k;
  int   pgm_d, read_d, final;

  disp('p', 0);
  read_d = pgm_d = final = 0;

  init();
  
  for(i=0; i<32; i++) {
    for(j=0; j<1; j++) {			/* limit of program X=25 */
        for(k=0; k<8; k++) {
          @Set 0, (7-k) * 32 + i;
          @Store pgm_d;
          if(pgm_d == 1) @Out DATIN, @L;
          else           @Out DATIN, @H;
          clock();
        }
        @Out DATIN, @L;
        @Out A[0..4], addr[i];
        @PinDelay PGM, @H, @L, 1000;
        @Out A[0..4], @L;
      }
  }
  exit();
  disp('p', 1);
}
/* -----------------------------------------------------------------------
 * ID prcess
 * ----------------------------------------------------------------------- */
int check_ID()
{
  int 	i;
  int   read_d, tmp;

  read_d = tmp = 0;

  init();

  @Out VER, @H;
  @Out VER, @L;

  @Out MUX, @H;
  for(i=0; i<8; i++) {
    @In DATOUT, tmp;
    clock();
    read_d |= tmp << i;
  }
  @Out MUX, @L;

  exit();
  if(read_d == 0xfe)      @Display 26, 6, "A step device !";
  else if(read_d == 0xfd) @Display 26, 6, "B step device !";
  else  {
    @Display 26, 6, "Invalid ID !";
    @Exit;
  }
}
/* -----------------------------------------------------------------------
 * MAIN Porgram
 * ----------------------------------------------------------------------- */
int main()
{
  index = 8;
  disp_flag = 0;

  check_ID();

  if     (FUNCTION == Program) {
    program_device();				/* Program device     */
    top('v');					/* Verify 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      */
  }
}
/* ------------------------------- End of program ------------------------ */
  