[Description]

Location = "Motorola\AZ32";

Info = " Module for programming EEPROMs memory of MC68HC08AZ32.",
    " Manufacturer: Motorola.",
    " -----------------------------------------------------------",
    " EEPROM size: 512 bytes.",
    " ROM size:  bytes",
    " -----------------------------------------------------------",
    " Supported functions:",
    "    EEPROM read/write/verify",
    "    ROM read",
    " -----------------------------------------------------------",
    " NOTE: SPECIAL ADAPTER ARE REQUIRED! ",
    " ",
    " Author: VKProg (C) 2008";

Picture = "MC68HC08AZ32.bmp";
Buffer1 = "EEPROM",512;
Buffer2 = "FULL",65536;
Voltage = 5.0;
VCC = 0;
GND = 0;


[Menu]

Read        	= "&Read",ReadButton,"Read chip",F2;
Div0        	= "-";
Write       	= "&Write",WriteButton,"Write chip",F5;
WriteChanges	= "Write &changes",WriteChangesButton,"Write changes",F6;
Div1        	= "-";
Compare     	= "&Verify",CheckButton,"Compare with buffer",F3;
Div3        	= "-";
InitBoot    	= "&Init bootloader",ChipInfoButton,"Init bootloader",F9;
Div4        	= "-";
Info        	= "&About module",ModuleInfoButton,"Show module info",F1;
Div5        	= "-";

[Interface]

Digit(Crystal,"Crystal frequency (KHz)",10,6,16000);
CheckBox(PTC3,"PTC3 = 0?",250,5,1);
CheckBox(Reboot,"Unsecured?",350,5,1);

[Host]

dim BootSec[] = 
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x5E,
0xF9,0xF1,0xF9,0xF4,0xF9,0xF7,0xF9,0xFA,
0x00,0x00,0x00,0xB4,0x00,0x00,0x00,0xB4,
0xF9,0xF1,0xFF,0xF4,0xF9,0xF7,0xF9,0xFA,
0x28,0x28,0x28,0x28,0x28,0x28,0x28,0x28,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0x10,0x0C,0x10,0x09,0x10,0x06,0x10,0x03,
0x11,0x11,0x11,0x11,0x11,0x11,0x11,0x11,
0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,
0x46,0xFF,0x8E,0x00,0x49,0xE2,0x8E,0x00,
0x49,0x50,0x59,0xBB,0x0D,0x02,0x08,0x82,
0x86,0xC9,0x86,0xC9,0x86,0xC9,0x86,0xC9,
0xA7,0x05,0xA6,0x58,0xA6,0x58,0xA6,0x55,
0xAA,0x08,0xAA,0x08,0xA9,0x94,0xAA,0x08,
0xBC,0xB8,0xC8,0x06,0xC8,0x06,0xC8,0x06,
0xDA,0xA7,0xE5,0x27,0xE5,0x27,0xE5,0x27,
0xDA,0xAC,0xE5,0x2C,0xE5,0x2C,0xE5,0x2C, 
0xDC,0xA0,0xE7,0x20,0xE7,0x20,0xE7,0x20,
0xDC,0xA6,0xE7,0x26,0xE7,0x26,0xE7,0x26,
0xDF,0xC8,0xEA,0x48,0xEA,0x48,0xEA,0x48,
0xE5,0x78,0xEE,0xF9,0xEE,0xF9,0xEE,0xF9,
0xE5,0xDD,0xE4,0x0A,0x67,0x2B,0xE5,0xDD,
0xE5,0xE3,0xE4,0x10,0x67,0x2B,0xE5,0xE3,
0xF8,0xF2,0xF9,0xF3,0xF9,0xF5,0xF9,0xF7,
0xAB,0xFB,0xAB,0xFB,0xAB,0x87,0xAB,0xFB,
0x7F,0xC0,0x7F,0xC1,0x7F,0xC2,0x7F,0xC5,
0x6D,0x04,0x69,0x70,0x6E,0x07,0x69,0x70,
0x05,0xBF,0x05,0xC2,0x05,0xC5,0x05,0xC8,
0xE8,0x03,0xF1,0x9E,0xF1,0x9E,0xF1,0x9E,
0xDC,0xA0,0xDC,0x93,0xE1,0x24,0xDC,0xA0,
0x50,0x2A,0x50,0x2D,0x50,0x30,0x50,0x33,
0xFD,0xDD,0xFD,0xE0,0xFD,0xE3,0xFD,0xE6,
0x9F,0x9C,0xF6,0x98,0xF6,0x98,0xF6,0x98,
0x9F,0xA4,0xF6,0xE0,0xF6,0xE0,0xF6,0xE0,
0xBD,0x57,0xBD,0x57,0xBD,0x57,0xBD,0x54,
0xBE,0x07,0xBD,0x57,0xBD,0x57,0xBD,0x54,
0xE0,0xA6,0xB1,0x24,0xB1,0x24,0xE0,0xA6,
0xF1,0x6A,0xFA,0x04,0xE4,0xDA,0xFA,0x04,
0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,
0x76,0xB5,0x40,0x00,0x79,0xBB,0x40,0x00,
0xA1,0xCB,0x80,0x00,0xA4,0xEE,0x80,0x00
};

