[Description]
Location = "EEPROM\Misubishi\M6M80021";
Info = " Module for programming 3-Wire Serial EEPROMs",
	   " Memory types M6M80021",
       " ---------------------------------------------------------- ",
       "    Bus: MicroWire. Features: Mitsubishi",
       "    Size: 128 byte (64 words).",
       " ---------------------------------------------------------- ",
       "    Author: fomalex@biz.by (C) 2007";

Buffer1 = "EEPROM",256;
Picture = "M6M80021.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        = "-";
Compare     = "&Verify",CheckButton,"Compare with buffer",F3;
Divisor     = "-";
Info        = "&About module",ModuleInfoButton,"Show module info",F1;

[Interface]


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

[Host]

define  END_ADR 128

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

    print("");
    print("  Reading chip. ");
    PBPos = 0; PBMax = END_ADR;
    Run(Read);
    while(Counter != END_ADR)
    {
        Value = USB;FBUF[Address] = Value;
        Address++;
        Value = USB;FBUF[Address] = Value;
        Address++;
        Counter++;
        PBpos++;
    }
    if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF); }
    print("  Reading is done. %i words were read.",Counter);
}


Proc Write  {

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

Proc Compare {

    var Address,Error,Value,Value2,Mismatch,Counter;
    print("");
    print("   Verifying chip...");
    Run(Read);
    Address = 0;PBPos = 0;PBMax = END_ADR;
    Mismatch = 0;
    while(Counter != END_ADR)
    {
            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++;
    }
 
    if(ForceCloseAfterVerify != 0) { return(0x7FFFFFFF); }
    Mismatch = Mismatch / 2;
    print("   Verifying is done. %i words were read. Number of mismatch: %i",Counter,Mismatch);
}


Proc WriteChanges
{
    var Address,Temp,Value,Mismatch,Writed,Counter;
    Address = 0;Temp = 0;
    print("");
    while(Address != 256)
    {
        Value = FSEL[Address];
        if(Value = 0) {Temp++;}
        Address++;    
    }
    if(Temp = 0)
    {        
        print("   Any mismatch is not found. Writing is canceled.");
        return;
    }
    else
    {
        print("   Writing changes...");
        PBpos = 0; PBMax = END_ADR;Address = 0;Counter = 0;
        Mismatch = 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;
                Value = USB;
                Writed++;
            }
            Counter++;
            PBPos++;
        }
        if(GlobalVerify != 0) { Compare(); }
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }
        print("   Writing is done. %i words were write",Writed);
    }
}

Proc Info
{
  print("");
  print("  Module for programming 3-Wire eeprom types M6M80021.");
  print("");
  print("  Copyright (C) 2007 fomalex@biz.by.");
}

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

[Device]

define      CS          POUT.0
define      SK          POUT.1
define      DI          POUT.2
define      DO          POUT.3
define      DOIN        PIN.3
define      RESET       POUT.5
define		RDY			PIN.6

define      END_ADR     128    

define      READINS     0b00010101
define      WRITEINS    0b00100101
define      WREN		0b11000101                  


// Variables

var Address,Byte,Counter,DataH,DataL;

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

    POUT = 0b11101111;                  // init
                                        // CS=1, SK=1, DI=1, DO=1, RESET = 1
	while(RDY = 0) {}
    RESET = 0;
    while(Address != END_ADR)
    {
        CS = 0;                         // Select chip
         
    	Byte = READINS;ShiftOutByte;
    	Byte = Address;ShiftOutByte;
    	DO = 1;
    	ShiftInByte; USB = Byte;
    	ShiftInByte; USB = Byte;
    	CS = 1;
    	Address++;
    }
    RESET = 1;
}

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

Proc Write
{
    POUT = 0b11101111;                  // init
                                        // CS=1, SK=1, DI=1, DO=1, RESET = 1
	while(RDY = 0) {}
    RESET = 0;
	Address = USB;
	DataH = USB;
	DataL = USB;
	Reset = 0;
	CS = 0;	Byte = WREN; ShiftOutByte; 
	ShiftOutByte; 
	CS = 1;
	CS = 0; Byte = WRITEINS; ShiftOutByte;
	Byte = Address; ShiftOutByte;
	Byte = DataH;ShiftOutByte;
	Byte = DataL;ShiftOutByte;
	while(RDY = 0) {}
	CS = 1;
	RESET = 1;
	USB = 0;	 
}


Proc ShiftOutByte
{
    Counter = 8;                   // bit counter
    while(Counter != 0)
    {
        DI = Byte.0;           		// msb on the input data pin
        SK = 0;SK = 1;             	// rising edge SK
        Byte = Byte >> 1;   		// next bit
        Counter--;          
    }    
}

Proc ShiftInByte
{
    Counter = 8;                    // bit counter
    while(Counter != 0)
    {
        SK = 1;   SK = 0;           // falling edge SK
        Byte = Byte >> 1;   		// next bit
        Byte.7 = DOIN;
        Counter--;          
    }    
    SK = 1;
}




