[Description]
Location = "Sony\CXK1012P";
Info = " Module for programming 3-Wire serial EEPROMs memory types: CXK1012P.",
       " ---------------------------------------------------------- ",
       "    Bus: MicroWire. Features: Sony",
       "    Size: 128 byte (64 words).",
       " ---------------------------------------------------------- ",
       "    Author: ETL (C) 2005";

Buffer1 = "EEPROM",128;
Picture = "CXK101X.bmp";
VCC = 8;
GND = 5;

Voltage =5.0;
PinTest=10011000;

[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",F9;
div2    = "-";
Info    = "&About module",ModuleInfoButton,"Show module info",F1;

[Interface]


// ==================================================
//                  Host section
// ==================================================

[Host]

define  END_ADR 128

Proc Read {
    
    var Address,Error,Value;
    print("");
    print("  Reading chip...");
    Run(Read);
    Address = 0;PBPos = 0;PBMax = END_ADR;
    while(Address != END_ADR)
    {
        Value = USB;
        FBUF[Address] = Value;
        Address++;
        PBPos++;
    }
    if(Error = 0x8000)
    {
        print("  Reading error: Time out");
    }
    if(Error = 0)
    {
        if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF); }
        print("  Reading is done. %i words were read.",Address);
    }
}


Proc Write  {

    var Address,Error,Value;
    print("");
    print("  Writing chip...");
    Run(Write);
    Address = 0;PBPos = 0;PBMax = END_ADR;
    while(Address != END_ADR)
    {
        Value = FBUF[Address];
        USB = Value;
        Error = USB;
        if(Error != 0) { break; }
        Address++;
        PBPos++;
    }
    if(Error = 0x8000)
    {
        print("  Writing error: Time out");
    }
    if(Error = 0)
    {
        if(GlobalVerify != 0) { Compare(); }
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF);}
        print("  Writing is done. %i words were write.",Address);
    }
}

Proc Compare {

    var Address,Error,Value,Value2,Mismatch;
    print("");
    print("  Verifying chip...");
    Run(Read);
    Address = 0;PBPos = 0;PBMax = END_ADR;
    Mismatch = 0;
    while(Address != END_ADR)
    {
        Value = USB;
        Value2 = FBUF[Address];
        if(Value != Value2)
        {
            Mismatch++;
            FSEL[Address] = 0;    
        }
        else
        {
            FSEL[Address] = 1;
        }
        Address++;
        PBPos++;
    }
    if(Error = 0x8000)
    {
        print("  Verifying error: Time out");
    }
    if(Error = 0)
    {
        if(ForceCloseAfterVerify != 0) { return(0x7FFFFFFF); }
        print("  Verifying is done. %i bytes were compared. Number of mismatch: %i",Address,Mismatch);
    }
}


Proc WriteChanges
{
    var Address,Error,Value,Value2,Mismatch,Writed;
    Address = 0;
    Mismatch = 0;
    while(Address != END_ADR)
    {
        Value = FSEL[Address];
        if(Value = 0) {     Mismatch++; }
        Address++;
    }
    if(Mismatch = 0)
    {        
        print("   Any mismatch is not found. Writing is canceled.");
        return;
    }
    else
    {
        print("   Write changes...");
        PBMax = END_ADR;
        PBPos = 0;
        Address = 0;
        Writed = 0;
        while(Address != END_ADR)
        {
            Mismatch = FSEL[Address];
            if(Mismatch = 0)
            {
                Run(WriteByte);
                USB = Address;
                Value = FBUF[Address];
                USB = Value;
                Error = USB;
                if(Error != 0) { break; }
                Writed++;
            }
            Address++;
            PBPos++;
        }
        if(Error = 0)
        {
            if(GlobalVerify != 0) { Compare(); }
            if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF);}
            print("  Writing is done. %i words were write.",Writed);
        }
        if(Error = 0x8000)
        {
            print("   Writing error. Time out. Address: %i",Address);
        }
    }
}

Proc Info
{
  print("");
  print("  Module for programming 3-Wire eeprom types CXK1012P.");
  print("");
  print("  Copyright (C) 2006 ETL.");
}

// ========================================================
//                    Device section
// ========================================================

[Device]

define      CE          POUT.0
define      SCK         POUT.1
define      DI          POUT.2
define      DO          POUT.3
define      DOIN        PIN.3
define      OSC         POUT.5
define      BUSY        PIN.6
define      BUSYOUT     POUT.6

define      END_ADR     128    

define      READINS     0b0000000010100000
define      WRITEINS    0b0000000000100000

// Variables

var Address,Error,Temp,DataWord,Counter,DataL,DelayCounter;

//***********************************************************
//*       Read                                             **
//***********************************************************
Proc Read {
 
    Error = 0;Address = 0;

    POUT = 0b11111111;                  // init
                                        // CE=1, SCK=1, DI=1, DO=1, OSC=1
                                        // BUSYOUT = 1

    while(Address != END_ADR)
    {
        CE = 0;                         // select chip
        DataWord = Address;             // shift into chip
        ShiftOutByte;                   // address field
        DataWord = READINS;             // and READ instruction
        ShiftOutByte;                   
        DO = 1;DI = 1;                  // release bus
        ShiftInByte;                    // get data byte from chip
        CE = 1;                         // release chip
        USB = DataWord;                // send to host low byte of data word
        Address++;                      // next address
    }
}

//************************************************************
//*      Write                                              **
//************************************************************

Proc Write
{
    Error = 0;Address = 0;

    POUT = 0b11111111;                  // init
                                        // CE=1, SCK=1, DI=1, DO=1, OSC=1
                                        // BUSYOUT = 1

    while(Address != END_ADR)
    {
        DataL = USB;                    // get data byte from host
        CE = 0;                         // select chip
                                        // shift into chip
        DataWord = Address;             // address filed
        ShiftOutByte;                   
        DataWord = WRITEINS;            // WRITE instruction
        ShiftOutByte;
        DataWord = DataL;               // and data byte
        ShiftOutByte;

        // write control sequence

        SCK = 0; SCK = 1; Delay(15000);
        SCK = 0; SCK = 1; Delay(15000);

        CE = 1;                         // release chip
        USB = 0;
        Address++;
    }
}

Proc WriteByte
{
    Error = 0;

    POUT = 0b11111111;                  // init
                                        // CE=1, SCK=1, DI=1, DO=1, OSC=1
                                        // BUSYOUT = 1
    Address = USB;
    DataL = USB;

    CE = 0;                         // select chip
    DataWord = Address;             // address filed
    ShiftOutByte;                   
    DataWord = WRITEINS;            // WRITE instruction
    ShiftOutByte;
    DataWord = DataL;               // and data byte
    ShiftOutByte;

    // write control sequence

    SCK = 0; SCK = 1; Delay(15000);
    SCK = 0; SCK = 1; Delay(15000);

    CE = 1;                         // release chip
    USB = 0;
}


Proc ShiftOutByte
{
    Counter = 8;                    // bit counter
    while(Counter != 0)
    {
        SCK = 0;                    // falling edge SCK
        DI = DataWord.7;            // place current bit on the input data pin
        SCK = 1;                    // rising edge SCK
        DataWord = DataWord << 1;   // next bit
        Counter--;          
    }    
}


Proc ShiftInByte
{
    Counter = 8;                  // bit counter
    DataWord = 0;
    while(Counter != 0)
    {
        DataWord = DataWord << 1; // next bit
        SCK = 0;                  // falling edge SCK         
        DataWord.0 = DOIN;        // next bit from bus into lowest bit DataWord
        SCK = 1;                  // rising edge SCK
        Counter--;
    }
}

