Index: branches/MESI/modules/vci_mem_cache/caba/source/src/vci_mem_cache.cpp
===================================================================
--- branches/MESI/modules/vci_mem_cache/caba/source/src/vci_mem_cache.cpp	(revision 678)
+++ branches/MESI/modules/vci_mem_cache/caba/source/src/vci_mem_cache.cpp	(revision 680)
@@ -2844,4 +2844,5 @@
                                             nline,
                                             nb_copies,
+                                            false,
                                             index);
 
@@ -3241,5 +3242,5 @@
                             r_read_to_cc_send_brdcast_req   = not multi_req;
                             r_read_to_cc_send_nline         = nline;
-                            //r_read_to_cc_send_srcid         = m_cmd_read_srcid_fifo.read();
+                            r_read_to_cc_send_srcid         = m_cmd_read_srcid_fifo.read();
                             //r_read_to_cc_send_is_shared     = r_read_state.read() == ENTRY_SHARED;
 
@@ -3276,4 +3277,5 @@
                                       nline,
                                       r_read_count.read(),
+                                      true,             //is a command read
                                       index);
                                 
@@ -3449,5 +3451,5 @@
                                                            false,             // multi_inval  
                                                            true,              // it needs a read response
-                                                           false,              // acknowledge required
+                                                           false,             // acknowledge required
                                                            m_cmd_read_srcid_fifo.read(),
                                                            m_cmd_read_trdid_fifo.read(),
@@ -3455,4 +3457,5 @@
                                                            nline,
                                                            0x01,
+                                                           true,              // is a command read
                                                            index);
 
@@ -4139,13 +4142,13 @@
                             else // invalid the line and send multi_inval or brdcast to cache L1, and set trt table to evict the line
                             {
-                                if(entry.state == ENTRY_EXCLUSIVE)
-                                {
-                                    r_write_inval_trt_send = false;
-                                }
-                                else
-                                {
-                                    r_write_inval_trt_send = true;
-                                }
-                                r_write_fsm = WRITE_INVAL_TRT_LOCK;
+                               // if(entry.state == ENTRY_EXCLUSIVE)
+                               // {
+                               //     r_write_inval_trt_send = false;
+                               // }
+                               // else
+                               // {
+                               //     r_write_inval_trt_send = true;
+                               // }
+                                r_write_fsm = WRITE_INVAL_IVT_LOCK;
 
                             }
@@ -4155,14 +4158,14 @@
                     {
                         // if a miss write matched with a trt wait situation(wait for dirty data), it must block the command write until the dirty data has arrived in memcache
-                        if(r_write_to_cleanup_req.read() and 
-                           r_write_to_cleanup_nline.read() == nline) 
-                        {
-                            std::cout << "hit write cleanup nline =" << std::hex << nline << std::endl;
-                            r_write_fsm = WRITE_WAIT;
-                        }
-                        else
-                        {
+                       // if(r_write_to_cleanup_req.read() and 
+                       //    r_write_to_cleanup_nline.read() == nline) 
+                       // {
+                       //     std::cout << "hit write cleanup nline =" << std::hex << nline << std::endl;
+                       //     r_write_fsm = WRITE_WAIT;
+                       // }
+                       // else
+                       // {
                             r_write_fsm = WRITE_MISS_TRT_LOCK;
-                        }
+                       // }
                     }
 
@@ -4419,5 +4422,5 @@
                     m_heap.write(r_write_next_ptr.read(),last_entry);
 
-                    r_write_fsm = WRITE_INVAL_XRAM_REQ;  //this is sure that this line is in shared state, it can be send to xram
+                    r_write_fsm = WRITE_IDLE; 
 
 #if DEBUG_MEMC_XRAM_RSP
@@ -4904,12 +4907,14 @@
                             "MEMC ERROR in WRITE_INVAL_IVT_LOCK state: Bad DIR allocation");
 
