[Description]

Location = "Motorola\Motorola11\MC68HC11KA4";

Info = " Module for programming EEPROMs memory of MC68HC11KA4.",
    " Manufacturer: Motorola.",
    " ------------------------------------------------------------------------------------------------------------------",
    " EEPROM size: 640 bytes.",
    " ROM size:  24576 bytes (24 KBytes)",
    " ------------------------------------------------------------------------------------------------------------------ ",
    " Supported functions:",
    "    EEPROM read/write/verify",
    "    ROM read",
    " ------------------------------------------------------------------------------------------------------------------  ",
    " NOTE: SPECIAL ADAPTER ARE REQUIRED! ",
    " ",
    " Author: ETL (C) 2007";

Picture = "MC68HC11KA4.bmp";
Buffer1 = "EEPROM",640;
Buffer2 = "ROM",24576;
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        = "-";
CalcBRR     = "BRR";

[Interface]

Digit(Crystal,"Crystal frequency (KHz)",10,6,8664);
CheckBox(Reboot,"Download bootloader",250,5,1);

[Host]

dim Bootloader[] = 
{
 $CE, $00, $00, $6F, $72, $CC, $00, $1A, $ED, $70, $86, $0C, $A7, $73, $6F, 
 $35, $86, $08, $8D, $41, $4A, $26, $FB, $86, $A5, $A7, $77, $1F, $74, $20, $FC, 
 $A6, $77, $81, $52, $27, $4C, $81, $57, $27, $02, $20, $F0, $8D, $4A, $1F, $74, 
 $20, $FC, $A6, $77, $C6, $16, $E7, $3B, $18, $E7, $00, $C6, $17, $E7, $3B, $8D, 
 $15, $6F, $3B, $C6, $02, $E7, $3B, $18, $A7, $00, $C6, $03, $E7, $3B, $8D, $06, 
 $6F, $3B, $8D, $14, $20, $C6, $36, $EC, $0E, $C3, $9C, $40, $ED, $1C, $86, $10, 
 $A7, $23, $1F, $23, $10, $FC, $32, $39, $1F, $74, $80, $FC, $18, $A6, $00, $A7, 
 $77, $39, $8D, $04, $8D, $F2, $20, $A4, $1F, $74, $20, $FC, $A6, $77, $1F, $74, 
 $20, $FC, $E6, $77, $18, $8F, $39, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF,
 $FF, $ff, $FF, $FF, $FF
};


define      EEPROM_BEGIN    0x0D80
define      EEPROM_END      0x0FFF
define      EEPROM_SIZE     640               
define      ROM_BEGIN       0xA000
define      ROM_SIZE        24576


Proc Read 
{
    var Value,Counter,Ret,Address;
    Run(CheckAdapter);
    Value = USB;
    if(Value != 1) { print("   Adapter not found. "); return; }
    if(Reboot != 0)
    {
        Ret = Init();
        if(Ret != 0) { return; }
    }
    print("   ");
    if(ActiveBuffer = 0)
    {
        print("   Read EEPROM...");
        Address = EEPROM_BEGIN;
        PBPos = 0;PBMax = 640;Counter = 0;
        while(Counter != EEPROM_SIZE)
        {
            Run(ReadEEPROMByte);
            BeginBlock;
            Value = Address >> 8; USB = Value;
            USB = Address;
            EndBlock;
            Value = USB;
            FBUF[Counter] = Value;
            Counter++;Address++;PBPos++;
        }
    }
    else
    {
        print("   Read ROM...");
        PBPos = 0;PBMax = ROM_SIZE;Counter = 0;
        Run(ReadROM);
        while(Counter != ROM_SIZE)
        {
            Value = USB;
            if(Value = 0x8000) { print("   Error."); break;}
            EBUF[Counter] = Value;
            Counter++;PBPos++;
        }
    }
    if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF);}
    print("\; Success");
}

