[Description]

Location = "Toshiba (TP)\TC89102P";
Info = " Module for programming 3-Wire Serial EEPROMs.",
       " Memory types: TC89102P (Toshiba).",
        " -----------------------------------------------------------------",
    "    Bus: MicroWire. Features Toshiba",
    "    Size: 256 bytes (128 words).",
        " -----------------------------------------------------------------",
    "    Author: ETL (C) 2005";

Buffer1 = "EEPROM",256;
Picture = "TP89X10X.bmp";
VCC = 8;
GND = 5;
Voltage = 5.0;
View1 = 16;

[Menu]

Read            = "&Read",ReadButton,"Reading chip",F2;
Div10           = "-";
Write           = "&Write",WriteButton,"Writing chip",F5;
WriteChanges    = "&Write changes",WriteChangesButton,"Write changes",F6;
Div             = "-";
Compare         = "&Verify",CheckButton,"Verify chip",F3;
Div1            = "-";
Erase           = "&Erase",EraseButton,"Erase chip",F9;
Div2            = "-";
Info            = "&About module",ModuleInfoButton,"Show module info",F1;

[Interface]

[Host]

// ************************************************************************
//       Host section
// ************************************************************************


define  END_ADR     256 

//**************************************************************************
//*  Read                                                                 **
//**************************************************************************
Proc Read {

  var Address,Temp;  
  
  print("");
  print("   Reading chip TC89101P...");
  PBPos = 0;
  PBMax = END_ADR;
  Run(Read);
  // ---------- Send to device mode byte
  USB = WordMode1;
  if(WordMode1 = 1) {   print("  16-bit mode"); }
  else              {   print("  8-bit mode"); }
  Address = 0;
  while(Address != END_ADR)
  {
    Temp = USB;
    if(Temp = 0x8000) { break; }
    FBUF[Address] = Temp;
    Address++; 
    PBPos++;
  }   
  if(Temp = 0x8000)
  {
    print("   Reading error. Time out.");
  }
  else
  {
    if(ForceCloseAfterRead != 0) { return(0x7FFFFFFF); }
    if(WordMode1 = 1) { Address = Address >> 1; }
    print("   Reading is done. %i cells were read",Address);
  }
}

//**************************************************************************
//*           Write                                                       **
//**************************************************************************
Proc Write
{
  var Address,Temp;

  print("");
  print("   Writing chip TC89101P...");
  PBPos = 0;
  Run(Write);
  // ---------- Send to device mode byte
  USB = WordMode1;
  if(WordMode1 = 1) {   print("  16-bit mode"); }
  else              {   print("  8-bit mode"); }
  PBMax = END_ADR;
  Address = 0;
  while(Address != END_ADR)
  {
    Temp = FBUF[Address];     // send to device data byte
    USB = Temp;
    Address++;
    PBPos++;
    if(WordMode1 = 1)
    {
        // in word mode send to device second byte of word
        Temp = FBUF[Address];
        USB = Temp;
        Address++;
        PBPos++;
    }
    // wait until byte(word) is programmed
    Temp = USB;
    if(Temp != 0) {break;}
  }
  if(Temp = 0x8000)
  {
    print("   Writing error. Time out.");
  }
  else
  {
    if(GlobalVerify != 0) { Compare(); }
    if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }
    if(WordMode1 = 1) { Address = Address >> 1; }
    print("   Writing is done. %i bytes were write.",Address);
  }
}

//**************************************************************************
//*  Compare                                                              **
//**************************************************************************
Proc Compare {

  var Address,Temp,Byte,Mismatch;

  print("");
  print("   Verifying chip TC89101P...");
  PBPos = 0;
  PBMax = END_ADR;
  Run(Read);
  // ---------- Send to device mode byte
  USB = WordMode1;
  if(WordMode1 = 1) {   print("  16-bit mode"); }
  else              {   print("  8-bit mode"); }
  Address = 0;
  Mismatch = 0;
  while(Address != END_ADR)
  {
    Temp = USB;
    if(Temp = 0x8000) { break; }
    Byte = FBUF[Address];
    if(Byte != Temp)
    {
        FSEL[Address] = 0;
        Mismatch++;
    }
    else             { FSEL[Address] = 1; }
    Address++;
    PBPos++;
  }
  if(Temp = 0x8000)
  {
    print("   Verifying error. Time out.");
  }
  else
  {
    if(ForceCloseAfterVerify != 0) { return(0x7FFFFFFF); }
    print("   Verifying is done. Number of mismatch %i",mismatch);
  }
}