dim EEprom1[] = 
{
0x55,0x54,0x4E,0x56,0x58,0xF6,0x43,0x27,
0x21,0xA6,0x0C,0xC7,0xFE,0x1D,0xF7,0x5C,
0xA6,0x0D,0xC7,0xFE,0x1D,0xA6,0x0B,0xCD,
0x01,0x66,0x4A,0x26,0xFA,0xA6,0x0C,0xC7,
0xFE,0x1D,0xCD,0x01,0x66,0xA6,0x00,0xC7,
0xFE,0x1D,0x3A,0x58,0x27,0x03,0xCC,0x01,
0x05,0x55,0x54,0x4E,0x56,0x58,0xA6,0x04,
0xC7,0xFE,0x1D,0x5E,0x60,0xA6,0x05,0xC7,
0xFE,0x1D,0xA6,0x0B,0xCD,0x01,0x66,0x4A,
0x26,0xFA,0xA6,0x04,0xC7,0xFE,0x1D,0xCD,
0x01,0x66,0xA6,0x00,0xC7,0xFE,0x1D,0x3A,
0x58,0x27,0x0A,0xC6,0x01,0x3C,0x4C,0xC7,
0x01,0x3C,0xCC,0x01,0x36,0x83,0x35,0x5A,
0x45,0xFF,0xFF,0xAF,0x01,0x65,0x00,0x00,
0x26,0xF9,0x55,0x5A,0x81
};		//loader 117 byte 


//.................................................
define      SECQty    42		// Количество SEC строк



Proc InitBoot
{
var Value,Counter,CounterH,Ret,Address,Temp,Error,Num,Delay_co;
	Run(AVRreset);
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 1. "); return; }
	Run(CheckAdapter);
	Value = USB;
	if(Value != 0x04) { print("   Adapter not found 2. "); return; }
	print("   Adapter OK!");
////////////////////////////////
	Value = CalcBRR();
	Run(BaudStart);
	Temp = Value >> 8;USB = Temp;
	USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 3. "); return; }
///////////////////////////////
if(Reboot!=0)
{
	Error = 1; 		//SEC-activity
	Num = 0;
	while 
	{
	Run(PowerOFF);
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 4. "); return; }
//...........................................
Delay_co=0;
while(Delay_co!=5)
{
	Run(Delay_Mh);
	Value = USB;
	if(Value != 0x00) { print("   disconnect_delay "); return; }
	Delay_co++;
}
//.............................................
	Ret = BootSecur(Num);
	if(Ret=0x8000){break;}
	if(Ret!=0xAD){break;}
	Num++;
	if(Num=SECQty){break;}
	}
	if(Ret=0x8000){print("   Adapter not found 5. ");return;}
	if(Ret=0xAD){print("   SEC not open! ");return;}
	Counter=1025;CounterH=Num*8;
	while(Counter!=1033)
	{
	Value=BootSec[CounterH];
	FBUF[Counter]=Value;
	Counter++;CounterH++;	
	}
	print("   UnSecured! Flash is available!   ");
	Reboot = 0;
}
else
{
	Run(PowerOFF);
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 4. "); return; }
//...........................................
Delay_co=0;
while(Delay_co!=5)
{
	Run(Delay_Mh);
	Value = USB;
	if(Value != 0x00) { print("   disconnect_delay "); return; }
	Delay_co++;
}
//.............................................
	Ret=UnSec();
	if(Ret=0x8000){print("   Adapter not found 5. ");return;}
	if(Ret=0xAD){print("   SEC not open! ");return;}
}
}