Proc Compare
{
    var Value,Counter,Ret,Address,Mismatch,Temp;
    Run(CheckAdapter);
    Value = USB;
    if(Value != 1) { print("   Adapter not found. "); return; }
    if(Reboot != 0)
    {
        Ret = Init();
        if(Ret != 0) { return; }
    }
    print("   ");
    Mismatch = 0;
    if(ActiveBuffer = 0)
    {
        //Run(SetSpeed); 
        //Value = WorkSpeeds[Crystal];
        //USB = Value;
        print("   Verify EEPROM...");
        Address = EEPROM_BEGIN;
        PBPos = 0;PBMax = 640;Counter = 0;
        while(Counter != EEPROM_SIZE)
        {
            Run(ReadEEPROMByte);
            BeginBlock;
            Value = Address >> 8; USB = Value;
            USB = Address;
            EndBlock;
            Value = USB;
            Temp = FBUF[Counter];
            if(Value != Temp) { Mismatch++; FSEL[Counter] = 0;}
            else { FSEL[Counter] = 1;}
            Counter++;Address++;PBPos++;
        }
    }
    else
    {
        //Run(SetSpeed); 
        //Value = WorkSpeeds[Crystal];
        //USB = Value;
        print("   Verify ROM...");
        PBPos = 0;PBMax = ROM_SIZE;Counter = 0;
        Run(ReadROM);
        while(Counter != ROM_SIZE)
        {
            Value = USB;
            if(Value = 0x8000) { print("   Error."); break;}
            Temp = EBUF[Counter];
            if(Value != Temp) { Mismatch++; ESEL[Counter] = 0;}
            else { ESEL[Counter] = 1;}
            Counter++;PBPos++;
        }
    }
    if(Mismatch = 0)
    {
        if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF);}
    }
    print("\; Done. Number of mismatch: %i",Mismatch);
}

Proc Write
{
    var Value,Counter,Ret,Address,Mismatch,Temp;
    print("   ");
    if(ActiveBuffer = 1)
    {
        print("   ROM area can not be writed.");
        print("   Writing canceled.");
        return;
    }
    Run(CheckAdapter);
    Value = USB;
    if(Value != 1) { print("   Adapter not found. "); return; }
    if(Reboot != 0)
    {
        Ret = Init();
        if(Ret != 0) { return; }
    }
    //Run(SetSpeed); 
    //Value = WorkSpeeds[Crystal];
    //USB = Value;
    print("   Writing EEPROM... ");
    Address = EEPROM_BEGIN;
    PBPos = 0;PBMax = 640;Counter = 0;Ret = 0;
    while(Counter != EEPROM_SIZE)
    {
        Run(WriteEEPROMByte);
        BeginBlock;
        Value = Address >> 8; USB = Value;
        USB = Address;
        Value = FBUF[Counter]; USB = Value;
        EndBlock;
        Temp = USB;
        if(Temp != Value)
        {   
            print("   Writing error. Address %h",Address);
            Ret = 255;
            break;
        }
        Counter++;Address++;PBPos++;
    }   
    if(Ret = 0)
    {
        if(GlobalVerify != 0) { Compare(); }
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }    
        print("\; Success. %i bytes were write.",Counter);
    }
}

Proc WriteChanges
{
    var Value,Counter,Ret,Address,Mismatch,Temp;
    print("   ");
    if(ActiveBuffer = 1)
    {
        print("   ROM area can not be writed.");
        print("   Writing canceled.");
        return;
    }
    Run(CheckAdapter);
    Value = USB;
    if(Value != 1) { print("   Adapter not found. "); return; }
    if(Reboot != 0)
    {
        Ret = Init();
        if(Ret != 0) { return; }
    }
    //Run(SetSpeed); 
    //Value = WorkSpeeds[Crystal];
    //USB = Value;
    print("   Writing changes to EEPROM... ");
    Address = EEPROM_BEGIN;
    PBPos = 0;PBMax = 640;Counter = 0;Ret = 0;
    while(Counter != EEPROM_SIZE)
    {
        Value = FSEL[Counter];
        if(Value = 0)
        {
            Run(WriteEEPROMByte);
            BeginBlock;
            Value = Address >> 8; USB = Value;
            USB = Address;
            Value = FBUF[Counter]; USB = Value;
            EndBlock;
            Temp = USB;
            if(Temp != Value)
            {   
                print("   Writing error. Address %h",Address);
                Ret = 255;
                break;
            }
        }
        Counter++;Address++;PBPos++;
    }   
    if(Ret = 0)
    {
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }    
        print("\; Success. %i bytes were write.",Counter);
    }
}

Proc InitBoot
{
    var Value;
    Run(CheckAdapter);
    Value = USB;
    if(Value != 1) { print("   Adapter not found. "); return; }
    Value = Init();
    if(Value = 0)
    {
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }
    }
}

