[Description]

Location = "Motorola\XC68HC912DG128";

Info = " Module for programming EEPROM and FLASH memory of XC68HC912DG128.",
	" Maskset	",
	" Manufacturer: Motorola.",
    " -----------------------------------------------------------",
    " Supported functions:",
    "    EEPROM read/write/verify",
    "    FLASH read",
    " -----------------------------------------------------------",
    " NOTE: SPECIAL ADAPTER ARE REQUIRED! ",
    " ",
    " Author: VKProg (C) 2008";

Picture = "XC68HC912DG128.bmp";
Buffer1 = "EEPROM",0x800;
Buffer2 = "FLASH",0x20000;
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        = "-";
//Erase 	= "&Erase", EraseButton, "Erase memory", F9;
//Div2        = "-";
Compare     = "&Verify",CheckButton,"Compare with buffer",F3;
Div3        = "-";
Info        = "&About module",ModuleInfoButton,"Show module info",F1;
Div5        = "-";

[Interface]

ComboBox(Crystal,"Crystal frequency (KHz)",10,6,90,"4000","8000","16000",2);
CheckBox(aCrystal,"Auto Crystal",250,5,1);
CheckBox(Preerase,"Erase before write?",350,5,1);

[Host]



dim Boot_EE[] = 
{
0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x10,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x1B,0xDE,0x18,0x0B,0xC0,0x00,0xF1,0xEC,0xF9,0x90,0x27,0xFB,0xEC,0xF9,0x83,0x15,
0xF8,0x27,0x8C,0x00,0x20,0x27,0x32,0x8C,0x00,0x30,0x27,0x5B,0x8C,0x00,0x40,0x27,
0x6A,0x8C,0x00,0x50,0x27,0x38,0xCC,0x00,0x80,0x20,0x03,0xCC,0x00,0x00,0x6C,0xF9,
0x6D,0xCC,0x00,0x00,0x6C,0xF9,0x63,0x20,0xCE,0x37,0x04,0x60,0x02,0x86,0x20,0xC6,
0xFA,0x12,0x59,0x6C,0xF9,0x9A,0x87,0x33,0x3D,0xED,0xF9,0x4A,0xE6,0xF9,0x4A,0x1A,
0xF9,0x4E,0xA6,0x70,0x6A,0x30,0x04,0x31,0xF9,0x6D,0xF9,0x3A,0x20,0xCD,0xCD,0x08,
0x00,0xCE,0x08,0x00,0x86,0xFF,0xA4,0x70,0x04,0x35,0xFB,0x41,0x18,0x27,0xFF,0xBB,
0xCC,0x00,0xFD,0x18,0x20,0xFF,0xB7,0xC6,0x06,0x5B,0xF3,0x7A,0x08,0x00,0x72,0x00,
0xF3,0x07,0x5D,0x5B,0xF3,0xC6,0x80,0x5B,0xF3,0x20,0xD3,0x1A,0xF9,0x12,0xE6,0xF9,
0x08,0xED,0xF9,0x02,0xA6,0x30,0x07,0x16,0x04,0x60,0x0C,0x02,0x6D,0xFA,0xFE,0xF6,
0x04,0x31,0xF1,0x18,0x20,0xFF,0x84,0x18,0x0E,0x87,0x18,0x20,0xFF,0x80,0x37,0xA1,
0x40,0x27,0x26,0x0E,0x40,0xFF,0x0D,0xC6,0x96,0x5B,0xF3,0x6A,0x40,0x72,0x00,0xF3,
0x07,0x1E,0x5B,0xF3,0xC6,0x82,0x5B,0xF3,0x6A,0x40,0x72,0x00,0xF3,0x07,0x11,0x5B,
0xF3,0xC6,0x80,0x5B,0xF3,0xA1,0x40,0x26,0x03,0x87,0x33,0x3D,0x86,0xFF,0x20,0xFA,
0x34,0xEE,0xFA,0xFE,0xFB,0x09,0xA7,0x26,0xFC,0x30,0x3D,0x3D};