Proc UnSec
{
	var Counter,Value,Temp,Error,Delay_co;
	Run(PowerON);
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 6. "); return; }
//...........................................
Delay_co=0;
while(Delay_co!=5)
{
	Run(Delay_Mh);
	Value = USB;
	if(Value != 0x00) { print("   disconnect_delay "); return; }
	Delay_co++;
}
//.............................................
	Run(SEC);
	Counter=1025;
	while(Counter!=1033)
	{
	Value = FBUF[Counter];
	USB = Value;
	Value=USB;
	Counter++;    
	}
	Run(ReadByte);
	USB=0xFF;USB=0xFE;
	Error = USB;
	return(Error);
}

Proc BootSecur(Num)
{
	var Counter,Value,Temp,Error,Delay_co;
	Run(PowerON);
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 6. "); return; }
//...........................................
Delay_co=0;
while(Delay_co!=5)
{
	Run(Delay_Mh);
	Value = USB;
	if(Value != 0x00) { print("   disconnect_delay "); return; }
	Delay_co++;
}
//.............................................
	Run(SEC);
	print("\r   secured No.   ");
	Counter=Num*8;
	Temp = Counter+8;	
	while(Counter!=Temp)
	{
	Value = BootSec[Counter];
	print("\; %b,",Value);
	USB = Value;
	Value=USB;
	Counter++;    
	}
	Run(ReadByte);
	USB=0xFF;USB=0xFE;
	Error = USB;
	return(Error);
}

Proc Head(Func)
{
var Value;
//	print("   reset BREAK. "); 
	Run(WrWord);
	Value=0xFE;USB=Value;Value=0x03;USB=Value;Value=0x80;USB=Value;
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 7. "); return; }
//	print("   reset COP. ");
	Run(WrWord);
	Value=0x00;USB=Value;Value=0x1F;USB=Value;Value=0x01;USB=Value;
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 8. "); return; }
//	print("   set AZ-mode. ");
	Run(WrWord);
	Value=0xFE;USB=Value;Value=0x09;USB=Value;Value=1;USB=Value;
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found 9. "); return; }
	return(0);
}
//////////////////////////////////////////////////////////////////