-                    assert( (r_alloc_trt_fsm.read() == ALLOC_TRT_WRITE) and
-                            "MEMC ERROR in WRITE_INVAL_IVT_LOCK state: Bad TRT allocation");
-
-                    size_t set          = m_y[(addr_t)(r_write_address.read())];
+                    //assert( (r_alloc_trt_fsm.read() == ALLOC_TRT_WRITE) and
+                    //        "MEMC ERROR in WRITE_INVAL_IVT_LOCK state: Bad TRT allocation");
+                    
+                    size_t set         = m_y[(addr_t)(r_write_address.read())];
+                    size_t way         = r_write_way.read();
 
                     bool cc_send_valid = not r_write_to_cc_send_multi_req.read() and
-                                         not r_write_to_cc_send_brdcast_req.read() and  
-                                            (r_write_inval_trt_send.read() or not r_write_to_cleanup_req.read());
+                                         not r_write_to_cc_send_brdcast_req.read() and 
+                                         not (r_write_state.read() == ENTRY_EXCLUSIVE and r_write_to_cleanup_req.read());   
+                                           // (r_write_inval_trt_send.read() or not r_write_to_cleanup_req.read());
 
                     if(r_alloc_ivt_fsm.read() == ALLOC_IVT_WRITE)
@@ -4939,4 +4944,5 @@
                                             nline,
                                             nb_copies,
+                                            false,       // is a command write
                                             index);
                          /*//   std::cout << " trt wait nline =" << std::hex << nline 
@@ -4948,24 +4954,54 @@
                                 << " / nb_copies = " << r_write_count.read() << std::endl;
 #endif
+                           // for(size_t word=0 ; word<m_words ; word++)
+                           // {
+                           //     r_write_to_cleanup_be[word]   = r_write_be[word];
+                           //     r_write_to_cleanup_data[word] = r_write_data[word];
+
+                           //     data_t mask = 0;
+                           //     if(r_write_be[word].read() & 0x1) mask = mask | 0x000000FF;
+                           //     if(r_write_be[word].read() & 0x2) mask = mask | 0x0000FF00;
+                           //     if(r_write_be[word].read() & 0x4) mask = mask | 0x00FF0000;
+                           //     if(r_write_be[word].read() & 0x8) mask = mask | 0xFF000000;
+        
+                           //     // complete only if mask is not null (for energy consumption)
+                           //     r_write_data[word] =
+                           //         (r_write_data[word].read()         &  mask) |
+                           //         (m_cache_data.read(r_write_way.read(), set, word) & ~mask);
+                           ///*//     std::cout << std::hex 
+                           //              << ((r_write_data[word].read()         &  mask) |
+                           //         (m_cache_data.read(r_write_way.read(), set, word) & ~mask)) << std::endl;*/
+                           // }
+
+                            DirectoryEntry entry;
+                            entry.state         = ENTRY_LOCKED;
+                            entry.dirty         = true;
+                            entry.tag           = r_write_tag.read();
+                            entry.is_cnt        = r_write_is_cnt.read();
+                            entry.lock          = r_write_lock.read();
+                            entry.owner.srcid   = r_write_copy.read();
+                            entry.owner.inst    = r_write_copy_inst.read();
+                            entry.ptr           = 0;
+                            entry.count         = r_write_count.read();
+            
+                            m_cache_directory.write(set, way, entry);
+            
+                            if ((r_write_pktid.read() & 0x7) == TYPE_SC)
+                            {
+                                m_llsc_table.sc(r_write_address.read(),
+                                        r_write_sc_key.read());
+                            }
+
                             for(size_t word=0 ; word<m_words ; word++)
                             {
                                 r_write_to_cleanup_be[word]   = r_write_be[word];
                                 r_write_to_cleanup_data[word] = r_write_data[word];
-
-                                data_t mask = 0;
-                                if(r_write_be[word].read() & 0x1) mask = mask | 0x000000FF;
-                                if(r_write_be[word].read() & 0x2) mask = mask | 0x0000FF00;
-                                if(r_write_be[word].read() & 0x4) mask = mask | 0x00FF0000;
-                                if(r_write_be[word].read() & 0x8) mask = mask | 0xFF000000;
-        
-                                // complete only if mask is not null (for energy consumption)
-                                r_write_data[word] =
-                                    (r_write_data[word].read()         &  mask) |
-                                    (m_cache_data.read(r_write_way.read(), set, word) & ~mask);
-                           /*//     std::cout << std::hex 
-                                         << ((r_write_data[word].read()         &  mask) |
-                                    (m_cache_data.read(r_write_way.read(), set, word) & ~mask)) << std::endl;*/
+                                
+                                if(r_write_state.read() == ENTRY_SHARED)
+                                {
+                                    m_cache_data.write(way, set, word, r_write_data[word].read(), r_write_be[word].read());
+                                }
                             }