//.................................................

dim Boot_FL[] = 
{
0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x10,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1B,0xDE,0xC6,0x00,0x5B,0xFF,0x4D,0xF5,
0x01,0x52,0x52,0xC1,0x08,0x26,0xF5,0xEC,0xF9,0x88,0x27,0xFB,0xEC,0xF9,0x7B,
0x15,0xF8,0x2F,0x8C,0x00,0x20,0x18,0x27,0x00,0x3A,0x8C,0x00,0x30,0x18,0x27,
0x00,0xC4,0x8C,0x00,0x40,0x18,0x27,0x00,0x7F,0x8C,0x00,0x50,0x18,0x27,0x00,
0x4F,0xCC,0x00,0x80,0x20,0x03,0xCC,0x00,0x00,0x6C,0xF9,0x5D,0xCC,0x00,0x00,
0x6C,0xF9,0x53,0x20,0xC6,0x37,0x04,0x60,0x02,0x86,0x20,0x6A,0xF9,0x93,0xC6,
0x32,0x12,0x6C,0xF9,0x8E,0x87,0x33,0x3D,0x1A,0xF9,0x42,0xA6,0xF9,0x38,0x36,
0xEC,0xF9,0x2F,0xED,0xF9,0x2E,0x15,0xF8,0xB3,0x5B,0xFF,0xA6,0x40,0x6A,0x30,
0x62,0xF9,0x23,0x26,0x08,0x62,0xF9,0x1D,0x26,0x03,0x62,0xF9,0x17,0x63,0x80,
0x26,0xE0,0x32,0x20,0xB6,0xCD,0x80,0x00,0xCE,0x40,0x00,0x86,0xFF,0xC6,0x00,
0x5B,0xFF,0x07,0x11,0x52,0xC1,0x08,0x26,0xF7,0x41,0x18,0x27,0xFF,0x9E,0xCC,
0x00,0xFD,0x18,0x20,0xFF,0x9A,0x34,0x35,0xA4,0x70,0x04,0x35,0xFB,0x31,0x30,
0x3D,0x1A,0xFA,0xFE,0xEE,0xA6,0xFA,0xFE,0xE3,0x36,0xEC,0xFA,0xFE,0xD9,0xED,
0xFA,0xFE,0xD7,0x15,0xF8,0x5C,0x5B,0xFF,0xA6,0x30,0x07,0x75,0x04,0x60,0x19,
0x62,0xFA,0xFE,0xC8,0x26,0x0A,0x62,0xFA,0xFE,0xC1,0x26,0x04,0x62,0xFA,0xFE,
0xBA,0x63,0x80,0x26,0xD8,0x32,0x18,0x20,0xFF,0x57,0x33,0x18,0x0E,0x87,0x18,
0x20,0xFF,0x52,0xCC,0x00,0xFE,0x4F,0xF7,0x08,0x27,0xCE,0x00,0xF4,0xCC,0x80,
0x00,0x6C,0xFA,0xFE,0xE3,0xCC,0x40,0x00,0x6C,0xFA,0xFE,0xDE,0xC6,0x00,0x5B,
0xFF,0x15,0xF8,0x56,0x04,0x60,0x09,0x52,0xC1,0x08,0x26,0xF3,0x18,0x20,0xFF,
0x24,0x18,0x0E,0x87,0x18,0x20,0xFF,0x20,0x35,0x3B,0x68,0x82,0x65,0x81,0x68,
0x82,0x65,0x81,0x64,0x82,0x14,0x01,0x66,0x82,0x3A,0x97,0x2B,0x06,0x19,0xC6,
0x19,0xED,0xE6,0x5F,0x31,0x3D,0x06,0x00,0x07,0x37,0x34,0xA1,0x40,0x27,0x12,
0xE6,0x40,0xC1,0xFF,0x26,0x14,0x4F,0xF7,0x08,0x0C,0xCE,0x00,0xF4,0x07,0x7F,
0x30,0x33,0x3D,0x86,0x00,0x20,0xF9,0x86,0xFE,0x20,0xF5,0x86,0xFD,0x20,0xF1,
0x37,0x35,0x69,0xFA,0xFE,0x7D,0xC6,0x06,0x6B,0x03,0xED,0xFA,0xFE,0x71,0x6B,
0x40,0x07,0x3E,0x62,0xFA,0xFE,0x6D,0x07,0x23,0x27,0x0A,0xE6,0xFA,0xFE,0x65,
0xC1,0x05,0x26,0xEE,0x20,0x13,0x07,0x2A,0x63,0xFA,0xFE,0x59,0x26,0xF8,0x07,
0x0D,0x26,0x07,0x86,0x00,0x69,0x03,0x31,0x33,0x3D,0x86,0xFF,0x20,0xF7,0x34,
0x35,0xED,0xFA,0xFE,0x3E,0xEE,0xFA,0xFE,0x3C,0xC6,0xFF,0xE4,0x70,0x04,0x35,
0xFB,0x51,0x31,0x30,0x3D,0x14,0x10,0x62,0x03,0x07,0x05,0x63,0x03,0x07,0x0B,
0x3D,0x36,0x86,0x05,0x07,0x05,0x04,0x30,0xFB,0x32,0x3D,0x34,0xEE,0xFA,0xFE,
0x1B,0xA7,0x09,0x26,0xFC,0x30,0x3D,0x37,0x69,0xFA,0xFE,0x0E,0xC6,0x02,0x6B,
0x03,0x6A,0x40,0x07,0x28,0x62,0xFA,0xFE,0x02,0xA1,0x40,0x27,0x0A,0xE6,0xFA,
0xFD,0xFA,0xC1,0x32,0x26,0xEE,0x27,0x12,0x07,0x14,0x63,0xFA,0xFD,0xEE,0x26,
0xF8,0xA1,0x40,0x26,0x06,0x86,0x00,0x69,0x03,0x33,0x3D,0x86,0xFF,0x20,0xF8,
0x36,0x62,0x03,0xA6,0xFA,0xFD,0xD8,0x43,0xA7,0x26,0xFC,0x63,0x03,0xA6,0xFA,
0xFD,0xCE,0x44,0x43,0xA7,0x26,0xFC,0x32,0x3D};