Proc Read
{
var Value,Counter,Counter1,Ret,Address,Temp;

        InitBoot();
	Value=Head(0);
	if(Value != 0x00) { print("   Adapter not found 9. "); Ret=255; return; }
//......................................
/////////////////////////////////////////////////////////
if(ActiveBuffer = 0)
{
	print("   Read EEPROM...");
	PBPos=0;PBMax = 0x200;
	Counter=0;
	Run(ReadROM);
	Value = 0x08;USB=Value;
	Value = 0x00;USB=Value;
	Value = 0x0A;USB=Value;	//0xFE
	Value = 0x00;USB=Value; //0x00
//..........................
	while(Counter != 0x200) 
	{
		Value = USB;
		if(Value = 0x8000) { print("   Error."); break;}
		FBUF[Counter] = Value;
		Counter++;PBPos++;
	}
	Value=USB;
	if(Value != 0) { print("   Adapter not found 26. "); return;}

//	Run(ReadByte);
//	USB=0xFE;
//	USB=0x18;
//	Value=USB;
//	print(" %b ",Value);	


//	Run(ReadByte);
//	USB=0xFE;
//	USB=0x1C;
//	Value=USB;
//	print(" %b ",Value);
//
	if(ForceCloseAfterRead != 0) {return(0x7FFFFFFF);}
	print("\; Success");
	return;
}
if(ActiveBuffer = 1)
{
	print("   Read FULL.");
//...................................
	PBPos=0;PBMax = 0xFFFF;
	Counter=0x8000;
	Run(ReadROM);
	Value = 0x80;USB=Value;
	Value = 0x00;USB=Value;
	Value = 0xFE;USB=Value;	//0xFE
	Value = 0x00;USB=Value; //0x00
//..........................
	while(Counter != 0xFE00) 
	{
		Value = USB;
		if(Value = 0x8000) { print("   Error."); break;}
		EBUF[Counter] = Value;
		Counter++;PBPos++;
	}
	Value=USB;
	if(Value != 0) { print("   Adapter not found 26. "); return;}		
//.........................
//	Counter=0x500;
//	while(Counter!=0x580)
//	{
//		EBUF[Counter]=0xFF;
//		Counter++;
//	}
//.........................
//	Counter=0xA00;
//	while(Counter!=0xE00)
//	{
//		EBUF[Counter]=0xFF;
//		Counter++;
//	}
//....................................
	PBPos = 0xFFC0;
//...............................
	Run(ReadROM);
	Counter=0xFFC0;
	Value = 0xFF;USB=Value;
	Value = 0xC0;USB=Value;
	Value = 0x00;USB=Value;
	Value = 0x00;USB=Value;
	while(Counter != 0x10000)
	{
		Value = USB;
		if(Value = 0x8000) { print("   Error."); break;}
		EBUF[Counter] = Value;
		Counter++;PBPos++;
	}
	Value=USB;
	if(Value != 0x00) { print("   Adapter not found 27. "); return;}
//....................................
	if(ForceCloseAfterRead != 0) {return(0x7FFFFFFF);}
	print("\;   Success");
	return;
}
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

Proc Compare
{
var Value,Counter,Counter1,Ret,Address,Temp,Mismatch;


      InitBoot();
	Value=Head(0);
	if(Value != 0x00) { print("   Adapter not found 9. "); Ret=255; return; }
//......................................
	Mismatch=0;
/////////////////////////////////////////////////////////
if(ActiveBuffer = 0)
{
	print("   Verify EEPROM...");
	Counter=0;
	PBPos=0;PBMax = 0x200;
	Run(ReadROM);
	Value = 0x08;USB=Value;
	Value = 0x00;USB=Value;
	Value = 0x0A;USB=Value;	//0xFE
	Value = 0x00;USB=Value; //0x00
//..........................
	while(Counter != 0x200) 
	{
		Value = USB;
		if(Value = 0x8000) { print("   Error."); break;}
//.................................
		Temp=FBUF[Counter];
		if(Value!=Temp)
		{
		FSEL[Counter]=0;
		Mismatch++;
		print("   Error addr=%h,  %b,  %b",Counter,Temp,Value);
		}
		else
		{
		FSEL[Counter]=1;
		}
//................................
		Counter++;PBPos++;
	}
	Value=USB;
	if(Value != 0x00) { print("   Adapter not found 31. "); return;}	
}
else
{
	print("   Verify FULL...");
//...................................
	PBPos=0;PBMax = 0xFFFF;
	Counter=0x8000;
	Run(ReadROM);
	Value = 0x80;USB=Value;
	Value = 0x00;USB=Value;
	Value = 0xFE;USB=Value;	//0xFE
	Value = 0x00;USB=Value; //0x00
//..........................
	while(Counter != 0xFE00) 
	{
		Value = USB;
		if(Value = 0x8000) { print("   Error."); break;}
//.................................
		Temp=EBUF[Counter];
		if(Value!=Temp)
		{
		ESEL[Counter]=0;
		Mismatch++;
		print("   Error addr=%h,  %b,  %b",Counter,Temp,Value);
		}
		else
		{
		ESEL[Counter]=1;
		}
//................................
		Counter++;PBPos++;
	}
	Value=USB;
	if(Value != 0x00) { print("   Adapter not found 31. "); return;}	
////////////////////////////////////////////////////////////////////////
	PBPos = 0xFFC0;
	Counter=0xFFC0;
	Run(ReadROM);
	Value = 0xFF;USB=Value;
	Value = 0xC0;USB=Value;
	Value = 0x00;USB=Value;
	Value = 0x00;USB=Value;
	while(Counter != 0x10000)
	{
		Value = USB;
		if(Value = 0x8000) { print("   Error."); break;}
//.................................
		Temp=EBUF[Counter];
		if(Value!=Temp)
		{
		ESEL[Counter]=0;
		Mismatch++;
		print("   Error addr=%h,  %b,  %b",Counter,Temp,Value);
		}
		else
		{
		ESEL[Counter]=1;
		}
//................................
		Counter++;PBPos++;
	}
	Value=USB;
	if(Value != 0x00) { print("   Adapter not found 34. "); return;}
//....................................
}
	if(Mismatch = 0)
	{
		if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF);}
	}
	print("   Done. Number of mismatch: %i",Mismatch);
}


Proc BootldrE1
{
var Value,Counter,Ret;

	Run(WRBytesCo);
	Counter=117;USB=Counter;
	USB=0x01; USB=0x00;
	Counter=0;
	PBPos = 0;PBMax = 117;
	while(Counter!=117)
	{
	Value = EEprom1[Counter];
	USB = Value;
	Value=USB;
	if(Value!=0) { return(255); }
	Counter++;PBPos++;    
	}
	return(0);
}


