[Description]
Location = "Microchip\EEPROM\MW EEPROM\ER59256";
Info = " Module for programming 3-Wire Serial EEPROMs",
       " Memory types: ER59256.",
       " ---------------------------------------------------------- ",
       "    Bus: MicroWire.",
       "    Size: 16 words.",
       " ",
       " !!!!!!!!!!!!!!!!  Very important !!!!!!!!!!!!!!!!1",
       "    PIN 6 MUST BE UNCONNECTED !!!",
       "    PIN 6 MUST BE UNCONNECTED !!!",
       "    PIN 6 MUST BE UNCONNECTED !!!",
       "    PIN 6 MUST BE UNCONNECTED !!!",
       "    PIN 6 MUST BE UNCONNECTED !!!",
       "    PIN 6 MUST BE UNCONNECTED !!!",
       " ---------------------------------------------------------- ",
       "    Author: fomalex@biz.by (C) 2007";

Buffer1 = "EEPROM",32;
Picture = "93CXX.bmp";
VCC = 8;
GND = 5;
View1 = 16;

Voltage = 5.0;

[Menu]

Read        = "&Read",ReadButton,"Read chip",F2;
Div10       = "-";
Write       = "&Write",WriteButton,"Write chip",F5;
WriteChanges= "Write &changes",WriteChangesButton,"Write changes",F6;
div1        = "-";
Erase       = "&Erase",EraseButton,"Erase chip",F9;
Compare     = "&Verify",CheckButton,"Compare with buffer",F3;
Divisor     = "-";
Info        = "&About module",ModuleInfoButton,"Show module info",F1;

[Interface]


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

[Host]

define  END_ADR 16

Proc Read {
    
    var Address,Error,Value,Counter;

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


Proc Write  {

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

Proc Compare {

    var Address,Error,Value,Value2,Mismatch,Counter;
    print("");
    print("  Verifying chip...");
    Address = 0;Counter = 0; PBPos = 0;PBMax = END_ADR;
    Run(Read);
    while(Counter != END_ADR)
    {
        Error = USB;
        if(Error = 0)
        {
            Value = USB;
            Value2 = FBUF[Address];
            if(Value != Value2) { Mismatch++; FSEL[Address] = 0; }
            else                { FSEL[Address] = 1; }
            Address++;
            Value = USB;
            Value2 = FBUF[Address];
            if(Value != Value2) { Mismatch++; FSEL[Address] = 0; }
            else                { FSEL[Address] = 1; }
            Address++;
            PBPos++;
            Counter++;
        }
        else { break; }
    }
    
    if(Error = 0x8000)
    {
        print("  Verifying error: Time out");
    }
    if(Error = 0)
    {
        if(ForcecloseAfterVerify != 0) { return(0x7FFFFFFF); }
        else
        {
            Mismatch = Mismatch / 2;
        	print("  Verifying is done. %i words were read. Number of mismatch: %i",Counter,Mismatch);
        }
    }
    if(Error = 0xFF)
    {
        print("  Verifying error. Chip not response. Address %i", Counter);
    }
}

Proc Erase
{
    var Address,Error;
    print("");
    print("  Erasing chip...");
    Run(Erase);
    Error = USB;
    if(Error = 0)
    {
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF);}
        else
        {
            print("  Erasing is done.");
        }
    }
    if(Error = 0x8000)
    {
        print("  Erasing error: Time out");
    }
}

Proc WriteChanges
{
    var Address,Temp,Value,Error,Mismatch,Writed,Counter;
    Address = 0;Temp = 0; Error = END_ADR * 2;
    print("");
    while(Address != Error)
    {
        Value = FSEL[Address];
        if(Value = 0) {Temp++;}
        Address++;    
    }
    if(Temp = 0)
    {        
        print("   Any mismatch is not found. Writing is canceled.");
        return;
    }
    print("   Writing changes...");
    PBpos = 0; PBMax = END_ADR;Address = 0;Error = 0;Counter = 0;
    Writed = 0;
    while(Counter != END_ADR)
    {
    	Mismatch = FSEL[Address];
        Value = FBUF[Address];                    
        Address++;
        Temp = FSEL[Address];
        Mismatch = Mismatch + Temp;
        Temp = FBUF[Address];
        Address++;
        if(Mismatch < 2)
        {
        	Run(Write);
            USB = Counter;
            USB = Value;
            USB = Temp;
            Error = USB;
            if(Error != 0) { break;}
            Writed++;
        }    
        PBPos++;
        Counter++;
    }
    if(Error = 0)
    {
    	if(GlobalVerify != 0) { Compare(); }
        print("   Writing is done. %i word were write.",writed);
        if(ForcecloseAfterWrite != 0) { return(0x7FFFFFFF); }
    }
    if(Error = 255)
    {
        print("   Writing error. Address %i",Counter);
    }
    if(Error = 0x8000)
    {
            print("   Writing error. Time out. Address %i",Counter);
    }
}