Proc Init
{
    var Counter,Value,Temp,Error;
    Value = Crystal;
    print("");
    print("   Crystal frequency: ");Print("\;%i KHz",Value);
    Value = CalcBRR();
    print("   Download bootloader...");
    PBPos = 0;PBMax = 256;
    //print("   Set speed: ");
    Run(Reboot);
    // setup Boot speed
    Temp = Value >> 24;USB = Temp;
    Temp = Value >> 16;USB = Temp;
    // setup Work speed
    Temp = Value >> 8;USB = Temp;
    USB = Value;

    Counter = 0;
    Error = 0;
    while(Counter != 256)
    {
        Value = BootLoader[Counter];
        USB = Value;
        Temp = USB;
        if(Temp = 0x8000) { Error = 0x8000; break;}
        if(Value != Temp)
        {
            Error = 255;
            print("\; Error.");
            print("   Write: %b, Read: %b",Value,Temp);
            break;
        }
        Counter++;    
        PBPos++;
    }
    if(Error != 0)
    {
        if(Error = 0x8000)
        {
            print("\; Error. Device not response.");
        }
    }
    else 
    {
        Value = USB; 
        if(Value = 0xA5) 
        {
            Reboot = 0;
            print("\; OK.");
            return(0);
        } 
        else 
        {
            print("   Error. Ack = %i",Value); 
            Error = 255;
        }
    }
    return(Error);
}

Proc CalcBRR
{
    var BootBRR,WorkBRR,Temp;
    Temp = 1536007680 / Crystal;
    Temp = Temp / 100;
    //Temp = Temp / 200;
    Temp = Int(Temp);
    //Temp = 24000/Crystal;
    //BootBRR = Temp;
    BootBRR = Temp/10;
    BootBRR--;
    Temp = 1919992320 / Crystal;
    Temp = Temp / 1000;
    Temp = Int(Temp);
    WorkBRR = Temp / 10;
    WorkBRR--;
    BootBRR = BootBRR << 16;
    BootBRR = BootBRR | WorkBRR;
    return(BootBRR);
}

Proc Int(Value)
{
    var Temp;
    Temp = Value / 10;
    Temp = Temp * 10;
    Temp = Value - Temp;
    if(Temp > 5) { Value = Value + 10;}
    return(Value);
}

Proc Info
{
  print("");
  print("  Module for programming Motorola MCU type MC68HC11KA4.");
  print("");
  print("  Next functions are support:");
  print("");
  print("     EEPROM read/write/modify/verify.");
  print("     ROM read.");
  print("--------------------------------------------------------------");
  print("  Note: special adapter are required !");
  print("--------------------------------------------------------------");
  print("  Copyright (C) 2007 ETL.");
}

[Device]

areg    DR,0
areg    CR,1
areg    SR,1
areg    BRRL,2
areg    BRRH,3
areg    VER,7
areg    IO,4

define      ROM_BEGIN       0xA000

var Value,Counter,Temp,Byte,AH,AL;

Proc CheckAdapter
{
    Value = VER;
    if(Value != 1)
    {
        Value = VER;
    }
    USB = Value;
}

Proc Reboot
{
    while
    {
        Temp = SR;
        if(Temp.1 = 0) { break;}
        Temp = DR;         
    }
    Value = USB;BRRH = Value;
    Value = USB;BRRL = Value;
    AH = USB;
    AL = USB;
    IO = 3; Delay(50000);   // Reset = 1
    IO = 0; Delay(50000);   // Reset = 0
    IO = 3;                 // Reset = 1
    Counter = 8;
    while
    {
        Temp = SR;
        if(Temp.1 = 1) 
        { 
            Temp = DR;    
        }
        Delay(10000);
        Counter--;
        if(Counter = 0) { break;}
    }    
    Byte = 255; TXByte;
    Counter = 0;
    while(Counter != 256)
    {
        Byte = USB; TXByte;
        WaitRXComplete;
        Byte = DR;
        USB = Byte;
        Counter++;
    }
    BRRH = AH;
    BRRL = AL;
    WaitRXComplete;
    Value = DR;
    USB = Value;
}

Proc ReadROM
{
    AL = ROM_BEGIN;
    while(AL != 0)
    {
        Byte = 'R'; TXByte;
        Byte = AL >> 8; TXByte;
        Byte = AL; TXByte;
        WaitRXComplete;
        Byte = DR;
        USB = Byte;
        AL++;
    }
}

Proc ReadEEPROMByte
{
    AH = USB;AL = USB;
    Byte = 'R'; TXByte;
    Byte = AH; TXByte;
    Byte = AL; TXByte;
    WaitRXComplete;
    Value = DR;
    USB = Value;
}

Proc WriteEEPROMByte
{
    AH = USB;AL = USB; Value = USB;
    Byte = 'W'; TXByte;
    Byte = AH; TXByte;
    Byte = AL; TXByte;
    Byte = Value; TXByte;
    WaitRXComplete;
    Value = DR;
    USB = Value;
}

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

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