Proc Write
{
var Value,Address,Temp,Counter,CounterH,Error,Num,Ret;
	
	Ret=0;
        InitBoot();
	Value=Head(0);
	if(Value != 0x00) { print("   Adapter not found ee_0. "); Ret=255; return; }
//......................................
if(ActiveBuffer = 0)
{
	print(" bootloader WREE1");
	Value=BootldrE1();
	if(Value != 0x00) { print("   Adapter not found ee_0. "); Ret=255; return; }
//.....................................
	print("   WRITE EEPROM1");
	Address=0x800;
	Counter=512;
	PBPos = 0;PBMax = 512;
	Num=1;
//....................................
	while(Address!=0xA00)
	{	
		Run(WRAddr908);
		Temp=Address>>8;
		USB=Temp;
		USB=Address;
		USB=Num;
		Value=USB;
		if(Value!=0) { print("   Adapter not found ee1_0. "); Ret=255;return; }

	Run(WrWord);
	Value=0x01;USB=Value;Value=0x3C;USB=Value;Value=0x60;USB=Value;
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found ee1_1. "); Ret=255;return; }
//.................................
		Run(WRBytesCo);
		Value = Num;USB=Value;
		Value = 0;USB=Value;
		Value = 0x60;USB=Value;
		CounterH=0;
		while(CounterH!=Num)
		{
		print("\r Address= %h",Address);
			Value = FBUF[Counter];
			USB = Value;
			Ret = USB;
			if(Ret!=0) { print("   Adapter not found ee1_2. "); Ret=255;return; }
			Counter++;Address++;PBPos++;  
			CounterH++;  
		}
		Run(StartWR);
		Value=USB;
		if(Value!=0) { print("   Write error ee1"); Ret=255;return; }

	}
}
else
{
	print("  ROM ");
	Ret=0;
}
    if(Ret = 0)
    {
	if(ActiveBuffer = 0)
		{
        	if(GlobalVerify != 0) { Compare(); }
		}
		if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }    
    }
}

Proc WriteChanges
{
var Value,Address,Counter,Ret,Temp;
        InitBoot();
//////////////////////////////////////////
if(ActiveBuffer = 0)
{
	Value=Head(0);
	if(Value != 0x00) { print("   Adapter not found. "); Ret=255;return; }
	Ret=0;
//..................................
	print("   WRITE EEPROM");
		print(" bootloader WREE1");
		Value=BootldrE1();
		if(Value != 0x00) { print("   Adapter not found ee_0. "); Ret=255; return; }
		Address=0x800;
		Counter=512;
		PBPos = 0;PBMax = 512;
	while(Address!=0xA00)
	{	
		Value = FSEL[Counter];
		if(Value = 0)
		{
			print("\r Address= %h",Address);
			Run(WRAddr908);
			Temp=Address>>8;
			USB=Temp;
			USB=Address;
			USB=1;
			Value=USB;
			if(Value!=0) { print("   Adapter not found ee1_0. "); Ret=255;return; }
			Run(WrWord);
			Value=0x00;USB=Value;Value=0x60;USB=Value;Value = FBUF[Counter];USB=Value;
			Value = USB;
			if(Value != 0x00) { print("   Adapter not found ee1_1. "); Ret=255;return; }  
			Run(StartWR);
			Value=USB;
			if(Value!=0) { print("   Write error ee1"); Ret=255;return; }
		}
		Counter++;Address++;PBPos++;
	}
}
else
{
return;
}
    if(Ret = 0)
    {
        if(GlobalVerify != 0) { Compare(); }
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }    
        print("\; Success. %i bytes were write.",Counter);
    }
}

//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

Proc CalcBRR
{
    var BaudDEC,WorkBRR,Temp;
	
	Temp=Crystal;
	BaudDEC = Temp * 18;
	BaudDEC = BaudDEC/10;
	if(PTC3=0){BaudDEC=BaudDec*2;}
	WorkBRR =16000000/BaudDEC;
	WorkBRR =WorkBRR/16;
	WorkBRR--;
	return(WorkBRR);
}


///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

Proc Info
{
  print("");
  print("  Module for programming Motorola MCU type MC68HC08AZ32.");
  print("	Maskset 2J66");
  print("  Next functions are support:");
  print("");
  print("     EEPROM read/write/verify.");
  print("     FLASH read/write/verify.");
  print("-------------------------------------------------------");
  print("  Note: special adapter are required !");
  print("-------------------------------------------------------");
}


///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////


[Device]

areg		DR,0
areg		SR,1
areg		BRRL,2
areg		BRRH,3
areg		Begin,5
areg		IORD,4
areg		IFASE,4
areg		WRAdr,5
areg		VER,7

define	RST		EP.5

var Value,Counter,CounterH,CounterL,Temp,Byte,AH,AL,Ret,Num;