Proc Info
{
  print("");
  print("  Module for programming 3-Wire eeprom types ER59256.");
  print("  ----------------------------------------------------------------");
  print("  PIN 6 MUST BE UNCONNECTED !!!");
  print("  PIN 6 MUST BE UNCONNECTED !!!");
  print("  PIN 6 MUST BE UNCONNECTED !!!");
  print("  PIN 6 MUST BE UNCONNECTED !!!");
  print("  PIN 6 MUST BE UNCONNECTED !!!");
  print("  ----------------------------------------------------------------");
  print("  Copyright (C) 2007 fomalex@biz.by.");
}


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

[Device]

define      CS          POUT.0
define      CSIN		PIN.0	
define      SK          POUT.1
define      DI          POUT.2
define      DO          POUT.3
define      DOIN        PIN.3
define      ORG         POUT.5

define      END_ADR     16    

define      READINS     0b0000000110000000
define      WRITEINS    0b0000000101000000
define      ERALINS     0b0000000100100000
define      ERASEINS    0b0000000111000000
define      EWENINS     0b0000000100110000                  


// Variables

var Address,Error,Temp,DataWord,Counter,DataL,DataH,Mode,WordAddress,
    ControlWord;

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

    POUT = 0b00101100;                  // init
                                        // CS=0, SK=0, DI=1, DO=1, ORG=1
    ControlWord = READINS;

    while(Address != END_ADR)
    {
        CS = 1;                         // Select chip
        DataWord = ControlWord;
        ShiftOutWord;                   
        DO = 1;DI = 1;  
        if(DOIN != 0)
        {
            // chip not present
            // send to host error byte (0xFF) and break
            USB = 0xFF;break;
        }
        USB = 0x00;                    // send to host OK byte (0x00)
        ShiftInWord;                   // get data word from chip
        CS = 0;                        // release chip
        Temp = DataWord >> 8;
        USB = Temp;                // send to host low byte of data word
        USB = DataWord;                // send to host high byte of data word
        Address++;                     // next address
        ControlWord++;
    }
    CS = 0;
}

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

Proc Write
{
    Error = 0;Address = 0;

    POUT = 0b10001100;                  // init
                                        // CS=0, SK=0, DI=1, DO=1, TEST = 0
    Address = USB;
    DataH = USB;
    DataL = USB;

    DataWord = EWENINS;                 // enable erase/write 
    CS = 1;
    ShiftOutWord;                 
    CS = 0;

    DataWord = ERASEINS;                // erase cell 
    DataWord = DataWord | Address;
    CS = 1;
    ShiftOutWord;
    CS = 0;
    Delay(30000);

    DataWord = WRITEINS;
    DataWord = DataWord | Address;

    CS = 1;
    ShiftOutWord;
    DataWord = DataH << 8;
    DataWord = DataWord | DataL;
    ShiftOutWord;
    CS = 0;
    ShiftOutWord;
    Delay(30000);
    CS = 1;SK = 1;SK = 0; CS = 0;
    USB = 0;
}


Proc ShiftOutWord
{              
    Counter = 16;                   // bit counter
    while(Counter != 0)
    {
        DI = DataWord.15;           // msb on the input data pin
        SK = 1; SK = 0;             // rising edge SK
        DataWord = DataWord << 1;   // next bit
        Counter--;          
    }    
}

Proc ShiftInWord
{
    Counter = 16;                   // bit counter
    DataWord = 0;
    while(Counter != 0)
    {
        DataWord = DataWord << 1;   // next bit
        SK = 1;                             
        DataWord.0 = DOIN;       // next bit from bus into lowest bit DataWord
        SK = 0;
        Counter--;
    }
}


Proc Erase
{
    POUT = 0b11101100;                  // init
                                        // CS=0, SK=0, DI=1, DO=1, TEST=0
    CS = 1;
    DataWord = EWENINS;                 // Send to chip WRITE ENABLE command
    ShiftOutWord;
    CS = 0;

    CS = 1;
    DataWord = ERALINS;                 // Send to chip ERASE ALL command
    ShiftOutWord;
    CS = 0;
    Delay(30000);
    CS = 1;SK = 1;SK = 0;CS =0;
    USB = 0;
}