-                            r_write_fsm     = WRITE_DIR_INVAL;
+                            r_write_fsm     = WRITE_INVAL_CC_SEND;
                         }
                         else
@@ -4986,33 +5022,33 @@
                             "MEMC ERROR in WRITE_DIR_INVAL state: Bad DIR allocation");
 
-                    assert( (r_alloc_trt_fsm.read() == ALLOC_TRT_WRITE) and
-                            "MEMC ERROR in WRITE_DIR_INVAL state: Bad TRT allocation");
+                  //  assert( (r_alloc_trt_fsm.read() == ALLOC_TRT_WRITE) and
+                  //          "MEMC ERROR in WRITE_DIR_INVAL state: Bad TRT allocation");
 
                     assert( (r_alloc_ivt_fsm.read() == ALLOC_IVT_WRITE) and
                             "MEMC ERROR in WRITE_DIR_INVAL state: Bad IVT allocation");
 
-                    // register PUT request in TRT
-                    std::vector<data_t> data_vector;
-                    data_vector.clear();
-                    for(size_t i=0; i<m_words; i++) data_vector.push_back(r_write_data[i].read());
-
-                    m_trt.set( r_write_trt_index.read(),
-                               false,             // PUT request
-                               m_nline[(addr_t)(r_write_address.read())],
-                               0,                 // unused
-                               0,                 // unused
-                               0,                 // unused
-                               false,             // not a processor read
-                               0,                 // unused
-                               0,                 // unused
-                               std::vector<be_t> (m_words,0),
-                               data_vector );
+                  // register PUT request in TRT
+                  //  std::vector<data_t> data_vector;
+                  //  data_vector.clear();
+                  //  for(size_t i=0; i<m_words; i++) data_vector.push_back(r_write_data[i].read());
+
+                  //  m_trt.set( r_write_trt_index.read(),
+                  //             false,             // PUT request
+                  //             m_nline[(addr_t)(r_write_address.read())],
+                  //             0,                 // unused
+                  //             0,                 // unused
+                  //             0,                 // unused
+                  //             false,             // not a processor read
+                  //             0,                 // unused
+                  //             0,                 // unused
+                  //             std::vector<be_t> (m_words,0),
+                  //             data_vector );
 
                     // invalidate directory entry
                     DirectoryEntry entry;
-                    entry.state         = ENTRY_INVALID;
-                    entry.dirty         = false;
-                    entry.tag           = 0;
-                    entry.is_cnt        = false;
+                    entry.state         = ENTRY_LOCKED;
+                    entry.dirty         = true;
+                    entry.tag           = r_write_tag.read();
+                    entry.is_cnt        = 0;
                     entry.lock          = false;
                     entry.owner.srcid   = 0;
@@ -5049,4 +5085,5 @@
                         bool not_last_multi_inval      = (r_write_count.read()  != 1) and not r_write_is_cnt.read();
                         bool multi_inval               = not r_write_is_cnt.read();
+
                         r_write_to_cc_send_multi_req   = last_multi_inval;
                         r_write_to_cc_send_brdcast_req = not multi_inval;
@@ -5057,4 +5094,10 @@
                         r_write_next_ptr               = r_write_ptr.read();
 