Proc AVRreset
{
	RST = 1;
	Delay(10000);
	RST = 0;
	Delay(10000);
	USB = 0;
}


Proc PowerOFF
{
	IFASE = 0;   // POWER 908 OFF
	Delay(50);
	USB = 0;
}

proc PowerON
{
	IFASE = 1;   // POWER 908 ON
	Delay(50);
	USB = 0;
}

Proc CheckAdapter
{
	Delay(10000);
    	Value = VER;
    	if(Value != 1)
    	{
        Value = VER;
    	}
	VER = 4;
    	Value = VER;
    	USB = Value;
}
/////////////////////////////////////////
Proc BaudStart
{
	Value = USB;
	BRRH = Value;
	Value =USB;
	BRRL = Value;
	USB = 0;
}
////////////////////////////////////////

Proc SEC
{	
	Counter = 8;
	while
	{
	Byte = USB; TXByte;
	Delay(50);
	Counter--;
	USB = Byte;
	if(Counter = 0) { break;}
	}
}

Proc WRAddr908
{
	AH=USB;AL=USB;Counter=USB;
	WRAdr=AH;WrAdr=AL;WrAdr=Counter;
	USB=0;
}


Proc WRData908
{
	AH=USB;AL=USB;Counter=USB;
	WRAdr=AH;WrAdr=AL;WrAdr=Counter;
	USB=0;
}


Proc StartWR
{
	Value=Begin;
	USB=0;
}

Proc WrWord
{
    	Byte = 0x49; TXByte;
    	Byte = USB; TXByte;
    	Byte = USB; TXByte;
	Byte = USB; TXByte;
	USB = 0;
}	


Proc WRBytes
{
	Byte = 0x49;TXByte;
	AH=USB;Byte=AH;TXByte;
	AL=USB;Byte=AL;TXByte;
	Value=USB;Byte=Value;TXByte;
	USB=0;
	Delay(2000);
	Counter=7;
//........................
	while
	{
	Byte = 0x19;TXByte;
	Value=USB;Byte=Value;TXByte;
	USB=0;
	Delay(20);
	Counter--;
	if(Counter=0){break;}
	}
}

Proc WRBytesCo
{
	Counter=USB;AH=USB;AL=USB;Value=USB;
	Byte = 0x49;TXByte;
	Byte=AH;TXByte;
	Byte=AL;TXByte;
	Byte=Value;TXByte;
	Delay(2000);
	USB=0;
	Counter--;
	if(Counter=0){return;}
//........................
	while
	{
	Byte = 0x19;TXByte;
	Byte = USB;TXByte;
	Delay(2000);
	USB=0;
	Counter--;
	if(Counter=0){break;}
	}
}

Proc WaitAnswer
{
Byte = IORD;	
USB=Byte;
}

///////////////////////////////////////
Proc ReadROM
{
	Value=USB;
	AH = Value;
	Value=USB;
	AL = Value;
	AH=AH<<8;
	AL=AL|AH;
	Value=USB;
	CounterH=Value;
	Value=USB;
	CounterL=Value;
	CounterH=CounterH<<8;
	CounterL=CounterL|CounterH;
	Counter = AL;
	Num=0;
	while(Num!=2)
	{
		Byte = 0x4A; TXByte;
		Byte = AL >> 8; TXByte;
		Byte = AL; TXByte;
		WaitRXComplete;
		Byte = DR;
		USB = Byte;
        	AL++;
		Counter++;
		Num++;
	}
//......
	while(Counter!=CounterL)
	{
		Byte = 0x1A; TXByte;
		WaitRXComplete;
		Byte = DR;
		USB = Byte;
		WaitRXComplete;
		Byte = DR;
		USB = Byte;
        	AL++;AL++;
		Counter++;Counter++;
	}
	USB =0;
}
/////////////////////////////////
Proc ReadByte
{
    AH = USB;AL = USB;
    Byte = 0x4A; TXByte;
    Byte = AH; TXByte;
    Byte = AL; TXByte;
    WaitRXComplete;
    Value = DR;
    USB = Value;
}


Proc TXByte 
{ 
while { Temp = SR; if(Temp.0 = 1) { break; } }  DR = Byte; 
WaitRXComplete;
Byte = DR;
WaitRXComplete;
Byte = DR;
}

Proc WaitRXComplete { while { Temp = SR; if(Temp.1 = 1) { break; } } }

Proc Delay_Mh
{
delay(25000);
USB=0;
}

