[Description]

Location = "FLASH\Intel\P28F001";
Info = " Manufacturer: Intel",
       " Device: P28F001BX-T/B",
       " Memory size: 128 Kbytes (1 MBits)",
       " ",
       "  Note: special adapter are required!",
       " ",
       "  Supported functions:",
       "      Auto erase chip and auto program chip",
       "      Chip protect/unprotect",
       "      Chip read/verify",
       "------------------------------------------------",
       "  Copyright (C) ETL 2006";

Buffer1 = "FLASH",131072 ;
Voltage = 5.0;
Adapter = 1;

[Menu]

Read        = "&Read",ReadButton,"Read chip",F2;
Compare     = "&Verify",CheckButton,"Compare with buffer",F3;
Div0        = "-";
Write       = "&Write",WriteButton,"Write chip",F5;
Erase       = "&Erase",EraseButton,"Erase chip",F9;
Div1        = "-";
AutoDetect  = "Read &signature",ChipInfoButton,"Read signature bytes";
Div2        = "-";
CheckProtect= "Verify chip protect/unprotect";
Protect     = "Protect chip";
UnProtect   = "Unprotect chip";
Div3        = "-";
Info        = "&About module",ModuleInfoButton,"Show module info",F1;

[Interface]

CheckBox(EraseBeforeWrite,"Erase before write",10,10,1);
CheckBox(VerifyAfterErase,"Checking chip cleanness after erase",10,30,1);

[Host]


Proc Read 
{
    var Value,Address;
    print("");
    print("   Reading chip...");
    Value = 1;
    Address = 0;
    PBMax = 131072;
    Run(Read);
    while(Address != 131072) 
    {
        Value = USB;
        FBUF[Address] = Value;
        Address++;
        PBPos++;
    }
    if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF);}
    print("");
    print("   Reading is done. %i bytes were readed.",Address);      
}


Proc Compare 
{
    var Value,Address,Temp,Mismatch;
    print("");
    print("   Verifying chip...");
    Value = 1;
    Address = 0;
    PBMax = 131072;
    Mismatch = 0;
    Run(Read);
    while(Address != 131072) 
    {
        Value = USB;
        Temp = FBUF[Address];
        if(Temp != Value)
        {
            Mismatch++;
            FSEL[Address] = 0;    
        }
        else
        {
            FSEL[Address] = 1;
        }
        Address++;
        PBPos++;
    }
    if(Mismatch != 0)
    {
        print("");
        print("   Verifying is done. %i bytes were compared. Mismatch: %i.",Address,Mismatch);
        return;
    }
    if(ForceCloseAfterVerify != 0) { return(0x7FFFFFFF); }
}

Proc AutoDetect
{
    var MC,DC;
    Run(Detect);
    MC = USB;
    DC = USB;
    print("   Read signature....");
    print("");
    print("   Next code was readed: %b%b.",MC,DC);      
}

Proc CheckProtect
{
    var Value;
    print("");
    print("   Check protect status...");
    Run(CheckProtect);
    Value = USB;
    if(Value = 1)
    {
        print("\; Device is protected.");
    }
    else
    {
        print("\; Device is unprotected.");
    }
}

Proc Erase
{
    var Value,NotErase,Address;
    print("");
    print("   Erasing chip, wait please...");
    Run(EraseChip);
    while
    {
        USB = 1;
        Value = USB;
        if(Value.7 = 1) { break;}
    }
    USB = 0;
    print("\; Done.");
    if(VerifyAfterErase != 0)
    {
        print("");
        print("   Checking chip cleanness...");
        PBPos = 0;
        PBMax = 131072;
        NotErase = 0;
        Run(Read);
        while(Address != 131072) 
        {   
            Value = USB;
            if(Value != 255) { NotErase++; }
            PBPos++;
            Address++;
        }
        if(NotErase = 0)
        {
            print("\; Success.");   
        }
        else
        {
            print("\; Error. %i bytes not cleared.",NotErase);
        }
    }
}


Proc Write
{
    var Value,Address,Counter,NotErase;
    
    PBMax = 131072;

    print("");
    if(EraseBeforeWrite != 0)
    {
        //--------- Erase device
        print("   Erasing chip, wait please...");
        Run(EraseChip);
        while
        {
            USB = 1;
            Value = USB;
            if(Value.7 = 1) { break;}
        }
        USB = 0;
        print("\; Done.");
        if(VerifyAfterErase != 0)
        {
            print("   Checking chip cleanness...");
            PBPos = 0;
            NotErase = 0;
            Run(Read);
            while(Address != 131072) 
            {   
                Value = USB;
                if(Value != 255) { NotErase++; }
                PBPos++;
                Address++;
            }
            if(NotErase = 0)
            {
                print("\; Success.");   
            }
            else
            {
                print("\; Error. %i bytes not cleared.",NotErase);
                print("Programming canceled");
                return;
            }
        }
    }
    //-------- Write device
    print("   Writing chip...");
    Address = 0;
    PBPos = 0;
    Run(Write);
    while(Address != 131072)
    {
        Counter = 0;
        BeginBlock;
        while(Counter != 256)
        {
            Value = FBUF[Address];
            USB = Value;
            Counter++;
            Address++;
            if(Counter = 64) 
            { 
                EndBlock;
                Value = USB;
                BeginBlock; 
            }
            if(Counter = 128) 
            { 
                EndBlock;
                Value = USB;
                BeginBlock; 
            }
            if(Counter = 192)
            { 
                EndBlock;
                Value = USB;
                BeginBlock; 
            }
        }
        EndBlock;
        Value = USB;
        print(" Result: %i",Value);
        if(Value != 0)
        {
            Value = USB; Address = Value << 16;
            Value = USB; Value = Value << 8;Address = Address | Value;
            Value = USB; Address = Address | Value;

            print("\; Error. Address: %i",Address);
            Value = 1;
            break;
        }
        PBPos = Address;
    }
    if(Value = 0)
    {
        print("\; Success.");
        print("\; %i bytes writed and verified",Address);    
    }
}