+                        if(r_write_state.read() == ENTRY_EXCLUSIVE)
+                        {
+                            r_write_to_cleanup_req   = true;
+                            r_write_to_cleanup_nline = m_nline[(addr_t)(r_write_address.read())]; 
+                        }
+
                         if( not last_multi_inval and multi_inval)
                         {
@@ -5063,15 +5106,14 @@
                         else
                         {
-                            if(r_write_inval_trt_send.read())
-                            {
-                                r_write_fsm = WRITE_INVAL_XRAM_REQ;
-                            }
-                            else //the line is in exlusive state, must wait for the 
-                                 //response of multi_inval in reason of receiving the updated data
-                            {
+                           // if(r_write_inval_trt_send.read())
+                           // {
+                           //     r_write_fsm = WRITE_INVAL_XRAM_REQ;
+                           // }
+                           // else //the line is in exlusive state, must wait for the 
+                           //      //response of multi_inval in reason of receiving the updated data
+                           // {
                                 r_write_fsm              = WRITE_IDLE;
-                                r_write_to_cleanup_req   = true;
-                                r_write_to_cleanup_nline = m_nline[(addr_t)(r_write_address.read())]; 
-                            }
+
+                           // }
                         }
 #if DEBUG_MEMC_WRITE
@@ -5904,4 +5946,5 @@
                                                r_xram_rsp_victim_nline.read(),
                                                count_copies,
+                                               false,      //unuse
                                                index);
 
@@ -6642,5 +6685,5 @@
                             << " CLEANUP_LOCKED_IVT_LOCK> :"
                             << "/ is_updt =" << m_ivt.is_update(index)
-                            << "/ is_change =" << m_ivt.is_change(index)
+                            << "/ is_changed =" << m_ivt.is_changed(index)
                             << "/ match =" << match_inval
                             << "/ need rsp  =" << m_ivt.need_rsp(index)
@@ -6654,7 +6697,8 @@
                         r_cleanup_locked_index      = index;
                         r_cleanup_locked_is_updt    = m_ivt.is_update(index);
-                        r_cleanup_locked_is_change  = m_ivt.is_change(index);
-
-                        if( m_ivt.is_update(index) and not m_ivt.is_change(index))
+                        r_cleanup_locked_is_changed = m_ivt.is_changed(index);
+                        r_cleanup_locked_is_read    = m_ivt.is_read(index);
+
+                        if( m_ivt.is_update(index) and not m_ivt.is_changed(index))
                         {
                             r_cleanup_fsm = CLEANUP_LOCKED_IVT_CLEAR;
@@ -6712,9 +6756,12 @@
 
                     addr_t cleanup_address = r_cleanup_nline.read() * m_words * 4;
-                    size_t match_cas_multi_inval = r_cas_to_cleanup_req.read() and
-                                                  (r_cas_to_cleanup_nline.read() == r_cleanup_nline.read()); 
+                    //size_t match_cas_multi_inval = r_cas_to_cleanup_req.read() and
+                    //                              (r_cas_to_cleanup_nline.read() == r_cleanup_nline.read()); 
+
+                    size_t match_cas_inval = (r_cleanup_locked_pktid.read() & 0x7) == TYPE_CAS;
 
                     bool match_ll = ((r_cleanup_locked_pktid.read() & 0x07) == TYPE_LL);
-                    DirectoryEntry  entry;
+
+                    DirectoryEntry      entry;
                     entry.is_cnt        = false;
                     entry.lock          = r_cleanup_lock.read();
@@ -6739,5 +6786,5 @@
                     }
 
-                    if(r_cleanup_locked_is_updt.read() and not r_cleanup_locked_is_change.read()) //cleanup req cross with multi ack rsp. here, just decrements the count and remains the state.
+                    if(r_cleanup_locked_is_updt.read() and not r_cleanup_locked_is_changed.read()) //cleanup req cross with multi ack rsp. here, just decrements the count and remains the state.
                     {
                         assert(r_cleanup_count.read() == 1 && " only one copy when cleanup fsm receives a req for is_updt command");
@@ -6748,5 +6795,5 @@
                         r_cleanup_fsm       = CLEANUP_SEND_CLACK;
                     }