define      BLOADER_EE		348
define      BLOADER_FL		654
define      RAM_BEGIN		0x2000
define      EEPROM_BEGIN		0x0800
define      EEPROM_END		0x0FFF
define      FLASH_BEGIN		0x0000
define      FLASH_END		0x20000
define		StartADR	0x70

//////////////////////////////////////////////////////////
Proc Connect
{
    var Value,Temp,Data0B,DataF1,Ret;
	Run(AVRreset);
	Value = USB;
	Ret=0;
	if(Value != 0x00) { print("   Adapter not found. ");Ret=0xEE;return(Ret); }
	Run(CheckAdapter);
	Value = USB;
	if(Value != 0x05) { print("   Adapter not found. ");Ret=0xEE;return(Ret); }
	if(aCrystal=1){Ret=aCalcBRR();}
	if(aCrystal!=1){Ret=CalcBRR();}
	return(Ret);
}


Proc Read 
{
    var Value,Counter,CounterH,Ret,Temp,Address,Data;


	Ret=Connect();
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
//...............
if(ActiveBuffer = 0)
{
	Run(LoadADR);
	Value = RAM_BEGIN>>8; USB = Value;
	Value = RAM_BEGIN; USB = Value;
	Value = EEPROM_BEGIN>>24; USB = Value;
	Value = EEPROM_BEGIN>>16; USB = Value;
	Value = EEPROM_BEGIN>>8; USB = Value;
	Value = EEPROM_BEGIN; USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0 "); return; }
//.................................
	Address=BLOADER_EE;
	Counter=0;
	Run(LoadBoot);
	Value=Address>>8;USB=Value;
	USB=Address;
	Data = Calc();
	Data=Data*2;
	while(Counter!=BLOADER_EE)
	{
	Value=Boot_EE[Counter];
	if(Counter=2){Value=Data;}
	if(Counter=3){Value=0x20;}
	if(Counter=0x0A){Value=0;}
	if(Counter=0x0B){Value=0;}
	USB=Value;
	Counter++;
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0_1 "); return; }
	USB=StartADR;
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 1 "); return; }	
	print("   Read EEPROM...");
	CounterH = EEPROM_END + 1;
	CounterH = CounterH-EEPROM_BEGIN;
	PBPos = 0;PBMax = CounterH;Address=0;
	while(Address!=CounterH)
	{
	Run(RDHeader);
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 2 "); return; }	
	Counter=0;
	Run(ReadEEPROM);	
	while(Counter!=0x40)
	{
	Value = USB;
	if(Value = 0x8000) { print("   disconnect 3 "); break;}
	FBUF[Address] = Value;
	Counter++;PBPos++;Address++;		
	}
	}
}
else
{
	Run(LoadADR);
	Value = RAM_BEGIN>>8; USB = Value;
	Value = RAM_BEGIN; USB = Value;
	Address=FLASH_BEGIN+0x80000000;
	Value = Address>>24; USB = Value;
	Value = Address>>16; USB = Value;
	Value = Address>>8; USB = Value;
	Value = Address; USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   disconnect 4 "); return; }
//..............................
	Address=BLOADER_FL;
	Counter=0;
	Run(LoadBoot);
	Value=Address>>8;USB=Value;
	USB=Address;
	Data = Calc();
	Data=Data*2;
	while(Counter!=BLOADER_FL)
	{
	Value=Boot_FL[Counter];
	if(Counter=2){Value=Data;}
	if(Counter=3){Value=0x20;}
	if(Counter=0x0A){Value=0;}
	if(Counter=0x0B){Value=0;}
	USB=Value;
	Counter++;
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0_1 "); return; }
	USB=StartADR;
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 5 "); return; }	
	print("   Read FLASH...");
	CounterH = FLASH_END-FLASH_BEGIN;
	PBPos = 0;PBMax = CounterH;Address=0;
	while(Address!=CounterH)
	{
	Run(RDHeader);
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 6 "); return; }	
	Counter=0;
	Run(ReadEEPROM);	
	while(Counter!=0x40)
	{
	Value = USB;
	if(Value = 0x8000) { print("   disconnect 7 "); break;}
	EBUF[Address] = Value;
	Counter++;PBPos++;Address++;		
	}
	}
}
	if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF);}
	print("\; Success");
}