Proc Protect
{
    var Counter,Temp;
    print("");
    print("   Protect chip, wait please...");
    Counter = 32;
    Run(LockUnlock);
    while(Counter != 0)
    {
        USB = 1;
        USB = 1;
        Temp = USB;
        if(Temp = 1) { break;}
        Counter--;
    }
    USB = 0;
    if(Temp = 1)
    {
        print("\; Success.");
    }
    else
    {
        print("\; Error. Device failed.");
    }
}

Proc Unprotect
{
    var Counter,Temp;
    print("");
    print("   Unprotect chip, wait please...");
    Counter = 1000;
    Run(LockUnlock);
    while(Counter != 0)
    {
        USB = 1;
        USB = 0;
        Temp = USB;
        if(Temp = 0) { break;}
        Counter--;
    }
    USB = 0;
    if(Temp = 0)
    {
        print("\; Success.");
    }
    else
    {
        print("\; Error. Device failed.");
    }
}

Proc Info
{
    print("  Module for programming CMOS FLASH memory.");
    print("  ---------------------------------------------");
    print("  Note: special adapter are required!");
    print("  ---------------------------------------------");
    print("  Memory type:  P28F001");
    print("  Manufacturer: Intel");
    print("  Supported functions:");
    print("      Auto erase chip and auto program chip");
    print("      Chip protect/unprotect");
    print("      Chip read/verify");
    print("  ---------------------------------------------");
    print("  Copyright (C) ETL 2007");
}


[Device]

ereg    ADRL,   0,1
ereg    ADRH,   1,1
ereg    CTRL,   2,1
ereg    DATAIN, 3,0    
ereg    DATAOUT,4,1
ereg    PWRCTRL,5,1

define  CS_AND_OE_LOW       0b11010111
define  CS_AND_OE_HIGH      0b00101000

define  OE  3
define  WE  4
define  CS  5
define  DE  7

var AH,AL,A16,Control,Value,TimeOut;

Proc Read
{
    PWRCTRL = 1;                            // VCC on pin 32
    Delay(5000);
    AH = 0;AL = 0;A16 = 0;DATAOUT = 255;
    Control = 0b11010000;                   // select chip for read
    while(A16 != 2)
    {
        Control = 0b11010000 | A16;CTRL = Control;AH = 0;
        while(AH != 256)
        {
            ADRH = AH;AL = 0;
            while(AL != 256)
            {
                ADRL = AL;Value = DATAIN;USB = Value;AL++;
            }
            AH++;
        }
        A16++;
    }
    CTRL = 0b11111000;                      // deselect chip
    PWRCTRL = 3;                            // power OFF
}

Proc Write
{
    PWRCTRL = 1;                            // VCC on pin 32
    AH = 0;AL = 0;A16 = 0;DATAOUT = 255;
    CTRL = 0b11010010;
    PWRCTRL = 0b00000101;                   // VPP on pin 1
    while(A16 != 2)
    {
        Control = 0b11010010 | A16;CTRL = Control;    // RP# = 1
        while(AH != 256)
        {
            // ---------- Get page form host into internal buffer
            ADRH = AH;
            AL = 0;
            while(AL != 256)
            {
                Value = USB;@AL = Value;
                AL++;
                if(AL = 64) { USB = 0;}
                if(AL = 128) { USB = 0;}
                if(AL = 192) { USB = 0;}
            }
            // ---------- Write page into chip
            AL = 0;
            while(AL != 256)
            {
                ADRL = AL;
                Value = @AL;
                DATAOUT = 0x40;WritePulse;
                DATAOUT = Value;WritePulse;
                // ---- Read status register
                DATAOUT = 255;    
                TimeOut = 2000;
                while
                {
                    TimeOut--;
                    Control = Control & CS_AND_OE_LOW;CTRL = Control;
                    Value = DATAIN;
                    Control = Control | CS_AND_OE_HIGH;CTRL = Control;
                    if(Value.7 = 1) { break;}
                    if(TimeOut = 0) { break;}
                }
                if(Value.7 = 0)
                {
                    // ---- error writing
                    USB = 255;
                    USB = A16;
                    USB = AH;
                    USB = AL;
                    PWRCTRL = 3;                            // power OFF    
                    return;
                }
                AL++;
            }
            AH++;
        }
    }
    PWRCTRL = 3;                            // power OFF    
}

Proc WritePulse
{
    Control.CS = 0;CTRL = Control;
    Control.WE = 0;CTRL = Control;
    Control.WE = 1;CTRL = Control;
    Control.CS = 1;CTRL = Control;
}