-                    else if(r_cleanup_locked_is_updt.read() and r_cleanup_locked_is_change.read())
+                    else if(r_cleanup_locked_is_updt.read() and r_cleanup_locked_is_changed.read())
                     {
                         entry.state = ENTRY_SHARED;
@@ -6762,5 +6809,5 @@
                         }
 
-                        if((r_cleanup_locked_pktid.read() & 0x7) == 0x03) 
+                        if((r_cleanup_locked_pktid.read() & 0x7) == TYPE_READ_INS_MISS) 
                         {
                             entry.owner.inst = true;
@@ -6776,15 +6823,21 @@
                     else // cas, getm, one copy updated in dir
                     {
-                        if(match_cas_multi_inval)
+                        if(match_cas_inval)
                         {
                             entry.state     = ENTRY_SHARED;
                             entry.count     = 0;
                         }
+                        else if(r_cleanup_locked_is_read.read())
+                        {
+                            entry.state         = ENTRY_EXCLUSIVE;
+                            entry.count         = 1;
+                            entry.owner.srcid   = r_cleanup_locked_srcid.read();
+                        }
                         else
                         {
                             entry.state     = ENTRY_EXCLUSIVE;
-                            entry.count     = 1;
-                        }
-                        entry.owner.srcid   = r_cleanup_locked_srcid.read();
+                            entry.count     = 0;
+                        }
+
                         entry.owner.inst    = 0;
                         r_cleanup_fsm       = CLEANUP_LOCKED_RSP;
@@ -6814,4 +6867,6 @@
                     if(r_cleanup_to_tgt_rsp_req.read()) break;
 
+                    size_t match_cas_inval = (r_cleanup_locked_pktid.read() & 0x7) == TYPE_CAS;
+
                     r_cleanup_to_tgt_rsp_req     = true;
                     r_cleanup_to_tgt_rsp_srcid   = r_cleanup_locked_srcid.read();
@@ -6819,5 +6874,5 @@
                     r_cleanup_to_tgt_rsp_nline   = r_cleanup_nline.read();
                     
-                    if(r_cleanup_locked_is_change.read() and r_cleanup_locked_is_updt.read())
+                    if(r_cleanup_locked_is_changed.read() and r_cleanup_locked_is_updt.read())
                     {
                         r_cleanup_to_tgt_rsp_pktid   = r_cleanup_locked_pktid.read(); // rsp for is_updt in shared state
@@ -6830,5 +6885,5 @@
 
                     if(r_read_to_multi_ack_req.read() and (r_read_to_multi_ack_nline.read() == r_cleanup_nline.read()) and // rsp for LL or pheriph
-                      (r_cleanup_locked_is_change.read() and r_cleanup_locked_is_updt.read()))
+                      (r_cleanup_locked_is_changed.read() and r_cleanup_locked_is_updt.read()))
                     {
                         r_cleanup_to_tgt_rsp_ll_key     = r_read_to_multi_ack_ll_key.read(); 
@@ -6851,13 +6906,8 @@
                     }
 
-                    if(r_cas_to_cleanup_req.read())
-                    {
-                        r_cas_to_cleanup_req        = false;
-                        r_cleanup_to_tgt_rsp_type   = true;
-                    }
-                    else
-                    {
-                        r_cleanup_to_tgt_rsp_type   = false;
-                    }
+                    if(r_write_to_cleanup_req.read())
+                        r_write_to_cleanup_req = false;
+
+                    r_cleanup_to_tgt_rsp_type   = not r_cleanup_locked_is_read.read();
 
                     for(size_t i = 0; i<m_words; i++)
@@ -7224,5 +7274,6 @@
                         config_rsp_lines_cleanup_decr = true;
                     }
