/* ****************************************************************************
 *    Manufacturer: DALLAS
 *
 *    Device Name : DS1235
 *    Pin num	  : 28    (A0..A14	D0..D7)
 *    Memory Size : 256K  (7FFF) 
 *
 *    Device Name : DS1230
 *    Pin num	  : 28    (A0..A13	D0..D7)
 *    Memory Size : 64K   (3FFF)
 *
 *    Device Name : DS1225
 *    Pin num	  : 28    (A0..A12	D0..D7)
 *    Memory Size : 64K   (1FFF)
 *
 *    Last Modify Date : 3. 8. 1991.
 * ************************************************************************* */
declaration
   dataunit : byte;
   functions
     Program
     Read
     Verify
   ;
   Manufacturers
     DALLAS
   ;
   Devices
     DDS1235
     DDS1230
     DDS1225
   ;
   chips = A14, 
           A12, 
           A7, 
           A6, 
           A5, 
           A4, 
           A3, 
           A2, 
           A1, 
           A0, 
           D0, 
           D1, 
           D2, 
           GND,
	   
	   D3, D4, D5, D6, D7, CE, A10, OE, A11, A9, A8, A13, WE, VCC;
	   
   $v3 = VCC;
endd

int disp_flag, index;
/* -----------------------------------------------------------------------
 * init () ---> Initial processing
 * ----------------------------------------------------------------------- */
int init()
{
   @Vout 3,        5.0; 
   
   @Out GND,	   @G;  
   @Delay 10, @ms;

   @Out D [0..7],  @Z;
   @Out A [0..14], @W;
   @Out CE,	   @W;
   @Out OE,	   @W;
   @Out WE,        @W;
   @Out VCC,	   @W;

   @Out A [0..14], @W;
   @Out CE,	   @H;
   @Out OE,	   @H;
   @Out WE,        @H;
   @Out VCC,	   @H;

   @Out VCC, @O;
   @Delay 100, @Ms;
}
/* -----------------------------------------------------------------------
 * 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 == 'v') @Display 26, index, "Verifying ...   ";
  }
  else {   
    if     (option == 'p') @Display 26, index, "Program OK!    ";
    else if(option == 'r') @Display 26, index, "Read OK!   ";
    else if(option == 'v') @Display 26, index, "Verification OK!";
    index ++;
  }
}
/* -----------------------------------------------------------------------
 * Failf () ---> Fail address setting
 * ----------------------------------------------------------------------- */
int Failf (hwhb, hwlb, lwhb, lwlb, tmp)
int hwhb, hwlb, lwhb, lwlb, tmp;
{
int high, low;

   high = (hwhb << 8) | hwlb;
   low  = (lwhb << 8) | lwlb;

   @Fail high, low, tmp;
}
/* -----------------------------------------------------------------------
 * Verify 
 * ----------------------------------------------------------------------- */
int verifyf(read_d, pgm_d, option)
int read_d, pgm_d, option;
{
  int tmp;
  
  if     (option == 'r') @Load read_d;		/* Read data	   */
  else if(option == 'v' || option == 'p') {	/* Verify check    */
    if(read_d != pgm_d)  return(1);
  }
  return(0);
}
/* ----------------------------------------------------------------------- 
 * top () ---> read, program, verify processing
 * ----------------------------------------------------------------------- */
int top (option)
int	option;
{
int  hwhb, hwlb, lwhb, lwlb;
int  slh, elh, smask, emask;
int  read_d, pgm_d;

init();
disp(option, 0);

   @Set	B_HADDR, B_LADDR;   
   @Inc	__B_START__;
   
   lwlb = lwhb = hwhb = hwlb = 0;
   slh = elh = smask = emask = 0;
   read_d = pgm_d = 0;
   
   for (hwhb=0; hwhb<=0 ; hwhb++)
   {
      @Display 64, 9, hwhb;
      for (hwlb=S_HWLB; hwlb<=E_HWLB; hwlb++)
      {
         @Display 66, 9, hwlb;
         if (hwlb == S_HWLB) slh = S_LWHB;
         else                slh = 0;
         if (hwlb == E_HWLB) elh = E_LWHB;
         else                elh = 0xFF;
         
	 for (lwhb=slh; lwhb<=elh; lwhb++)
         {
            @Display 68, 9, lwhb;
	    @Out A [8..14], lwhb;   		/* Output High word address */
	    
	    if (lwhb == S_LWHB) smask = S_LWLB;
            else                smask = 0;
            if (lwhb == E_LWHB) emask = E_LWLB;
            else                emask = 0xFF;

	    for (lwlb=smask; lwlb<=emask; lwlb++)
	    {
	       @Store pgm_d;
	       @Out A [0..7], lwlb;    		/* Low byte address output */
	       if(option == 'p') {
	         @Out D[0..7], @W;
	         @Out D[0..7], pgm_d;
	         @Out CE,      @L;
	         @Out WE,      @L;
	         @Out WE,      @H;
	         @Out CE,      @H;
	         @Out D[0..7], @Z;
	       }

               @Out CE,      @L;
	       @Out OE,      @L;
	       @In D[0..7], read_d;
	       @Out OE,      @H;
	       @Out CE,      @H;
	       if(verifyf(read_d, pgm_d, option) == 1) {
	         Failf(hwhb, hwlb, lwhb, lwlb, read_d);
	       }
	       @Inc SPLIT;
          }
        }
     }
  }
  disp(option, 1);
}
/* -----------------------------------------------------------------------
 * main () ---> Controll main
 * ----------------------------------------------------------------------- */
int main ()
{
  index = 6;

  if(FUNCTION == Program) {          		/* Program device */
    top('p');
    top('v');
  }
  else if(FUNCTION == Read) {           	/* Read device        */
    top('r');	
    @CheckSumOn;
  }
  else if(FUNCTION == Verify)      top('v'); 	/* Verify device      */
}
/* ---------------------------- End of program --------------------------- */