Proc WriteChanges
{
    var Address,Value,Mismatch,Error,ValueH,Writed;
    Address = 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...");
        Writed = 0;
        if(WordMode1 = 1) 
        {   
            print("   16-bit mode"); 
            PBMax = END_ADR;
            PBPos = 0;
            Address = 0;
            while(Address != END_ADR)
            {
                Mismatch = FSEL[Address];
                Value = FBUF[Address];
                Address++;
                PBPos++;
                ValueH = FSEL[Address];
                Mismatch = Mismatch + ValueH;
                ValueH = FBUF[Address];

                if(Mismatch < 2)
                {
                    Run(WriteCell);
                    BeginBlock;
                    USB = WordMode1;
                    Error = Address >> 1;
                    USB = Error;
                    USB = Value;
                    USB = ValueH;
                    EndBlock;
                    Error = USB;
                    if(Error != 0) { break;}
                    Writed++;                  
                }
                Address++;
                PBPos++;
            }
        }
        else              
        {   
            print("   8-bit mode"); 
            PBMax = END_ADR;
            PBPos = 0;
            Address = 0;
            while(Address != END_ADR)
            {
                Mismatch = FSEL[Address];
                if(Mismatch = 0)
                {
                    Run(WriteCell);
                    BeginBlock;
                    USB = WordMode1;
                    USB = Address;
                    Value = FBUF[Address];
                    USB = Value;
                    EndBlock;
                    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 cells were write.",Writed); 
    }
    if(Error = 0x8000)
    {
        if(WordMode1 = 1) { Address = Address >> 1; }
        print("   Writing error. Time out. Address: %i",Address);
    }    
}

Proc Erase
{
    var Temp;
    print("");
    print("   Hardware erasing chip TC89101P...");
    Run(Erase);
    Temp = USB;
    if(Temp = 0)
    {
        if(ForceCloseAfterWrite != 0) { return(0x7FFFFFFF); }
        print("   Erasing is done.");
        print("");
        print("   Note: all cells of chip are zero (00) !");
    }
}

Proc Info {
  print("");
  print("  Module for programming a chips TC89102P (Toshiba).");
  print("");
  print("  Copyright (C) 2006 ETL.");
}

[Device]


// ************************************************************************
//          Device section
// ************************************************************************


define      CS      POUT.0
define      SK      POUT.1  
define      DI      POUT.2
define      DO      POUT.3
define      DOIN    PIN.3
define      ORG     POUT.5
define      RDYOUT  POUT.6
define      READY   PIN.6

define      END_ADR 256

var Mode,Address,Byte,Counter,EndAdr;

Proc Read
{
    POUT = 0b11001111;  // CS = 1;SK = 1;DO = 1;DI = 1;RDYOUT = 1;ORG = 0;   
    EndAdr = END_ADR;
    Mode = USB;        // get from host read mode (byte/word)
    if(Mode = 1) { EndAdr = EndAdr >> 1; }
    ORG = Mode.0;       // set pin ORG accordingly
    Address = 0;

        while(Address != EndAdr)
        {
            CS = 0;             // select chip
            Byte = Address;     // send to chip address byte
            Shout;
            Byte = 1;           // send to chip read instruction
            Shout;
            DO = 1;             // prepare to read
            Shin;               // get data byte from chip
            USB = Byte;        // send data byte to host
            if(Mode = 1)
            {
                // in word mode read second byte of word
                Shin;               // get data byte from chip
                USB = Byte;        // send data byte to host
            }
            Address++;
            CS = 1;             // release chip 
        }
}


Proc Write
{
    POUT = 0b11001111;  // CS = 1;SK = 1;DO = 1;DI = 1;RDYOUT = 1;ORG = 0;
    EndAdr = END_ADR;
    Mode = USB;        // get from host read mode (byte/word)
    if(Mode = 1) { EndAdr = EndAdr >> 1; }
    ORG = Mode.0;       // set pin ORG accordingly
    Address = 0;
    Ewen;               // enable write

    while(Address != EndAdr)
    {
        CS = 0;                     // select chip
        Byte = Address; Shout;      // send to chip address byte
        Byte = 0b00000110; Shout;   // send to chip write command
        Byte = USB; Shout;          // send to chip data byte
        if(Mode = 1)
        {
            // in word mode send to second byte of word
            Byte = USB; Shout;      // send to chip data byte
        }
        CS = 1;
        while(READY = 0) {}         // wait until device is ready
        USB = 0;                    // get from host next byte or word    
        Address++;
    }
}

// ************************************************************************
// *    Erase - hardware erasing chip                                    **
// ************************************************************************
Proc Erase
{
    POUT = 0b11001111;  // CS = 1;SK = 1;DO = 1;DI = 1;RDYOUT = 1;ORG = 0;
    Ewen;
    CS = 0;
    Byte = 255;Shout;
    Byte = 0b00001100;Shout;
    CS = 1;
    while(READY = 0) {}
    USB = 0;
}

Proc Shout
{

  Counter = 8;
  while(Counter != 0)
  {
    DI = Byte.0;
    SK = 0;
    SK = 1;
    Byte = Byte >> 1;
    Counter--;
  }
}

Proc Shin
{

  Counter = 8; 
  while(Counter != 0)
  {
    Byte = Byte >> 1;
    SK = 0;
    SK = 1;
    Byte.7 = DOIN;
    Counter--;
  }
}

Proc Ewen 
{
  CS = 0;DI = 1;
  Byte = 255;
  Shout;
  Byte = 0b00001001;
  Shout;
  CS = 1;  
}

Proc WriteCell
{
    Mode = USB;
    POUT = 0b11001111;  // CS = 1;SK = 1;DO = 1;DI = 1;RDYOUT = 1;ORG = 0;
    ORG = Mode.0;       // set pin ORG accordingly
    Address = USB;
    Ewen;               // enable write
    CS = 0;                     // select chip
    Byte = Address; Shout;      // send to chip address byte
    Byte = 0b00000110; Shout;   // send to chip write command
    Byte = USB; Shout;          // send to chip data byte
    if(Mode = 1)
    {
        // in word mode send to second byte of word
        Byte = USB; Shout;      // send to chip data byte
    }
    CS = 1;
    while(READY = 0) {}         // wait until device is ready
    USB = 0;                    // get from host next byte or word    
      
}