proc Compare
{
    var Value,Temp,Data0B,DataF1,Mismatch,Active,Ret;
	Run(AVRreset);
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found. ");return; }
	Run(CheckAdapter);
	Value = USB;
	if(Value != 0x05) { print("   Adapter not found. ");return; }

	Ret=Connect();
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
	Active=1;
	Mismatch=0;
	if(ActiveBuffer = 0)
	{
		Mismatch=CompareEE(Active);
	}
	else
	{
		Mismatch=CompareFL(Active);
	}
	if(Mismatch = 0)
	{
		if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF);}
	}
	print("\; Done. Number of mismatch: %i",Mismatch);
}




Proc CompareEE(Active)
{
    var Value,Counter,CounterH,Ret,Temp,Mismatch,Address,Data;

if(Active=0)
	{
	Ret=Connect();
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
	}
//..........................
	Mismatch=0;
//..........................
	Run(LoadADR);
	Value = RAM_BEGIN>>8; USB = Value;
	Value = RAM_BEGIN; USB = Value;
	Value = EEPROM_BEGIN>>24; USB = Value;
	Value = EEPROM_BEGIN>>16; USB = Value;
	Value = EEPROM_BEGIN>>8; USB = Value;
	Value = EEPROM_BEGIN; USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   disconnect 10 "); return; }
//.................................
	Address=BLOADER_EE;
	Counter=0;
	Run(LoadBoot);
	Value=Address>>8;USB=Value;
	USB=Address;
	Data = Calc();
	Data=Data*2;
	while(Counter!=BLOADER_EE)
	{
	Value=Boot_EE[Counter];
	if(Counter=2){Value=Data;}
	if(Counter=3){Value=0x20;}
	if(Counter=0x0A){Value=0;}
	if(Counter=0x0B){Value=0;}
	USB=Value;
	Counter++;
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0_1 "); return; }
	USB=StartADR;
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 11 "); return; }	
//..............................
	print("   Compare EEPROM...");
	CounterH = EEPROM_END + 1;
	CounterH = CounterH-EEPROM_BEGIN;
	PBPos = 0;PBMax = CounterH;Address=0;
	while(Address!=CounterH)
	{
	Run(RDHeader);
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 12 "); return; }	
	Counter=0;
	Run(ReadEEPROM);	
	while(Counter!=0x40)
	{
	Value = USB;
	if(Value = 0x8000) { print("   disconnect 13 "); break;}
	Temp=FBUF[Address];
        if(Value != Temp) { Mismatch++;FSEL[Address] = 0;}
        	else { FSEL[Address] = 1;}
	Counter++;PBPos++;Address++;		
	}
	}
	return(Mismatch);
}


Proc CompareFL(Active)
{
    var Value,Counter,CounterH,Ret,Temp,Mismatch,Address,Data;

if(Active=0)
	{
	Ret=Connect();
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
	}
//..........................
	Mismatch=0;
//..........................
	Run(LoadADR);
	Value = RAM_BEGIN>>8; USB = Value;
	Value = RAM_BEGIN; USB = Value;
	Address=FLASH_BEGIN+0x80000000;
	Value = Address>>24; USB = Value;
	Value = Address>>16; USB = Value;
	Value = Address>>8; USB = Value;
	Value = Address; USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   disconnect 14 "); return; }
//...........................
	Address=BLOADER_FL;
	Counter=0;
	Run(LoadBoot);
	Value=Address>>8;USB=Value;
	USB=Address;
	Data = Calc();
	Data=Data*2;
	while(Counter!=BLOADER_FL)
	{
	Value=Boot_FL[Counter];
	if(Counter=2){Value=Data;}
	if(Counter=3){Value=0x20;}
	if(Counter=0x0A){Value=0;}
	if(Counter=0x0B){Value=0;}
	USB=Value;
	Counter++;
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0_1 "); return; }
	USB=StartADR;
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 15 "); return; }	
	print("   Compare FLASH...");
	CounterH = FLASH_END-FLASH_BEGIN;
	PBPos = 0;PBMax = CounterH;Address=0;
	while(Address!=CounterH)
	{
	Run(RDHeader);
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 16 "); return; }	
	Counter=0;
	Run(ReadEEPROM);	
	while(Counter!=0x40)
	{
	Value = USB;
	if(Value = 0x8000) { print("   disconnect 17 "); break;}
	Temp=EBUF[Address];
        if(Value != Temp) { Mismatch++;ESEL[Address] = 0;}
            else { ESEL[Address] = 1;}
	Counter++;PBPos++;Address++;		
	}
	}
	return(Mismatch);
}

proc Erase
{
    var Value,Temp,Mismatch,Active,Ret;
	Run(AVRreset);
	Value = USB;
	if(Value != 0x00) { print("   Adapter not found. ");return; }
	Run(CheckAdapter);
	Value = USB;
	if(Value != 0x05) { print("   Adapter not found. ");return; }
//...........................................
	if(aCrystal=1){Ret=aCalcBRR();}
	if(aCrystal!=1){Ret=CalcBRR();}
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
	Active=1;
	if(ActiveBuffer = 0)
	{
		Mismatch=Erase_EEP(Active);
	}
	else
	{
		Mismatch=Erase_FLASH(Active);
	}
	print("\; Done.");
}


Proc Erase_EEP(Active) 
{
    var Value,Counter,CounterH,Ret,Temp,Address,Data;

if(Active=0)
	{
	Ret=Connect();
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
	}
/////////////////////////////////////
	Run(LoadADR);
	Value = RAM_BEGIN>>8; USB = Value;
	Value = RAM_BEGIN; USB = Value;
	Value = EEPROM_BEGIN>>24; USB = Value;
	Value = EEPROM_BEGIN>>16; USB = Value;
	Value = EEPROM_BEGIN>>8; USB = Value;
	Value = EEPROM_BEGIN; USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   disconnect 18 "); return(0xEE); }
//.........................
	Address=BLOADER_EE;
	Counter=0;
	Run(LoadBoot);
	Value=Address>>8;USB=Value;
	USB=Address;
	Data = Calc();
	Data=Data*2;
	while(Counter!=BLOADER_EE)
	{
	Value=Boot_EE[Counter];
	if(Counter=2){Value=Data;}
	if(Counter=3){Value=0x30;}
	if(Counter=0x0A){Value=0;}
	if(Counter=0x0B){Value=0;}
	USB=Value;
	Counter++;
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0_1 "); return; }
	USB=StartADR;
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 19 "); return(0xEE); }	
	print("   ERASE EEPROM...");
	Run(ERHeader);
	Value=USB;
	if(Value != 0xFF) { print("   disconnect 20 "); return(0xEE); }	
	return(0x00);
}


Proc Erase_FLASH(Active)
{
    var Value,Counter,CounterH,Ret,Temp,Address,Data;

if(Active=0)
	{
	Ret=Connect();
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
	}
//................................
	Run(LoadADR);
	Value = RAM_BEGIN>>8; USB = Value;
	Value = RAM_BEGIN; USB = Value;
	Address=FLASH_BEGIN+0x80000000;
	Value = Address>>24; USB = Value;
	Value = Address>>16; USB = Value;
	Value = Address>>8; USB = Value;
	Value = Address; USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   disconnect 21 "); return(0xEE); }
//..............................
	Address=BLOADER_FL;
	Counter=0;
	Run(LoadBoot);
	Value=Address>>8;USB=Value;
	USB=Address;
	Data = Calc();
	Data=Data*2;
	while(Counter!=BLOADER_FL)
	{
	Value=Boot_FL[Counter];
	if(Counter=2){Value=Data;}
	if(Counter=3){Value=0x30;}
	if(Counter=0x0A){Value=0;}
	if(Counter=0x0B){Value=0;}
	USB=Value;
	Counter++;
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0_1 "); return; }
	USB=StartADR;
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 22 "); return(0xEE); }	
	print("   ERASE FLASH...");
	Run(ERHeader);
	Value=USB;
	if(Value != 0xFF) { print("   disconnect 23 "); return(0xEE); }	
	return(0x00);
}



Proc Write
{
    var Value,Counter,CounterH,Ret,Temp,Address,Mismatch,Data,Active,delay_co;


///////////////////////////////
Active=0;
if(ActiveBuffer = 0)
{
	if(Preerase=1)
	{
	Ret=Erase_EEP();
	if(Ret!=0x00){ print("   Not connected. "); return;}	
	}
	else
	{
	Ret=Connect();
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
	}
//...........................................................
	Run(LoadADR);
	Value = RAM_BEGIN>>8; USB = Value;
	Value = RAM_BEGIN; USB = Value;
	Value = EEPROM_BEGIN>>24; USB = Value;
	Value = EEPROM_BEGIN>>16; USB = Value;
	Value = EEPROM_BEGIN>>8; USB = Value;
	Value = EEPROM_BEGIN; USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   disconnect 24 "); return; }
//.......................
	Address=BLOADER_EE;
	Counter=0;
	Run(LoadBoot);
	Value=Address>>8;USB=Value;
	USB=Address;
	Data = Calc();
	Data=Data*2;
	while(Counter!=BLOADER_EE)
	{
	Value=Boot_EE[Counter];
	if(Counter=2){Value=Data;}
	if(Counter=3){Value=0x40;}
	if(Counter=0x0A){Value=0;}
	if(Counter=0x0B){Value=0;}
	USB=Value;
	Counter++;
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0_1 "); return; }
	USB=StartADR;
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 25 "); return; }	
	print("   Wirte EEPROM...");
	CounterH = EEPROM_END + 1;
	CounterH = CounterH-EEPROM_BEGIN;
	PBPos = 0;PBMax = CounterH;Address=0;
	while(Address!=CounterH)
	{
	Run(WRHeader);
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 26 "); return; }	
	Counter=0;
	Run(WriteEEPROM);	
	while(Counter!=0x40)
	{
	Value=FBUF[Address];
	USB=Value;
	Counter++;PBPos++;Address++;		
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 27 "); return; }
	Run(WRWait);
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 28 "); return;}
//...........................................
Delay_co=0;
while(Delay_co!=20)
{
	Run(Delay_Mh);
	Value = USB;
	if(Value != 0x00) { print("   disconnect_delay "); return; }
	Delay_co++;
}
//.............................................
	}

	Mismatch=0;
	Active=0;
	Mismatch=CompareEE(Active);
	if(Mismatch = 0)
	{
		if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF);}
	}
	print("\; Done. Number of mismatch: %i",Mismatch);
}
else
{
	if(Preerase=1)
	{
	Ret=Erase_FLASH(Active);
	if(Ret!=0x00){ print("   Not connected. "); return;}	
	}
	else
	{
	Ret=Connect();
	if(Ret=0xEC){ print("   SEC is active! "); return;}
	if(Ret=0xEE){ print("   wrong CLK! "); return;}	
	print("   BDM is open! ");
	}
//...............................
	Run(LoadADR);
	Value = RAM_BEGIN>>8; USB = Value;
	Value = RAM_BEGIN; USB = Value;
	Address=FLASH_BEGIN+0x80000000;
	Value = Address>>24; USB = Value;
	Value = Address>>16; USB = Value;
	Value = Address>>8; USB = Value;
	Value = Address; USB = Value;
	Value = USB;
	if(Value != 0x00) { print("   disconnect 29 ");  return; }
//..............................
	Address=BLOADER_FL;
	Counter=0;
	Run(LoadBoot);
	Value=Address>>8;USB=Value;
	USB=Address;
	Data = Calc();
	Data=Data*2;
	while(Counter!=BLOADER_FL)
	{
	Value=Boot_FL[Counter];
	if(Counter=2){Value=Data;}
	if(Counter=3){Value=0x40;}
	if(Counter=0x0A){Value=0;}
	if(Counter=0x0B){Value=0;}
	USB=Value;
	Counter++;
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 0_1 "); return; }
	USB=StartADR;
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 30 ");  return; }	
	print("   Write FLASH...");
	CounterH = FLASH_END-FLASH_BEGIN;
	PBPos = 0;PBMax = CounterH;Address=0;
	while(Address!=CounterH)
	{
	Run(WRHeader);
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 31 ");  return; }	
	Counter=0;
	Run(WriteEEPROM);	
	while(Counter!=0x40)
	{
	Value=EBUF[Address];
	USB=Value;
	Counter++;PBPos++;Address++;		
	}
	Value = USB;
	if(Value != 0x00) { print("   disconnect 32 ");  return; }
	Run(WRWait);
	Value=USB;
	if(Value != 0xAA) { print("   disconnect 33 ");  return;}
	}
	Mismatch=0;
	Active=0;
	Mismatch=CompareFL(Active);
	if(Mismatch = 0)
	{
		if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF);}
	}
	print("\; Done. Number of mismatch: %i",Mismatch);

}
	print("\; Success");
}



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

Proc aCalcBRR
{
var	Value,Temp,Data1,Data2,Ret;

	Ret=0xEE;
	Crystal=0;
	while
	{
	Value=Calc();
	Run(Reboot);
	USB = Value;
	Value = USB;
	if(Value != 0xAA) { print("   BDM not open!  ");Ret=0xEE;return(Ret); }
//...........................................
	Temp=USB;
	Data1=Temp<<8;
	Temp=USB;
	Data1=Data1|Temp;
	Temp=USB;
	Data2=Temp<<8;
	Temp=USB;
	Data2=Data2|Temp;	
	if(Data1!=0xFFFF){Ret=0;break;}
	if(Data2!=0xFFFF){Ret=0;break;}
	Crystal++;
	if(Crystal=3){break;}
	}
	Data1=Data1|Data2;
	if(Ret=0)
		{if(Data1=0x0000){Ret=0xEC;}}
	return(Ret);
}

Proc CalcBRR
{
var	Value,Temp,Data1,Data2,Ret;
	Ret=0xEE;
	Value=Calc();
	Run(Reboot);
	USB = Value;
	Value = USB;
	if(Value != 0xAA) { print("   BDM not open!  ");Ret=0xEE;return(Ret); }
//...........................................
	Temp=USB;
	Data1=Temp<<8;
	Temp=USB;
	Data1=Data1|Temp; 
//	print("Data1: %h",Data1);
	Temp=USB;
	Data2=Temp<<8;
	Temp=USB;
	Data2=Data2|Temp;
//	print("Data2: %h",Data2);	
	if(Data1!=0xFFFF){Ret=0;}
	if(Data2!=0xFFFF){Ret=0;}
	Data1=Data1|Data2;
	if(Ret=0)
		{if(Data1=0x0000){Ret=0xEC;}}
	Data1=Data1&Data2;
	if(Data1=0xFFFF){Ret=0xEE;}
	return(Ret);
}

Proc Calc
{
	var BootBRR;
if(Crystal=2){BootBRR=16;}
if(Crystal=1){BootBRR=8;}
if(Crystal=0){BootBRR=4;}
    return(BootBRR);
}
//..............................


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


[Device]

areg	DR,0
areg	RDY,1
areg	BRR,1
areg	START_BOOT,2
areg	ADR_MCU,2
areg	BLOAD,3
areg	RD_HEAD,3
areg	WR_HEAD,4
areg	ER_HEAD,5
areg	START_ADR,5
areg	WR_WAIT,6
areg	VER,7
areg	IO,4
areg	IORD,4

define	RST		EP.5

var Value,Counter,BtH,BtL,Temp,ByteH,ByteL,AH,AL;

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

Proc CheckAdapter
{
	Delay(10000);
    	Value = VER;
    	if(Value != 1)
    	{
        Value = VER;
    	}
	VER = 5;
    	Value = VER;
    	USB = Value;
}
/////////////////////////////////////////

Proc Reboot
{
	Value = USB;BRR = Value;
	IO = 0; Delay(5000);		// Reset = 0
	IO = 3; Delay(5000);		// Reset = 1
	Counter = 0;
	while(Counter!=5)
	{
	Value = RDY;
	USB = Value;
	Counter++;
	}
}

Proc LoadADR
{
	Counter = 0;
	while(Counter!=6)
	{
	Value = USB;
	ADR_MCU = Value;
	Counter++;
	}
	USB=0;
}


Proc LoadBoot
{
	Value=0;
	Value=USB;
	Counter=Value<<8;
	Value=0;
	Value=USB;
	Counter=Counter|Value;
	while(Counter!=0)
	{
	Value=USB;
	BLOAD=Value;
	Counter--;
	}
	USB=0;
	Value=USB;
	START_ADR=Value;
	Value=START_BOOT;
	USB=Value;
}

Proc RDHeader
{
	Value=RD_HEAD;
	USB=Value;
}

Proc WRHeader
{
	Value=WR_HEAD;
	USB=Value;
}

Proc WRWait
{
	Value=WR_WAIT;
	USB=Value;
}

Proc ERHeader
{
	Value=ER_HEAD;
	USB=Value;
}

Proc ReadEEPROM
{
	Counter=0;
    	while(Counter!=0x40)
    	{
	Value=DR; 
	USB = Value;
      	Counter++;
    	}
}

Proc WriteEEPROM
{
	Counter = 0;
	while(Counter!= 0x40)
    	{
	Value = USB;
	DR=Value;
	Counter++;
	}
	USB=0;
}

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