-
+                    
+                    assert(r_cleanup_miss_need_rsp.read() == false && "THERE IS A CLEANUP MISS NEEDS RSP");
                     if (r_cleanup_miss_need_rsp.read())         
                     {
@@ -7293,8 +7344,8 @@
                             r_cleanup_to_ixr_cmd_req     = true;
 
-                            if( (r_write_to_cleanup_req == true) and (r_write_to_cleanup_nline.read() == r_cleanup_nline.read()) )
-                            {
-                                r_write_to_cleanup_req   = false;
-                            }
+                           // if( (r_write_to_cleanup_req == true) and (r_write_to_cleanup_nline.read() == r_cleanup_nline.read()) )
+                           // {
+                           //     r_write_to_cleanup_req   = false;
+                           // }
 
                             if (r_cleanup_contains_data.read())
@@ -7685,5 +7736,4 @@
                         not r_cas_to_cc_send_multi_req.read()   and
                         not r_cas_to_cc_send_brdcast_req.read() and 
-                        not r_cas_to_cleanup_req.read()         and
                             m_cas_to_cc_send_inst_fifo.wok())
                     {
@@ -7695,12 +7745,13 @@
                         addr_t      nline           = m_nline[(addr_t)(m_cmd_cas_addr_fifo.read())];
                         size_t      nb_copies_sent  = 0;
-                        if(r_cas_state.read() == ENTRY_EXCLUSIVE)
-                        {
-                            nb_copies_sent = 1;
-                        }
-                        else
-                        {  
+
+                       // if(r_cas_state.read() == ENTRY_EXCLUSIVE)
+                       // {
+                       //     nb_copies_sent = 1;
+                       // }
+                       // else
+                       // {  
                             nb_copies_sent = r_cas_count.read(); 
-                        }
+                       // }
 //                        if(m_ivt.search_inval(nline, index) and r_cas_to_cleanup_req.read())
 //                        {
@@ -7723,4 +7774,5 @@
                                         nline,
                                         nb_copies_sent,
+                                        false,   // is a command write
                                         index);
                         if(wok)   // coherence transaction registered in ivt
@@ -7773,6 +7825,6 @@
                          
                                 r_cas_to_cc_send_nline        = m_nline[(addr_t)(m_cmd_cas_addr_fifo.read())];
-                                r_cas_to_cleanup_req          = true;
-                                r_cas_to_cleanup_nline        = m_nline[(addr_t)(m_cmd_cas_addr_fifo.read())];
+                                //r_cas_to_cleanup_req          = true;
+                                //r_cas_to_cleanup_nline        = m_nline[(addr_t)(m_cmd_cas_addr_fifo.read())];
                                 r_cas_fsm        = CAS_IDLE; 
                                 cmd_cas_fifo_get = true;
@@ -7904,7 +7956,7 @@
                         if(entry.next == r_cas_next_ptr.read())    // last copy
                         {
-                            r_cas_to_cleanup_nline      = nline;
+                            //r_cas_to_cleanup_nline      = nline;
                             r_cas_to_cc_send_multi_req  = true;
-                            r_cas_to_cleanup_req        = true;
+                            //r_cas_to_cleanup_req        = true;
                             r_cas_fsm                   = CAS_HEAP_LAST;   // Response will be sent after receiving
                         }   
@@ -8020,4 +8072,5 @@
                                         nline,
                                         nb_copies,
+                                        false,  // is a command write
                                         index);
 
@@ -11614,10 +11667,10 @@
 
                     DspinDhccpParam::dspin_set( flit,
-                            m_broadcast_boundaries,
-                            DspinDhccpParam::BROADCAST_BOX);
-
-                   // DspinDhccpParam::dspin_set( flit,
-                   //         r_read_to_cc_send_srcid.read(),    // send the srcid itself which demands the request getM
-                   //         DspinDhccpParam::BROADCAST_SRCID);
+                             m_broadcast_boundaries,
+                             DspinDhccpParam::BROADCAST_BOX);
+
+                     DspinDhccpParam::dspin_set( flit,
+                             r_read_to_cc_send_srcid.read(),    // send the srcid itself which demands the request getM
+                             DspinDhccpParam::BROADCAST_SRCID);
 
                    // DspinDhccpParam::dspin_set( flit,
