source: trunk/modules/vci_cc_vcache_wrapper/caba/source/src/vci_cc_vcache_wrapper.cpp @ 771

Last change on this file since 771 was 752, checked in by meunier, 10 years ago
  • Added initialization to some components in order to avoid valgrind errors
  • Corrected two bugs which caused the simulations to be non-deterministic:
    • one in the memcache (possibly uninitialized paddr variable used for cache access, modifying LRU bits)
    • one in the run_simus.py script (replacing a file with an identical file in a hdd image does modify its size)
File size: 240.9 KB
Line 
1/* -*- c++ -*-
2 * File : vci_cc_vcache_wrapper.cpp
3 * Copyright (c) UPMC, Lip6, SoC
4 * Authors : Alain GREINER, Yang GAO
5 *
6 * SOCLIB_LGPL_HEADER_BEGIN
7 *
8 * This file is part of SoCLib, GNU LGPLv2.1.
9 *
10 * SoCLib is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published
12 * by the Free Software Foundation; version 2.1 of the License.
13 *
14 * SoCLib is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with SoCLib; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301 USA
23 *
24 * SOCLIB_LGPL_HEADER_END
25 *
26 * Maintainers: cesar.fuguet-tortolero@lip6.fr
27 *              alexandre.joannou@lip6.fr
28 */
29
30#include <cassert>
31#include <signal.h>
32
33#include "arithmetics.h"
34#include "../include/vci_cc_vcache_wrapper.h"
35
36#define DEBUG_DCACHE            1
37#define DEBUG_ICACHE            1
38#define DEBUG_CMD               0
39
40namespace soclib {
41namespace caba {
42
43namespace {
44const char *icache_fsm_state_str[] = {
45        "ICACHE_IDLE",
46
47        "ICACHE_XTN_TLB_FLUSH",
48        "ICACHE_XTN_CACHE_FLUSH",
49        "ICACHE_XTN_CACHE_FLUSH_GO",
50        "ICACHE_XTN_TLB_INVAL",
51        "ICACHE_XTN_CACHE_INVAL_VA",
52        "ICACHE_XTN_CACHE_INVAL_PA",
53        "ICACHE_XTN_CACHE_INVAL_GO",
54
55        "ICACHE_TLB_WAIT",
56
57        "ICACHE_MISS_SELECT",
58        "ICACHE_MISS_CLEAN",
59        "ICACHE_MISS_WAIT",
60        "ICACHE_MISS_DATA_UPDT",
61        "ICACHE_MISS_DIR_UPDT",
62
63        "ICACHE_UNC_WAIT",
64
65        "ICACHE_CC_CHECK",
66        "ICACHE_CC_UPDT",
67        "ICACHE_CC_INVAL",
68    };
69
70const char *dcache_fsm_state_str[] = {
71        "DCACHE_IDLE",
72
73        "DCACHE_TLB_MISS",
74        "DCACHE_TLB_PTE1_GET",
75        "DCACHE_TLB_PTE1_SELECT",
76        "DCACHE_TLB_PTE1_UPDT",
77        "DCACHE_TLB_PTE2_GET",
78        "DCACHE_TLB_PTE2_SELECT",
79        "DCACHE_TLB_PTE2_UPDT",
80        "DCACHE_TLB_LR_UPDT",
81        "DCACHE_TLB_LR_WAIT",
82        "DCACHE_TLB_RETURN",
83
84        "DCACHE_XTN_SWITCH",
85        "DCACHE_XTN_SYNC",
86        "DCACHE_XTN_IC_INVAL_VA",
87        "DCACHE_XTN_IC_FLUSH",
88        "DCACHE_XTN_IC_INVAL_PA",
89        "DCACHE_XTN_IC_PADDR_EXT",
90        "DCACHE_XTN_IT_INVAL",
91        "DCACHE_XTN_DC_FLUSH",
92        "DCACHE_XTN_DC_FLUSH_GO",
93        "DCACHE_XTN_DC_INVAL_VA",
94        "DCACHE_XTN_DC_INVAL_PA",
95        "DCACHE_XTN_DC_INVAL_END",
96        "DCACHE_XTN_DC_INVAL_GO",
97        "DCACHE_XTN_DT_INVAL",
98
99        "DCACHE_DIRTY_GET_PTE",
100        "DCACHE_DIRTY_WAIT",
101
102        "DCACHE_MISS_SELECT",
103        "DCACHE_MISS_CLEAN",
104        "DCACHE_MISS_WAIT",
105        "DCACHE_MISS_DATA_UPDT",
106        "DCACHE_MISS_DIR_UPDT",
107
108        "DCACHE_UNC_WAIT",
109        "DCACHE_LL_WAIT",
110        "DCACHE_SC_WAIT",
111
112        "DCACHE_CC_CHECK",
113        "DCACHE_CC_UPDT",
114        "DCACHE_CC_INVAL",
115
116        "DCACHE_INVAL_TLB_SCAN",
117    };
118
119const char *cmd_fsm_state_str[] = {
120        "CMD_IDLE",
121        "CMD_INS_MISS",
122        "CMD_INS_UNC",
123        "CMD_DATA_MISS",
124        "CMD_DATA_UNC_READ",
125        "CMD_DATA_UNC_WRITE",
126        "CMD_DATA_WRITE",
127        "CMD_DATA_LL",
128        "CMD_DATA_SC",
129        "CMD_DATA_CAS",
130    };
131
132const char *vci_pktid_type_str[] = {
133        "TYPE_DATA_UNC",
134        "TYPE_READ_DATA_MISS",           
135        "TYPE_READ_INS_UNC",         
136        "TYPE_READ_INS_MISS",
137        "TYPE_WRITE",
138        "TYPE_CAS",
139        "TYPE_LL",
140        "TYPE_SC",
141    };
142
143const char *vci_cmd_type_str[] = {
144        "NOP or STORE_COND",
145        "READ",
146        "WRITE",
147        "LOCKED_READ"
148    };
149
150const char *rsp_fsm_state_str[] = {
151        "RSP_IDLE",
152        "RSP_INS_MISS",
153        "RSP_INS_UNC",
154        "RSP_DATA_MISS",
155        "RSP_DATA_UNC",
156        "RSP_DATA_LL",
157        "RSP_DATA_WRITE",
158    };
159
160const char *cc_receive_fsm_state_str[] = {
161        "CC_RECEIVE_IDLE",
162        "CC_RECEIVE_BRDCAST_HEADER",
163        "CC_RECEIVE_BRDCAST_NLINE",
164        "CC_RECEIVE_INS_INVAL_HEADER",
165        "CC_RECEIVE_INS_INVAL_NLINE",
166        "CC_RECEIVE_INS_UPDT_HEADER",
167        "CC_RECEIVE_INS_UPDT_NLINE",
168        "CC_RECEIVE_INS_UPDT_DATA",
169        "CC_RECEIVE_DATA_INVAL_HEADER",
170        "CC_RECEIVE_DATA_INVAL_NLINE",
171        "CC_RECEIVE_DATA_UPDT_HEADER",
172        "CC_RECEIVE_DATA_UPDT_NLINE",
173        "CC_RECEIVE_DATA_UPDT_DATA",
174    };
175
176const char *cc_send_fsm_state_str[] = {
177        "CC_SEND_IDLE",
178        "CC_SEND_CLEANUP_1",
179        "CC_SEND_CLEANUP_2",
180        "CC_SEND_MULTI_ACK",
181    };
182}
183
184#define tmpl(...) \
185   template<typename vci_param, \
186            size_t   dspin_in_width, \
187            size_t   dspin_out_width, \
188            typename iss_t> __VA_ARGS__ \
189   VciCcVCacheWrapper<vci_param, dspin_in_width, dspin_out_width, iss_t>
190
191using namespace soclib::common;
192
193/////////////////////////////////
194tmpl(/**/)::VciCcVCacheWrapper(
195    sc_module_name                      name,
196    const int                           proc_id,
197    const MappingTable      &mtd,
198    const IntTab                &srcid,
199    const size_t                cc_global_id,
200    const size_t                        itlb_ways,
201    const size_t                        itlb_sets,
202    const size_t                        dtlb_ways,
203    const size_t                        dtlb_sets,
204    const size_t                        icache_ways,
205    const size_t                        icache_sets,
206    const size_t                        icache_words,
207    const size_t                        dcache_ways,
208    const size_t                        dcache_sets,
209    const size_t                        dcache_words,
210    const size_t                        wbuf_nlines,
211    const size_t                        wbuf_nwords,
212    const size_t                        x_width,
213    const size_t                        y_width,
214    const uint32_t                      max_frozen_cycles,
215    const uint32_t                      debug_start_cycle,
216    const bool                          debug_ok )
217    : soclib::caba::BaseModule(name),
218
219      p_clk("p_clk"),
220      p_resetn("p_resetn"),
221      p_vci("p_vci"),
222      p_dspin_m2p("p_dspin_m2p"),
223      p_dspin_p2m("p_dspin_p2m"),
224      p_dspin_clack("p_dspin_clack"),
225
226      m_cacheability_table( mtd.getCacheabilityTable() ),
227      m_srcid( mtd.indexForId(srcid) ),
228      m_cc_global_id( cc_global_id ),
229      m_nline_width( vci_param::N - (uint32_log2(dcache_words)) - 2 ),
230      m_itlb_ways( itlb_ways ),
231      m_itlb_sets( itlb_sets ),
232      m_dtlb_ways( dtlb_ways ),
233      m_dtlb_sets( dtlb_sets ),
234      m_icache_ways( icache_ways ),
235      m_icache_sets( icache_sets ),
236      m_icache_yzmask( (~0)<<(uint32_log2(icache_words) + 2) ),
237      m_icache_words( icache_words ),
238      m_dcache_ways( dcache_ways ),
239      m_dcache_sets( dcache_sets ),
240      m_dcache_yzmask( (~0)<<(uint32_log2(dcache_words) + 2) ),
241      m_dcache_words( dcache_words ),
242      m_x_width( x_width ),
243      m_y_width( y_width ),
244      m_proc_id( proc_id ),
245      m_max_frozen_cycles( max_frozen_cycles ),
246      m_paddr_nbits( vci_param::N ),
247      m_debug_start_cycle( debug_start_cycle ),
248      m_debug_ok( debug_ok ),
249      m_dcache_paddr_ext_reset(0),
250      m_icache_paddr_ext_reset(0),
251
252      r_mmu_ptpr("r_mmu_ptpr"),
253      r_mmu_mode("r_mmu_mode"),
254      r_mmu_word_lo("r_mmu_word_lo"),
255      r_mmu_word_hi("r_mmu_word_hi"),
256      r_mmu_ibvar("r_mmu_ibvar"),
257      r_mmu_dbvar("r_mmu_dbvar"),
258      r_mmu_ietr("r_mmu_ietr"),
259      r_mmu_detr("r_mmu_detr"),
260
261      r_icache_fsm("r_icache_fsm"),
262      r_icache_fsm_save("r_icache_fsm_save"),
263      r_icache_vci_paddr("r_icache_vci_paddr"),
264      r_icache_vaddr_save("r_icache_vaddr_save"),
265
266      r_icache_miss_way("r_icache_miss_way"),
267      r_icache_miss_set("r_icache_miss_set"),
268      r_icache_miss_word("r_icache_miss_word"),
269      r_icache_miss_inval("r_icache_miss_inval"),
270      r_icache_miss_clack("r_icache_miss_clack"),
271
272      r_icache_cc_way("r_icache_cc_way"),
273      r_icache_cc_set("r_icache_cc_set"),
274      r_icache_cc_word("r_icache_cc_word"),
275      r_icache_cc_need_write("r_icache_cc_need_write"),
276
277      r_icache_flush_count("r_icache_flush_count"),
278
279      r_icache_miss_req("r_icache_miss_req"),
280      r_icache_unc_req("r_icache_unc_req"),
281
282      r_icache_tlb_miss_req("r_icache_tlb_read_req"),
283      r_icache_tlb_rsp_error("r_icache_tlb_rsp_error"),
284
285      r_icache_cleanup_victim_req("r_icache_cleanup_victim_req"),
286      r_icache_cleanup_victim_nline("r_icache_cleanup_victim_nline"),
287
288      r_icache_cc_send_req("r_icache_cc_send_req"),
289      r_icache_cc_send_type("r_icache_cc_send_type"),
290      r_icache_cc_send_nline("r_icache_cc_send_nline"),
291      r_icache_cc_send_way("r_icache_cc_send_way"),
292      r_icache_cc_send_updt_tab_idx("r_icache_cc_send_updt_tab_idx"),
293
294      r_dcache_fsm("r_dcache_fsm"),
295      r_dcache_fsm_cc_save("r_dcache_fsm_cc_save"),
296      r_dcache_fsm_scan_save("r_dcache_fsm_scan_save"),
297
298      r_dcache_wbuf_req("r_dcache_wbuf_req"),
299      r_dcache_updt_req("r_dcache_updt_req"),
300      r_dcache_save_vaddr("r_dcache_save_vaddr"),
301      r_dcache_save_wdata("r_dcache_save_wdata"),
302      r_dcache_save_be("r_dcache_save_be"),
303      r_dcache_save_paddr("r_dcache_save_paddr"),
304      r_dcache_save_cache_way("r_dcache_save_cache_way"),
305      r_dcache_save_cache_set("r_dcache_save_cache_set"),
306      r_dcache_save_cache_word("r_dcache_save_cache_word"),
307
308      r_dcache_dirty_paddr("r_dcache_dirty_paddr"),
309      r_dcache_dirty_way("r_dcache_dirty_way"),
310      r_dcache_dirty_set("r_dcache_dirty_set"),
311
312      r_dcache_vci_paddr("r_dcache_vci_paddr"),
313      r_dcache_vci_wdata("r_dcache_vci_wdata"),
314      r_dcache_vci_miss_req("r_dcache_vci_miss_req"),
315      r_dcache_vci_unc_req("r_dcache_vci_unc_req"),
316      r_dcache_vci_unc_be("r_dcache_vci_unc_be"),
317      r_dcache_vci_unc_write("r_dcache_vci_unc_write"),
318      r_dcache_vci_cas_req("r_dcache_vci_cas_req"),
319      r_dcache_vci_cas_old("r_dcache_vci_cas_old"),
320      r_dcache_vci_cas_new("r_dcache_vci_cas_new"),
321      r_dcache_vci_ll_req("r_dcache_vci_ll_req"),
322      r_dcache_vci_sc_req("r_dcache_vci_sc_req"),
323      r_dcache_vci_sc_data("r_dcache_vci_sc_data"),
324
325      r_dcache_xtn_way("r_dcache_xtn_way"),
326      r_dcache_xtn_set("r_dcache_xtn_set"),
327
328      r_dcache_miss_type("r_dcache_miss_type"),
329      r_dcache_miss_word("r_dcache_miss_word"),
330      r_dcache_miss_way("r_dcache_miss_way"),
331      r_dcache_miss_set("r_dcache_miss_set"),
332      r_dcache_miss_inval("r_dcache_miss_inval"),
333
334      r_dcache_cc_way("r_dcache_cc_way"),
335      r_dcache_cc_set("r_dcache_cc_set"),
336      r_dcache_cc_word("r_dcache_cc_word"),
337      r_dcache_cc_need_write("r_dcache_cc_need_write"),
338
339      r_dcache_flush_count("r_dcache_flush_count"),
340
341      r_dcache_ll_rsp_count("r_dcache_ll_rsp_count"),
342
343      r_dcache_tlb_vaddr("r_dcache_tlb_vaddr"),
344      r_dcache_tlb_ins("r_dcache_tlb_ins"),
345      r_dcache_tlb_pte_flags("r_dcache_tlb_pte_flags"),
346      r_dcache_tlb_pte_ppn("r_dcache_tlb_pte_ppn"),
347      r_dcache_tlb_cache_way("r_dcache_tlb_cache_way"),
348      r_dcache_tlb_cache_set("r_dcache_tlb_cache_set"),
349      r_dcache_tlb_cache_word("r_dcache_tlb_cache_word"),
350      r_dcache_tlb_way("r_dcache_tlb_way"),
351      r_dcache_tlb_set("r_dcache_tlb_set"),
352
353      r_dcache_tlb_inval_line("r_dcache_tlb_inval_line"),
354      r_dcache_tlb_inval_set("r_dcache_tlb_inval_set"),
355
356      r_dcache_xtn_req("r_dcache_xtn_req"),
357      r_dcache_xtn_opcode("r_dcache_xtn_opcode"),
358
359      r_dcache_cleanup_victim_req("r_dcache_cleanup_victim_req"),
360      r_dcache_cleanup_victim_nline("r_dcache_cleanup_victim_nline"),
361   
362      r_dcache_cc_send_req("r_dcache_cc_send_req"),
363      r_dcache_cc_send_type("r_dcache_cc_send_type"),
364      r_dcache_cc_send_nline("r_dcache_cc_send_nline"),
365      r_dcache_cc_send_way("r_dcache_cc_send_way"),
366      r_dcache_cc_send_updt_tab_idx("r_dcache_cc_send_updt_tab_idx"),
367
368      r_vci_cmd_fsm("r_vci_cmd_fsm"),
369      r_vci_cmd_min("r_vci_cmd_min"),
370      r_vci_cmd_max("r_vci_cmd_max"),
371      r_vci_cmd_cpt("r_vci_cmd_cpt"),
372      r_vci_cmd_imiss_prio("r_vci_cmd_imiss_prio"),
373
374      r_vci_rsp_fsm("r_vci_rsp_fsm"),
375      r_vci_rsp_cpt("r_vci_rsp_cpt"),
376      r_vci_rsp_ins_error("r_vci_rsp_ins_error"),
377      r_vci_rsp_data_error("r_vci_rsp_data_error"),
378      r_vci_rsp_fifo_icache("r_vci_rsp_fifo_icache", 2),        // 2 words depth
379      r_vci_rsp_fifo_dcache("r_vci_rsp_fifo_dcache", 2),        // 2 words depth
380
381      r_cc_send_fsm("r_cc_send_fsm"),
382      r_cc_send_last_client("r_cc_send_last_client"),
383
384      r_cc_receive_fsm("r_cc_receive_fsm"),
385      r_cc_receive_data_ins("r_cc_receive_data_ins"),
386      r_cc_receive_word_idx("r_cc_receive_word_idx"),
387      r_cc_receive_updt_fifo_be("r_cc_receive_updt_fifo_be", 2),        // 2 words depth
388      r_cc_receive_updt_fifo_data("r_cc_receive_updt_fifo_data", 2),    // 2 words depth
389      r_cc_receive_updt_fifo_eop("r_cc_receive_updt_fifo_eop", 2),      // 2 words depth
390
391      r_cc_receive_icache_req("r_cc_receive_icache_req"),
392      r_cc_receive_icache_type("r_cc_receive_icache_type"),
393      r_cc_receive_icache_way("r_cc_receive_icache_way"),
394      r_cc_receive_icache_set("r_cc_receive_icache_set"),
395      r_cc_receive_icache_updt_tab_idx("r_cc_receive_icache_updt_tab_idx"),
396      r_cc_receive_icache_nline("r_cc_receive_icache_nline"),
397
398      r_cc_receive_dcache_req("r_cc_receive_dcache_req"),
399      r_cc_receive_dcache_type("r_cc_receive_dcache_type"),
400      r_cc_receive_dcache_way("r_cc_receive_dcache_way"),
401      r_cc_receive_dcache_set("r_cc_receive_dcache_set"),
402      r_cc_receive_dcache_updt_tab_idx("r_cc_receive_dcache_updt_tab_idx"),
403      r_cc_receive_dcache_nline("r_cc_receive_dcache_nline"),
404
405      r_iss(this->name(), proc_id),
406      r_wbuf("wbuf", wbuf_nwords, wbuf_nlines, dcache_words ),
407      r_icache("icache", icache_ways, icache_sets, icache_words),
408      r_dcache("dcache", dcache_ways, dcache_sets, dcache_words),
409      r_itlb("itlb", proc_id, itlb_ways,itlb_sets,vci_param::N),
410      r_dtlb("dtlb", proc_id, dtlb_ways,dtlb_sets,vci_param::N)
411{
412    std::cout << "  - Building VciCcVcacheWrapper : " << name << std::endl;
413
414    assert( ((icache_words*vci_param::B) < (1<<vci_param::K)) and
415             "Need more PLEN bits.");
416
417    assert( (vci_param::T > 2) and ((1<<(vci_param::T-1)) >= (wbuf_nlines)) and
418             "Need more TRDID bits.");
419
420    assert( (icache_words == dcache_words) and
421             "icache_words and dcache_words parameters must be equal");
422
423    assert( (itlb_sets == dtlb_sets) and
424             "itlb_sets and dtlb_sets parameters must be etqual");
425
426    assert( (itlb_ways == dtlb_ways) and
427             "itlb_ways and dtlb_ways parameters must be etqual");
428
429    r_mmu_params = (uint32_log2(m_dtlb_ways)   << 29)   | (uint32_log2(m_dtlb_sets)   << 25) |
430                   (uint32_log2(m_dcache_ways) << 22)   | (uint32_log2(m_dcache_sets) << 18) |
431                   (uint32_log2(m_itlb_ways)   << 15)   | (uint32_log2(m_itlb_sets)   << 11) |
432                   (uint32_log2(m_icache_ways) << 8)    | (uint32_log2(m_icache_sets) << 4)  |
433                   (uint32_log2(m_icache_words<<2));
434
435    r_mmu_release = (uint32_t)(1 << 16) | 0x1;
436
437    r_dcache_in_tlb       = new bool[dcache_ways*dcache_sets];
438    r_dcache_contains_ptd = new bool[dcache_ways*dcache_sets];
439
440    SC_METHOD(transition);
441    dont_initialize();
442    sensitive << p_clk.pos();
443
444    SC_METHOD(genMoore);
445    dont_initialize();
446    sensitive << p_clk.neg();
447
448    typename iss_t::CacheInfo cache_info;
449    cache_info.has_mmu = true;
450    cache_info.icache_line_size = icache_words*sizeof(uint32_t);
451    cache_info.icache_assoc = icache_ways;
452    cache_info.icache_n_lines = icache_sets;
453    cache_info.dcache_line_size = dcache_words*sizeof(uint32_t);
454    cache_info.dcache_assoc = dcache_ways;
455    cache_info.dcache_n_lines = dcache_sets;
456    r_iss.setCacheInfo(cache_info);
457}
458
459/////////////////////////////////////
460tmpl(/**/)::~VciCcVCacheWrapper()
461/////////////////////////////////////
462{
463    delete [] r_dcache_in_tlb;
464    delete [] r_dcache_contains_ptd;
465}
466
467////////////////////////
468tmpl(void)::print_cpi()
469////////////////////////
470{
471    std::cout << name() << " CPI = "
472        << (float)m_cpt_total_cycles/(m_cpt_total_cycles - m_cpt_frz_cycles) << std::endl ;
473}
474
475////////////////////////////////////
476tmpl(void)::print_trace(size_t mode)
477////////////////////////////////////
478{
479    // b0 : write buffer trace
480    // b1 : dump processor registers
481    // b2 : dcache trace
482    // b3 : icache trace
483    // b4 : dtlb trace
484    // b5 : itlb trace
485    // b6 : SR (ISS register 32)
486
487    std::cout << std::dec << "PROC " << name() << std::endl;
488
489    std::cout << "  " << m_ireq << std::endl;
490    std::cout << "  " << m_irsp << std::endl;
491    std::cout << "  " << m_dreq << std::endl;
492    std::cout << "  " << m_drsp << std::endl;
493
494    std::cout << "  " << icache_fsm_state_str[r_icache_fsm.read()]
495              << " | " << dcache_fsm_state_str[r_dcache_fsm.read()]
496              << " | " << cmd_fsm_state_str[r_vci_cmd_fsm.read()]
497              << " | " << rsp_fsm_state_str[r_vci_rsp_fsm.read()]
498              << " | " << cc_receive_fsm_state_str[r_cc_receive_fsm.read()]
499              << " | " << cc_send_fsm_state_str[r_cc_send_fsm.read()]
500              << " | MMU = " << r_mmu_mode.read();
501
502    if (r_dcache_updt_req.read() ) std::cout << " | P1_UPDT";
503    if (r_dcache_wbuf_req.read() ) std::cout << " | P1_WBUF";
504    std::cout << std::endl;
505
506    if(mode & 0x01)
507    {
508        if ( r_icache_miss_req.read()     ) std::cout << "  IMISS_REQ" << std::endl;
509        if ( r_icache_unc_req.read()      ) std::cout << "  IUNC_REQ" << std::endl;
510        if ( r_dcache_vci_miss_req.read() ) std::cout << "  DMISS_REQ" << std::endl;
511        if ( r_dcache_vci_unc_req.read()  ) std::cout << "  DUNC_REQ" << std::endl;
512
513        r_wbuf.printTrace((mode>>1)&1);
514    }
515    if(mode & 0x02)
516    {
517        r_iss.dump();
518    }
519    if(mode & 0x04)
520    {
521        std::cout << "  Data Cache" << std::endl;
522        r_dcache.printTrace();
523    }
524    if(mode & 0x08)
525    {
526        std::cout << "  Instruction Cache" << std::endl;
527        r_icache.printTrace();
528    }
529    if(mode & 0x10)
530    {
531        std::cout << "  Data TLB" << std::endl;
532        r_dtlb.printTrace();
533    }
534    if(mode & 0x20)
535    {
536        std::cout << "  Instruction TLB" << std::endl;
537        r_itlb.printTrace();
538    }
539    if(mode & 0x40)
540    {
541        uint32_t status = r_iss.debugGetRegisterValue( 32 );
542        std::cout << name();
543        if ( status != m_previous_status ) std::cout << " NEW ";
544        std::cout << " status = " << std::hex << status << " " << std::endl;
545        m_previous_status = status;
546    }
547}
548
549//////////////////////////////////////////
550tmpl(void)::cache_monitor( paddr_t addr )
551//////////////////////////////////////////
552{
553    bool        cache_hit;
554    size_t          cache_way = 0;
555    size_t          cache_set = 0;
556    size_t          cache_word = 0;
557    uint32_t    cache_rdata = 0;
558
559    cache_hit = r_dcache.read_neutral( addr,
560                                       &cache_rdata,
561                                       &cache_way,
562                                       &cache_set,
563                                       &cache_word );
564
565    if ( cache_hit != m_debug_previous_d_hit )
566    {
567        std::cout << "Monitor PROC " << name()
568                  << " DCACHE at cycle " << std::dec << m_cpt_total_cycles
569                  << " / HIT = " << cache_hit
570                  << " / PADDR = " << std::hex << addr
571                  << " / DATA = " << cache_rdata
572                  << " / WAY = " << cache_way << std::endl;
573            m_debug_previous_d_hit = cache_hit;
574    }
575
576    cache_hit = r_icache.read_neutral( addr,
577                                       &cache_rdata,
578                                       &cache_way,
579                                       &cache_set,
580                                       &cache_word );
581
582    if ( cache_hit != m_debug_previous_i_hit )
583    {
584        std::cout << "Monitor PROC " << name()
585                  << " ICACHE at cycle " << std::dec << m_cpt_total_cycles
586                  << " / HIT = " << cache_hit
587                  << " / PADDR = " << std::hex << addr
588                  << " / DATA = " << cache_rdata
589                  << " / WAY = " << cache_way << std::endl;
590            m_debug_previous_i_hit = cache_hit;
591    }
592}
593
594/*
595////////////////////////
596tmpl(void)::print_stats()
597////////////////////////
598{
599    float run_cycles = (float)(m_cpt_total_cycles - m_cpt_frz_cycles);
600    std::cout << name() << std::endl
601        << "- CPI                    = " << (float)m_cpt_total_cycles/run_cycles << std::endl
602        << "- READ RATE              = " << (float)m_cpt_read/run_cycles << std::endl
603        << "- WRITE RATE             = " << (float)m_cpt_write/run_cycles << std::endl
604        << "- IMISS_RATE             = " << (float)m_cpt_ins_miss/m_cpt_ins_read << std::endl
605        << "- DMISS RATE             = " << (float)m_cpt_data_miss/(m_cpt_read-m_cpt_unc_read) << std::endl
606        << "- INS MISS COST          = " << (float)m_cost_ins_miss_frz/m_cpt_ins_miss << std::endl
607        << "- DATA MISS COST         = " << (float)m_cost_data_miss_frz/m_cpt_data_miss << std::endl
608        << "- WRITE COST             = " << (float)m_cost_write_frz/m_cpt_write << std::endl
609        << "- UNC COST               = " << (float)m_cost_unc_read_frz/m_cpt_unc_read << std::endl
610        << "- UNCACHED READ RATE     = " << (float)m_cpt_unc_read/m_cpt_read << std::endl
611        << "- CACHED WRITE RATE      = " << (float)m_cpt_write_cached/m_cpt_write << std::endl
612        << "- INS TLB MISS RATE      = " << (float)m_cpt_ins_tlb_miss/m_cpt_ins_tlb_read << std::endl
613        << "- DATA TLB MISS RATE     = " << (float)m_cpt_data_tlb_miss/m_cpt_data_tlb_read << std::endl
614        << "- ITLB MISS COST         = " << (float)m_cost_ins_tlb_miss_frz/m_cpt_ins_tlb_miss << std::endl
615        << "- DTLB MISS COST         = " << (float)m_cost_data_tlb_miss_frz/m_cpt_data_tlb_miss << std::endl
616        << "- ITLB UPDATE ACC COST   = " << (float)m_cost_ins_tlb_update_acc_frz/m_cpt_ins_tlb_update_acc << std::endl
617        << "- DTLB UPDATE ACC COST   = " << (float)m_cost_data_tlb_update_acc_frz/m_cpt_data_tlb_update_acc << std::endl
618        << "- DTLB UPDATE DIRTY COST = " << (float)m_cost_data_tlb_update_dirty_frz/m_cpt_data_tlb_update_dirty << std::endl
619        << "- ITLB HIT IN DCACHE RATE= " << (float)m_cpt_ins_tlb_hit_dcache/m_cpt_ins_tlb_miss << std::endl
620        << "- DTLB HIT IN DCACHE RATE= " << (float)m_cpt_data_tlb_hit_dcache/m_cpt_data_tlb_miss << std::endl
621        << "- DCACHE FROZEN BY ITLB  = " << (float)m_cost_ins_tlb_occup_cache_frz/m_cpt_dcache_frz_cycles << std::endl
622        << "- DCACHE FOR TLB %       = " << (float)m_cpt_tlb_occup_dcache/(m_dcache_ways*m_dcache_sets) << std::endl
623        << "- NB CC BROADCAST        = " << m_cpt_cc_broadcast << std::endl
624        << "- NB CC UPDATE DATA      = " << m_cpt_cc_update_data << std::endl
625        << "- NB CC INVAL DATA       = " << m_cpt_cc_inval_data << std::endl
626        << "- NB CC INVAL INS        = " << m_cpt_cc_inval_ins << std::endl
627        << "- CC BROADCAST COST      = " << (float)m_cost_broadcast_frz/m_cpt_cc_broadcast << std::endl
628        << "- CC UPDATE DATA COST    = " << (float)m_cost_updt_data_frz/m_cpt_cc_update_data << std::endl
629        << "- CC INVAL DATA COST     = " << (float)m_cost_inval_data_frz/m_cpt_cc_inval_data << std::endl
630        << "- CC INVAL INS COST      = " << (float)m_cost_inval_ins_frz/m_cpt_cc_inval_ins << std::endl
631        << "- NB CC CLEANUP DATA     = " << m_cpt_cc_cleanup_data << std::endl
632        << "- NB CC CLEANUP INS      = " << m_cpt_cc_cleanup_ins << std::endl
633        << "- IMISS TRANSACTION      = " << (float)m_cost_imiss_transaction/m_cpt_imiss_transaction << std::endl
634        << "- DMISS TRANSACTION      = " << (float)m_cost_dmiss_transaction/m_cpt_dmiss_transaction << std::endl
635        << "- UNC TRANSACTION        = " << (float)m_cost_unc_transaction/m_cpt_unc_transaction << std::endl
636        << "- WRITE TRANSACTION      = " << (float)m_cost_write_transaction/m_cpt_write_transaction << std::endl
637        << "- WRITE LENGTH           = " << (float)m_length_write_transaction/m_cpt_write_transaction << std::endl
638        << "- ITLB MISS TRANSACTION  = " << (float)m_cost_itlbmiss_transaction/m_cpt_itlbmiss_transaction << std::endl
639        << "- DTLB MISS TRANSACTION  = " << (float)m_cost_dtlbmiss_transaction/m_cpt_dtlbmiss_transaction << std::endl;
640}
641
642////////////////////////
643tmpl(void)::clear_stats()
644////////////////////////
645{
646    m_cpt_dcache_data_read  = 0;
647    m_cpt_dcache_data_write = 0;
648    m_cpt_dcache_dir_read   = 0;
649    m_cpt_dcache_dir_write  = 0;
650    m_cpt_icache_data_read  = 0;
651    m_cpt_icache_data_write = 0;
652    m_cpt_icache_dir_read   = 0;
653    m_cpt_icache_dir_write  = 0;
654
655    m_cpt_frz_cycles        = 0;
656    m_cpt_dcache_frz_cycles = 0;
657    m_cpt_total_cycles      = 0;
658
659    m_cpt_read         = 0;
660    m_cpt_write        = 0;
661    m_cpt_data_miss    = 0;
662    m_cpt_ins_miss     = 0;
663    m_cpt_unc_read     = 0;
664    m_cpt_write_cached = 0;
665    m_cpt_ins_read     = 0;
666
667    m_cost_write_frz     = 0;
668    m_cost_data_miss_frz = 0;
669    m_cost_unc_read_frz  = 0;
670    m_cost_ins_miss_frz  = 0;
671
672    m_cpt_imiss_transaction      = 0;
673    m_cpt_dmiss_transaction      = 0;
674    m_cpt_unc_transaction        = 0;
675    m_cpt_write_transaction      = 0;
676    m_cpt_icache_unc_transaction = 0;
677
678    m_cost_imiss_transaction      = 0;
679    m_cost_dmiss_transaction      = 0;
680    m_cost_unc_transaction        = 0;
681    m_cost_write_transaction      = 0;
682    m_cost_icache_unc_transaction = 0;
683    m_length_write_transaction    = 0;
684
685    m_cpt_ins_tlb_read       = 0;
686    m_cpt_ins_tlb_miss       = 0;
687    m_cpt_ins_tlb_update_acc = 0;
688
689    m_cpt_data_tlb_read         = 0;
690    m_cpt_data_tlb_miss         = 0;
691    m_cpt_data_tlb_update_acc   = 0;
692    m_cpt_data_tlb_update_dirty = 0;
693    m_cpt_ins_tlb_hit_dcache    = 0;
694    m_cpt_data_tlb_hit_dcache   = 0;
695    m_cpt_ins_tlb_occup_cache   = 0;
696    m_cpt_data_tlb_occup_cache  = 0;
697
698    m_cost_ins_tlb_miss_frz          = 0;
699    m_cost_data_tlb_miss_frz         = 0;
700    m_cost_ins_tlb_update_acc_frz    = 0;
701    m_cost_data_tlb_update_acc_frz   = 0;
702    m_cost_data_tlb_update_dirty_frz = 0;
703    m_cost_ins_tlb_occup_cache_frz   = 0;
704    m_cost_data_tlb_occup_cache_frz  = 0;
705
706    m_cpt_itlbmiss_transaction      = 0;
707    m_cpt_itlb_ll_transaction       = 0;
708    m_cpt_itlb_sc_transaction       = 0;
709    m_cpt_dtlbmiss_transaction      = 0;
710    m_cpt_dtlb_ll_transaction       = 0;
711    m_cpt_dtlb_sc_transaction       = 0;
712    m_cpt_dtlb_ll_dirty_transaction = 0;
713    m_cpt_dtlb_sc_dirty_transaction = 0;
714
715    m_cost_itlbmiss_transaction      = 0;
716    m_cost_itlb_ll_transaction       = 0;
717    m_cost_itlb_sc_transaction       = 0;
718    m_cost_dtlbmiss_transaction      = 0;
719    m_cost_dtlb_ll_transaction       = 0;
720    m_cost_dtlb_sc_transaction       = 0;
721    m_cost_dtlb_ll_dirty_transaction = 0;
722    m_cost_dtlb_sc_dirty_transaction = 0;
723
724    m_cpt_cc_update_data = 0;
725    m_cpt_cc_inval_ins   = 0;
726    m_cpt_cc_inval_data  = 0;
727    m_cpt_cc_broadcast   = 0;
728
729    m_cost_updt_data_frz  = 0;
730    m_cost_inval_ins_frz  = 0;
731    m_cost_inval_data_frz = 0;
732    m_cost_broadcast_frz  = 0;
733
734    m_cpt_cc_cleanup_data = 0;
735    m_cpt_cc_cleanup_ins  = 0;
736}
737
738*/
739
740/////////////////////////
741tmpl(void)::transition()
742/////////////////////////
743{
744    if ( not p_resetn.read() )
745    {
746        r_iss.reset();
747        r_wbuf.reset();
748        r_icache.reset();
749        r_dcache.reset();
750        r_itlb.reset();
751        r_dtlb.reset();
752
753        r_dcache_fsm      = DCACHE_IDLE;
754        r_icache_fsm      = ICACHE_IDLE;
755        r_vci_cmd_fsm     = CMD_IDLE;
756        r_vci_rsp_fsm     = RSP_IDLE;
757        r_cc_receive_fsm  = CC_RECEIVE_IDLE;
758        r_cc_send_fsm     = CC_SEND_IDLE;
759
760        // reset data physical address extension
761        r_dcache_paddr_ext = m_dcache_paddr_ext_reset;
762
763        // reset inst physical address extension
764        r_icache_paddr_ext = m_icache_paddr_ext_reset;
765
766        // reset dcache directory extension
767        for (size_t i=0 ; i< m_dcache_ways*m_dcache_sets ; i++)
768        {
769            r_dcache_in_tlb[i]       = false;
770            r_dcache_contains_ptd[i] = false;
771        }
772
773        // Response FIFOs and cleanup buffer
774        r_vci_rsp_fifo_icache.init();
775        r_vci_rsp_fifo_dcache.init();
776
777        // ICACHE & DCACHE activated
778        // ITLB & DTLB desactivated
779        r_mmu_mode = 0x3;
780
781            // No request from ICACHE FSM to CMD FSM
782        r_icache_miss_req          = false;
783        r_icache_unc_req           = false;
784
785        // No request from ICACHE_FSM to DCACHE FSM
786        r_icache_tlb_miss_req      = false;
787
788        // No request from ICACHE_FSM to CC_SEND FSM
789        r_icache_cc_send_req       = false;
790        r_icache_cleanup_victim_req = false;
791
792        r_icache_clack_req         = false;
793
794        // No pending write in pipeline
795        r_dcache_wbuf_req          = false;
796        r_dcache_updt_req          = false;
797
798        // No request from DCACHE_FSM to CMD_FSM
799        r_dcache_vci_miss_req      = false;
800        r_dcache_vci_unc_req       = false;
801        r_dcache_vci_cas_req       = false;
802        r_dcache_vci_ll_req        = false;
803        r_dcache_vci_sc_req        = false;
804
805        // No processor XTN request pending
806        r_dcache_xtn_req           = false;
807
808        // No request from DCACHE FSM to CC_SEND FSM
809        r_dcache_cc_send_req       = false;
810        r_dcache_cleanup_victim_req = false;
811
812        r_dcache_clack_req         = false;
813
814        // No request from CC_RECEIVE FSM to ICACHE/DCACHE FSMs
815        r_cc_receive_icache_req    = false;
816        r_cc_receive_dcache_req    = false;
817
818        // last cc_send client was dcache
819        r_cc_send_last_client      = false;
820
821        // No pending cleanup after a replacement
822        r_icache_miss_clack        = false;
823        r_dcache_miss_clack        = false;
824
825        // No signalisation of a coherence request matching a pending miss
826        r_icache_miss_inval        = false;
827        r_dcache_miss_inval        = false;
828
829        r_dspin_clack_req          = false;
830
831        // No signalisation  of errors
832        r_vci_rsp_ins_error        = false;
833        r_vci_rsp_data_error       = false;
834
835        // Debug variables
836        m_debug_previous_i_hit     = false;
837        m_debug_previous_d_hit     = false;
838        m_debug_icache_fsm             = false;
839        m_debug_dcache_fsm             = false;
840        m_debug_cmd_fsm                = false;
841
842        // activity counters
843        m_cpt_dcache_data_read  = 0;
844        m_cpt_dcache_data_write = 0;
845        m_cpt_dcache_dir_read   = 0;
846        m_cpt_dcache_dir_write  = 0;
847        m_cpt_icache_data_read  = 0;
848        m_cpt_icache_data_write = 0;
849        m_cpt_icache_dir_read   = 0;
850        m_cpt_icache_dir_write  = 0;
851
852        m_cpt_frz_cycles        = 0;
853        m_cpt_total_cycles      = 0;
854        m_cpt_stop_simulation   = 0;
855
856        m_cpt_data_miss         = 0;
857        m_cpt_ins_miss          = 0;
858        m_cpt_unc_read          = 0;
859        m_cpt_write_cached      = 0;
860        m_cpt_ins_read          = 0;
861
862        m_cost_write_frz        = 0;
863        m_cost_data_miss_frz    = 0;
864        m_cost_unc_read_frz     = 0;
865        m_cost_ins_miss_frz     = 0;
866
867        m_cpt_imiss_transaction = 0;
868        m_cpt_dmiss_transaction = 0;
869        m_cpt_unc_transaction   = 0;
870        m_cpt_write_transaction = 0;
871        m_cpt_icache_unc_transaction = 0;
872
873        m_cost_imiss_transaction      = 0;
874        m_cost_dmiss_transaction      = 0;
875        m_cost_unc_transaction        = 0;
876        m_cost_write_transaction      = 0;
877        m_cost_icache_unc_transaction = 0;
878        m_length_write_transaction    = 0;
879
880        m_cpt_ins_tlb_read       = 0;
881        m_cpt_ins_tlb_miss       = 0;
882        m_cpt_ins_tlb_update_acc = 0;
883
884        m_cpt_data_tlb_read         = 0;
885        m_cpt_data_tlb_miss         = 0;
886        m_cpt_data_tlb_update_acc   = 0;
887        m_cpt_data_tlb_update_dirty = 0;
888        m_cpt_ins_tlb_hit_dcache    = 0;
889        m_cpt_data_tlb_hit_dcache   = 0;
890        m_cpt_ins_tlb_occup_cache   = 0;
891        m_cpt_data_tlb_occup_cache  = 0;
892
893        m_cost_ins_tlb_miss_frz          = 0;
894        m_cost_data_tlb_miss_frz         = 0;
895        m_cost_ins_tlb_update_acc_frz    = 0;
896        m_cost_data_tlb_update_acc_frz   = 0;
897        m_cost_data_tlb_update_dirty_frz = 0;
898        m_cost_ins_tlb_occup_cache_frz   = 0;
899        m_cost_data_tlb_occup_cache_frz  = 0;
900
901        m_cpt_ins_tlb_inval       = 0;
902        m_cpt_data_tlb_inval      = 0;
903        m_cost_ins_tlb_inval_frz  = 0;
904        m_cost_data_tlb_inval_frz = 0;
905
906        m_cpt_cc_broadcast   = 0;
907
908            m_cost_updt_data_frz  = 0;
909            m_cost_inval_ins_frz  = 0;
910            m_cost_inval_data_frz = 0;
911            m_cost_broadcast_frz  = 0;
912
913            m_cpt_cc_cleanup_data = 0;
914            m_cpt_cc_cleanup_ins  = 0;
915
916        m_cpt_itlbmiss_transaction      = 0;
917        m_cpt_itlb_ll_transaction       = 0;
918        m_cpt_itlb_sc_transaction       = 0;
919        m_cpt_dtlbmiss_transaction      = 0;
920        m_cpt_dtlb_ll_transaction       = 0;
921        m_cpt_dtlb_sc_transaction       = 0;
922        m_cpt_dtlb_ll_dirty_transaction = 0;
923        m_cpt_dtlb_sc_dirty_transaction = 0;
924
925        m_cost_itlbmiss_transaction      = 0;
926        m_cost_itlb_ll_transaction       = 0;
927        m_cost_itlb_sc_transaction       = 0;
928        m_cost_dtlbmiss_transaction      = 0;
929        m_cost_dtlb_ll_transaction       = 0;
930        m_cost_dtlb_sc_transaction       = 0;
931        m_cost_dtlb_ll_dirty_transaction = 0;
932        m_cost_dtlb_sc_dirty_transaction = 0;
933/*
934        m_cpt_dcache_frz_cycles = 0;
935        m_cpt_read              = 0;
936        m_cpt_write             = 0;
937            m_cpt_cc_update_data = 0;
938            m_cpt_cc_inval_ins   = 0;
939            m_cpt_cc_inval_data  = 0;
940*/
941
942        for (uint32_t i=0; i<32 ; ++i) m_cpt_fsm_icache      [i]   = 0;
943        for (uint32_t i=0; i<32 ; ++i) m_cpt_fsm_dcache      [i]   = 0;
944        for (uint32_t i=0; i<32 ; ++i) m_cpt_fsm_cmd         [i]   = 0;
945        for (uint32_t i=0; i<32 ; ++i) m_cpt_fsm_rsp         [i]   = 0;
946
947        // init the llsc reservation buffer
948        r_dcache_llsc_valid = false;
949        m_monitor_ok = false;
950
951        return;
952    }
953
954    // Response FIFOs default values
955    bool       vci_rsp_fifo_icache_get   = false;
956    bool       vci_rsp_fifo_icache_put   = false;
957    uint32_t   vci_rsp_fifo_icache_data  = 0;
958
959    bool       vci_rsp_fifo_dcache_get   = false;
960    bool       vci_rsp_fifo_dcache_put   = false;
961    uint32_t   vci_rsp_fifo_dcache_data  = 0;
962
963    // updt fifo
964    bool       cc_receive_updt_fifo_get  = false;
965    bool       cc_receive_updt_fifo_put  = false;
966    uint32_t   cc_receive_updt_fifo_be   = 0;
967    uint32_t   cc_receive_updt_fifo_data = 0;
968    bool       cc_receive_updt_fifo_eop  = false;
969
970#ifdef INSTRUMENTATION
971    m_cpt_fsm_dcache  [r_dcache_fsm.read() ] ++;
972    m_cpt_fsm_icache  [r_icache_fsm.read() ] ++;
973    m_cpt_fsm_cmd     [r_vci_cmd_fsm.read()] ++;
974    m_cpt_fsm_rsp     [r_vci_rsp_fsm.read()] ++;
975    m_cpt_fsm_tgt     [r_tgt_fsm.read()    ] ++;
976    m_cpt_fsm_cleanup [r_cleanup_cmd_fsm.read()] ++;
977#endif
978
979    m_cpt_total_cycles++;
980
981    m_debug_icache_fsm = m_debug_icache_fsm ||
982        ((m_cpt_total_cycles > m_debug_start_cycle) and m_debug_ok);
983    m_debug_dcache_fsm = m_debug_dcache_fsm ||
984        ((m_cpt_total_cycles > m_debug_start_cycle) and m_debug_ok);
985    m_debug_cmd_fsm = m_debug_cmd_fsm ||
986        ((m_cpt_total_cycles > m_debug_start_cycle) and m_debug_ok);
987
988    /////////////////////////////////////////////////////////////////////
989    // Get data and instruction requests from processor
990    ///////////////////////////////////////////////////////////////////////
991
992    r_iss.getRequests(m_ireq, m_dreq);
993
994    ////////////////////////////////////////////////////////////////////////////////////
995    //      ICACHE_FSM
996    //
997    // 1/ Coherence operations
998    //    They are handled as interrupts generated by the CC_RECEIVE FSM.
999    //    - There is a coherence request when r_tgt_icache_req is set.
1000    //    They are taken in IDLE, MISS_WAIT, MISS_DIR_UPDT, UNC_WAIT, states.
1001    //    - There is a cleanup ack request when r_cleanup_icache_req is set.
1002    //    They are taken in IDLE, MISS_SELECT, MISS_CLEAN, MISS_WAIT,
1003    //    MISS_DATA_UPDT, MISS_DIR_UPDT and UNC_WAIT states.
1004    //    - For both types of requests, actions associated to the pre-empted state
1005    //    are not executed. The DCACHE FSM goes to the proper sub-FSM (CC_CHECK
1006    //    or CC_CLACK) to execute the requested coherence operation, and returns
1007    //    to the pre-empted state.
1008    //
1009    // 2/ Processor requests
1010    //    They are taken in IDLE state only. In case of cache miss, or uncacheable
1011    //    instruction, the ICACHE FSM request a VCI transaction to CMD FSM,
1012    //    using the r_icache_miss_req or r_icache_unc_req flip-flops. These
1013    //    flip-flops are reset when the transaction starts.
1014    //    - In case of miss the ICACHE FSM  goes to the ICACHE_MISS_SELECT state
1015    //    to select a slot and possibly request a cleanup transaction to the CC_SEND FSM.
1016    //    It goes next to the ICACHE_MISS_WAIT state waiting a response from RSP FSM,
1017    //    The availability of the missing cache line is signaled by the response fifo,
1018    //    and the cache update is done (one word per cycle) in the ICACHE_MISS_DATA_UPDT
1019    //    and ICACHE_MISS_DIR_UPDT states.
1020    //    - In case of uncacheable instruction, the ICACHE FSM goes to ICACHE_UNC_WAIT
1021    //    to wait the response from the RSP FSM, through the response fifo.
1022    //    The missing instruction is directly returned to processor in this state.
1023    //
1024    // 3/ TLB miss
1025    //    In case of tlb miss, the ICACHE FSM request to the DCACHE FSM to update the
1026    //    ITLB using the r_icache_tlb_miss_req flip-flop and the r_icache_tlb_miss_vaddr
1027    //    register, and goes to the ICACHE_TLB_WAIT state.
1028    //    The tlb update is entirely done by the DCACHE FSM (who becomes the owner
1029    //    of ITLB until the update is completed, and reset r_icache_tlb_miss_req
1030    //    to signal the completion.
1031    //
1032    // 4/ XTN requests
1033    //    The DCACHE FSM signals XTN processor requests to ICACHE_FSM
1034    //    using the r_dcache_xtn_req flip-flop.
1035    //    The request opcode and the address to be invalidated are transmitted
1036    //    in the r_dcache_xtn_opcode and r_dcache_save_wdata registers respectively.
1037    //    The r_dcache_xtn_req flip-flop is reset by the ICACHE_FSM when the operation
1038    //    is completed.
1039    //
1040    // 5/ Error Handling
1041    //    The r_vci_rsp_ins_error flip-flop is set by the RSP FSM in case of bus error
1042    //    in a cache miss or uncacheable read VCI transaction. Nothing is written
1043    //    in the response fifo. This flip-flop is reset by the ICACHE-FSM.
1044    ////////////////////////////////////////////////////////////////////////////////////////
1045
1046    // default value for m_irsp
1047    m_irsp.valid       = false;
1048    m_irsp.error       = false;
1049    m_irsp.instruction = 0;
1050
1051    switch( r_icache_fsm.read() )
1052    {
1053    /////////////////
1054    case ICACHE_IDLE:   // In this state, we handle processor requests, XTN requests,
1055                        // and coherence requests with a fixed priority:
1056                        // 1/ Coherence requests                        => ICACHE_CC_CHECK
1057                        // 2/ XTN processor requests (from DCACHE FSM)  => ICACHE_XTN_*
1058                        // 3/ tlb miss                                  => ICACHE_TLB_WAIT
1059                        // 4/ cacheable read miss                       => ICACHE_MISS_SELECT
1060                        // 5/ uncacheable read miss                     => ICACHE_UNC_REQ
1061    {
1062        // coherence clack interrupt
1063        if ( r_icache_clack_req.read() )
1064        {
1065            r_icache_fsm = ICACHE_CC_CHECK;
1066            r_icache_fsm_save = r_icache_fsm.read();
1067            break;
1068        }
1069
1070        // coherence interrupt
1071        if ( r_cc_receive_icache_req.read() and not r_icache_cc_send_req.read())
1072        {
1073            r_icache_fsm = ICACHE_CC_CHECK;
1074            r_icache_fsm_save = r_icache_fsm.read();
1075            break;
1076        }
1077
1078        // XTN requests sent by DCACHE FSM
1079        // These request are not executed in this IDLE state (except XTN_INST_PADDR_EXT),
1080        // because they require access to icache or itlb, that are already accessed
1081        if ( r_dcache_xtn_req.read() )
1082        {
1083            if ( (int)r_dcache_xtn_opcode.read() == (int)iss_t::XTN_PTPR )
1084            {
1085                r_icache_fsm         = ICACHE_XTN_TLB_FLUSH;
1086            }
1087            else if ( (int)r_dcache_xtn_opcode.read() == (int)iss_t::XTN_ICACHE_FLUSH)
1088            {
1089                r_icache_flush_count = 0;
1090                r_icache_fsm         = ICACHE_XTN_CACHE_FLUSH;
1091            }
1092            else if ( (int)r_dcache_xtn_opcode.read() == (int)iss_t::XTN_ITLB_INVAL)
1093            {
1094                r_icache_fsm         = ICACHE_XTN_TLB_INVAL;
1095            }
1096            else if ( (int)r_dcache_xtn_opcode.read() == (int)iss_t::XTN_ICACHE_INVAL)
1097            {
1098                r_icache_fsm         = ICACHE_XTN_CACHE_INVAL_VA;
1099            }
1100            else if ( (int)r_dcache_xtn_opcode.read() == (int)iss_t::XTN_MMU_ICACHE_PA_INV)
1101            {
1102                if (sizeof(paddr_t) <= 32)
1103                {
1104                    assert(r_mmu_word_hi.read() == 0 &&
1105                    "illegal XTN request in ICACHE: high bits should be 0 for 32bit paddr");
1106                    r_icache_vci_paddr = (paddr_t)r_mmu_word_lo.read();
1107                }
1108                else
1109                {
1110                    r_icache_vci_paddr = (paddr_t)r_mmu_word_hi.read() << 32 |
1111                                         (paddr_t)r_mmu_word_lo.read();
1112                }
1113                r_icache_fsm = ICACHE_XTN_CACHE_INVAL_PA;
1114            }
1115            else if ( (int)r_dcache_xtn_opcode.read() == (int)iss_t::XTN_INST_PADDR_EXT)
1116            {
1117                r_icache_paddr_ext = r_dcache_save_wdata.read(); 
1118                r_dcache_xtn_req   = false;
1119            }
1120            else
1121            {
1122               assert( false and
1123               "undefined XTN request received by ICACHE FSM");
1124            }
1125            break;
1126        } // end if xtn_req
1127
1128        // processor request
1129        if ( m_ireq.valid )
1130        {
1131            bool            cacheable;
1132            paddr_t         paddr;
1133            bool        tlb_hit = false;
1134            pte_info_t  tlb_flags;
1135            size_t      tlb_way;
1136            size_t      tlb_set;
1137            paddr_t     tlb_nline;
1138            uint32_t    cache_inst = 0;
1139            size_t      cache_way;
1140            size_t      cache_set;
1141            size_t      cache_word;
1142            int         cache_state = CACHE_SLOT_STATE_EMPTY;
1143
1144            // We register processor request
1145            r_icache_vaddr_save = m_ireq.addr;
1146            paddr = (paddr_t) m_ireq.addr;
1147
1148            // sytematic itlb access (if activated)
1149            if ( r_mmu_mode.read() & INS_TLB_MASK )
1150            {
1151
1152#ifdef INSTRUMENTATION
1153m_cpt_itlb_read++;
1154#endif
1155                tlb_hit = r_itlb.translate( m_ireq.addr,
1156                                            &paddr,
1157                                            &tlb_flags,
1158                                            &tlb_nline, // unused
1159                                            &tlb_way,   // unused
1160                                            &tlb_set ); // unused
1161            }
1162            else if (vci_param::N > 32)
1163            {
1164                paddr = paddr | ((paddr_t) r_icache_paddr_ext.read() << 32);
1165            }
1166
1167            // systematic icache access (if activated)
1168            if ( r_mmu_mode.read() & INS_CACHE_MASK )
1169            {
1170
1171
1172#ifdef INSTRUMENTATION
1173m_cpt_icache_data_read++;
1174m_cpt_icache_dir_read++;
1175#endif
1176                r_icache.read( paddr,
1177                               &cache_inst,
1178                               &cache_way,
1179                               &cache_set,
1180                               &cache_word,
1181                               &cache_state );
1182            }
1183
1184            // We compute cacheability and check access rights:
1185            // - If MMU activated : cacheability is defined by the C bit in the PTE,
1186            //   and the access rights are defined by the U and X bits in the PTE.
1187            // - If MMU not activated : cacheability is defined by the segment table,
1188            //   and there is no access rights checking
1189
1190            if ( not (r_mmu_mode.read() & INS_TLB_MASK) )       // tlb not activated:
1191            {
1192                // cacheability
1193                if ( not (r_mmu_mode.read() & INS_CACHE_MASK) ) cacheable = false;
1194                else     cacheable = m_cacheability_table[(uint64_t)m_ireq.addr];
1195            }
1196            else                                                        // itlb activated
1197            {
1198                if ( tlb_hit )  // ITLB hit
1199                {
1200                    // cacheability
1201                    if ( not (r_mmu_mode.read() & INS_CACHE_MASK) ) cacheable = false;
1202                    else  cacheable = tlb_flags.c;
1203
1204                    // access rights checking
1205                    if ( not tlb_flags.u && (m_ireq.mode == iss_t::MODE_USER) )
1206                    {
1207                        r_mmu_ietr          = MMU_READ_PRIVILEGE_VIOLATION;
1208                        r_mmu_ibvar         = m_ireq.addr;
1209                        m_irsp.valid        = true;
1210                        m_irsp.error        = true;
1211                        m_irsp.instruction  = 0;
1212                        break;
1213                    }
1214                    else if ( not tlb_flags.x )
1215                    {
1216                        r_mmu_ietr          = MMU_READ_EXEC_VIOLATION;
1217                        r_mmu_ibvar         = m_ireq.addr;
1218                        m_irsp.valid        = true;
1219                        m_irsp.error        = true;
1220                        m_irsp.instruction  = 0;
1221                        break;
1222                    }
1223                }
1224                else           // ITLB miss
1225                {
1226
1227#ifdef INSTRUMENTATION
1228m_cpt_itlb_miss++;
1229#endif
1230                    r_icache_fsm          = ICACHE_TLB_WAIT;
1231                    r_icache_tlb_miss_req = true;
1232                    break;
1233                }
1234            } // end if itlb activated
1235
1236            // physical address registration
1237            r_icache_vci_paddr   = paddr;
1238
1239            // Finally, we send the response to processor, and compute next state
1240            if ( cacheable )
1241            {
1242                if (cache_state == CACHE_SLOT_STATE_EMPTY)          // cache miss
1243                {
1244
1245#ifdef INSTRUMENTATION
1246m_cpt_icache_miss++;
1247#endif
1248                    // we request a VCI transaction
1249                    r_icache_fsm      = ICACHE_MISS_SELECT;
1250#if DEBUG_ICACHE
1251if ( m_debug_icache_fsm )
1252std::cout << "  <PROC " << name() << " ICACHE_IDLE> READ MISS in icache" 
1253          << " : PADDR = " << std::hex << paddr << std::endl;
1254#endif
1255                   r_icache_miss_req = true;
1256                }
1257                else if (cache_state == CACHE_SLOT_STATE_ZOMBI )        // pending cleanup
1258                {
1259                    // stalled until cleanup is acknowledged
1260                    r_icache_fsm       = ICACHE_IDLE;
1261                }
1262                else                                                // cache hit
1263                {
1264
1265#ifdef INSTRUMENTATION
1266m_cpt_ins_read++;
1267#endif
1268                    // return instruction to processor
1269                    m_irsp.valid       = true;
1270                    m_irsp.instruction = cache_inst;
1271                    r_icache_fsm       = ICACHE_IDLE;
1272#if DEBUG_ICACHE
1273if ( m_debug_icache_fsm )
1274std::cout << "  <PROC " << name() << " ICACHE_IDLE> READ HIT in icache" 
1275          << " : PADDR = " << std::hex << paddr
1276          << " / INST  = " << cache_inst << std::endl;
1277#endif
1278                }
1279            }
1280            else                // non cacheable read
1281            {
1282                r_icache_unc_req  = true;
1283                r_icache_fsm      = ICACHE_UNC_WAIT;
1284
1285#if DEBUG_ICACHE
1286if ( m_debug_icache_fsm )
1287{
1288    std::cout << "  <PROC " << name()
1289              << " ICACHE_IDLE> READ UNCACHEABLE in icache" 
1290              << " : PADDR = " << std::hex << paddr << std::endl;
1291}
1292#endif
1293            }
1294        }    // end if m_ireq.valid
1295        break;
1296    }
1297    /////////////////////
1298    case ICACHE_TLB_WAIT:       // Waiting the itlb update by the DCACHE FSM after a tlb miss
1299                            // the itlb is udated by the DCACHE FSM, as well as the
1300                            // r_mmu_ietr and r_mmu_ibvar registers in case of error.
1301                            // the itlb is not accessed by ICACHE FSM until DCACHE FSM
1302                            // reset the r_icache_tlb_miss_req flip-flop
1303                            // external coherence request are accepted in this state.
1304    {
1305        // coherence clack interrupt
1306        if ( r_icache_clack_req.read() )
1307        {
1308            r_icache_fsm = ICACHE_CC_CHECK;
1309            r_icache_fsm_save = r_icache_fsm.read();
1310            break;
1311        }
1312
1313        // coherence interrupt
1314        if ( r_cc_receive_icache_req.read() and not r_icache_cc_send_req.read())
1315        {
1316            r_icache_fsm = ICACHE_CC_CHECK;
1317            r_icache_fsm_save = r_icache_fsm.read();
1318            break;
1319        }
1320
1321        if ( m_ireq.valid ) m_cost_ins_tlb_miss_frz++;
1322
1323        // DCACHE FSM signals response by reseting the request flip-flop
1324        if ( not r_icache_tlb_miss_req.read() )
1325        {
1326            if ( r_icache_tlb_rsp_error.read() ) // error reported : tlb not updated
1327            {
1328                r_icache_tlb_rsp_error = false;
1329                m_irsp.error             = true;
1330                m_irsp.valid             = true;
1331                r_icache_fsm             = ICACHE_IDLE;
1332            }
1333            else                                // tlb updated : return to IDLE state
1334            {
1335                r_icache_fsm  = ICACHE_IDLE;
1336            }
1337        }
1338        break;
1339    }
1340    //////////////////////////
1341    case ICACHE_XTN_TLB_FLUSH:          // invalidate in one cycle all non global TLB entries
1342    {
1343        r_itlb.flush();
1344        r_dcache_xtn_req     = false;
1345        r_icache_fsm         = ICACHE_IDLE;
1346        break;
1347    }
1348    ////////////////////////////
1349    case ICACHE_XTN_CACHE_FLUSH:        // Invalidate sequencially all cache lines, using
1350                                    // r_icache_flush_count as a slot counter,
1351                                        // looping in this state until all slots are visited.
1352                                    // It can require two cycles per slot:
1353                                    // We test here the slot state, and make the actual inval
1354                                    // (if line is valid) in ICACHE_XTN_CACHE_FLUSH_GO state.
1355                                        // A cleanup request is generated for each valid line
1356    {
1357        // coherence clack interrupt
1358        if ( r_icache_clack_req.read() )
1359        {
1360            r_icache_fsm = ICACHE_CC_CHECK;
1361            r_icache_fsm_save = r_icache_fsm.read();
1362            break;
1363        }
1364
1365        // coherence request (from CC_RECEIVE FSM)
1366        if ( r_cc_receive_icache_req.read() and not r_icache_cc_send_req.read())
1367        {
1368            r_icache_fsm = ICACHE_CC_CHECK;
1369            r_icache_fsm_save = r_icache_fsm.read();
1370            break;
1371        }
1372
1373        if ( not r_icache_cc_send_req.read() ) // blocked until previous cc_send request is sent
1374        {
1375            int       state;
1376            paddr_t   tag;
1377            size_t        way = r_icache_flush_count.read()/m_icache_sets;
1378            size_t        set = r_icache_flush_count.read()%m_icache_sets;
1379
1380#ifdef INSTRUMENTATION
1381m_cpt_icache_dir_read++;
1382#endif
1383            r_icache.read_dir( way,
1384                               set,
1385                               &tag,
1386                               &state );
1387
1388            if ( state == CACHE_SLOT_STATE_VALID )    // inval required
1389            {
1390                // request cleanup
1391                r_icache_cc_send_req   = true;
1392                r_icache_cc_send_nline = tag * m_icache_sets + set;
1393                r_icache_cc_send_way   = way;
1394                r_icache_cc_send_type  = CC_TYPE_CLEANUP;
1395
1396                // goes to ICACHE_XTN_CACHE_FLUSH_GO to make inval
1397                r_icache_miss_way     = way;
1398                r_icache_miss_set     = set;
1399                r_icache_fsm          = ICACHE_XTN_CACHE_FLUSH_GO;
1400            }
1401            else if ( r_icache_flush_count.read() ==
1402                      (m_icache_sets*m_icache_ways - 1) )  // last slot
1403            {
1404                r_dcache_xtn_req = false;
1405                m_drsp.valid     = true;
1406                r_icache_fsm     = ICACHE_IDLE;
1407            }
1408
1409            // saturation counter, to have the same last slot condition
1410            // in ICACHE_XTN_CACHE_FLUSH and ICACHE_XTN_CACHE_FLUSH_GO states
1411            if ( r_icache_flush_count.read() < (m_icache_sets*m_icache_ways - 1) )
1412            {
1413                r_icache_flush_count = r_icache_flush_count.read() + 1;
1414            }
1415        }
1416        break;
1417    }
1418    ///////////////////////////////
1419    case ICACHE_XTN_CACHE_FLUSH_GO:         // Switch slot state to ZOMBI for an XTN flush
1420    {
1421        size_t    way = r_icache_miss_way.read();
1422        size_t    set = r_icache_miss_set.read();
1423
1424#ifdef INSTRUMENTATION
1425m_cpt_icache_dir_write++;
1426#endif
1427
1428        r_icache.write_dir( way,
1429                            set,
1430                            CACHE_SLOT_STATE_ZOMBI );
1431
1432        if ( r_icache_flush_count.read() ==
1433                      (m_icache_sets*m_icache_ways - 1) )  // last slot
1434        {
1435                r_dcache_xtn_req = false;
1436            m_drsp.valid     = true;
1437                r_icache_fsm     = ICACHE_IDLE;
1438        }
1439        else
1440        {
1441            r_icache_fsm         = ICACHE_XTN_CACHE_FLUSH;
1442        }
1443        break;
1444    }
1445
1446    //////////////////////////
1447    case ICACHE_XTN_TLB_INVAL:          // invalidate one TLB entry selected by the virtual address
1448                                                    // stored in the r_dcache_save_wdata register
1449    {
1450        r_itlb.inval(r_dcache_save_wdata.read());
1451        r_dcache_xtn_req     = false;
1452        r_icache_fsm         = ICACHE_IDLE;
1453        break;
1454    }
1455    ///////////////////////////////
1456    case ICACHE_XTN_CACHE_INVAL_VA:     // Selective cache line invalidate with virtual address
1457                                    // requires 3 cycles (in case of hit on itlb and icache).
1458                                                        // In this state, access TLB to translate virtual address
1459                                                    // stored in the r_dcache_save_wdata register.
1460    {
1461        paddr_t         paddr;
1462        bool            hit;
1463
1464        // read physical address in TLB when MMU activated
1465        if ( r_mmu_mode.read() & INS_TLB_MASK )         // itlb activated
1466        {
1467
1468#ifdef INSTRUMENTATION
1469m_cpt_itlb_read++;
1470#endif
1471            hit = r_itlb.translate(r_dcache_save_wdata.read(),
1472                                   &paddr);
1473        }
1474        else                                            // itlb not activated
1475        {
1476            paddr       = (paddr_t)r_dcache_save_wdata.read();
1477            hit         = true;
1478        }
1479
1480        if ( hit )              // continue the selective inval process
1481        {
1482            r_icache_vci_paddr    = paddr;
1483            r_icache_fsm          = ICACHE_XTN_CACHE_INVAL_PA;
1484        }
1485        else                    // miss : send a request to DCACHE FSM
1486        {
1487
1488#ifdef INSTRUMENTATION
1489m_cpt_itlb_miss++;
1490#endif
1491            r_icache_tlb_miss_req = true;
1492                r_icache_vaddr_save   = r_dcache_save_wdata.read();
1493            r_icache_fsm          = ICACHE_TLB_WAIT;
1494        }
1495        break;
1496    }
1497    ///////////////////////////////
1498    case ICACHE_XTN_CACHE_INVAL_PA:     // selective invalidate cache line with physical address
1499                                    // require 2 cycles. In this state, we read directory
1500                                    // with address stored in r_icache_vci_paddr register.
1501    {
1502        int         state;
1503        size_t          way;
1504        size_t          set;
1505        size_t          word;
1506
1507#ifdef INSTRUMENTATION
1508m_cpt_icache_dir_read++;
1509#endif
1510        r_icache.read_dir(r_icache_vci_paddr.read(),
1511                          &state,
1512                          &way,
1513                          &set,
1514                          &word);
1515
1516        if ( state == CACHE_SLOT_STATE_VALID )  // inval to be done
1517        {
1518            r_icache_miss_way = way;
1519            r_icache_miss_set = set;
1520            r_icache_fsm      = ICACHE_XTN_CACHE_INVAL_GO;
1521        }
1522        else            // miss : acknowlege the XTN request and return
1523        {
1524            r_dcache_xtn_req = false;
1525            r_icache_fsm     = ICACHE_IDLE;
1526        }
1527        break;
1528    }
1529    ///////////////////////////////
1530    case ICACHE_XTN_CACHE_INVAL_GO:  // Switch slot to ZOMBI state for an XTN inval
1531    {
1532        if ( not r_icache_cc_send_req.read() )  // blocked until previous cc_send request not sent
1533        {
1534
1535#ifdef INSTRUMENTATION
1536m_cpt_icache_dir_write++;
1537#endif
1538            r_icache.write_dir( r_icache_miss_way.read(),
1539                                r_icache_miss_set.read(),
1540                                CACHE_SLOT_STATE_ZOMBI );
1541
1542            // request cleanup
1543            r_icache_cc_send_req   = true;
1544            r_icache_cc_send_nline = r_icache_vci_paddr.read() / (m_icache_words<<2);
1545            r_icache_cc_send_way   = r_icache_miss_way.read();
1546            r_icache_cc_send_type  = CC_TYPE_CLEANUP;
1547
1548            // acknowledge the XTN request and return
1549            r_dcache_xtn_req      = false;
1550            r_icache_fsm          = ICACHE_IDLE;
1551        }
1552        break;
1553    }
1554    ////////////////////////
1555    case ICACHE_MISS_SELECT:       // Try to select a slot in associative set,
1556                                   // Waiting in this state if no slot available.
1557                                   // If a victim slot has been choosen and the r_icache_cc_send_req is false,
1558                                   // we send the cleanup request in this state.
1559                                   // If not, a r_icache_cleanup_victim_req flip-flop is
1560                                   // utilized for saving this cleanup request, and it will be sent later
1561                                   // in state ICACHE_MISS_WAIT or ICACHE_MISS_UPDT_DIR.
1562                                   // The r_icache_miss_clack flip-flop is set
1563                                   // when a cleanup is required
1564    {
1565        if (m_ireq.valid) m_cost_ins_miss_frz++;
1566
1567        // coherence clack interrupt
1568        if ( r_icache_clack_req.read() )
1569        {
1570            r_icache_fsm = ICACHE_CC_CHECK;
1571            r_icache_fsm_save = r_icache_fsm.read();
1572            break;
1573        }
1574
1575        // coherence interrupt
1576        if ( r_cc_receive_icache_req.read() and not r_icache_cc_send_req.read())
1577        {
1578            r_icache_fsm = ICACHE_CC_CHECK;
1579            r_icache_fsm_save = r_icache_fsm.read();
1580            break;
1581        }
1582
1583
1584        bool        found;
1585        bool        cleanup;
1586        size_t          way;
1587        size_t          set;
1588        paddr_t         victim;
1589
1590#ifdef INSTRUMENTATION
1591m_cpt_icache_dir_read++;
1592#endif
1593        r_icache.read_select(r_icache_vci_paddr.read(),
1594                             &victim,
1595                             &way,
1596                             &set,
1597                             &found,
1598                             &cleanup );
1599        if ( not found )
1600        {
1601            break;
1602        }
1603        else
1604        {
1605            r_icache_miss_way     = way;
1606            r_icache_miss_set     = set;
1607
1608            if ( cleanup )
1609            {
1610                if ( not r_icache_cc_send_req.read() )
1611                {
1612                    r_icache_cc_send_req    = true;
1613                    r_icache_cc_send_nline  = victim;
1614                    r_icache_cc_send_way    = way;
1615                    r_icache_cc_send_type   = CC_TYPE_CLEANUP;   
1616                }
1617                else
1618                {
1619                    r_icache_cleanup_victim_req   = true;
1620                    r_icache_cleanup_victim_nline = victim;
1621                }
1622
1623                r_icache_miss_clack           = true;
1624                r_icache_fsm                  = ICACHE_MISS_CLEAN;
1625            }
1626            else
1627            {
1628                r_icache_fsm          = ICACHE_MISS_WAIT;
1629            }
1630
1631#if DEBUG_ICACHE
1632if ( m_debug_icache_fsm )
1633{
1634    std::cout << "  <PROC " << name()
1635              << " ICACHE_MISS_SELECT> Select a slot:" << std::dec
1636              << " / WAY = " << way
1637              << " / SET = " << set;
1638    if (cleanup) std::cout << " / VICTIM = " << std::hex << victim << std::endl;
1639    else         std::cout << std::endl;
1640}
1641#endif
1642        }
1643        break;
1644    }
1645    ///////////////////////
1646    case ICACHE_MISS_CLEAN:              // switch the slot to zombi state
1647    {
1648        if (m_ireq.valid) m_cost_ins_miss_frz++;
1649
1650#ifdef INSTRUMENTATION
1651m_cpt_icache_dir_write++;
1652#endif
1653        r_icache.write_dir( r_icache_miss_way.read(),
1654                            r_icache_miss_set.read(),
1655                            CACHE_SLOT_STATE_ZOMBI);
1656#if DEBUG_ICACHE
1657if ( m_debug_icache_fsm )
1658{
1659    std::cout << "  <PROC " << name()
1660              << " ICACHE_MISS_CLEAN> Switch to ZOMBI state" << std::dec
1661              << " / WAY = " << r_icache_miss_way.read()
1662              << " / SET = " << r_icache_miss_set.read() << std::endl;
1663}
1664#endif
1665
1666        r_icache_fsm = ICACHE_MISS_WAIT;
1667        break;
1668    }
1669    //////////////////////
1670    case ICACHE_MISS_WAIT:        // waiting response from VCI_RSP FSM
1671    {
1672        if (m_ireq.valid) m_cost_ins_miss_frz++;
1673
1674        // send cleanup victim request
1675        if ( r_icache_cleanup_victim_req.read() and not r_icache_cc_send_req.read() )
1676        {
1677            r_icache_cc_send_req        = true;
1678            r_icache_cc_send_nline      = r_icache_cleanup_victim_nline;
1679            r_icache_cc_send_way        = r_icache_miss_way;
1680            r_icache_cc_send_type       = CC_TYPE_CLEANUP; 
1681            r_icache_cleanup_victim_req = false;
1682        }   
1683
1684        // coherence clack interrupt
1685        if ( r_icache_clack_req.read() )
1686        {
1687            r_icache_fsm = ICACHE_CC_CHECK;
1688            r_icache_fsm_save = r_icache_fsm.read();
1689            break;
1690        }
1691       
1692        // coherence interrupt
1693        if ( r_cc_receive_icache_req.read() and not r_icache_cc_send_req.read() and not r_icache_cleanup_victim_req.read() )
1694        {
1695            r_icache_fsm = ICACHE_CC_CHECK;
1696            r_icache_fsm_save = r_icache_fsm.read();
1697            break;
1698        }
1699
1700        if ( r_vci_rsp_ins_error.read() ) // bus error
1701        {
1702            r_mmu_ietr          = MMU_READ_DATA_ILLEGAL_ACCESS;
1703            r_mmu_ibvar         = r_icache_vaddr_save.read();
1704            m_irsp.valid        = true;
1705            m_irsp.error        = true;
1706            r_vci_rsp_ins_error = false;
1707            r_icache_fsm        = ICACHE_IDLE;
1708        }
1709        else if ( r_vci_rsp_fifo_icache.rok() ) // response available
1710        {
1711            r_icache_miss_word = 0;
1712            r_icache_fsm       = ICACHE_MISS_DATA_UPDT;
1713        }
1714        break;
1715    }
1716    ///////////////////////////
1717    case ICACHE_MISS_DATA_UPDT:   // update the cache (one word per cycle)
1718    {
1719        if ( m_ireq.valid ) m_cost_ins_miss_frz++;
1720
1721        if ( r_vci_rsp_fifo_icache.rok() )      // response available
1722        {
1723
1724#ifdef INSTRUMENTATION
1725m_cpt_icache_data_write++;
1726#endif
1727            r_icache.write( r_icache_miss_way.read(),
1728                            r_icache_miss_set.read(),
1729                            r_icache_miss_word.read(),
1730                            r_vci_rsp_fifo_icache.read() );
1731#if DEBUG_ICACHE
1732if ( m_debug_icache_fsm )
1733{
1734    std::cout << "  <PROC " << name()
1735              << " ICACHE_MISS_DATA_UPDT> Write one word:"
1736              << " WDATA = " << std::hex << r_vci_rsp_fifo_icache.read()
1737              << " WAY = " << r_icache_miss_way.read()
1738              << " SET = " << r_icache_miss_set.read()
1739              << " WORD = " << r_icache_miss_word.read() << std::endl;
1740}
1741#endif
1742            vci_rsp_fifo_icache_get = true;
1743            r_icache_miss_word = r_icache_miss_word.read() + 1;
1744
1745            if ( r_icache_miss_word.read() == m_icache_words-1 )        // last word
1746            {
1747                r_icache_fsm = ICACHE_MISS_DIR_UPDT;
1748            }
1749        }
1750        break;
1751    }
1752    //////////////////////////
1753    case ICACHE_MISS_DIR_UPDT:  // Stalled if a victim line has been evicted,
1754                                // and the cleanup ack has not been received,
1755                                // as indicated by r_icache_miss_clack.
1756                                // - If no matching coherence request (r_icache_miss_inval)
1757                                //   switch directory slot to VALID state.
1758                                // - If matching coherence request, switch directory slot
1759                                //   to ZOMBI state, and send a cleanup request.
1760    {
1761        if ( m_ireq.valid ) m_cost_ins_miss_frz++;
1762
1763        // send cleanup victim request
1764        if ( r_icache_cleanup_victim_req.read() and not r_icache_cc_send_req.read() )
1765        {
1766            r_icache_cc_send_req        = true;
1767            r_icache_cc_send_nline      = r_icache_cleanup_victim_nline;
1768            r_icache_cc_send_way        = r_icache_miss_way;
1769            r_icache_cc_send_type       = CC_TYPE_CLEANUP; 
1770            r_icache_cleanup_victim_req = false;
1771        }   
1772
1773        // coherence clack interrupt
1774        if ( r_icache_clack_req.read() )
1775        {
1776            r_icache_fsm = ICACHE_CC_CHECK;
1777            r_icache_fsm_save = r_icache_fsm.read();
1778            break;
1779        }
1780
1781        // coherence interrupt
1782        if ( r_cc_receive_icache_req.read() and not r_icache_cc_send_req.read() and not r_icache_cleanup_victim_req.read() )
1783        {
1784            r_icache_fsm = ICACHE_CC_CHECK;
1785            r_icache_fsm_save = r_icache_fsm.read();
1786            break;
1787        }
1788
1789        if ( not r_icache_miss_clack.read() ) // waiting cleanup acknowledge for victim line
1790        {
1791            if ( r_icache_miss_inval )    // Switch slot to ZOMBI state, and new cleanup
1792            {
1793                if ( not r_icache_cc_send_req.read() )
1794                {
1795                    r_icache_miss_inval    = false;
1796                    // request cleanup
1797                    r_icache_cc_send_req   = true;
1798                    r_icache_cc_send_nline = r_icache_vci_paddr.read() / (m_icache_words<<2);
1799                    r_icache_cc_send_way   = r_icache_miss_way.read();
1800                    r_icache_cc_send_type  = CC_TYPE_CLEANUP;
1801
1802#ifdef INSTRUMENTATION
1803m_cpt_icache_dir_write++;
1804#endif
1805                    r_icache.write_dir( r_icache_vci_paddr.read(),
1806                                        r_icache_miss_way.read(),
1807                                        r_icache_miss_set.read(),
1808                                        CACHE_SLOT_STATE_ZOMBI );
1809#if DEBUG_ICACHE
1810if ( m_debug_icache_fsm )
1811{
1812    std::cout << "  <PROC " << name()
1813              << " ICACHE_MISS_DIR_UPDT> Switch cache slot to ZOMBI state"
1814              << " PADDR = " << std::hex << r_icache_vci_paddr.read()
1815              << " WAY = " << std::dec << r_icache_miss_way.read()
1816              << " SET = " << r_icache_miss_set.read() << std::endl;
1817}
1818#endif
1819                }
1820                else
1821                    break;
1822            }
1823            else                          // Switch slot to VALID state
1824            {
1825
1826#ifdef INSTRUMENTATION
1827m_cpt_icache_dir_write++;
1828#endif
1829                r_icache.write_dir( r_icache_vci_paddr.read(),
1830                                    r_icache_miss_way.read(),
1831                                    r_icache_miss_set.read(),
1832                                    CACHE_SLOT_STATE_VALID );
1833#if DEBUG_ICACHE
1834if ( m_debug_icache_fsm )
1835{
1836    std::cout << "  <PROC " << name()
1837              << " ICACHE_MISS_DIR_UPDT> Switch cache slot to VALID state"
1838              << " PADDR = " << std::hex << r_icache_vci_paddr.read()
1839              << " WAY = " << std::dec << r_icache_miss_way.read()
1840              << " SET = " << r_icache_miss_set.read() << std::endl;
1841}
1842#endif
1843            }
1844
1845            r_icache_fsm = ICACHE_IDLE;
1846        }
1847        break;
1848    }
1849    ////////////////////
1850    case ICACHE_UNC_WAIT:       // waiting a response to an uncacheable read from VCI_RSP FSM
1851    {
1852        // coherence clack interrupt
1853        if ( r_icache_clack_req.read() )
1854        {
1855            r_icache_fsm = ICACHE_CC_CHECK;
1856            r_icache_fsm_save = r_icache_fsm.read();
1857            break;
1858        }
1859
1860        // coherence interrupt
1861        if ( r_cc_receive_icache_req.read() and not r_icache_cc_send_req.read())
1862        {
1863            r_icache_fsm = ICACHE_CC_CHECK;
1864            r_icache_fsm_save = r_icache_fsm.read();
1865            break;
1866        }
1867
1868        if ( r_vci_rsp_ins_error.read() ) // bus error
1869        {
1870            r_mmu_ietr          = MMU_READ_DATA_ILLEGAL_ACCESS;
1871            r_mmu_ibvar         = m_ireq.addr;
1872            r_vci_rsp_ins_error = false;
1873            m_irsp.valid        = true;
1874            m_irsp.error        = true;
1875            r_icache_fsm        = ICACHE_IDLE;
1876        }
1877        else if (r_vci_rsp_fifo_icache.rok() ) // instruction available
1878        {
1879            vci_rsp_fifo_icache_get = true;
1880            r_icache_fsm            = ICACHE_IDLE;
1881            if ( m_ireq.valid and
1882                (m_ireq.addr == r_icache_vaddr_save.read()) ) // request unmodified
1883            {
1884                m_irsp.valid       = true;
1885                m_irsp.instruction = r_vci_rsp_fifo_icache.read();
1886            }
1887        }
1888        break;
1889    }
1890    /////////////////////
1891    case ICACHE_CC_CHECK:       // This state is the entry point of a sub-fsm
1892                                // handling coherence requests.
1893                                // if there is a matching pending miss, it is
1894                                // signaled in the r_icache_miss_inval flip-flop.
1895                                // The return state is defined in r_icache_fsm_save.
1896    {
1897        paddr_t  paddr = r_cc_receive_icache_nline.read() * m_icache_words * 4;
1898        paddr_t  mask  = ~((m_icache_words<<2)-1);
1899
1900        // CLACK handler
1901        // We switch the directory slot to EMPTY state
1902        // and reset r_icache_miss_clack if the cleanup ack
1903        // is matching a pending miss.
1904        if ( r_icache_clack_req.read() )
1905        {
1906
1907            if ( m_ireq.valid ) m_cost_ins_miss_frz++;
1908
1909#ifdef INSTRUMENTATION
1910m_cpt_icache_dir_write++;
1911#endif
1912            r_icache.write_dir( 0,
1913                                r_icache_clack_way.read(),
1914                                r_icache_clack_set.read(),
1915                                CACHE_SLOT_STATE_EMPTY);
1916
1917            if ( (r_icache_miss_set.read() == r_icache_clack_set.read()) and
1918                 (r_icache_miss_way.read() == r_icache_clack_way.read()) )
1919            {
1920                r_icache_miss_clack = false;
1921            }
1922
1923            r_icache_clack_req = false;
1924
1925            // return to cc_save state
1926            r_icache_fsm = r_icache_fsm_save.read();
1927
1928#if DEBUG_ICACHE
1929if ( m_debug_icache_fsm )
1930{
1931    std::cout << "  <PROC " << name()
1932        << " ICACHE_CC_CHECK>  CC_TYPE_CLACK slot returns to empty state"
1933        << " set = " << r_icache_clack_set.read()
1934        << " / way = " << r_icache_clack_way.read() << std::endl;
1935}
1936#endif
1937
1938            break;
1939        }
1940
1941        assert ( not r_icache_cc_send_req.read() and "CC_SEND must be available in ICACHE_CC_CHECK");
1942
1943        // Match between MISS address and CC address
1944        if (r_cc_receive_icache_req.read() and
1945          ((r_icache_fsm_save.read() == ICACHE_MISS_SELECT  )  or
1946           (r_icache_fsm_save.read() == ICACHE_MISS_WAIT    )  or
1947           (r_icache_fsm_save.read() == ICACHE_MISS_DIR_UPDT)) and
1948          ((r_icache_vci_paddr.read() & mask) == (paddr & mask)) ) // matching
1949        {
1950            // signaling the matching
1951            r_icache_miss_inval     = true;
1952
1953            // in case of update, go to CC_UPDT
1954            // JUST TO POP THE FIFO
1955            if (r_cc_receive_icache_type.read() == CC_TYPE_UPDT)
1956            {
1957                r_icache_fsm        = ICACHE_CC_UPDT;
1958                r_icache_cc_word    = r_cc_receive_word_idx.read();
1959
1960                // just pop the fifo , don't write in icache
1961                r_icache_cc_need_write = false;
1962            }
1963            // the request is dealt with
1964            else
1965            {
1966                r_cc_receive_icache_req = false;
1967                r_icache_fsm          = r_icache_fsm_save.read();
1968            }
1969#if DEBUG_ICACHE
1970if ( m_debug_icache_fsm )
1971{
1972    std::cout << "  <PROC " << name()
1973              << " ICACHE_CC_CHECK> Coherence request matching a pending miss:"
1974              << " PADDR = " << std::hex << paddr << std::endl;
1975}
1976#endif
1977        }
1978
1979        // CC request handler
1980
1981        int             state = 0;
1982        size_t      way = 0;
1983        size_t      set = 0;
1984        size_t      word = 0;
1985
1986#ifdef INSTRUMENTATION
1987m_cpt_icache_dir_read++;
1988#endif
1989        r_icache.read_dir(paddr,
1990                          &state,
1991                          &way,
1992                          &set,
1993                          &word);
1994
1995        r_icache_cc_way = way;
1996        r_icache_cc_set = set;
1997
1998        if ( state == CACHE_SLOT_STATE_VALID)            // hit
1999        {
2000            // need to update the cache state
2001            if (r_cc_receive_icache_type.read() == CC_TYPE_UPDT)  // hit update
2002            {
2003                r_icache_cc_need_write = true;
2004                r_icache_fsm           = ICACHE_CC_UPDT;
2005                r_icache_cc_word       = r_cc_receive_word_idx.read();
2006            }
2007            else if ( r_cc_receive_icache_type.read() == CC_TYPE_INVAL ) // hit inval
2008            {
2009                r_icache_fsm           = ICACHE_CC_INVAL;
2010            }
2011        }
2012        else                                      // miss
2013        {
2014            // multicast acknowledgement required in case of update
2015            if(r_cc_receive_icache_type.read() == CC_TYPE_UPDT)
2016            {
2017                r_icache_fsm           = ICACHE_CC_UPDT;
2018                r_icache_cc_word       = r_cc_receive_word_idx.read();
2019
2020                // just pop the fifo , don't write in icache
2021                r_icache_cc_need_write = false;
2022            }
2023            else // No response needed
2024            {
2025                r_cc_receive_icache_req = false;
2026                r_icache_fsm            = r_icache_fsm_save.read();
2027            }
2028        }
2029        break;
2030    }
2031    /////////////////////
2032    case ICACHE_CC_INVAL:       // hit inval : switch slot to ZOMBI state
2033    {
2034        assert (not r_icache_cc_send_req.read() &&
2035                "ERROR in ICACHE_CC_INVAL: the r_icache_cc_send_req "
2036                "must not be set");
2037
2038#ifdef INSTRUMENTATION
2039m_cpt_icache_dir_read++;
2040#endif
2041
2042        // Switch slot state to ZOMBI and send CLEANUP command
2043        r_icache.write_dir( r_icache_cc_way.read(),
2044                            r_icache_cc_set.read(),
2045                            CACHE_SLOT_STATE_ZOMBI );
2046
2047        // coherence request completed
2048        r_icache_cc_send_req   = true;
2049        r_icache_cc_send_nline = r_cc_receive_icache_nline.read();
2050        r_icache_cc_send_way   = r_icache_cc_way.read();
2051        r_icache_cc_send_type  = CC_TYPE_CLEANUP;
2052
2053        r_icache_fsm           = r_icache_fsm_save.read();
2054
2055#if DEBUG_ICACHE
2056if ( m_debug_icache_fsm )
2057{
2058std::cout << "  <PROC " << name()
2059          << " ICACHE_CC_INVAL> slot returns to ZOMBI state"
2060          << " set = " << r_icache_cc_set.read()
2061          << " / way = " << r_icache_cc_way.read() << std::endl;
2062}
2063#endif
2064
2065        break;
2066    }
2067    ////////////////////
2068    case ICACHE_CC_UPDT:        // hit update : write one word per cycle
2069    {
2070        assert (not r_icache_cc_send_req.read() &&
2071                "ERROR in ICACHE_CC_UPDT: the r_icache_cc_send_req "
2072                "must not be set");
2073
2074        if ( not r_cc_receive_updt_fifo_be.rok() ) break;
2075
2076
2077        size_t  word  = r_icache_cc_word.read();
2078        size_t  way   = r_icache_cc_way.read();
2079        size_t  set   = r_icache_cc_set.read();
2080
2081        if (r_icache_cc_need_write.read())
2082        {
2083            r_icache.write( way,
2084                            set,
2085                            word,
2086                            r_cc_receive_updt_fifo_data.read(),
2087                            r_cc_receive_updt_fifo_be.read() );
2088
2089            r_icache_cc_word = word+1;
2090
2091#ifdef INSTRUMENTATION
2092m_cpt_icache_data_write++;
2093#endif
2094
2095#if DEBUG_ICACHE
2096if ( m_debug_icache_fsm )
2097{
2098    std::cout << "  <PROC " << name()
2099              << " ICACHE_CC_UPDT> Write one word "
2100              << " set = " << r_icache_cc_set.read()
2101              << " / way = " << r_icache_cc_way.read()
2102              << " / word = " << r_icache_cc_word.read() << std::endl;
2103}
2104#endif
2105        }
2106
2107        if ( r_cc_receive_updt_fifo_eop.read() )        // last word
2108        {
2109            // no need to write in the cache anymore
2110            r_icache_cc_need_write        = false;
2111
2112            // coherence request completed
2113            r_cc_receive_icache_req       = false;
2114
2115            // request multicast acknowledgement
2116            r_icache_cc_send_req          = true;
2117            r_icache_cc_send_nline        = r_cc_receive_icache_nline.read();
2118            r_icache_cc_send_updt_tab_idx = r_cc_receive_icache_updt_tab_idx.read();
2119            r_icache_cc_send_type         = CC_TYPE_MULTI_ACK;
2120
2121            r_icache_fsm                  = r_icache_fsm_save.read();
2122        }
2123        //consume fifo if not eop
2124        cc_receive_updt_fifo_get  = true;
2125
2126        break;
2127    }
2128
2129    } // end switch r_icache_fsm
2130
2131    ////////////////////////////////////////////////////////////////////////////////////
2132    //      DCACHE FSM
2133    //
2134    // 1/ Coherence operations
2135    //    They are handled as interrupts generated by the CC_RECEIVE FSM.
2136    //    - There is a coherence request when r_tgt_dcache_req is set.
2137    //    They are taken in IDLE, MISS_WAIT, MISS_DIR_UPDT, UNC_WAIT, LL_WAIT
2138    //    and SC_WAIT states.
2139    //    - There is a cleanup acknowledge request when r_cleanup_dcache_req is set.
2140    //    They are taken in IDLE, MISS_SELECT, MISS_CLEAN, MISS_WAIT, MISS_DATA_UPDT,
2141    //    MISS_DIR_UPDT, UNC_WAIT, LL_WAIT, SC_WAIT states.
2142    //    - For both types of requests, actions associated to the pre-empted state
2143    //    are not executed. The DCACHE FSM goes to the proper sub-FSM (CC_CHECK
2144    //    or CC_CLACK) to execute the requested coherence operation, and returns
2145    //    to the pre-empted state.
2146    //
2147    // 2/ TLB miss
2148    //    The page tables are generally cacheable.
2149    //    In case of miss in itlb or dtlb, the tlb miss is handled by a dedicated
2150    //    sub-fsm (DCACHE_TLB_MISS state), that handle possible miss in DCACHE,
2151    //    this sub-fsm implement the table-walk...
2152    //
2153    // 3/ processor requests
2154    //    Processor requests are taken in IDLE state only.
2155    //    The IDLE state implements a two stages pipe-line to handle write bursts:
2156    //    - Both DTLB and DCACHE are accessed in stage P0 (if processor request valid).
2157    //    - The registration in wbuf and the dcache update is done in stage P1
2158    //      (if the processor request is a write).
2159    //    The two r_dcache_wbuf_req and r_dcache_updt_req flip-flops define
2160    //    the operations that must be done in P1 stage, and the access type
2161    //    (read or write) to the DATA part of DCACHE depends on r_dcache_updt_req.
2162    //    READ requests are delayed if a cache update is requested.
2163    //    WRITE or SC requests can require a PTE Dirty bit update (in memory),
2164    //    that is done (before handling the processor request) by a dedicated sub-fsm.
2165    //    If a PTE is modified, both the itlb and dtlb are selectively, but sequencially
2166    //    cleared by a dedicated sub_fsm (DCACHE_INVAL_TLB_SCAN state).
2167    //
2168    // 4/ Atomic instructions LL/SC
2169    //    The LL/SC address are non cacheable (systematic access to memory).
2170    //    The llsc buffer contains a registration for an active LL/SC operation
2171    //    (with an address, a registration key, an aging counter and a valid bit).
2172    //    - LL requests from the processor are transmitted as a one flit VCI command
2173    //      (CMD_LOCKED_READ as CMD, and TYPE_LL as PKTID value). PLEN must
2174    //      be 8 as the response is 2 flits long (data and registration key)
2175    //    - SC requests from the processor are systematically transmitted to the
2176    //      memory cache as 2 flits VCI command (CMD_STORE_COND as CMD, and TYPE_SC
2177    //      as PKTID value).  The first flit contains the registration key, the second
2178    //      flit contains the data to write in case of success.
2179    //      The cache is not updated, as this is done in case of success by the
2180    //      coherence transaction.
2181    //
2182    // 5/ Non cacheable access:
2183    //    This component implement a strong order between non cacheable access
2184    //    (read or write) : A new non cacheable VCI transaction starts only when
2185    //    the previous non cacheable transaction is completed. After send the VCI
2186    //    transaction, the DCACHE FSM wait for the respone in the DCACHE_UNC_WAIT state.
2187    //    So the processor is blocked until the respone arrives in CACHE L1.
2188    //
2189    // 6/ Error handling:
2190    //    When the MMU is not activated, Read Bus Errors are synchronous events,
2191    //    Some Write Bus Errors are synchronous events when the request is a non cacheable access
2192    //    but some Write Bus Errors are asynchronous events when the request is cacheable access
2193    //    (processor is not frozen).
2194    //    - If a Read Bus Error or a Non Cacheable Write Bus Error is detected, the VCI_RSP FSM sets the
2195    //      r_vci_rsp_data_error flip-flop, without writing any data in the
2196    //      r_vci_rsp_fifo_dcache FIFO, and the synchronous error is signaled
2197    //      by the DCACHE FSM.
2198    //    - If a Cacheable Write Bus Error is detected, the VCI_RSP_FSM signals
2199    //    the asynchronous error using the setWriteBerr() method.
2200    //    When the MMU is activated bus error are rare events, as the MMU
2201    //    checks the physical address before the VCI transaction starts.
2202    ////////////////////////////////////////////////////////////////////////////////////////
2203
2204    // default value for m_drsp
2205    m_drsp.valid = false;
2206    m_drsp.error = false;
2207    m_drsp.rdata = 0;
2208
2209    switch ( r_dcache_fsm.read() )
2210    {
2211    case DCACHE_IDLE: // There are 10 conditions to exit the IDLE state :
2212                      // 1) ITLB/DTLB inval request (update)  => DCACHE_INVAL_TLB_SCAN
2213                      // 2) Coherence request (TGT FSM)       => DCACHE_CC_CHECK
2214                      // 3) ITLB miss request (ICACHE FSM)    => DCACHE_TLB_MISS
2215                      // 4) XTN request (processor)           => DCACHE_XTN_*
2216                      // 5) DTLB miss (processor)             => DCACHE_TLB_MISS
2217                      // 6) Dirty bit update (processor)      => DCACHE_DIRTY_GET_PTE
2218                      // 7) Cacheable read miss (processor)   => DCACHE_MISS_SELECT
2219                      // 8) Uncacheable read (processor)      => DCACHE_UNC_WAIT
2220                      // 9) LL access (processor)             => DCACHE_LL_WAIT
2221                      // 10) SC access (processor)            => DCACHE_SC_WAIT
2222                      //
2223                      // There is a fixed priority to handle requests to DCACHE:
2224                      //    1/ the ITLB/DTLB invalidate requests
2225                      //    2/ the coherence requests,
2226                      //    3/ the processor requests (including DTLB miss),
2227                      //    4/ the ITLB miss requests,
2228                      // The address space processor request are handled as follows:
2229                      // - WRITE request is blocked if the Dirty bit mus be set.
2230                      // If DTLB hit, the P1 stage is activated (writes WBUF, and
2231                      // updates DCACHE if DCACHE hit) & processor request acknowledged.
2232                      // - READ request generate a simultaneouss access to  DCACHE.DATA
2233                      // and DCACHE.DIR, but is delayed if DCACHE update required.
2234                      //
2235                      // There is 4 configurations defining the access type to
2236                      // DTLB, DCACHE.DATA, and DCACHE.DIR, depending on the
2237                      // dreq.valid (dreq) and r_dcache_updt_req (updt) signals:
2238                      //    dreq / updt / DTLB  / DCACHE.DIR / DCACHE.DATA /
2239                      //     0   /  0   / NOP   / NOP        / NOP         /
2240                      //     0   /  1   / NOP   / NOP        / WRITE       /
2241                      //     1   /  0   / READ  / READ       / NOP         /
2242                      //     1   /  1   / READ  / READ       / WRITE       /
2243                      // Those two registers are set at each cycle from the 3 signals
2244                      // updt_request, wbuf_request, wbuf_write_miss.
2245    {
2246        paddr_t     paddr;
2247        pte_info_t      tlb_flags;
2248        size_t          tlb_way;
2249        size_t          tlb_set;
2250        paddr_t         tlb_nline = 0;
2251        size_t          cache_way;
2252        size_t          cache_set;
2253        size_t          cache_word;
2254        uint32_t        cache_rdata = 0;
2255        bool        tlb_hit = false;
2256        int             cache_state = CACHE_SLOT_STATE_EMPTY;
2257
2258        bool        tlb_inval_required = false;     // request TLB inval after cache update
2259        bool        wbuf_write_miss    = false;     // miss a WBUF write request
2260        bool        updt_request       = false;     // request DCACHE update in P1 stage
2261        bool        wbuf_request       = false;     // request WBUF write in P1 stage
2262
2263        // physical address computation : systematic DTLB access if activated)
2264        paddr = (paddr_t) m_dreq.addr;
2265        if ( m_dreq.valid )
2266        {
2267            if ( r_mmu_mode.read() & DATA_TLB_MASK )  // DTLB activated
2268            {
2269                tlb_hit = r_dtlb.translate( m_dreq.addr,
2270                                            &paddr,
2271                                            &tlb_flags,
2272                                            &tlb_nline,
2273                                            &tlb_way,
2274                                            &tlb_set );
2275#ifdef INSTRUMENTATION
2276m_cpt_dtlb_read++;
2277#endif
2278            }
2279            else                                    // identity mapping
2280            {
2281                // we take into account the paddr extension
2282                if (vci_param::N > 32) 
2283                    paddr = paddr | ((paddr_t)(r_dcache_paddr_ext.read()) << 32);
2284            }
2285        } // end physical address computation
2286
2287        // systematic DCACHE access depending on r_dcache_updt_req (if activated)
2288        if ( r_mmu_mode.read() & DATA_CACHE_MASK)
2289        {
2290
2291            if ( m_dreq.valid and r_dcache_updt_req.read() ) // read DIR and write DATA
2292            {
2293                r_dcache.read_dir( paddr,
2294                                   &cache_state,
2295                                   &cache_way,
2296                                   &cache_set,
2297                                   &cache_word );
2298
2299                r_dcache.write( r_dcache_save_cache_way.read(),
2300                                r_dcache_save_cache_set.read(),
2301                                r_dcache_save_cache_word.read(),
2302                                r_dcache_save_wdata.read(),
2303                                r_dcache_save_be.read() );
2304#ifdef INSTRUMENTATION
2305m_cpt_dcache_dir_read++;
2306m_cpt_dcache_data_write++;
2307#endif
2308            }
2309            else if ( m_dreq.valid and not r_dcache_updt_req.read() ) // read DIR and DATA
2310            {
2311                r_dcache.read( paddr,
2312                               &cache_rdata,
2313                               &cache_way,
2314                               &cache_set,
2315                               &cache_word,
2316                               &cache_state );
2317
2318#ifdef INSTRUMENTATION
2319m_cpt_dcache_dir_read++;
2320m_cpt_dcache_data_read++;
2321#endif
2322            }
2323            else if ( not m_dreq.valid and r_dcache_updt_req.read() ) // write DATA
2324            {
2325                r_dcache.write( r_dcache_save_cache_way.read(),
2326                                r_dcache_save_cache_set.read(),
2327                                r_dcache_save_cache_word.read(),
2328                                r_dcache_save_wdata.read(),
2329                                r_dcache_save_be.read() );
2330#ifdef INSTRUMENTATION
2331m_cpt_dcache_data_write++;
2332#endif
2333            }
2334        } // end dcache access
2335
2336        // DCACHE update in P1 stage can require ITLB / DTLB inval or flush
2337        if ( r_dcache_updt_req.read() )
2338        {
2339            size_t way = r_dcache_save_cache_way.read();
2340            size_t set = r_dcache_save_cache_set.read();
2341
2342            if ( r_dcache_in_tlb[way*m_dcache_sets+set] )
2343            {
2344                tlb_inval_required       = true;
2345                    r_dcache_tlb_inval_set   = 0;
2346                    r_dcache_tlb_inval_line  = r_dcache_save_paddr.read()>>
2347                                           (uint32_log2(m_dcache_words<<2));
2348                    r_dcache_in_tlb[way*m_dcache_sets+set] = false;
2349            }
2350            else if ( r_dcache_contains_ptd[way*m_dcache_sets+set] )
2351            {
2352                r_itlb.reset();
2353                r_dtlb.reset();
2354                    r_dcache_contains_ptd[way*m_dcache_sets+set] = false;
2355            }
2356
2357#if DEBUG_DCACHE
2358if ( m_debug_dcache_fsm )
2359std::cout << "  <PROC " << name() << " DCACHE_IDLE>"
2360          << " Cache update in P1 stage" << std::dec
2361          << " / WAY = " << r_dcache_save_cache_way.read()
2362          << " / SET = " << r_dcache_save_cache_set.read()
2363          << " / WORD = " << r_dcache_save_cache_word.read() << std::hex
2364          << " / WDATA = " << r_dcache_save_wdata.read()
2365          << " / BE = " << r_dcache_save_be.read() << std::endl;
2366#endif
2367        } // end test TLB inval
2368
2369        // Try WBUF update in P1 stage
2370        // Miss if the write request is non cacheable, and there is a pending
2371        // non cacheable write, or if the write buffer is full.
2372        if ( r_dcache_wbuf_req.read() )
2373        {
2374            bool wok = r_wbuf.write( r_dcache_save_paddr.read(),
2375                                     r_dcache_save_be.read(),
2376                                     r_dcache_save_wdata.read(),
2377                                     true);
2378#ifdef INSTRUMENTATION
2379m_cpt_wbuf_write++;
2380#endif
2381            if ( not wok ) // miss if write buffer full
2382            {
2383                wbuf_write_miss = true;
2384            }
2385        } // end WBUF update
2386
2387        // Computing the response to processor,
2388        // and the next value for r_dcache_fsm
2389
2390        // itlb/dtlb invalidation self-request
2391        if ( tlb_inval_required )
2392        {
2393            r_dcache_fsm_scan_save = r_dcache_fsm.read();
2394            r_dcache_fsm           = DCACHE_INVAL_TLB_SCAN;
2395        }
2396
2397        // coherence clack request (from DSPIN CLACK)
2398        else if ( r_dcache_clack_req.read() )
2399        {
2400            r_dcache_fsm = DCACHE_CC_CHECK;
2401            r_dcache_fsm_cc_save = r_dcache_fsm.read();
2402        }
2403        // coherence request (from CC_RECEIVE FSM)
2404        else if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
2405        {
2406            r_dcache_fsm = DCACHE_CC_CHECK;
2407            r_dcache_fsm_cc_save = r_dcache_fsm.read();
2408        }
2409
2410        // processor request (READ, WRITE, LL, SC, XTN_READ, XTN_WRITE)
2411        // we don't take the processor request, and registers
2412        // are frozen in case of wbuf_write_miss
2413        else if ( m_dreq.valid and not wbuf_write_miss )
2414        {
2415            // register processor request and DCACHE response
2416            r_dcache_save_vaddr      = m_dreq.addr;
2417            r_dcache_save_be         = m_dreq.be;
2418            r_dcache_save_wdata      = m_dreq.wdata;
2419            r_dcache_save_paddr      = paddr;
2420            r_dcache_save_cache_way  = cache_way;
2421            r_dcache_save_cache_set  = cache_set;
2422            r_dcache_save_cache_word = cache_word;
2423
2424            // READ XTN requests from processor
2425            // They are executed in this DCACHE_IDLE state.
2426            // The processor must not be in user mode
2427            if (m_dreq.type == iss_t::XTN_READ)
2428            {
2429                int xtn_opcode = (int)m_dreq.addr/4;
2430
2431                // checking processor mode:
2432                if (m_dreq.mode  == iss_t::MODE_USER)
2433                {
2434                    r_mmu_detr   = MMU_READ_PRIVILEGE_VIOLATION;
2435                    r_mmu_dbvar  = m_dreq.addr;
2436                    m_drsp.valid = true;
2437                    m_drsp.error = true;
2438                    m_drsp.rdata = 0;
2439                    r_dcache_fsm = DCACHE_IDLE;
2440                }
2441                else
2442                {
2443                    switch( xtn_opcode )
2444                    {
2445                    case iss_t::XTN_INS_ERROR_TYPE:
2446                        m_drsp.rdata = r_mmu_ietr.read();
2447                        m_drsp.valid = true;
2448                        m_drsp.error = false;
2449                        break;
2450
2451                    case iss_t::XTN_DATA_ERROR_TYPE:
2452                        m_drsp.rdata = r_mmu_detr.read();
2453                        m_drsp.valid = true;
2454                        m_drsp.error = false;
2455                        break;
2456
2457                    case iss_t::XTN_INS_BAD_VADDR:
2458                        m_drsp.rdata = r_mmu_ibvar.read();
2459                        m_drsp.valid = true;
2460                        m_drsp.error = false;
2461                        break;
2462
2463                    case iss_t::XTN_DATA_BAD_VADDR:
2464                        m_drsp.rdata = r_mmu_dbvar.read();
2465                        m_drsp.valid = true;
2466                        m_drsp.error = false;
2467                        break;
2468
2469                    case iss_t::XTN_PTPR:
2470                        m_drsp.rdata = r_mmu_ptpr.read();
2471                        m_drsp.valid = true;
2472                        m_drsp.error = false;
2473                        break;
2474
2475                    case iss_t::XTN_TLB_MODE:
2476                        m_drsp.rdata = r_mmu_mode.read();
2477                        m_drsp.valid = true;
2478                        m_drsp.error = false;
2479                        break;
2480
2481                    case iss_t::XTN_MMU_PARAMS:
2482                        m_drsp.rdata = r_mmu_params;
2483                        m_drsp.valid = true;
2484                        m_drsp.error = false;
2485                        break;
2486
2487                    case iss_t::XTN_MMU_RELEASE:
2488                        m_drsp.rdata = r_mmu_release;
2489                        m_drsp.valid = true;
2490                        m_drsp.error = false;
2491                        break;
2492
2493                    case iss_t::XTN_MMU_WORD_LO:
2494                        m_drsp.rdata = r_mmu_word_lo.read();
2495                        m_drsp.valid = true;
2496                        m_drsp.error = false;
2497                        break;
2498
2499                    case iss_t::XTN_MMU_WORD_HI:
2500                        m_drsp.rdata = r_mmu_word_hi.read();
2501                        m_drsp.valid = true;
2502                        m_drsp.error = false;
2503                        break;
2504
2505                    case iss_t::XTN_DATA_PADDR_EXT:
2506                        m_drsp.rdata = r_dcache_paddr_ext.read();
2507                        m_drsp.valid = true;
2508                        m_drsp.error = false;
2509                        break;
2510
2511                    case iss_t::XTN_INST_PADDR_EXT:
2512                        m_drsp.rdata = r_icache_paddr_ext.read();
2513                        m_drsp.valid = true;
2514                        m_drsp.error = false;
2515                        break;
2516
2517                    default:
2518                        r_mmu_detr = MMU_READ_UNDEFINED_XTN;
2519                        r_mmu_dbvar  = m_dreq.addr;
2520                        m_drsp.valid = true;
2521                        m_drsp.error = true;
2522                        m_drsp.rdata = 0;
2523                        break;
2524                    } // end switch xtn_opcode
2525                } // end else
2526            } // end if XTN_READ
2527
2528            // Handling WRITE XTN requests from processor.
2529            // They are not executed in this DCACHE_IDLE state
2530            // if they require access to the caches or the TLBs
2531            // that are already accessed.
2532            // Caches can be invalidated or flushed in user mode,
2533            // and the sync instruction can be executed in user mode
2534            else if (m_dreq.type == iss_t::XTN_WRITE)
2535            {
2536                int xtn_opcode      = (int)m_dreq.addr/4;
2537                r_dcache_xtn_opcode = xtn_opcode;
2538
2539                // checking processor mode:
2540                if ( (m_dreq.mode  == iss_t::MODE_USER) &&
2541                     (xtn_opcode != iss_t::XTN_SYNC) &&
2542                     (xtn_opcode != iss_t::XTN_DCACHE_INVAL) &&
2543                     (xtn_opcode != iss_t::XTN_DCACHE_FLUSH) &&
2544                     (xtn_opcode != iss_t::XTN_ICACHE_INVAL) &&
2545                     (xtn_opcode != iss_t::XTN_ICACHE_FLUSH) )
2546                {
2547                    r_mmu_detr   = MMU_WRITE_PRIVILEGE_VIOLATION;
2548                    r_mmu_dbvar  = m_dreq.addr;
2549                    m_drsp.valid = true;
2550                    m_drsp.error = true;
2551                    m_drsp.rdata = 0;
2552                    r_dcache_fsm = DCACHE_IDLE;
2553                }
2554                else
2555                {
2556                    switch( xtn_opcode )
2557                    {
2558                    case iss_t::XTN_PTPR:                       // itlb & dtlb must be flushed
2559                        r_dcache_xtn_req = true;
2560                        r_dcache_fsm     = DCACHE_XTN_SWITCH;
2561                        break;
2562
2563                    case iss_t::XTN_TLB_MODE:                   // no cache or tlb access
2564                        r_mmu_mode       = m_dreq.wdata;
2565                        m_drsp.valid     = true;
2566                        r_dcache_fsm     = DCACHE_IDLE;
2567                        break;
2568
2569                    case iss_t::XTN_DTLB_INVAL:                 // dtlb access
2570                        r_dcache_fsm     = DCACHE_XTN_DT_INVAL;
2571                        break;
2572
2573                    case iss_t::XTN_ITLB_INVAL:                 // itlb access
2574                        r_dcache_xtn_req = true;
2575                        r_dcache_fsm     = DCACHE_XTN_IT_INVAL;
2576                        break;
2577
2578                    case iss_t::XTN_DCACHE_INVAL:               // dcache, dtlb & itlb access
2579                        r_dcache_fsm     = DCACHE_XTN_DC_INVAL_VA;
2580                        break;
2581
2582                    case iss_t::XTN_MMU_DCACHE_PA_INV:          // dcache, dtlb & itlb access
2583                        r_dcache_fsm     = DCACHE_XTN_DC_INVAL_PA;
2584                        if (sizeof(paddr_t) <= 32)
2585                        {
2586                            assert(r_mmu_word_hi.read() == 0 &&
2587                            "high bits should be 0 for 32bit paddr");
2588                            r_dcache_save_paddr = (paddr_t)r_mmu_word_lo.read();
2589                        }
2590                        else
2591                        {
2592                            r_dcache_save_paddr = (paddr_t)r_mmu_word_hi.read() << 32 |
2593                                                  (paddr_t)r_mmu_word_lo.read();
2594                        }
2595                        break;
2596
2597                    case iss_t::XTN_DCACHE_FLUSH:              // itlb and dtlb must be reset
2598                        r_dcache_flush_count = 0;
2599                        r_dcache_fsm     = DCACHE_XTN_DC_FLUSH;
2600                        break;
2601
2602                    case iss_t::XTN_ICACHE_INVAL:               // icache and itlb access
2603                        r_dcache_xtn_req = true;
2604                        r_dcache_fsm     = DCACHE_XTN_IC_INVAL_VA;
2605                        break;
2606
2607                    case iss_t::XTN_MMU_ICACHE_PA_INV:          // icache access
2608                        r_dcache_xtn_req = true;
2609                        r_dcache_fsm     = DCACHE_XTN_IC_INVAL_PA;
2610                        break;
2611
2612                    case iss_t::XTN_ICACHE_FLUSH:               // icache access
2613                        r_dcache_xtn_req = true;
2614                        r_dcache_fsm     = DCACHE_XTN_IC_FLUSH;
2615                        break;
2616
2617                    case iss_t::XTN_SYNC:                       // wait until write buffer empty
2618                        r_dcache_fsm     = DCACHE_XTN_SYNC;
2619                        break;
2620
2621                    case iss_t::XTN_MMU_WORD_LO:                // no cache or tlb access
2622                        r_mmu_word_lo    = m_dreq.wdata;
2623                        m_drsp.valid     = true;
2624                        r_dcache_fsm     = DCACHE_IDLE;
2625                        break;
2626
2627                    case iss_t::XTN_MMU_WORD_HI:                // no cache or tlb access
2628                        r_mmu_word_hi    = m_dreq.wdata;
2629                        m_drsp.valid     = true;
2630                        r_dcache_fsm     = DCACHE_IDLE;
2631                        break;
2632
2633                    case iss_t::XTN_MMU_LL_RESET:      // no cache or tlb access
2634                        r_dcache_llsc_valid = false;
2635                        m_drsp.valid     = true;
2636                        r_dcache_fsm     = DCACHE_IDLE;
2637                    break;
2638
2639                    case iss_t::XTN_DATA_PADDR_EXT:     // no cache or tlb access
2640                        r_dcache_paddr_ext = m_dreq.wdata;
2641                        m_drsp.valid       = true;
2642                        r_dcache_fsm       = DCACHE_IDLE;
2643                    break;
2644
2645                    case iss_t::XTN_INST_PADDR_EXT:     // no cache or tlb access
2646                        r_dcache_xtn_req = true;
2647                        r_dcache_fsm     = DCACHE_XTN_IC_PADDR_EXT;
2648                    break;
2649
2650                    case iss_t::XTN_ICACHE_PREFETCH:        // not implemented : no action
2651                    case iss_t::XTN_DCACHE_PREFETCH:        // not implemented : no action
2652                        m_drsp.valid     = true;
2653                        r_dcache_fsm     = DCACHE_IDLE;
2654                    break;
2655
2656                    case iss_t::XTN_DEBUG_MASK:     // debug mask
2657                        m_debug_dcache_fsm = ((m_dreq.wdata & 0x1) != 0);
2658                        m_debug_icache_fsm = ((m_dreq.wdata & 0x2) != 0);
2659                        m_debug_cmd_fsm = ((m_dreq.wdata & 0x4) != 0);
2660                        m_drsp.valid = true;
2661                        r_dcache_fsm = DCACHE_IDLE;
2662                        break;
2663
2664                    default:
2665                        r_mmu_detr   = MMU_WRITE_UNDEFINED_XTN;
2666                        r_mmu_dbvar  = m_dreq.addr;
2667                        m_drsp.valid = true;
2668                        m_drsp.error = true;
2669                        r_dcache_fsm = DCACHE_IDLE;
2670                        break;
2671                    } // end switch xtn_opcode
2672                } // end else
2673            } // end if XTN_WRITE
2674
2675            // Handling processor requests to address space (READ/WRITE/LL/SC)
2676            // The dtlb and dcache can be activated or not.
2677            // We compute the cacheability, and check processor request validity:
2678            // - If DTLB not activated : cacheability is defined by the segment table,
2679            //   and there is no access rights checking.
2680            // - If DTLB activated : cacheability is defined by the C bit in the PTE,
2681            //   and the U & W bits of the PTE are checked, as well as the DTLB hit.
2682            //   Jumps to the TLB_MISS sub-fsm in case of dtlb miss.
2683            else
2684            {
2685                bool        valid_req;
2686                bool        cacheable;
2687
2688                if ( not (r_mmu_mode.read() & DATA_TLB_MASK) )          // dtlb not activated
2689                {
2690                    valid_req     = true;
2691
2692                    if ( not (r_mmu_mode.read() & DATA_CACHE_MASK) ) cacheable = false;
2693                    else cacheable = m_cacheability_table[(uint64_t)m_dreq.addr];
2694                }
2695                else                                                                       // dtlb activated
2696                {
2697                    if ( tlb_hit )                                      // tlb hit
2698                    {
2699                        // cacheability
2700                        if ( not (r_mmu_mode.read() & DATA_CACHE_MASK) ) cacheable = false;
2701                        else cacheable = tlb_flags.c;
2702
2703                        // access rights checking
2704                        if ( not tlb_flags.u and (m_dreq.mode == iss_t::MODE_USER))
2705                        {
2706                            if ( (m_dreq.type == iss_t::DATA_READ) or
2707                                 (m_dreq.type == iss_t::DATA_LL) )
2708                            {
2709                                r_mmu_detr = MMU_READ_PRIVILEGE_VIOLATION;
2710                            }
2711                            else
2712                            {
2713                                r_mmu_detr = MMU_WRITE_PRIVILEGE_VIOLATION;
2714                            }
2715                            valid_req    = false;
2716                            r_mmu_dbvar  = m_dreq.addr;
2717                            m_drsp.valid = true;
2718                            m_drsp.error = true;
2719                            m_drsp.rdata = 0;
2720#if DEBUG_DCACHE
2721if ( m_debug_dcache_fsm )
2722std::cout << "  <PROC " << name() << " DCACHE_IDLE>"
2723          << " HIT in dtlb, but privilege violation" << std::endl;
2724#endif
2725                        }
2726                        else if ( not tlb_flags.w and
2727                                  ((m_dreq.type == iss_t::DATA_WRITE) or
2728                                   (m_dreq.type == iss_t::DATA_SC)) )
2729                        {
2730                            r_mmu_detr   = MMU_WRITE_ACCES_VIOLATION;
2731                            valid_req    = false;
2732                            r_mmu_dbvar  = m_dreq.addr;
2733                            m_drsp.valid = true;
2734                            m_drsp.error = true;
2735                            m_drsp.rdata = 0;
2736#if DEBUG_DCACHE
2737if ( m_debug_dcache_fsm )
2738std::cout << "  <PROC " << name() << " DCACHE_IDLE>"
2739          << " HIT in dtlb, but writable violation" << std::endl;
2740#endif
2741                        }
2742                        else
2743                        {
2744                            valid_req    = true;
2745                        }
2746                    }
2747                    else                                                // tlb miss
2748                    {
2749                        valid_req            = false;
2750                        r_dcache_tlb_vaddr   = m_dreq.addr;
2751                        r_dcache_tlb_ins     = false;
2752                        r_dcache_fsm         = DCACHE_TLB_MISS;
2753                    }
2754                }    // end DTLB activated
2755
2756                if ( valid_req )        // processor request is valid (after MMU check)
2757                {
2758                    // READ request
2759                    // The read requests are taken only if there is no cache update.
2760                    // We request a VCI transaction to CMD FSM if miss or uncachable
2761
2762                    if ( ((m_dreq.type == iss_t::DATA_READ))
2763                          and not r_dcache_updt_req.read() )
2764                    {
2765                        if ( cacheable )                // cacheable read
2766                        {
2767                            if ( cache_state == CACHE_SLOT_STATE_EMPTY )   // cache miss
2768                            {
2769#ifdef INSTRUMENTATION
2770m_cpt_dcache_miss++;
2771#endif
2772                                // request a VCI DMISS transaction
2773                                r_dcache_vci_paddr    = paddr;
2774                                r_dcache_vci_miss_req = true;
2775                                r_dcache_miss_type    = PROC_MISS;
2776                                r_dcache_fsm          = DCACHE_MISS_SELECT;
2777#if DEBUG_DCACHE
2778if ( m_debug_dcache_fsm )
2779std::cout << "  <PROC " << name() << " DCACHE_IDLE>"
2780          << " READ MISS in dcache" 
2781          << " / PADDR = " << std::hex << paddr << std::endl;
2782#endif
2783                            }
2784                            else if (cache_state == CACHE_SLOT_STATE_ZOMBI ) // pending cleanup
2785                            {
2786                                // stalled until cleanup is acknowledged
2787                                r_dcache_fsm   = DCACHE_IDLE;
2788#if DEBUG_DCACHE
2789if ( m_debug_dcache_fsm )
2790std::cout << "  <PROC " << name() << " DCACHE_IDLE>"
2791          << " Pending cleanup, stalled until cleanup acknowledge"
2792          << " / PADDR = " << std::hex << paddr << std::endl;
2793#endif
2794                            }
2795                            else                                      // cache hit
2796                            {
2797#ifdef INSTRUMENTATION
2798m_cpt_data_read++;
2799#endif
2800                                // returns data to processor
2801                                m_drsp.valid   = true;
2802                                m_drsp.error   = false;
2803                                m_drsp.rdata   = cache_rdata;
2804#if DEBUG_DCACHE
2805if ( m_debug_dcache_fsm )
2806std::cout << "  <PROC " << name() << " DCACHE_IDLE>"
2807          << " READ HIT in dcache" 
2808          << " : PADDR = " << std::hex << paddr
2809          << " / DATA  = " << std::hex << cache_rdata << std::endl;
2810#endif
2811                            }
2812                        }
2813                        else                                    // uncacheable read
2814                        {
2815                            r_dcache_vci_paddr      = paddr;
2816                            r_dcache_vci_unc_be     = m_dreq.be;
2817                            r_dcache_vci_unc_write  = false;
2818                            r_dcache_vci_unc_req    = true;
2819                            r_dcache_fsm            = DCACHE_UNC_WAIT;
2820#if DEBUG_DCACHE
2821if ( m_debug_dcache_fsm )
2822std::cout << "  <PROC " << name() << " DCACHE_IDLE>"
2823          << " READ UNCACHEABLE in dcache" 
2824          << " / PADDR = " << std::hex << paddr << std::endl;
2825#endif
2826                        }
2827                    } // end READ
2828
2829                    // LL request (non cachable)
2830                    // We request a VCI LL transaction to CMD FSM and register
2831                    // the LL/SC operation in llsc buffer.
2832                    else if (m_dreq.type == iss_t::DATA_LL)
2833                    {
2834                        // register paddr in LLSC buffer
2835                        r_dcache_llsc_paddr = paddr;
2836                        r_dcache_llsc_count = LLSC_TIMEOUT;
2837                        r_dcache_llsc_valid = true;
2838
2839                        // request an LL VCI transaction and go to DCACHE_LL_WAIT state
2840                        r_dcache_vci_ll_req   = true;
2841                        r_dcache_vci_paddr    = paddr;
2842                        r_dcache_ll_rsp_count = 0;
2843                        r_dcache_fsm          = DCACHE_LL_WAIT;
2844
2845                    }// end LL
2846
2847                    // WRITE request:
2848                    // If the TLB is activated and the PTE Dirty bit is not set, we stall
2849                    // the processor and set the Dirty bit before handling the write request,
2850                    // going to the DCACHE_DIRTY_GT_PTE state.
2851                    // If we don't need to set the Dirty bit, we can acknowledge
2852                    // the processor request, as the write arguments (including the
2853                    // physical address) are registered in r_dcache_save registers,
2854                    // and the write will be done in the P1 pipeline stage.
2855                    else if ( m_dreq.type == iss_t::DATA_WRITE )
2856                    {
2857                        if ( (r_mmu_mode.read() & DATA_TLB_MASK )
2858                              and not tlb_flags.d )             // Dirty bit must be set
2859                        {
2860                            // The PTE physical address is obtained from the nline value (dtlb),
2861                            // and from the virtual address (word index)
2862                            if ( tlb_flags.b )  // PTE1
2863                            {
2864                                r_dcache_dirty_paddr = (paddr_t)(tlb_nline*(m_dcache_words<<2)) |
2865                                                       (paddr_t)((m_dreq.addr>>19) & 0x3c);
2866                            }
2867                            else                // PTE2
2868                            {
2869                                r_dcache_dirty_paddr = (paddr_t)(tlb_nline*(m_dcache_words<<2)) |
2870                                                       (paddr_t)((m_dreq.addr>>9) & 0x38);
2871                            }
2872                            r_dcache_fsm      = DCACHE_DIRTY_GET_PTE;
2873                        }
2874                        else                                    // Write request accepted
2875                        {
2876#ifdef INSTRUMENTATION
2877m_cpt_data_write++;
2878#endif
2879                            // cleaning llsc buffer if address matching
2880                            if ( paddr == r_dcache_llsc_paddr.read() )
2881                                r_dcache_llsc_valid = false;
2882
2883                            if (not cacheable) 
2884                            {                           
2885                                r_dcache_vci_paddr      = paddr;
2886                                r_dcache_vci_wdata      = m_dreq.wdata;
2887                                r_dcache_vci_unc_write  = true;
2888                                r_dcache_vci_unc_be     = m_dreq.be;
2889                                r_dcache_vci_unc_req    = true;
2890                                r_dcache_fsm            = DCACHE_UNC_WAIT;
2891                            }
2892                            else
2893                            {
2894                                // response to processor
2895                                m_drsp.valid        = true;
2896                                // activating P1 stage
2897                                wbuf_request = true;
2898                                updt_request = (cache_state == CACHE_SLOT_STATE_VALID);
2899                            }
2900                        }
2901                    } // end WRITE
2902
2903                    // SC request:
2904                    // If the TLB is activated and the PTE Dirty bit is not set, we stall
2905                    // the processor and set the Dirty bit before handling the write request,
2906                    // going to the DCACHE_DIRTY_GT_PTE state.
2907                    // If we don't need to set the Dirty bit, we test the llsc buffer:
2908                    // If failure, we send a negative response to processor.
2909                    // If success, we request a SC transaction to CMD FSM and go
2910                    // to DCACHE_SC_WAIT state.
2911                    // We don't check a possible write hit in dcache, as the cache update
2912                    // is done by the coherence transaction induced by the SC...
2913                    else if ( m_dreq.type == iss_t::DATA_SC )
2914                    {
2915                        if ( (r_mmu_mode.read() & DATA_TLB_MASK )
2916                              and not tlb_flags.d )                     // Dirty bit must be set
2917                        {
2918                            // The PTE physical address is obtained from the nline value (dtlb),
2919                            // and the word index (virtual address)
2920                            if ( tlb_flags.b )  // PTE1
2921                            {
2922                                r_dcache_dirty_paddr = (paddr_t)(tlb_nline*(m_dcache_words<<2)) |
2923                                                       (paddr_t)((m_dreq.addr>>19) & 0x3c);
2924                            }
2925                            else                        // PTE2
2926                            {
2927                                r_dcache_dirty_paddr = (paddr_t)(tlb_nline*(m_dcache_words<<2)) |
2928                                                       (paddr_t)((m_dreq.addr>>9) & 0x38);
2929                            }
2930                            r_dcache_fsm           = DCACHE_DIRTY_GET_PTE;
2931                            m_drsp.valid = false;
2932                            m_drsp.error = false;
2933                            m_drsp.rdata = 0;
2934                        }
2935                        else                                    // SC request accepted
2936                        {
2937#ifdef INSTRUMENTATION
2938m_cpt_data_sc++;
2939#endif
2940                            // checking local success
2941                            if( r_dcache_llsc_valid.read() and
2942                                (r_dcache_llsc_paddr.read() == paddr) )  // local success
2943                            {
2944                                // request an SC CMD and go to DCACHE_SC_WAIT state
2945                                r_dcache_vci_paddr   = paddr;
2946                                r_dcache_vci_sc_req  = true;
2947                                r_dcache_vci_sc_data = m_dreq.wdata;
2948                                r_dcache_fsm         = DCACHE_SC_WAIT;
2949                            }
2950                            else                                          // local fail
2951                            {
2952                                    m_drsp.valid = true;
2953                                    m_drsp.error = false;
2954                                    m_drsp.rdata = 0x1;
2955                            }
2956                        }
2957                    } // end SC
2958                } // end valid_req
2959            }  // end if read/write/ll/sc request
2960        } // end processor request
2961
2962        // itlb miss request
2963        else if ( r_icache_tlb_miss_req.read() and not wbuf_write_miss )
2964        {
2965            r_dcache_tlb_ins    = true;
2966            r_dcache_tlb_vaddr  = r_icache_vaddr_save.read();
2967            r_dcache_fsm        = DCACHE_TLB_MISS;
2968        }
2969
2970        // Computing requests for P1 stage : r_dcache_wbuf_req & r_dcache_updt_req
2971        r_dcache_updt_req = updt_request;
2972        r_dcache_wbuf_req = wbuf_request or
2973                            (r_dcache_wbuf_req.read() and wbuf_write_miss);
2974        break;
2975    }
2976    /////////////////////
2977    case DCACHE_TLB_MISS: // This is the entry point for the sub-fsm handling all tlb miss.
2978                          // Input arguments are:
2979                          // - r_dcache_tlb_vaddr
2980                          // - r_dcache_tlb_ins (true when itlb miss)
2981                          // The sub-fsm access the dcache to find the missing TLB entry,
2982                          // and activates the cache miss procedure in case of miss.
2983                          // It bypass the first level page table access if possible.
2984                          // It uses atomic access to update the R/L access bits
2985                          // in the page table if required.
2986                          // It directly updates the itlb or dtlb, and writes into the
2987                          // r_mmu_ins_* or r_mmu_data* error reporting registers.
2988    {
2989        uint32_t        ptba = 0;
2990        bool            bypass;
2991        paddr_t         pte_paddr;
2992
2993        // evaluate bypass in order to skip first level page table access
2994        if ( r_dcache_tlb_ins.read() )                          // itlb miss
2995        {
2996            bypass = r_itlb.get_bypass(r_dcache_tlb_vaddr.read(), &ptba);
2997        }
2998        else                                                    // dtlb miss
2999        {
3000            bypass = r_dtlb.get_bypass(r_dcache_tlb_vaddr.read(), &ptba);
3001        }
3002
3003        if ( not bypass )     // Try to read PTE1/PTD1 in dcache
3004        {
3005            pte_paddr = (((paddr_t)r_mmu_ptpr.read()) << (INDEX1_NBITS+2)) |
3006                        ((((paddr_t)r_dcache_tlb_vaddr.read()) >> PAGE_M_NBITS) << 2);
3007            r_dcache_tlb_paddr = pte_paddr;
3008            r_dcache_fsm       = DCACHE_TLB_PTE1_GET;
3009        }
3010        else                  // Try to read PTE2 in dcache
3011        {
3012            pte_paddr = (paddr_t)ptba << PAGE_K_NBITS |
3013                        (paddr_t)(r_dcache_tlb_vaddr.read()&PTD_ID2_MASK)>>(PAGE_K_NBITS-3);
3014            r_dcache_tlb_paddr = pte_paddr;
3015            r_dcache_fsm       = DCACHE_TLB_PTE2_GET;
3016        }
3017
3018#if DEBUG_DCACHE
3019if ( m_debug_dcache_fsm )
3020{
3021    if ( r_dcache_tlb_ins.read() )
3022        std::cout << "  <PROC " << name() << " DCACHE_TLB_MISS> ITLB miss";
3023    else
3024        std::cout << "  <PROC " << name() << " DCACHE_TLB_MISS> DTLB miss";
3025    std::cout << " / VADDR = " << std::hex << r_dcache_tlb_vaddr.read()
3026              << " / ptpr  = " << (((paddr_t)r_mmu_ptpr.read()) << (INDEX1_NBITS+2))
3027              << " / BYPASS = " << bypass
3028              << " / PTE_ADR = " << pte_paddr << std::endl;
3029}
3030#endif
3031
3032        break;
3033    }
3034    /////////////////////////
3035    case DCACHE_TLB_PTE1_GET:   // try to read a PT1 entry in dcache
3036    {
3037        // coherence clack request (from DSPIN CLACK)
3038        if ( r_dcache_clack_req.read() )
3039        {
3040            r_dcache_fsm = DCACHE_CC_CHECK;
3041            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3042            break;
3043        }
3044
3045        // coherence request (from CC_RECEIVE FSM)
3046        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
3047        {
3048            r_dcache_fsm = DCACHE_CC_CHECK;
3049            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3050            break;
3051        }
3052
3053        uint32_t        entry;
3054        size_t          way;
3055        size_t          set;
3056        size_t          word;
3057        int         cache_state;
3058        r_dcache.read( r_dcache_tlb_paddr.read(),
3059                       &entry,
3060                       &way,
3061                       &set,
3062                       &word,
3063                       &cache_state );
3064#ifdef INSTRUMENTATION
3065m_cpt_dcache_data_read++;
3066m_cpt_dcache_dir_read++;
3067#endif
3068        if ( cache_state == CACHE_SLOT_STATE_VALID )   // hit in dcache
3069        {
3070            if ( not (entry & PTE_V_MASK) )     // unmapped
3071            {
3072                if ( r_dcache_tlb_ins.read() )
3073                {
3074                    r_mmu_ietr             = MMU_READ_PT1_UNMAPPED;
3075                    r_mmu_ibvar            = r_dcache_tlb_vaddr.read();
3076                    r_icache_tlb_miss_req  = false;
3077                    r_icache_tlb_rsp_error = true;
3078                }
3079                else
3080                {
3081                    r_mmu_detr             = MMU_READ_PT1_UNMAPPED;
3082                    r_mmu_dbvar            = r_dcache_tlb_vaddr.read();
3083                    m_drsp.valid             = true;
3084                    m_drsp.error             = true;
3085                }
3086                r_dcache_fsm          = DCACHE_IDLE;
3087
3088#if DEBUG_DCACHE
3089if ( m_debug_dcache_fsm )
3090{
3091    std::cout << "  <PROC " << name() 
3092              << " DCACHE_TLB_PTE1_GET> HIT in dcache, but unmapped"
3093              << std::hex << " / paddr = " << r_dcache_tlb_paddr.read()
3094              << std::dec << " / way = " << way
3095              << std::dec << " / set = " << set
3096              << std::dec << " / word = " << word
3097              << std::hex << " / PTE1 = " << entry << std::endl;
3098}
3099#endif
3100
3101            }
3102            else if( entry & PTE_T_MASK )       //  PTD : me must access PT2
3103            {
3104                // mark the cache line ac containing a PTD
3105                r_dcache_contains_ptd[m_dcache_sets*way+set] = true;
3106
3107                // register bypass
3108                if ( r_dcache_tlb_ins.read() )          // itlb
3109                {
3110                    r_itlb.set_bypass(r_dcache_tlb_vaddr.read(),
3111                                      entry & ((1 << (m_paddr_nbits-PAGE_K_NBITS)) - 1),
3112                                      r_dcache_tlb_paddr.read() / (m_icache_words<<2) );
3113                }
3114                else                                    // dtlb
3115                {
3116                    r_dtlb.set_bypass(r_dcache_tlb_vaddr.read(),
3117                                      entry & ((1 << (m_paddr_nbits-PAGE_K_NBITS)) - 1),
3118                                      r_dcache_tlb_paddr.read() / (m_dcache_words<<2) );
3119                }
3120                r_dcache_tlb_paddr =
3121                    (paddr_t)(entry & ((1<<(m_paddr_nbits-PAGE_K_NBITS))-1)) << PAGE_K_NBITS |
3122                    (paddr_t)(((r_dcache_tlb_vaddr.read() & PTD_ID2_MASK) >> PAGE_K_NBITS) << 3);
3123                r_dcache_fsm       = DCACHE_TLB_PTE2_GET;
3124
3125#if DEBUG_DCACHE
3126if ( m_debug_dcache_fsm )
3127{
3128    std::cout << "  <PROC " << name() 
3129              << " DCACHE_TLB_PTE1_GET> HIT in dcache"
3130              << std::hex << " / paddr = " << r_dcache_tlb_paddr.read()
3131              << std::dec << " / way = " << way
3132              << std::dec << " / set = " << set
3133              << std::dec << " / word = " << word
3134              << std::hex << " / PTD = " << entry << std::endl;
3135}
3136#endif
3137            }
3138            else                        //  PTE1 :  we must update the TLB
3139            {
3140                r_dcache_in_tlb[m_icache_sets*way+set] = true;
3141                r_dcache_tlb_pte_flags  = entry;
3142                r_dcache_tlb_cache_way  = way;
3143                r_dcache_tlb_cache_set  = set;
3144                r_dcache_tlb_cache_word = word;
3145                r_dcache_fsm            = DCACHE_TLB_PTE1_SELECT;
3146
3147#if DEBUG_DCACHE
3148if ( m_debug_dcache_fsm )
3149{
3150    std::cout << "  <PROC " << name() 
3151              << " DCACHE_TLB_PTE1_GET> HIT in dcache"
3152              << std::hex << " / paddr = " << r_dcache_tlb_paddr.read()
3153              << std::dec << " / way = " << way
3154              << std::dec << " / set = " << set
3155              << std::dec << " / word = " << word
3156              << std::hex << " / PTE1 = " << entry << std::endl;
3157}
3158#endif
3159            }
3160        }
3161        else if ( cache_state == CACHE_SLOT_STATE_ZOMBI ) // pending cleanup
3162        {
3163            // stalled until cleanup is acknowledged
3164            r_dcache_fsm   = DCACHE_TLB_PTE1_GET;
3165        }
3166        else            // we must load the missing cache line in dcache
3167        {
3168            r_dcache_vci_miss_req  = true;
3169            r_dcache_vci_paddr     = r_dcache_tlb_paddr.read();
3170            r_dcache_save_paddr    = r_dcache_tlb_paddr.read();
3171            r_dcache_miss_type     = PTE1_MISS;
3172            r_dcache_fsm           = DCACHE_MISS_SELECT;
3173
3174#if DEBUG_DCACHE
3175if ( m_debug_dcache_fsm )
3176{
3177    std::cout << "  <PROC " << name() 
3178              << " DCACHE_TLB_PTE1_GET> MISS in dcache:"
3179              << " PTE1 address = " << std::hex << r_dcache_tlb_paddr.read() << std::endl;
3180}
3181#endif
3182        }
3183        break;
3184    }
3185    ////////////////////////////
3186    case DCACHE_TLB_PTE1_SELECT:        // select a slot for PTE1
3187    {
3188        size_t  way;
3189        size_t  set;
3190
3191        if ( r_dcache_tlb_ins.read() )
3192        {
3193            r_itlb.select( r_dcache_tlb_vaddr.read(),
3194                           true,  // PTE1
3195                           &way,
3196                           &set );
3197#ifdef INSTRUMENTATION
3198m_cpt_itlb_read++;
3199#endif
3200        }
3201        else
3202        {
3203            r_dtlb.select( r_dcache_tlb_vaddr.read(),
3204                           true,  // PTE1
3205                           &way,
3206                           &set );
3207#ifdef INSTRUMENTATION
3208m_cpt_dtlb_read++;
3209#endif
3210        }
3211        r_dcache_tlb_way = way;
3212        r_dcache_tlb_set = set;
3213        r_dcache_fsm     = DCACHE_TLB_PTE1_UPDT;
3214
3215#if DEBUG_DCACHE
3216if ( m_debug_dcache_fsm )
3217{
3218    if ( r_dcache_tlb_ins.read() )
3219        std::cout << "  <PROC " << name() 
3220                  << " DCACHE_TLB_PTE1_SELECT> Select a slot in ITLB:";
3221    else
3222        std::cout << "  <PROC " << name() 
3223                  << ".DCACHE_TLB_PTE1_SELECT> Select a slot in DTLB:";
3224        std::cout << " way = " << std::dec << way
3225                  << " / set = " << set << std::endl;
3226}
3227#endif
3228        break;
3229    }
3230    //////////////////////////
3231    case DCACHE_TLB_PTE1_UPDT:  // write a new PTE1 in tlb after testing the L/R bit
3232                                // - if L/R bit already set, exit the sub-fsm.
3233                                // - if not, we update the page table but we dont write
3234                                //   neither in DCACHE, nor in TLB, as this will be done by
3235                                //   the coherence mechanism.
3236    {
3237        paddr_t   nline    = r_dcache_tlb_paddr.read() >> (uint32_log2(m_dcache_words)+2);
3238        uint32_t  pte      = r_dcache_tlb_pte_flags.read();
3239        bool      pt_updt  = false;
3240        bool      local    = true;
3241
3242        // We should compute the access locality:
3243        // The PPN MSB bits define the destination cluster index.
3244        // The m_srcid MSB bits define the source cluster index.
3245        // The number of bits to compare depends on the number of clusters,
3246        // and can be obtained in the mapping table.
3247        // As long as this computation is not done, all access are local.
3248
3249        if ( local )                                            // local access
3250        {
3251            if ( not ((pte & PTE_L_MASK) == PTE_L_MASK) ) // we must set the L bit
3252            {
3253                pt_updt                = true;
3254                r_dcache_vci_cas_old    = pte;
3255                r_dcache_vci_cas_new    = pte | PTE_L_MASK;
3256                pte                    = pte | PTE_L_MASK;
3257                r_dcache_tlb_pte_flags = pte;
3258            }
3259        }
3260        else                                                    // remote access
3261        {
3262            if ( not ((pte & PTE_R_MASK) == PTE_R_MASK) ) // we must set the R bit
3263            {
3264                pt_updt                = true;
3265                r_dcache_vci_cas_old    = pte;
3266                r_dcache_vci_cas_new    = pte | PTE_R_MASK;
3267                pte                    = pte | PTE_R_MASK;
3268                r_dcache_tlb_pte_flags = pte;
3269            }
3270        }
3271
3272        if ( not pt_updt )                                      // update TLB and return
3273        {
3274            if ( r_dcache_tlb_ins.read() )
3275            {
3276                r_itlb.write( true,             // 2M page
3277                              pte,
3278                              0,                // argument unused for a PTE1
3279                              r_dcache_tlb_vaddr.read(),
3280                              r_dcache_tlb_way.read(),
3281                              r_dcache_tlb_set.read(),
3282                              nline );
3283#ifdef INSTRUMENTATION
3284m_cpt_itlb_write++;
3285#endif
3286
3287#if DEBUG_DCACHE
3288if ( m_debug_dcache_fsm )
3289{
3290    std::cout << "  <PROC " << name() 
3291              << " DCACHE_TLB_PTE1_UPDT> write PTE1 in ITLB"
3292              << " / set = " << std::dec << r_dcache_tlb_set.read()
3293              << " / way = " << r_dcache_tlb_way.read() << std::endl;
3294    r_itlb.printTrace();
3295}
3296#endif
3297            }
3298            else
3299            {
3300                r_dtlb.write( true,             // 2M page
3301                              pte,
3302                              0,                // argument unused for a PTE1
3303                              r_dcache_tlb_vaddr.read(),
3304                              r_dcache_tlb_way.read(),
3305                              r_dcache_tlb_set.read(),
3306                              nline );
3307#ifdef INSTRUMENTATION
3308m_cpt_dtlb_write++;
3309#endif
3310
3311#if DEBUG_DCACHE
3312if ( m_debug_dcache_fsm )
3313{
3314    std::cout << "  <PROC " << name() 
3315              << " DCACHE_TLB_PTE1_UPDT> write PTE1 in DTLB"
3316              << " / set = " << std::dec << r_dcache_tlb_set.read()
3317              << " / way = " << r_dcache_tlb_way.read() << std::endl;
3318    r_dtlb.printTrace();
3319}
3320#endif
3321            }
3322            r_dcache_fsm = DCACHE_TLB_RETURN;
3323        }
3324        else                            // update page table but not TLB
3325        {
3326            r_dcache_fsm = DCACHE_TLB_LR_UPDT;
3327
3328#if DEBUG_DCACHE
3329if ( m_debug_dcache_fsm )
3330{
3331    std::cout << "  <PROC " << name() 
3332              << " DCACHE_TLB_PTE1_UPDT> L/R bit update required"
3333              << std::endl;
3334}
3335#endif
3336        }
3337        break;
3338    }
3339    /////////////////////////
3340    case DCACHE_TLB_PTE2_GET:   // Try to get a PTE2 (64 bits) in the dcache
3341    {
3342        // coherence clack request (from DSPIN CLACK)
3343        if ( r_dcache_clack_req.read() )
3344        {
3345            r_dcache_fsm = DCACHE_CC_CHECK;
3346            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3347            break;
3348        }
3349
3350        // coherence request (from CC_RECEIVE FSM)
3351        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
3352        {
3353            r_dcache_fsm = DCACHE_CC_CHECK;
3354            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3355            break;
3356        }
3357
3358        uint32_t        pte_flags;
3359        uint32_t        pte_ppn;
3360        size_t          way;
3361        size_t          set;
3362        size_t          word;
3363        int         cache_state;
3364
3365        r_dcache.read( r_dcache_tlb_paddr.read(),
3366                       &pte_flags,
3367                       &pte_ppn,
3368                       &way,
3369                       &set,
3370                       &word,
3371                       &cache_state );
3372#ifdef INSTRUMENTATION
3373m_cpt_dcache_data_read++;
3374m_cpt_dcache_dir_read++;
3375#endif
3376        if ( cache_state == CACHE_SLOT_STATE_VALID )   // hit in dcache
3377        {
3378            if ( not (pte_flags & PTE_V_MASK) ) // unmapped
3379            {
3380                if ( r_dcache_tlb_ins.read() )
3381                {
3382                    r_mmu_ietr             = MMU_READ_PT2_UNMAPPED;
3383                    r_mmu_ibvar            = r_dcache_tlb_vaddr.read();
3384                    r_icache_tlb_miss_req  = false;
3385                    r_icache_tlb_rsp_error = true;
3386                }
3387                else
3388                {
3389                    r_mmu_detr             = MMU_READ_PT2_UNMAPPED;
3390                    r_mmu_dbvar            = r_dcache_tlb_vaddr.read();
3391                    m_drsp.valid           = true;
3392                    m_drsp.error           = true;
3393                }
3394                r_dcache_fsm               = DCACHE_IDLE;
3395
3396#if DEBUG_DCACHE
3397if ( m_debug_dcache_fsm )
3398{
3399    std::cout << "  <PROC " << name()
3400              << " DCACHE_TLB_PTE2_GET> HIT in dcache, but PTE unmapped"
3401              << " PTE_FLAGS = " << std::hex << pte_flags
3402              << " PTE_PPN = " << std::hex << pte_ppn << std::endl;
3403}
3404#endif
3405            }
3406            else                                // mapped : we must update the TLB
3407            {
3408                r_dcache_in_tlb[m_dcache_sets*way+set] = true;
3409                r_dcache_tlb_pte_flags  = pte_flags;
3410                r_dcache_tlb_pte_ppn    = pte_ppn;
3411                r_dcache_tlb_cache_way  = way;
3412                r_dcache_tlb_cache_set  = set;
3413                r_dcache_tlb_cache_word = word;
3414                r_dcache_fsm            = DCACHE_TLB_PTE2_SELECT;
3415
3416#if DEBUG_DCACHE
3417if ( m_debug_dcache_fsm )
3418{
3419    std::cout << "  <PROC " << name() 
3420              << " DCACHE_TLB_PTE2_GET> HIT in dcache:"
3421              << " PTE_FLAGS = " << std::hex << pte_flags
3422              << " PTE_PPN = " << std::hex << pte_ppn << std::endl;
3423}
3424#endif
3425             }
3426        }
3427        else if ( cache_state == CACHE_SLOT_STATE_ZOMBI ) // pending cleanup
3428        {
3429            // stalled until cleanup is acknowledged
3430            r_dcache_fsm   = DCACHE_TLB_PTE2_GET;
3431
3432#if DEBUG_DCACHE
3433if ( m_debug_dcache_fsm )
3434{
3435    std::cout << "  <PROC " << name() 
3436              << " DCACHE_TLB_PTE2_GET> ZOMBI in dcache: waiting cleanup ack"
3437              << std::endl;
3438}
3439#endif
3440        }
3441        else            // we must load the missing cache line in dcache
3442        {
3443            r_dcache_fsm          = DCACHE_MISS_SELECT;
3444            r_dcache_vci_miss_req = true;
3445            r_dcache_vci_paddr    = r_dcache_tlb_paddr.read();
3446            r_dcache_save_paddr   = r_dcache_tlb_paddr.read();
3447            r_dcache_miss_type    = PTE2_MISS;
3448
3449#if DEBUG_DCACHE
3450if ( m_debug_dcache_fsm )
3451{
3452    std::cout << "  <PROC " << name()
3453              << " DCACHE_TLB_PTE2_GET> MISS in dcache:"
3454              << " PTE address = " << std::hex << r_dcache_tlb_paddr.read() << std::endl;
3455}
3456#endif
3457        }
3458        break;
3459    }
3460    ////////////////////////////
3461    case DCACHE_TLB_PTE2_SELECT:    // select a slot for PTE2
3462    {
3463        size_t way;
3464        size_t set;
3465
3466        if ( r_dcache_tlb_ins.read() )
3467        {
3468            r_itlb.select( r_dcache_tlb_vaddr.read(),
3469                           false,       // PTE2
3470                           &way,
3471                           &set );
3472#ifdef INSTRUMENTATION
3473m_cpt_itlb_read++;
3474#endif
3475        }
3476        else
3477        {
3478            r_dtlb.select( r_dcache_tlb_vaddr.read(),
3479                           false,       // PTE2
3480                           &way,
3481                           &set );
3482#ifdef INSTRUMENTATION
3483m_cpt_dtlb_read++;
3484#endif
3485        }
3486
3487#if DEBUG_DCACHE
3488if ( m_debug_dcache_fsm )
3489{
3490    if ( r_dcache_tlb_ins.read() )
3491        std::cout << "  <PROC " << name()
3492                  << " DCACHE_TLB_PTE2_SELECT> Select a slot in ITLB:";
3493    else
3494        std::cout << "  <PROC " << name()
3495                  << " DCACHE_TLB_PTE2_SELECT> Select a slot in DTLB:";
3496        std::cout << " way = " << std::dec << way
3497                  << " / set = " << set << std::endl;
3498}
3499#endif
3500        r_dcache_tlb_way = way;
3501        r_dcache_tlb_set = set;
3502        r_dcache_fsm     = DCACHE_TLB_PTE2_UPDT;
3503        break;
3504    }
3505    //////////////////////////
3506    case DCACHE_TLB_PTE2_UPDT:  // write a new PTE2 in tlb after testing the L/R bit
3507                                // - if L/R bit already set, exit the sub-fsm.
3508                                // - if not, we update the page table but we dont write
3509                                //   neither in DCACHE, nor in TLB, as this will be done by
3510                                //   the coherence mechanism.
3511    {
3512        paddr_t         nline     = r_dcache_tlb_paddr.read() >> (uint32_log2(m_dcache_words)+2);
3513        uint32_t        pte_flags = r_dcache_tlb_pte_flags.read();
3514        uint32_t        pte_ppn   = r_dcache_tlb_pte_ppn.read();
3515        bool            pt_updt   = false;
3516        bool            local     = true;
3517
3518        // We should compute the access locality:
3519        // The PPN MSB bits define the destination cluster index.
3520        // The m_srcid MSB bits define the source cluster index.
3521        // The number of bits to compare depends on the number of clusters,
3522        // and can be obtained in the mapping table.
3523        // As long as this computation is not done, all access are local.
3524
3525        if ( local )                                            // local access
3526        {
3527            if ( not ((pte_flags & PTE_L_MASK) == PTE_L_MASK) ) // we must set the L bit
3528            {
3529                pt_updt                = true;
3530                r_dcache_vci_cas_old   = pte_flags;
3531                r_dcache_vci_cas_new   = pte_flags | PTE_L_MASK;
3532                pte_flags              = pte_flags | PTE_L_MASK;
3533                        r_dcache_tlb_pte_flags = pte_flags;
3534            }
3535        }
3536        else                                                    // remote access
3537        {
3538            if ( not ((pte_flags & PTE_R_MASK) == PTE_R_MASK) ) // we must set the R bit
3539            {
3540                pt_updt                = true;
3541                r_dcache_vci_cas_old   = pte_flags;
3542                r_dcache_vci_cas_new   = pte_flags | PTE_R_MASK;
3543                pte_flags              = pte_flags | PTE_R_MASK;
3544                        r_dcache_tlb_pte_flags = pte_flags;
3545            }
3546        }
3547
3548        if ( not pt_updt )                       // update TLB
3549        {
3550            if ( r_dcache_tlb_ins.read() )
3551            {
3552                r_itlb.write( false,    // 4K page
3553                              pte_flags,
3554                              pte_ppn,
3555                              r_dcache_tlb_vaddr.read(),
3556                              r_dcache_tlb_way.read(),
3557                              r_dcache_tlb_set.read(),
3558                              nline );
3559#ifdef INSTRUMENTATION
3560m_cpt_itlb_write++;
3561#endif
3562
3563#if DEBUG_DCACHE
3564if ( m_debug_dcache_fsm )
3565{
3566    std::cout << "  <PROC " << name()
3567              << " DCACHE_TLB_PTE2_UPDT> write PTE2 in ITLB"
3568              << " / set = " << std::dec << r_dcache_tlb_set.read()
3569              << " / way = " << r_dcache_tlb_way.read() << std::endl;
3570    r_itlb.printTrace();
3571}
3572#endif
3573            }
3574            else
3575            {
3576                r_dtlb.write( false,    // 4K page
3577                              pte_flags,
3578                              pte_ppn,
3579                              r_dcache_tlb_vaddr.read(),
3580                              r_dcache_tlb_way.read(),
3581                              r_dcache_tlb_set.read(),
3582                              nline );
3583#ifdef INSTRUMENTATION
3584m_cpt_dtlb_write++;
3585#endif
3586
3587#if DEBUG_DCACHE
3588if ( m_debug_dcache_fsm )
3589{
3590    std::cout << "  <PROC " << name()
3591              << " DCACHE_TLB_PTE2_UPDT> write PTE2 in DTLB"
3592              << " / set = " << std::dec << r_dcache_tlb_set.read()
3593              << " / way = " << r_dcache_tlb_way.read() << std::endl;
3594    r_dtlb.printTrace();
3595}
3596#endif
3597
3598            }
3599            r_dcache_fsm = DCACHE_TLB_RETURN;
3600        }
3601        else                                   // update page table but not TLB
3602        {
3603            r_dcache_fsm = DCACHE_TLB_LR_UPDT;  // dcache and page table update
3604
3605#if DEBUG_DCACHE
3606if ( m_debug_dcache_fsm )
3607{
3608    std::cout << "  <PROC " << name()
3609              << " DCACHE_TLB_PTE2_UPDT> L/R bit update required" << std::endl;
3610}
3611#endif
3612        }
3613        break;
3614    }
3615    ////////////////////////
3616    case DCACHE_TLB_LR_UPDT:        // request a CAS transaction to update L/R bit
3617    {
3618#if DEBUG_DCACHE
3619if ( m_debug_dcache_fsm )
3620{
3621    std::cout << "  <PROC " << name()
3622              << " DCACHE_TLB_LR_UPDT> Update dcache: (L/R) bit" << std::endl;
3623}
3624#endif
3625        // r_dcache_vci_cas_old & r_dcache_vci_cas_new registers are already set
3626        r_dcache_vci_paddr = r_dcache_tlb_paddr.read();
3627
3628        // checking llsc reservation buffer
3629        if ( r_dcache_llsc_paddr.read() == r_dcache_tlb_paddr.read() )
3630            r_dcache_llsc_valid = false;
3631
3632        // request a CAS CMD and go to DCACHE_TLB_LR_WAIT state
3633        r_dcache_vci_cas_req = true;
3634        r_dcache_fsm         = DCACHE_TLB_LR_WAIT;
3635        break;
3636    }
3637    ////////////////////////
3638    case DCACHE_TLB_LR_WAIT:            // Waiting the response to SC transaction for DIRTY bit.
3639                                    // We consume the response in rsp FIFO,
3640                                    // and exit the sub-fsm, but we don't
3641                                    // analyse the response, because we don't
3642                                    // care if the L/R bit update is not done.
3643                                    // We must take the coherence requests because
3644                                    // there is a risk of dead-lock
3645
3646    {
3647        // coherence clack request (from DSPIN CLACK)
3648        if ( r_dcache_clack_req.read() )
3649        {
3650            r_dcache_fsm = DCACHE_CC_CHECK;
3651            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3652            break;
3653        }
3654
3655        // coherence request (from CC_RECEIVE FSM)
3656        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
3657        {
3658            r_dcache_fsm = DCACHE_CC_CHECK;
3659            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3660            break;
3661        }
3662
3663        if ( r_vci_rsp_data_error.read() )      // bus error
3664        {
3665            std::cout << "BUS ERROR in DCACHE_TLB_LR_WAIT state" << std::endl;
3666            std::cout << "This should not happen in this state" << std::endl;
3667            exit(0);
3668        }
3669        else if ( r_vci_rsp_fifo_dcache.rok() ) // response available
3670        {
3671#if DEBUG_DCACHE
3672if ( m_debug_dcache_fsm )
3673{
3674    std::cout << "  <PROC " << name()
3675              << " DCACHE_TLB_LR_WAIT> SC response received" << std::endl;
3676}
3677#endif
3678            vci_rsp_fifo_dcache_get = true;
3679            r_dcache_fsm            = DCACHE_TLB_RETURN;
3680        }
3681        break;
3682    }
3683    ///////////////////////
3684    case DCACHE_TLB_RETURN:             // return to caller depending on tlb miss type
3685    {
3686#if DEBUG_DCACHE
3687if ( m_debug_dcache_fsm )
3688{
3689    std::cout << "  <PROC " << name()
3690              << " DCACHE_TLB_RETURN> TLB MISS completed" << std::endl;
3691}
3692#endif
3693        if ( r_dcache_tlb_ins.read() ) r_icache_tlb_miss_req = false;
3694        r_dcache_fsm = DCACHE_IDLE;
3695        break;
3696    }
3697    ///////////////////////
3698    case DCACHE_XTN_SWITCH:             // The r_ptpr registers must be written,
3699                                // and both itlb and dtlb must be flushed.
3700                                // Caution : the itlb miss requests must be taken
3701                                // to avoid dead-lock in case of simultaneous ITLB miss
3702                                // Caution : the clack and cc requests must be taken
3703                                // to avoid dead-lock
3704    {
3705        // coherence clack request (from DSPIN CLACK)
3706        if ( r_dcache_clack_req.read() )
3707        {
3708            r_dcache_fsm = DCACHE_CC_CHECK;
3709            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3710            break;
3711        }
3712
3713        // coherence request (from CC_RECEIVE FSM)
3714        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
3715        {
3716            r_dcache_fsm = DCACHE_CC_CHECK;
3717            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3718            break;
3719        }
3720
3721        // itlb miss request
3722        if ( r_icache_tlb_miss_req.read() )
3723        {
3724            r_dcache_tlb_ins    = true;
3725            r_dcache_tlb_vaddr  = r_icache_vaddr_save.read();
3726            r_dcache_fsm        = DCACHE_TLB_MISS;
3727            break;
3728        }
3729
3730        if ( not r_dcache_xtn_req.read() )
3731        {
3732            r_dtlb.flush();
3733            r_mmu_ptpr   = m_dreq.wdata;
3734            r_dcache_fsm = DCACHE_IDLE;
3735            m_drsp.valid = true;
3736        }
3737        break;
3738    }
3739    /////////////////////
3740    case DCACHE_XTN_SYNC:               // waiting until write buffer empty
3741                                // The coherence request must be taken
3742                                // as there is a risk of dead-lock
3743    {
3744        // coherence clack request (from DSPIN CLACK)
3745        if ( r_dcache_clack_req.read() )
3746        {
3747            r_dcache_fsm = DCACHE_CC_CHECK;
3748            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3749            break;
3750        }
3751
3752        // coherence request (from CC_RECEIVE FSM)
3753        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
3754        {
3755            r_dcache_fsm = DCACHE_CC_CHECK;
3756            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3757            break;
3758        }
3759
3760        if ( r_wbuf.empty() )
3761        {
3762            m_drsp.valid   = true;
3763            r_dcache_fsm = DCACHE_IDLE;
3764        }
3765        break;
3766    }
3767    ////////////////////////
3768    case DCACHE_XTN_IC_FLUSH:       // Waiting completion of an XTN request to the ICACHE FSM
3769    case DCACHE_XTN_IC_INVAL_VA:    // Caution : the itlb miss requests must be taken
3770    case DCACHE_XTN_IC_INVAL_PA:    // because the XTN_ICACHE_INVAL request to icache
3771    case DCACHE_XTN_IC_PADDR_EXT:   // can generate an itlb miss,
3772    case DCACHE_XTN_IT_INVAL:       // and because it can exist a simultaneous ITLB miss
3773
3774    {
3775        // coherence clack request (from DSPIN CLACK)
3776        if ( r_dcache_clack_req.read() )
3777        {
3778            r_dcache_fsm = DCACHE_CC_CHECK;
3779            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3780            break;
3781        }
3782
3783        // coherence request (from CC_RECEIVE FSM)
3784        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
3785        {
3786            r_dcache_fsm = DCACHE_CC_CHECK;
3787            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3788            break;
3789        }
3790
3791        // itlb miss request
3792        if ( r_icache_tlb_miss_req.read() )
3793        {
3794            r_dcache_tlb_ins    = true;
3795            r_dcache_tlb_vaddr  = r_icache_vaddr_save.read();
3796            r_dcache_fsm        = DCACHE_TLB_MISS;
3797            break;
3798        }
3799
3800        // test if XTN request to icache completed
3801        if ( not r_dcache_xtn_req.read() )
3802        {
3803            r_dcache_fsm = DCACHE_IDLE;
3804            m_drsp.valid = true;
3805        }
3806        break;
3807    }
3808    /////////////////////////
3809    case DCACHE_XTN_DC_FLUSH:   // Invalidate sequencially all cache lines, using
3810                                // r_dcache_flush_count as a slot counter,
3811                                // looping in this state until all slots have been visited.
3812                                // It can require two cycles per slot:
3813                                // We test here the slot state, and make the actual inval
3814                                // (if line is valid) in DCACHE_XTN_DC_FLUSH_GO state.
3815                                // A cleanup request is generated for each valid line.
3816                                // returns to IDLE and flush TLBs when last slot
3817    {
3818        // coherence clack request (from DSPIN CLACK)
3819        if ( r_dcache_clack_req.read() )
3820        {
3821            r_dcache_fsm = DCACHE_CC_CHECK;
3822            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3823            break;
3824        }
3825
3826        // coherence request (from CC_RECEIVE FSM)
3827        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
3828        {
3829            r_dcache_fsm = DCACHE_CC_CHECK;
3830            r_dcache_fsm_cc_save = r_dcache_fsm.read();
3831            break;
3832        }
3833
3834        if ( not r_dcache_cc_send_req.read() ) // blocked until previous cc_send request is sent
3835        {
3836            int       state;
3837            paddr_t   tag;
3838            size_t    way = r_dcache_flush_count.read()/m_dcache_sets;
3839            size_t    set = r_dcache_flush_count.read()%m_dcache_sets;
3840
3841#ifdef INSTRUMENTATION
3842m_cpt_dcache_dir_read++;
3843#endif
3844            r_dcache.read_dir( way,
3845                               set,
3846                               &tag,
3847                               &state );
3848
3849            if ( state == CACHE_SLOT_STATE_VALID )         // inval required
3850            {
3851                // request cleanup
3852                r_dcache_cc_send_req   = true;
3853                r_dcache_cc_send_nline = tag * m_dcache_sets + set;
3854                r_dcache_cc_send_way   = way;
3855                r_dcache_cc_send_type  = CC_TYPE_CLEANUP;
3856
3857                // goes to DCACHE_XTN_DC_FLUSH_GO to inval directory
3858                r_dcache_miss_way     = way;
3859                r_dcache_miss_set     = set;
3860                r_dcache_fsm          = DCACHE_XTN_DC_FLUSH_GO;
3861            }
3862            else if ( r_dcache_flush_count.read() ==
3863                      (m_dcache_sets*m_dcache_ways - 1) )  // last slot
3864            {
3865                r_dtlb.reset();
3866                r_itlb.reset();
3867                r_dcache_fsm = DCACHE_IDLE;
3868                m_drsp.valid = true;
3869            }
3870
3871            // saturation counter
3872            if ( r_dcache_flush_count.read() < (m_dcache_sets*m_dcache_ways - 1) )
3873                r_dcache_flush_count = r_dcache_flush_count.read() + 1;
3874        }
3875        break;
3876    }
3877    ////////////////////////////
3878    case DCACHE_XTN_DC_FLUSH_GO:    // Switch the cache slot to ZOMBI state
3879                                    // and reset directory extension.
3880                                    // returns to IDLE and flush TLBs when last slot
3881    {
3882        size_t way = r_dcache_miss_way.read();
3883        size_t set = r_dcache_miss_set.read();
3884
3885        r_dcache_in_tlb[m_dcache_sets*way+set]       = false;
3886        r_dcache_contains_ptd[m_dcache_sets*way+set] = false;
3887
3888#ifdef INSTRUMENTATION
3889m_cpt_dcache_dir_write++;
3890#endif
3891        r_dcache.write_dir( way,
3892                            set,
3893                            CACHE_SLOT_STATE_ZOMBI );
3894
3895        if ( r_dcache_flush_count.read() ==
3896             (m_dcache_sets*m_dcache_ways - 1) )  // last slot
3897        {
3898            r_dtlb.reset();
3899            r_itlb.reset();
3900            r_dcache_fsm = DCACHE_IDLE;
3901            m_drsp.valid = true;
3902        }
3903        else
3904        {
3905            r_dcache_fsm = DCACHE_XTN_DC_FLUSH;
3906        }
3907        break;
3908    }
3909    /////////////////////////
3910    case DCACHE_XTN_DT_INVAL:   // handling processor XTN_DTLB_INVAL request
3911    {
3912        r_dtlb.inval(r_dcache_save_wdata.read());
3913        r_dcache_fsm        = DCACHE_IDLE;
3914        m_drsp.valid          = true;
3915        break;
3916    }
3917    ////////////////////////////
3918    case DCACHE_XTN_DC_INVAL_VA:  // selective cache line invalidate with virtual address
3919                                  // requires 3 cycles: access tlb, read cache, inval cache
3920                                  // we compute the physical address in this state
3921    {
3922        paddr_t paddr;
3923        bool    hit;
3924
3925        if ( r_mmu_mode.read() & DATA_TLB_MASK )        // dtlb activated
3926        {
3927
3928#ifdef INSTRUMENTATION
3929m_cpt_dtlb_read++;
3930#endif
3931            hit = r_dtlb.translate( r_dcache_save_wdata.read(),
3932                                    &paddr );
3933        }
3934        else                                            // dtlb not activated
3935        {
3936            paddr = (paddr_t)r_dcache_save_wdata.read();
3937            if (vci_param::N > 32) 
3938                paddr = paddr | ((paddr_t)(r_dcache_paddr_ext.read()) << 32);
3939            hit   = true;
3940        }
3941
3942        if ( hit )              // tlb hit
3943        {
3944            r_dcache_save_paddr = paddr;
3945            r_dcache_fsm      = DCACHE_XTN_DC_INVAL_PA;
3946        }
3947        else                    // tlb miss
3948        {
3949
3950#ifdef INSTRUMENTATION
3951m_cpt_dtlb_miss++;
3952#endif
3953            r_dcache_tlb_ins    = false;                // dtlb
3954            r_dcache_tlb_vaddr  = r_dcache_save_wdata.read();
3955            r_dcache_fsm        = DCACHE_TLB_MISS;
3956        }
3957
3958#if DEBUG_DCACHE
3959if ( m_debug_dcache_fsm )
3960{
3961    std::cout << "  <PROC " << name()
3962              << " DCACHE_XTN_DC_INVAL_VA> Compute physical address" << std::hex
3963              << " / VADDR = " << r_dcache_save_wdata.read()
3964              << " / PADDR = " << paddr << std::endl;
3965}
3966#endif
3967
3968        break;
3969    }
3970    ////////////////////////////
3971    case DCACHE_XTN_DC_INVAL_PA:  // selective cache line invalidate with physical address
3972                                  // requires 2 cycles: read cache / inval cache
3973                                  // In this state we read dcache.
3974    {
3975        size_t          way;
3976        size_t          set;
3977        size_t          word;
3978        int         state;
3979
3980#ifdef INSTRUMENTATION
3981m_cpt_dcache_dir_read++;
3982#endif
3983        r_dcache.read_dir( r_dcache_save_paddr.read(),
3984                           &state,
3985                           &way,
3986                           &set,
3987                           &word );
3988
3989        if ( state == CACHE_SLOT_STATE_VALID )  // inval to be done
3990        {
3991            r_dcache_xtn_way = way;
3992            r_dcache_xtn_set = set;
3993            r_dcache_fsm      = DCACHE_XTN_DC_INVAL_GO;
3994        }
3995        else            // miss : nothing to do
3996        {
3997            r_dcache_fsm      = DCACHE_IDLE;
3998            m_drsp.valid      = true;
3999        }
4000
4001#if DEBUG_DCACHE
4002if ( m_debug_dcache_fsm )
4003{
4004    std::cout << "  <PROC " << name()
4005              << " DCACHE_XTN_DC_INVAL_PA> Test hit in dcache" << std::hex
4006              << " / PADDR = " << r_dcache_save_paddr.read() << std::dec
4007              << " / HIT = " << (state == CACHE_SLOT_STATE_VALID)
4008              << " / SET = " << set
4009              << " / WAY = " << way << std::endl;
4010}
4011#endif
4012        break;
4013    }
4014    ////////////////////////////
4015    case DCACHE_XTN_DC_INVAL_GO:  // In this state, we invalidate the cache line
4016                                              // Blocked if previous cleanup not completed
4017                                  // Test if itlb or dtlb inval is required
4018    {
4019        if ( not r_dcache_cc_send_req.read() ) // blocked until previous cc_send request is sent
4020        {
4021            size_t      way        = r_dcache_xtn_way.read();
4022            size_t      set        = r_dcache_xtn_set.read();
4023            paddr_t nline      = r_dcache_save_paddr.read() / (m_dcache_words<<2);
4024
4025#ifdef INSTRUMENTATION
4026m_cpt_dcache_dir_write++;
4027#endif
4028            r_dcache.write_dir( way,
4029                                set,
4030                                CACHE_SLOT_STATE_ZOMBI );
4031
4032            // request cleanup
4033            r_dcache_cc_send_req   = true;
4034            r_dcache_cc_send_nline = nline;
4035            r_dcache_cc_send_way   = way;
4036            r_dcache_cc_send_type  = CC_TYPE_CLEANUP;
4037
4038            // possible itlb & dtlb invalidate
4039            if ( r_dcache_in_tlb[way*m_dcache_sets+set] )
4040            {
4041                r_dcache_tlb_inval_line = nline;
4042                r_dcache_tlb_inval_set  = 0;
4043                r_dcache_fsm_scan_save  = DCACHE_XTN_DC_INVAL_END;
4044                r_dcache_fsm            = DCACHE_INVAL_TLB_SCAN;
4045                r_dcache_in_tlb[way*m_dcache_sets+set] = false;
4046            }
4047            else if ( r_dcache_contains_ptd[way*m_dcache_sets+set] )
4048            {
4049                r_itlb.reset();
4050                r_dtlb.reset();
4051                r_dcache_contains_ptd[way*m_dcache_sets+set] = false;
4052                r_dcache_fsm = DCACHE_IDLE;
4053                m_drsp.valid = true;
4054            }
4055            else
4056            {
4057                r_dcache_fsm = DCACHE_IDLE;
4058                m_drsp.valid = true;
4059            }
4060
4061#if DEBUG_DCACHE
4062if ( m_debug_dcache_fsm )
4063{
4064    std::cout << "  <PROC " << name()
4065              << " DCACHE_XTN_DC_INVAL_GO> Actual dcache inval" << std::hex
4066              << " / PADDR = " << r_dcache_save_paddr.read() << std::endl;
4067}
4068#endif
4069            }
4070        break;
4071    }
4072    //////////////////////////////
4073    case DCACHE_XTN_DC_INVAL_END:       // send response to processor XTN request
4074    {
4075        r_dcache_fsm = DCACHE_IDLE;
4076        m_drsp.valid = true;
4077        break;
4078    }
4079    ////////////////////////
4080    case DCACHE_MISS_SELECT:       // Try to select a slot in associative set,
4081                                   // Waiting in this state if no slot available.
4082                                   // If a victim slot has been choosen and the r_icache_cc_send_req is false,
4083                                   // we send the cleanup request in this state.
4084                                   // If not, a r_icache_cleanup_victim_req flip-flop is
4085                                   // utilized for saving this cleanup request, and it will be sent later
4086                                   // in state ICACHE_MISS_WAIT or ICACHE_MISS_UPDT_DIR.
4087                                   // The r_icache_miss_clack flip-flop is set
4088                                   // when a cleanup is required
4089    {
4090        if ( m_dreq.valid) m_cost_data_miss_frz++;
4091
4092        // coherence clack request (from DSPIN CLACK)
4093        if ( r_dcache_clack_req.read() )
4094        {
4095            r_dcache_fsm = DCACHE_CC_CHECK;
4096            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4097            break;
4098        }
4099
4100        // coherence request (from CC_RECEIVE FSM)
4101        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
4102        {
4103            r_dcache_fsm = DCACHE_CC_CHECK;
4104            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4105            break;
4106        }
4107
4108        bool     found = false;
4109        bool     cleanup = false;
4110        size_t   way = 0;
4111        size_t   set = 0;
4112        paddr_t  victim = 0;
4113
4114#ifdef INSTRUMENTATION
4115m_cpt_dcache_dir_read++;
4116#endif
4117        r_dcache.read_select( r_dcache_save_paddr.read(),
4118                              &victim,
4119                              &way,
4120                              &set,
4121                              &found,
4122                              &cleanup );
4123
4124        if (  not found )
4125        {
4126            break;
4127        }
4128        else
4129        {
4130            r_dcache_miss_way = way;
4131            r_dcache_miss_set = set;
4132
4133            if ( cleanup )
4134            {
4135                if ( not r_dcache_cc_send_req.read() )
4136                {
4137                    r_dcache_cc_send_req    = true;
4138                    r_dcache_cc_send_nline  = victim;
4139                    r_dcache_cc_send_way    = way;
4140                    r_dcache_cc_send_type   = CC_TYPE_CLEANUP;   
4141
4142                }
4143                else
4144                {
4145                    r_dcache_cleanup_victim_req   = true;
4146                    r_dcache_cleanup_victim_nline = victim;
4147                }
4148
4149                r_dcache_miss_clack           = true;
4150                r_dcache_fsm                  = DCACHE_MISS_CLEAN;
4151            }
4152            else
4153            {
4154                r_dcache_fsm          = DCACHE_MISS_WAIT;
4155            }
4156
4157#if DEBUG_DCACHE
4158if ( m_debug_dcache_fsm )
4159{
4160    std::cout << "  <PROC " << name()
4161              << " DCACHE_MISS_SELECT> Select a slot:" << std::dec
4162              << " / WAY = "   << way
4163              << " / SET = "   << set
4164              << " / PADDR = " << std::hex << r_dcache_save_paddr.read();
4165    if(cleanup) std::cout << " / VICTIM = " << (victim*m_dcache_words*4) << std::endl;
4166    else        std::cout << std::endl;
4167}
4168#endif
4169        } // end found
4170        break;
4171    }
4172    ///////////////////////
4173    case DCACHE_MISS_CLEAN:             // switch the slot to ZOMBI state
4174                                // and possibly request itlb or dtlb invalidate
4175    {
4176        if ( m_dreq.valid) m_cost_data_miss_frz++;
4177
4178        size_t  way   = r_dcache_miss_way.read();
4179        size_t  set   = r_dcache_miss_set.read();
4180
4181#ifdef INSTRUMENTATION
4182m_cpt_dcache_dir_read++;
4183#endif
4184        r_dcache.write_dir( way,
4185                            set,
4186                            CACHE_SLOT_STATE_ZOMBI );
4187#if DEBUG_DCACHE
4188if ( m_debug_dcache_fsm )
4189{
4190    std::cout << "  <PROC " << name()
4191              << " DCACHE_MISS_CLEAN> Switch to ZOMBI state" << std::dec
4192              << " / way = "   << way
4193              << " / set = "   << set << std::endl;
4194}
4195#endif
4196        // if selective itlb & dtlb invalidate are required
4197        // the miss response is not handled before invalidate completed
4198        if ( r_dcache_in_tlb[way*m_dcache_sets+set] )
4199        {
4200            r_dcache_in_tlb[way*m_dcache_sets+set] = false;
4201
4202            if( not r_dcache_cleanup_victim_req.read() )
4203                r_dcache_tlb_inval_line  = r_dcache_cc_send_nline.read();
4204            else
4205                r_dcache_tlb_inval_line = r_dcache_cleanup_victim_nline.read();
4206
4207            r_dcache_tlb_inval_set   = 0;
4208            r_dcache_fsm_scan_save   = DCACHE_MISS_WAIT;
4209            r_dcache_fsm             = DCACHE_INVAL_TLB_SCAN;
4210        }
4211        else if ( r_dcache_contains_ptd[way*m_dcache_sets+set] )
4212        {
4213            r_itlb.reset();
4214            r_dtlb.reset();
4215            r_dcache_contains_ptd[way*m_dcache_sets+set] = false;
4216            r_dcache_fsm = DCACHE_MISS_WAIT;
4217        }
4218        else
4219        {
4220            r_dcache_fsm = DCACHE_MISS_WAIT;
4221        }
4222        break;
4223    }
4224    //////////////////////
4225    case DCACHE_MISS_WAIT:      // waiting the response to a miss request from VCI_RSP FSM
4226                            // This state is in charge of error signaling
4227                            // There is 5 types of error depending on the requester
4228    {
4229        if ( m_dreq.valid) m_cost_data_miss_frz++;
4230
4231        // send cleanup victim request
4232        if ( r_dcache_cleanup_victim_req.read() and not r_dcache_cc_send_req.read() )
4233        {
4234            r_dcache_cc_send_req        = true;
4235            r_dcache_cc_send_nline      = r_dcache_cleanup_victim_nline;
4236            r_dcache_cc_send_way        = r_dcache_miss_way;
4237            r_dcache_cc_send_type       = CC_TYPE_CLEANUP; 
4238            r_dcache_cleanup_victim_req = false;
4239        }   
4240
4241        // coherence clack request (from DSPIN CLACK)
4242        if ( r_dcache_clack_req.read() )
4243        {
4244            r_dcache_fsm = DCACHE_CC_CHECK;
4245            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4246            break;
4247        }
4248
4249        // coherence request (from CC_RECEIVE FSM)
4250        if ( r_cc_receive_dcache_req.read() and
4251             not r_dcache_cc_send_req.read() and
4252             not r_dcache_cleanup_victim_req.read())
4253        {
4254            r_dcache_fsm = DCACHE_CC_CHECK;
4255            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4256            break;
4257        }
4258
4259        if ( r_vci_rsp_data_error.read() )                      // bus error
4260        {
4261            switch ( r_dcache_miss_type.read() )
4262            {
4263                case PROC_MISS:
4264                {
4265                    r_mmu_detr            = MMU_READ_DATA_ILLEGAL_ACCESS;
4266                    r_mmu_dbvar           = r_dcache_save_vaddr.read();
4267                    m_drsp.valid            = true;
4268                    m_drsp.error            = true;
4269                    r_dcache_fsm          = DCACHE_IDLE;
4270                    break;
4271                }
4272                case PTE1_MISS:
4273                {
4274                    if ( r_dcache_tlb_ins.read() )
4275                    {
4276                        r_mmu_ietr              = MMU_READ_PT1_ILLEGAL_ACCESS;
4277                        r_mmu_ibvar             = r_dcache_tlb_vaddr.read();
4278                        r_icache_tlb_miss_req   = false;
4279                        r_icache_tlb_rsp_error  = true;
4280                    }
4281                    else
4282                    {
4283                        r_mmu_detr              = MMU_READ_PT1_ILLEGAL_ACCESS;
4284                        r_mmu_dbvar             = r_dcache_tlb_vaddr.read();
4285                        m_drsp.valid              = true;
4286                        m_drsp.error              = true;
4287                    }
4288                    r_dcache_fsm                = DCACHE_IDLE;
4289                    break;
4290                }
4291                case PTE2_MISS:
4292                {
4293                    if ( r_dcache_tlb_ins.read() )
4294                    {
4295                        r_mmu_ietr              = MMU_READ_PT2_ILLEGAL_ACCESS;
4296                        r_mmu_ibvar             = r_dcache_tlb_vaddr.read();
4297                        r_icache_tlb_miss_req   = false;
4298                        r_icache_tlb_rsp_error  = true;
4299                    }
4300                    else
4301                    {
4302                        r_mmu_detr              = MMU_READ_PT2_ILLEGAL_ACCESS;
4303                        r_mmu_dbvar             = r_dcache_tlb_vaddr.read();
4304                        m_drsp.valid              = true;
4305                        m_drsp.error              = true;
4306                    }
4307                    r_dcache_fsm                = DCACHE_IDLE;
4308                    break;
4309                }
4310            } // end switch type
4311            r_vci_rsp_data_error = false;
4312        }
4313        else if ( r_vci_rsp_fifo_dcache.rok() )         // valid response available
4314        {
4315            r_dcache_miss_word = 0;
4316                r_dcache_fsm       = DCACHE_MISS_DATA_UPDT;
4317        }
4318        break;
4319    }
4320    //////////////////////////
4321    case DCACHE_MISS_DATA_UPDT:   // update the dcache (one word per cycle)
4322    {
4323        if ( m_dreq.valid) m_cost_data_miss_frz++;
4324
4325        if ( r_vci_rsp_fifo_dcache.rok() )      // one word available
4326        {
4327#ifdef INSTRUMENTATION
4328m_cpt_dcache_data_write++;
4329#endif
4330                r_dcache.write( r_dcache_miss_way.read(),
4331                                r_dcache_miss_set.read(),
4332                                r_dcache_miss_word.read(),
4333                                r_vci_rsp_fifo_dcache.read() );
4334#if DEBUG_DCACHE
4335if ( m_debug_dcache_fsm )
4336{
4337    std::cout << "  <PROC " << name()
4338              << " DCACHE_MISS_DATA_UPDT> Write one word:"
4339              << " / DATA = "  << std::hex << r_vci_rsp_fifo_dcache.read()
4340              << " / WAY = "   << std::dec << r_dcache_miss_way.read()
4341              << " / SET = "   << r_dcache_miss_set.read()
4342              << " / WORD = "  << r_dcache_miss_word.read() << std::endl;
4343}
4344#endif
4345            vci_rsp_fifo_dcache_get = true;
4346            r_dcache_miss_word = r_dcache_miss_word.read() + 1;
4347
4348            if ( r_dcache_miss_word.read() == (m_dcache_words-1) ) // last word
4349            {
4350                r_dcache_fsm = DCACHE_MISS_DIR_UPDT;
4351            }
4352        }
4353        break;
4354    }
4355    //////////////////////////
4356    case DCACHE_MISS_DIR_UPDT:  // Stalled if a victim line has been evicted
4357                                // and the cleanup ack has not been received,
4358                                // as indicated by the r_dcache_miss clack.
4359                                // - If no matching coherence request (r_dcache_inval_miss)
4360                                //   switch directory slot to VALID state.
4361                                // - If matching coherence request, switch directory slot
4362                                //   to ZOMBI state, and send a cleanup request.
4363    {
4364        if ( m_dreq.valid) m_cost_data_miss_frz++;
4365
4366        // send cleanup victim request
4367        if ( r_dcache_cleanup_victim_req.read() and not r_dcache_cc_send_req.read() )
4368        {
4369            r_dcache_cc_send_req        = true;
4370            r_dcache_cc_send_nline      = r_dcache_cleanup_victim_nline;
4371            r_dcache_cc_send_way        = r_dcache_miss_way;
4372            r_dcache_cc_send_type       = CC_TYPE_CLEANUP; 
4373            r_dcache_cleanup_victim_req = false;
4374        }   
4375
4376        // coherence clack request (from DSPIN CLACK)
4377        if ( r_dcache_clack_req.read() )
4378        {
4379            r_dcache_fsm = DCACHE_CC_CHECK;
4380            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4381            break;
4382        }
4383
4384        // coherence request (from CC_RECEIVE FSM)
4385        if ( r_cc_receive_dcache_req.read() and
4386             not r_dcache_cc_send_req.read() and
4387             not r_dcache_cleanup_victim_req.read() )
4388        {
4389            r_dcache_fsm = DCACHE_CC_CHECK;
4390            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4391            break;
4392        }
4393
4394        if ( not r_dcache_miss_clack.read() )  // waiting cleanup acknowledge
4395        {
4396            if ( r_dcache_miss_inval.read() ) // switch slot to ZOMBI state, and new cleanup
4397            {
4398                if ( not r_dcache_cc_send_req.read() ) // blocked until previous request sent
4399                {
4400                    r_dcache_miss_inval     = false;
4401                    // request cleanup
4402                    r_dcache_cc_send_req   = true;
4403                    r_dcache_cc_send_nline = r_dcache_save_paddr.read()/(m_dcache_words<<2);
4404                    r_dcache_cc_send_way   = r_dcache_miss_way.read();
4405                    r_dcache_cc_send_type  = CC_TYPE_CLEANUP;
4406
4407#ifdef INSTRUMENTATION
4408m_cpt_dcache_dir_write++;
4409#endif
4410                    r_dcache.write_dir( r_dcache_save_paddr.read(),
4411                                        r_dcache_miss_way.read(),
4412                                        r_dcache_miss_set.read(),
4413                                        CACHE_SLOT_STATE_ZOMBI );
4414#if DEBUG_DCACHE
4415if ( m_debug_dcache_fsm )
4416std::cout << "  <PROC " << name()
4417          << " DCACHE_MISS_DIR_UPDT> Switch slot to ZOMBI state"
4418          << " PADDR = " << std::hex << r_dcache_save_paddr.read()
4419          << " / WAY = "   << std::dec << r_dcache_miss_way.read()
4420          << " / SET = "   << r_dcache_miss_set.read() << std::endl;
4421#endif
4422                }
4423                else
4424                    break;
4425            }
4426            else                              // switch slot to VALID state
4427            {
4428
4429#ifdef INSTRUMENTATION
4430m_cpt_dcache_dir_write++;
4431#endif
4432                r_dcache.write_dir( r_dcache_save_paddr.read(),
4433                                    r_dcache_miss_way.read(),
4434                                    r_dcache_miss_set.read(),
4435                                    CACHE_SLOT_STATE_VALID );
4436
4437#if DEBUG_DCACHE
4438if ( m_debug_dcache_fsm )
4439std::cout << "  <PROC " << name()
4440          << " DCACHE_MISS_DIR_UPDT> Switch slot to VALID state"
4441          << " PADDR = " << std::hex << r_dcache_save_paddr.read()
4442          << " / WAY = "   << std::dec << r_dcache_miss_way.read()
4443          << " / SET = "   << r_dcache_miss_set.read() << std::endl;
4444#endif
4445                // reset directory extension
4446                size_t way = r_dcache_miss_way.read();
4447                size_t set = r_dcache_miss_set.read();
4448                r_dcache_in_tlb[way*m_dcache_sets+set] = false;
4449                r_dcache_contains_ptd[way*m_dcache_sets+set] = false;
4450            }
4451            if      (r_dcache_miss_type.read()==PTE1_MISS) r_dcache_fsm = DCACHE_TLB_PTE1_GET;
4452            else if (r_dcache_miss_type.read()==PTE2_MISS) r_dcache_fsm = DCACHE_TLB_PTE2_GET;
4453            else                                           r_dcache_fsm = DCACHE_IDLE;
4454        }
4455        break;
4456    }
4457    /////////////////////
4458    case DCACHE_UNC_WAIT:  // waiting a response to an uncacheable read
4459    {
4460        // coherence clack request (from DSPIN CLACK)
4461        if ( r_dcache_clack_req.read() )
4462        {
4463            r_dcache_fsm = DCACHE_CC_CHECK;
4464            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4465            break;
4466        }
4467
4468        // coherence request (from CC_RECEIVE FSM)
4469        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
4470        {
4471            r_dcache_fsm = DCACHE_CC_CHECK;
4472            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4473            break;
4474        }
4475
4476        if ( r_vci_rsp_data_error.read() )      // bus error
4477        {
4478            if(r_dcache_vci_unc_write.read())
4479                r_mmu_detr           = MMU_WRITE_DATA_ILLEGAL_ACCESS;
4480            else
4481                r_mmu_detr           = MMU_READ_DATA_ILLEGAL_ACCESS;
4482               
4483            r_mmu_dbvar          = m_dreq.addr;
4484            r_vci_rsp_data_error = false;
4485            m_drsp.error         = true;
4486            m_drsp.valid         = true;
4487            r_dcache_fsm         = DCACHE_IDLE;
4488            break;
4489        }
4490            else if ( r_vci_rsp_fifo_dcache.rok() )     // data available
4491            {
4492            // consume data
4493            vci_rsp_fifo_dcache_get = true;
4494            r_dcache_fsm            = DCACHE_IDLE;
4495
4496            // acknowledge the processor request if it has not been modified
4497            if ( m_dreq.valid and (m_dreq.addr == r_dcache_save_vaddr.read()) )
4498            {
4499                    m_drsp.valid        = true;
4500                m_drsp.error        = false;
4501                    m_drsp.rdata        = r_vci_rsp_fifo_dcache.read();
4502            }
4503            }
4504        break;
4505    }
4506    /////////////////////
4507    case DCACHE_LL_WAIT:    // waiting VCI response to a LL transaction
4508    {
4509        // coherence clack request (from DSPIN CLACK)
4510        if ( r_dcache_clack_req.read() )
4511        {
4512            r_dcache_fsm = DCACHE_CC_CHECK;
4513            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4514            break;
4515        }
4516
4517        // coherence request (from CC_RECEIVE FSM)
4518        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
4519        {
4520            r_dcache_fsm = DCACHE_CC_CHECK;
4521            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4522            break;
4523        }
4524
4525        if ( r_vci_rsp_data_error.read() )      // bus error
4526        {
4527            r_mmu_detr           = MMU_READ_DATA_ILLEGAL_ACCESS;
4528            r_mmu_dbvar          = m_dreq.addr;
4529            r_vci_rsp_data_error = false;
4530            m_drsp.error         = true;
4531            m_drsp.valid         = true;
4532            r_dcache_fsm         = DCACHE_IDLE;
4533            break;
4534        }
4535            else if ( r_vci_rsp_fifo_dcache.rok() )     // data available
4536            {
4537            // consume data
4538            vci_rsp_fifo_dcache_get = true;
4539
4540            if(r_dcache_ll_rsp_count.read() == 0) // first flit
4541            {
4542                // set key value in llsc reservation buffer
4543                r_dcache_llsc_key     = r_vci_rsp_fifo_dcache.read();
4544                r_dcache_ll_rsp_count = r_dcache_ll_rsp_count.read() + 1 ;
4545            }
4546            else                                  // last flit
4547            {
4548                // acknowledge the processor request if it has not been modified
4549                if ( m_dreq.valid and (m_dreq.addr == r_dcache_save_vaddr.read()) )
4550                {
4551                    m_drsp.valid        = true;
4552                    m_drsp.error        = false;
4553                    m_drsp.rdata        = r_vci_rsp_fifo_dcache.read();
4554                }
4555                r_dcache_fsm = DCACHE_IDLE;
4556            }
4557            }
4558        break;
4559    }
4560    ////////////////////
4561    case DCACHE_SC_WAIT:        // waiting VCI response to a SC transaction
4562    {
4563        // coherence clack request (from DSPIN CLACK)
4564        if ( r_dcache_clack_req.read() )
4565        {
4566            r_dcache_fsm = DCACHE_CC_CHECK;
4567            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4568            break;
4569        }
4570
4571        // coherence request (from CC_RECEIVE FSM)
4572        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
4573        {
4574            r_dcache_fsm = DCACHE_CC_CHECK;
4575            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4576            break;
4577        }
4578
4579        if ( r_vci_rsp_data_error.read() )              // bus error
4580        {
4581            r_mmu_detr           = MMU_READ_DATA_ILLEGAL_ACCESS;
4582            r_mmu_dbvar          = m_dreq.addr;
4583            r_vci_rsp_data_error = false;
4584            m_drsp.error         = true;
4585            m_drsp.valid         = true;
4586            r_dcache_fsm         = DCACHE_IDLE;
4587            break;
4588        }
4589            else if ( r_vci_rsp_fifo_dcache.rok() ) // response available
4590            {
4591            // consume response
4592            vci_rsp_fifo_dcache_get = true;
4593            m_drsp.valid            = true;
4594            m_drsp.rdata            = r_vci_rsp_fifo_dcache.read();
4595            r_dcache_fsm            = DCACHE_IDLE;
4596            }
4597        break;
4598    }
4599    //////////////////////////
4600    case DCACHE_DIRTY_GET_PTE:          // This sub_fsm set the PTE Dirty bit in memory
4601                                    // before handling a processor WRITE or SC request
4602                                    // Input argument is r_dcache_dirty_paddr
4603                                    // In this first state, we get PTE value in dcache
4604                                    // and post a CAS request to CMD FSM
4605    {
4606        // get PTE in dcache
4607        uint32_t pte;
4608        size_t   way;
4609        size_t   set;
4610        size_t   word; // unused
4611        int      state;
4612
4613#ifdef INSTRUMENTATION
4614m_cpt_dcache_data_read++;
4615m_cpt_dcache_dir_read++;
4616#endif
4617        r_dcache.read( r_dcache_dirty_paddr.read(),
4618                       &pte,
4619                       &way,
4620                       &set,
4621                       &word,
4622                       &state );
4623
4624        assert( (state == CACHE_SLOT_STATE_VALID) and
4625        "error in DCACHE_DIRTY_TLB_SET: the PTE should be in dcache" );
4626
4627        // request CAS transaction to CMD_FSM
4628        r_dcache_dirty_way  = way;
4629        r_dcache_dirty_set  = set;
4630
4631        // check llsc reservation buffer
4632        if (r_dcache_llsc_paddr.read() == r_dcache_dirty_paddr.read() )
4633            r_dcache_llsc_valid = false;
4634
4635        // request a CAS CMD and go to DCACHE_DIRTY_WAIT state
4636        r_dcache_vci_cas_req = true;
4637        r_dcache_vci_paddr   = r_dcache_dirty_paddr.read();
4638        r_dcache_vci_cas_old = pte;
4639        r_dcache_vci_cas_new = pte | PTE_D_MASK;
4640        r_dcache_fsm         = DCACHE_DIRTY_WAIT;
4641
4642#if DEBUG_DCACHE
4643if ( m_debug_dcache_fsm )
4644{
4645    std::cout << "  <PROC " << name()
4646              << " DCACHE_DIRTY_GET_PTE> CAS request" << std::hex
4647              << " / PTE_PADDR = " << r_dcache_dirty_paddr.read()
4648              << " / PTE_VALUE = " << pte << std::dec
4649              << " / SET = " << set
4650              << " / WAY = " << way << std::endl;
4651}
4652#endif
4653        break;
4654    }
4655    ///////////////////////
4656    case DCACHE_DIRTY_WAIT:         // wait completion of CAS for PTE Dirty bit,
4657                                    // and return to IDLE state when response is received.
4658                                    // we don't care if the CAS is a failure:
4659                                    // - if the CAS is a success, the coherence mechanism
4660                                    //   updates the local copy.
4661                                    // - if the CAS is a failure, we just retry the write.
4662    {
4663        // coherence clack request (from DSPIN CLACK)
4664        if ( r_dcache_clack_req.read() )
4665        {
4666            r_dcache_fsm = DCACHE_CC_CHECK;
4667            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4668            break;
4669        }
4670
4671        // coherence request (from CC_RECEIVE FSM)
4672        if ( r_cc_receive_dcache_req.read() and not r_dcache_cc_send_req.read())
4673        {
4674            r_dcache_fsm = DCACHE_CC_CHECK;
4675            r_dcache_fsm_cc_save = r_dcache_fsm.read();
4676            break;
4677        }
4678
4679        if ( r_vci_rsp_data_error.read() )      // bus error
4680        {
4681            std::cout << "BUS ERROR in DCACHE_DIRTY_WAIT state" << std::endl;
4682            std::cout << "This should not happen in this state" << std::endl;
4683            exit(0);
4684        }
4685        else if ( r_vci_rsp_fifo_dcache.rok() ) // response available
4686        {
4687            vci_rsp_fifo_dcache_get = true;
4688            r_dcache_fsm            = DCACHE_IDLE;
4689
4690#if DEBUG_DCACHE
4691if ( m_debug_dcache_fsm )
4692{
4693    std::cout << "  <PROC " << name()
4694              << " DCACHE_DIRTY_WAIT> CAS completed" << std::endl;
4695}
4696#endif
4697        }
4698        break;
4699    }
4700    /////////////////////
4701    case DCACHE_CC_CHECK:   // This state is the entry point for the sub-FSM
4702                            // handling coherence requests for DCACHE.
4703                            // If there is a matching pending miss on the modified cache
4704                            // line this is signaled in the r_dcache_miss inval flip-flop.
4705                            // If the updated (or invalidated) cache line has copies in TLBs
4706                            // these TLB copies are invalidated.
4707                            // The return state is defined in r_dcache_fsm_cc_save
4708    {
4709        paddr_t  paddr = r_cc_receive_dcache_nline.read() * m_dcache_words * 4;
4710        paddr_t  mask = ~((m_dcache_words<<2)-1);
4711
4712        // CLACK handler
4713        // We switch the directory slot to EMPTY state and reset
4714        // r_dcache_miss_clack if the cleanup ack is matching a pending miss.
4715        if ( r_dcache_clack_req.read() )
4716        {
4717            if ( m_dreq.valid ) m_cost_data_miss_frz++;
4718
4719#ifdef INSTRUMENTATION
4720m_cpt_dcache_dir_write++;
4721#endif
4722            r_dcache.write_dir( 0,
4723                                r_dcache_clack_way.read(),
4724                                r_dcache_clack_set.read(),
4725                                CACHE_SLOT_STATE_EMPTY);
4726
4727            if ( (r_dcache_miss_set.read() == r_dcache_clack_set.read()) and
4728                 (r_dcache_miss_way.read() == r_dcache_clack_way.read()) )
4729            {
4730                  r_dcache_miss_clack = false;
4731            }
4732
4733            r_dcache_clack_req = false;
4734
4735            // return to cc_save state
4736            r_dcache_fsm = r_dcache_fsm_cc_save.read() ;
4737
4738#if DEBUG_DCACHE
4739if ( m_debug_dcache_fsm )
4740{
4741    std::cout << "  <PROC " << name()
4742              << " DCACHE_CC_CHECK> CLACK for PADDR " << paddr
4743              << " Switch slot to EMPTY state : "
4744              << " set = " << r_dcache_clack_set.read()
4745              << " / way = " << r_dcache_clack_way.read() << std::endl;
4746}
4747#endif
4748            break;
4749        }
4750
4751        assert ( not r_dcache_cc_send_req.read() and
4752        "CC_SEND must be available in DCACHE_CC_CHECK" );
4753
4754        // Match between MISS address and CC address
4755        if (r_cc_receive_dcache_req.read() and
4756          ((r_dcache_fsm_cc_save == DCACHE_MISS_SELECT  )  or
4757           (r_dcache_fsm_cc_save == DCACHE_MISS_WAIT    )  or
4758           (r_dcache_fsm_cc_save == DCACHE_MISS_DIR_UPDT)) and
4759          ((r_dcache_vci_paddr.read() & mask) == (paddr & mask))) // matching
4760        {
4761            // signaling matching
4762            r_dcache_miss_inval = true;
4763
4764            // in case of update, go to CC_UPDT
4765            // JUST TO POP THE FIFO
4766            if (r_cc_receive_dcache_type.read() == CC_TYPE_UPDT)
4767            {
4768                r_dcache_fsm        = DCACHE_CC_UPDT;
4769                r_dcache_cc_word    = r_cc_receive_word_idx.read();
4770
4771                // just pop the fifo , don't write in icache
4772                r_dcache_cc_need_write = false;
4773            }
4774            // the request is dealt with
4775            else
4776            {
4777                r_cc_receive_dcache_req = false;
4778                r_dcache_fsm            = r_dcache_fsm_cc_save.read();
4779            }
4780
4781#if DEBUG_DCACHE
4782if ( m_debug_dcache_fsm )
4783{
4784    std::cout << "  <PROC " << name()
4785              << " DCACHE_CC_CHECK> Coherence request matching a pending miss:"
4786              << " PADDR = " << std::hex << paddr << std::endl;
4787}
4788#endif
4789        }
4790
4791        // CC request handler
4792       
4793        int    state = 0;
4794        size_t way   = 0;
4795        size_t set   = 0;
4796        size_t word  = 0;
4797
4798#ifdef INSTRUMENTATION
4799m_cpt_dcache_dir_read++;
4800#endif
4801        r_dcache.read_dir( paddr,
4802                           &state,
4803                           &way,
4804                           &set,
4805                           &word ); // unused
4806
4807        r_dcache_cc_way = way;
4808        r_dcache_cc_set = set;
4809
4810        if ( state == CACHE_SLOT_STATE_VALID) // hit
4811        {
4812            // need to update the cache state
4813            if (r_cc_receive_dcache_type.read() == CC_TYPE_UPDT) // hit update
4814            {
4815                r_dcache_cc_need_write = true;
4816                r_dcache_fsm           = DCACHE_CC_UPDT;
4817                r_dcache_cc_word       = r_cc_receive_word_idx.read();
4818            }
4819            else if ( r_cc_receive_dcache_type.read() == CC_TYPE_INVAL ) // hit inval
4820            {
4821                r_dcache_fsm           = DCACHE_CC_INVAL;
4822            }
4823        }
4824        else                                  // miss
4825        {
4826            // multicast acknowledgement required in case of update
4827            if(r_cc_receive_dcache_type.read() == CC_TYPE_UPDT)
4828            {
4829                r_dcache_fsm           = DCACHE_CC_UPDT;
4830                r_dcache_cc_word       = r_cc_receive_word_idx.read();
4831
4832                // just pop the fifo , don't write in icache
4833                r_dcache_cc_need_write = false;
4834            }
4835            else // No response needed
4836            {
4837                r_cc_receive_dcache_req = false;
4838                r_dcache_fsm            = r_dcache_fsm_cc_save.read();
4839            }
4840        }
4841
4842#if DEBUG_DCACHE
4843if ( m_debug_dcache_fsm )
4844{
4845    std::cout << "  <PROC " << name()
4846              << " DCACHE_CC_CHECK> Coherence request received:"
4847              << " PADDR = " << std::hex << paddr
4848              << " / TYPE = " << std::dec << r_cc_receive_dcache_type.read()
4849              << " / HIT = " << (state == CACHE_SLOT_STATE_VALID) << std::endl;
4850}
4851#endif
4852
4853        break;
4854    }
4855    /////////////////////
4856    case DCACHE_CC_INVAL: // hit inval: switch slot to ZOMBI state and send a
4857                          // CLEANUP after possible invalidation of copies in
4858                          // TLBs
4859    {
4860        size_t way    = r_dcache_cc_way.read();
4861        size_t set    = r_dcache_cc_set.read();
4862
4863        if ( r_dcache_in_tlb[way*m_dcache_sets+set] )       // selective TLB inval
4864        {
4865            r_dcache_in_tlb[way*m_dcache_sets+set] = false;
4866            r_dcache_tlb_inval_line  = r_cc_receive_dcache_nline.read();
4867            r_dcache_tlb_inval_set   = 0;
4868            r_dcache_fsm_scan_save   = r_dcache_fsm.read();
4869            r_dcache_fsm             = DCACHE_INVAL_TLB_SCAN;
4870            break;
4871        }
4872
4873        if ( r_dcache_contains_ptd[way*m_dcache_sets+set] ) // TLB flush
4874        {
4875            r_itlb.reset();
4876            r_dtlb.reset();
4877            r_dcache_contains_ptd[way*m_dcache_sets+set] = false;
4878
4879#if DEBUG_DCACHE
4880if ( m_debug_dcache_fsm )
4881{
4882    std::cout << "  <PROC " << name()
4883              << " DCACHE_CC_INVAL> Flush DTLB & ITLB" << std::endl;
4884}
4885#endif
4886        }
4887
4888        assert (not r_dcache_cc_send_req.read() &&
4889                "ERROR in DCACHE_CC_INVAL: the r_dcache_cc_send_req "
4890                "must not be set");
4891
4892        // Switch slot state to ZOMBI and send CLEANUP command
4893        r_dcache.write_dir( way,
4894                            set,
4895                            CACHE_SLOT_STATE_ZOMBI );
4896
4897        // coherence request completed
4898        r_cc_receive_dcache_req = false;
4899        r_dcache_cc_send_req    = true;
4900        r_dcache_cc_send_nline  = r_cc_receive_dcache_nline.read();
4901        r_dcache_cc_send_way    = r_dcache_cc_way.read();
4902        r_dcache_cc_send_type   = CC_TYPE_CLEANUP;
4903        r_dcache_fsm            = r_dcache_fsm_cc_save.read();
4904
4905#if DEBUG_DCACHE
4906if ( m_debug_dcache_fsm )
4907{
4908    std::cout << "  <PROC " << name()
4909        << " DCACHE_CC_INVAL> Switch slot to EMPTY state:" << std::dec
4910        << " / WAY = " << way
4911        << " / SET = " << set << std::endl;
4912}
4913#endif
4914        break;
4915    }
4916    ///////////////////
4917    case DCACHE_CC_UPDT:        // hit update: write one word per cycle,
4918                                // after possible invalidation of copies in TLBs
4919    {
4920        size_t word       = r_dcache_cc_word.read();
4921        size_t way        = r_dcache_cc_way.read();
4922        size_t set        = r_dcache_cc_set.read();
4923
4924        if ( r_dcache_in_tlb[way*m_dcache_sets+set] )       // selective TLB inval
4925        {
4926            r_dcache_in_tlb[way*m_dcache_sets+set] = false;
4927            r_dcache_tlb_inval_line = r_cc_receive_dcache_nline.read();
4928            r_dcache_tlb_inval_set  = 0;
4929            r_dcache_fsm_scan_save  = r_dcache_fsm.read();
4930            r_dcache_fsm            = DCACHE_INVAL_TLB_SCAN;
4931
4932            break;
4933        }
4934
4935        if ( r_dcache_contains_ptd[way*m_dcache_sets+set] ) // TLB flush
4936        {
4937            r_itlb.reset();
4938            r_dtlb.reset();
4939            r_dcache_contains_ptd[way*m_dcache_sets+set] = false;
4940
4941#if DEBUG_DCACHE
4942if ( m_debug_dcache_fsm )
4943{
4944    std::cout << "  <PROC " << name()
4945              << " DCACHE_CC_UPDT> Flush DTLB & ITLB" << std::endl;
4946}
4947#endif
4948        }
4949
4950        assert (not r_dcache_cc_send_req.read() &&
4951                "ERROR in DCACHE_CC_INVAL: the r_dcache_cc_send_req "
4952                "must not be set");
4953       
4954        if ( not r_cc_receive_updt_fifo_be.rok() ) break;
4955
4956        if (r_dcache_cc_need_write.read())
4957        {
4958       
4959#ifdef INSTRUMENTATION
4960m_cpt_dcache_data_write++;
4961#endif
4962            r_dcache.write( way,
4963                            set,
4964                            word,
4965                            r_cc_receive_updt_fifo_data.read(),
4966                            r_cc_receive_updt_fifo_be.read() );
4967
4968            r_dcache_cc_word = word + 1;
4969
4970#if DEBUG_DCACHE
4971if ( m_debug_dcache_fsm )
4972{
4973    std::cout << "  <PROC " << name()
4974              << " DCACHE_CC_UPDT> Write one word" << std::dec
4975              << " / WAY = " << way
4976              << " / SET = " << set
4977              << " / WORD = " << word
4978              << " / VALUE = " << std::hex << r_cc_receive_updt_fifo_data.read() << std::endl;
4979}
4980#endif
4981        }
4982
4983        if ( r_cc_receive_updt_fifo_eop.read() )  // last word
4984        {
4985            // no need to write in the cache anymore
4986            r_dcache_cc_need_write = false;
4987
4988            // coherence request completed
4989            r_cc_receive_dcache_req = false;
4990
4991            // request multicast acknowledgement
4992            r_dcache_cc_send_req          = true;
4993            r_dcache_cc_send_nline        = r_cc_receive_dcache_nline.read();
4994            r_dcache_cc_send_updt_tab_idx = r_cc_receive_dcache_updt_tab_idx.read();
4995            r_dcache_cc_send_type         = CC_TYPE_MULTI_ACK;
4996
4997            r_dcache_fsm                  = r_dcache_fsm_cc_save.read();
4998        }
4999
5000        //consume fifo if not eop
5001        cc_receive_updt_fifo_get  = true;
5002
5003        break;
5004    }
5005    ///////////////////////////
5006    case DCACHE_INVAL_TLB_SCAN:         // Scan sequencially all sets for both ITLB & DTLB
5007                                        // It makes assumption: m_itlb_sets == m_dtlb_sets
5008                                        // All ways are handled in parallel.
5009                                        // We enter this state when a DCACHE line is modified,
5010                                        // and there is a copy in itlb or dtlb.
5011                                        // It can be caused by:
5012                                        // - a coherence inval or updt transaction,
5013                                        // - a line inval caused by a cache miss
5014                                        // - a processor XTN inval request,
5015                                        // - a WRITE hit,
5016                                        // - a Dirty bit update
5017                                        // Input arguments are:
5018                                        // - r_dcache_tlb_inval_line
5019                                        // - r_dcache_tlb_inval_set
5020                                        // - r_dcache_fsm_scan_save
5021    {
5022        paddr_t line = r_dcache_tlb_inval_line.read();
5023        size_t  set  = r_dcache_tlb_inval_set.read();
5024        size_t  way;
5025        bool    ok;
5026
5027        for ( way = 0 ; way < m_itlb_ways ; way++ )
5028        {
5029            ok = r_itlb.inval( line, way, set );
5030
5031#if DEBUG_DCACHE
5032if ( m_debug_dcache_fsm and ok )
5033{
5034    std::cout << "  <PROC " << name()
5035              << ".DCACHE_INVAL_TLB_SCAN> Invalidate ITLB entry:" << std::hex
5036              << " line = " << line << std::dec
5037              << " / set = " << set
5038              << " / way = " << way << std::endl;
5039}
5040#endif
5041        }
5042
5043        for ( way = 0 ; way < m_dtlb_ways ; way++ )
5044        {
5045            ok = r_dtlb.inval( line, way, set );
5046
5047#if DEBUG_DCACHE
5048if ( m_debug_dcache_fsm and ok )
5049std::cout << "  <PROC " << name() << " DCACHE_INVAL_TLB_SCAN>"
5050          << " Invalidate DTLB entry" << std::hex
5051          << " / line = " << line << std::dec
5052          << " / set = " << set
5053          << " / way = " << way << std::endl;
5054#endif
5055        }
5056
5057        // return to the calling state when TLB inval completed
5058        if ( r_dcache_tlb_inval_set.read() == (m_dtlb_sets-1) )
5059        {
5060            r_dcache_fsm = r_dcache_fsm_scan_save.read();
5061        }
5062        r_dcache_tlb_inval_set = r_dcache_tlb_inval_set.read() + 1;
5063        break;
5064    }
5065    } // end switch r_dcache_fsm
5066
5067    ///////////////// wbuf update ///////////////////////////////////////////////////////
5068    r_wbuf.update();
5069
5070    ///////////////// llsc update ///////////////////////////////////////////////////////
5071    if (r_dcache_llsc_valid.read()) r_dcache_llsc_count = r_dcache_llsc_count.read() - 1;
5072    if (r_dcache_llsc_count.read() == 1) r_dcache_llsc_valid = false;
5073
5074    //////////////// test processor frozen //////////////////////////////////////////////
5075    // The simulation exit if the number of consecutive frozen cycles
5076    // is larger than the m_max_frozen_cycles (constructor parameter)
5077    if ( (m_ireq.valid and not m_irsp.valid) or (m_dreq.valid and not m_drsp.valid) )
5078    {
5079        m_cpt_frz_cycles++;             // used for instrumentation
5080        m_cpt_stop_simulation++;        // used for debug
5081        if ( m_cpt_stop_simulation > m_max_frozen_cycles )
5082        {
5083            std::cout << std::dec << "ERROR in CC_VCACHE_WRAPPER " << name() << std::endl
5084                      << " stop at cycle " << m_cpt_total_cycles << std::endl
5085                      << " frozen since cycle " << m_cpt_total_cycles - m_max_frozen_cycles
5086                      << std::endl;
5087                      r_iss.dump();
5088            exit(1);
5089        }
5090    }
5091    else
5092    {
5093        m_cpt_stop_simulation = 0;
5094    }
5095
5096    /////////// execute one iss cycle /////////////////////////////////
5097    {
5098    uint32_t it = 0;
5099    for (size_t i=0; i<(size_t)iss_t::n_irq; i++) if(p_irq[i].read()) it |= (1<<i);
5100    r_iss.executeNCycles(1, m_irsp, m_drsp, it);
5101    }
5102
5103    ////////////////////////////////////////////////////////////////////////////
5104    // The VCI_CMD FSM controls the following ressources:
5105    // - r_vci_cmd_fsm
5106    // - r_vci_cmd_min
5107    // - r_vci_cmd_max
5108    // - r_vci_cmd_cpt
5109    // - r_vci_cmd_imiss_prio
5110    // - wbuf (reset)
5111    // - r_icache_miss_req (reset)
5112    // - r_icache_unc_req (reset)
5113    // - r_dcache_vci_miss_req (reset)
5114    // - r_dcache_vci_unc_req (reset)
5115    // - r_dcache_vci_ll_req (reset)
5116    // - r_dcache_vci_sc_req (reset in case of local sc fail)
5117    // - r_dcache_vci_cas_req (reset)
5118    //
5119    // This FSM handles requests from both the DCACHE FSM & the ICACHE FSM.
5120    // There are 8 request types, with the following priorities :
5121    // 1 - Data Read Miss         : r_dcache_vci_miss_req and miss in the write buffer
5122    // 2 - Data Read Uncachable   : r_dcache_vci_unc_req
5123    // 3 - Instruction Miss       : r_icache_miss_req and miss in the write buffer
5124    // 4 - Instruction Uncachable : r_icache_unc_req
5125    // 5 - Data Write             : r_wbuf.rok()
5126    // 6 - Data Linked Load       : r_dcache_vci_ll_req
5127    // 7 - Data Store Conditionnal: r_dcache_vci_sc_req
5128    // 8 - Compare And Swap       : r_dcache_vci_cas_req
5129    //
5130    // As we want to support several simultaneous VCI transactions, the VCI_CMD_FSM
5131    // and the VCI_RSP_FSM are fully desynchronized.
5132    //
5133    // VCI formats:
5134    // According to the VCI advanced specification, all read requests packets
5135    // (data Uncached, Miss data, instruction Uncached, Miss instruction)
5136    // are one word packets.
5137    // For write burst packets, all words are in the same cache line,
5138    // and addresses must be contiguous (the BE field is 0 in case of "holes").
5139    // The sc command packet implements actually a compare-and-swap mechanism
5140    // and the packet contains two flits.
5141    ////////////////////////////////////////////////////////////////////////////////////
5142
5143
5144    switch ( r_vci_cmd_fsm.read() )
5145    {
5146        //////////////
5147        case CMD_IDLE:
5148        {
5149            // DCACHE read requests (r_dcache_vci_miss_req or r_dcache_vci_ll_req), and
5150            // ICACHE read requests (r_icache_miss_req) require both a write_buffer access
5151            // to check a possible pending write on the same cache line.
5152            // As there is only one possible access per cycle to write buffer, we implement
5153            // a round-robin priority between DCACHE and ICACHE for this access,
5154            // using the r_vci_cmd_imiss_prio flip-flop.
5155
5156            size_t      wbuf_min;
5157            size_t      wbuf_max;
5158
5159            bool dcache_miss_req = r_dcache_vci_miss_req.read() and
5160                 ( not r_icache_miss_req.read() or not r_vci_cmd_imiss_prio.read() );
5161
5162            bool dcache_ll_req   = r_dcache_vci_ll_req.read() and
5163                 ( not r_icache_miss_req.read() or not r_vci_cmd_imiss_prio.read() );
5164
5165            bool dcache_sc_req   = r_dcache_vci_sc_req.read() and
5166                 ( not r_icache_miss_req.read() or not r_vci_cmd_imiss_prio.read() );
5167
5168            bool dcache_cas_req   = r_dcache_vci_cas_req.read() and
5169                 ( not r_icache_miss_req.read() or not r_vci_cmd_imiss_prio.read() );
5170
5171            bool icache_miss_req = r_icache_miss_req.read() and
5172                 ( not (r_dcache_vci_miss_req.read() or
5173                        r_dcache_vci_ll_req.read()   or
5174                        r_dcache_vci_cas_req.read()  or
5175                        r_dcache_vci_sc_req.read())
5176                     or r_vci_cmd_imiss_prio.read() );
5177
5178            // 1 - Data unc write
5179            if ( r_dcache_vci_unc_req.read() and r_dcache_vci_unc_write.read())
5180            {
5181                r_vci_cmd_fsm        = CMD_DATA_UNC_WRITE;
5182                r_dcache_vci_unc_req = false;
5183//                m_cpt_dunc_transaction++;
5184            }
5185            // 2 data read miss
5186            else if ( dcache_miss_req and r_wbuf.miss(r_dcache_vci_paddr.read()) )
5187            {
5188                r_vci_cmd_fsm         = CMD_DATA_MISS;
5189                r_dcache_vci_miss_req = false;
5190                r_vci_cmd_imiss_prio  = true;
5191//                m_cpt_dmiss_transaction++;
5192            }
5193            // 3 - Data Read Uncachable
5194            else if ( r_dcache_vci_unc_req.read() and not r_dcache_vci_unc_write.read() )
5195            {
5196                r_vci_cmd_fsm        = CMD_DATA_UNC_READ;
5197                r_dcache_vci_unc_req = false;
5198//                m_cpt_dunc_transaction++;
5199            }
5200            // 4 - Data Linked Load
5201            else if ( dcache_ll_req and r_wbuf.miss(r_dcache_vci_paddr.read()))
5202            {
5203                r_vci_cmd_fsm         = CMD_DATA_LL;
5204                r_dcache_vci_ll_req   = false;
5205                r_vci_cmd_imiss_prio  = true;
5206//              m_cpt_ll_transaction++;
5207            }
5208            // 5 - Instruction Miss
5209            else if ( icache_miss_req and r_wbuf.miss(r_icache_vci_paddr.read()) )
5210            {
5211                r_vci_cmd_fsm        = CMD_INS_MISS;
5212                r_icache_miss_req    = false;
5213                r_vci_cmd_imiss_prio = false;
5214//                m_cpt_imiss_transaction++;
5215            }
5216            // 6 - Instruction Uncachable
5217            else if ( r_icache_unc_req.read() )
5218            {
5219                r_vci_cmd_fsm       = CMD_INS_UNC;
5220                r_icache_unc_req    = false;
5221//                m_cpt_iunc_transaction++;
5222            }
5223            // 7 - Data Write
5224            else if ( r_wbuf.rok(&wbuf_min, &wbuf_max) )
5225            {
5226                r_vci_cmd_fsm       = CMD_DATA_WRITE;
5227                r_vci_cmd_cpt       = wbuf_min;
5228                r_vci_cmd_min       = wbuf_min;
5229                r_vci_cmd_max       = wbuf_max;
5230//                m_cpt_write_transaction++;
5231//                m_length_write_transaction += (wbuf_max-wbuf_min+1);
5232            }
5233            // 8 - Data Store Conditionnal
5234            else if ( dcache_sc_req and r_wbuf.miss(r_dcache_vci_paddr.read()) )
5235            {
5236                r_vci_cmd_fsm           = CMD_DATA_SC;
5237                r_dcache_vci_sc_req     = false;
5238                r_vci_cmd_imiss_prio    = true;
5239                r_vci_cmd_cpt           = 0;
5240//              m_cpt_sc_transaction++;
5241            }
5242            // 9 - Compare And Swap
5243            else if ( dcache_cas_req and r_wbuf.miss(r_dcache_vci_paddr.read()) )
5244            {
5245                r_vci_cmd_fsm           = CMD_DATA_CAS;
5246                r_dcache_vci_cas_req    = false;
5247                r_vci_cmd_imiss_prio    = true;
5248                r_vci_cmd_cpt           = 0;
5249//              m_cpt_cas_transaction++;
5250            }
5251
5252#if DEBUG_CMD
5253if ( m_debug_cmd_fsm )
5254{
5255std::cout << "  <PROC " << name() << " CMD_IDLE>"
5256          << " / dmiss_req = " << dcache_miss_req
5257          << " / imiss_req = " << icache_miss_req
5258          << std::endl;
5259}
5260#endif
5261            break;
5262        }
5263        ////////////////////
5264        case CMD_DATA_WRITE:
5265        {
5266            if ( p_vci.cmdack.read() )
5267            {
5268                r_vci_cmd_cpt = r_vci_cmd_cpt + 1;
5269                if (r_vci_cmd_cpt == r_vci_cmd_max) // last flit sent
5270                {
5271                    r_vci_cmd_fsm = CMD_IDLE ;
5272                    r_wbuf.sent() ;
5273                }
5274            }
5275            break;
5276        }
5277        /////////////////
5278        case CMD_DATA_SC:
5279        case CMD_DATA_CAS:
5280        {
5281            // The CAS and SC VCI commands contain two flits
5282            if ( p_vci.cmdack.read() )
5283            {
5284               r_vci_cmd_cpt = r_vci_cmd_cpt + 1;
5285               if (r_vci_cmd_cpt == 1) r_vci_cmd_fsm = CMD_IDLE ;
5286            }
5287            break;
5288        }
5289        //////////////////
5290        case CMD_INS_MISS:
5291        case CMD_INS_UNC:
5292        case CMD_DATA_MISS:
5293        case CMD_DATA_UNC_READ:
5294        case CMD_DATA_UNC_WRITE:
5295        case CMD_DATA_LL:
5296        {
5297            // all read VCI commands contain one single flit
5298            if ( p_vci.cmdack.read() ) {
5299                r_vci_cmd_fsm = CMD_IDLE;
5300            }
5301            break;
5302        }
5303
5304    } // end  switch r_vci_cmd_fsm
5305
5306    //////////////////////////////////////////////////////////////////////////
5307    // The VCI_RSP FSM controls the following ressources:
5308    // - r_vci_rsp_fsm:
5309    // - r_vci_rsp_fifo_icache (push)
5310    // - r_vci_rsp_fifo_dcache (push)
5311    // - r_vci_rsp_data_error (set)
5312    // - r_vci_rsp_ins_error (set)
5313    // - r_vci_rsp_cpt
5314    // - r_dcache_vci_sc_req (reset when SC response recieved)
5315    //
5316    // As the VCI_RSP and VCI_CMD are fully desynchronized to support several
5317    // simultaneous VCI transactions, this FSM uses the VCI RPKTID field
5318    // to identify the transactions.
5319    //
5320    // VCI vormat:
5321    // This component checks the response packet length and accepts only
5322    // single word packets for write response packets.
5323    //
5324    // Error handling:
5325    // This FSM analyzes the VCI error code and signals directly the Write Bus Error.
5326    // In case of Read Data Error, the VCI_RSP FSM sets the r_vci_rsp_data_error
5327    // flip_flop and the error is signaled by the DCACHE FSM.
5328    // In case of Instruction Error, the VCI_RSP FSM sets the r_vci_rsp_ins_error
5329    // flip_flop and the error is signaled by the ICACHE FSM.
5330    // In case of Cleanup Error, the simulation stops with an error message...
5331    //////////////////////////////////////////////////////////////////////////
5332
5333    switch ( r_vci_rsp_fsm.read() )
5334    {
5335    //////////////
5336    case RSP_IDLE:
5337    {
5338        if ( p_vci.rspval.read() )
5339        {
5340            r_vci_rsp_cpt = 0;
5341
5342            if      ( (p_vci.rpktid.read() & 0x7) ==  TYPE_DATA_UNC  )
5343            {
5344                r_vci_rsp_fsm = RSP_DATA_UNC;
5345            }
5346            else if ( (p_vci.rpktid.read() & 0x7) ==  TYPE_READ_DATA_MISS )
5347            {
5348                r_vci_rsp_fsm = RSP_DATA_MISS;
5349            }
5350            else if ( (p_vci.rpktid.read() & 0x7) ==  TYPE_READ_INS_UNC   )
5351            {
5352                r_vci_rsp_fsm = RSP_INS_UNC;
5353            }
5354            else if ( (p_vci.rpktid.read() & 0x7) ==  TYPE_READ_INS_MISS  )
5355            {
5356                r_vci_rsp_fsm = RSP_INS_MISS;
5357            }
5358            else if ( (p_vci.rpktid.read() & 0x7) ==  TYPE_WRITE          )
5359            {
5360                r_vci_rsp_fsm = RSP_DATA_WRITE;
5361            }
5362            else if ( (p_vci.rpktid.read() & 0x7) ==  TYPE_CAS            )
5363            {
5364                r_vci_rsp_fsm = RSP_DATA_UNC;
5365            }
5366            else if ( (p_vci.rpktid.read() & 0x7) ==  TYPE_LL             )
5367            {
5368                r_vci_rsp_fsm = RSP_DATA_LL;
5369            }
5370            else if ( (p_vci.rpktid.read() & 0x7) == TYPE_SC             )
5371            {
5372                r_vci_rsp_fsm = RSP_DATA_UNC;
5373            }
5374            else
5375            {
5376                assert(false and "Unexpected VCI response");
5377            }
5378        }
5379        break;
5380    }
5381        //////////////////
5382        case RSP_INS_MISS:
5383        {
5384            if ( p_vci.rspval.read() )
5385            {
5386                if ( (p_vci.rerror.read()&0x1) != 0 )  // error reported
5387                {
5388                    r_vci_rsp_ins_error = true;
5389                    if ( p_vci.reop.read() ) r_vci_rsp_fsm = RSP_IDLE;
5390                }
5391                else                                        // no error reported
5392                {
5393                    if ( r_vci_rsp_fifo_icache.wok() )
5394                    {
5395                        if ( r_vci_rsp_cpt.read() >= m_icache_words )
5396                        {
5397                            std::cout << "ERROR in VCI_CC_VCACHE " << name()
5398                                      << " VCI response packet too long "
5399                                      << " for instruction miss" << std::endl;
5400                            exit(0);
5401                        }
5402                        r_vci_rsp_cpt                 = r_vci_rsp_cpt.read() + 1;
5403                        vci_rsp_fifo_icache_put       = true,
5404                        vci_rsp_fifo_icache_data      = p_vci.rdata.read();
5405                        if ( p_vci.reop.read() )
5406                        {
5407                            if ( r_vci_rsp_cpt.read() != (m_icache_words - 1) )
5408                            {
5409                                std::cout << "ERROR in VCI_CC_VCACHE " << name()
5410                                          << " VCI response packet too short" 
5411                                          << " for instruction miss" << std::endl;
5412                                exit(0);
5413                            }
5414                            r_vci_rsp_fsm    = RSP_IDLE;
5415                        }
5416                    }
5417                }
5418            }
5419            break;
5420        }
5421        /////////////////
5422        case RSP_INS_UNC:
5423        {
5424            if (p_vci.rspval.read() )
5425            {
5426                assert( p_vci.reop.read() and
5427                "illegal VCI response packet for uncachable instruction");
5428
5429                if ( (p_vci.rerror.read()&0x1) != 0 )  // error reported
5430                {
5431                    r_vci_rsp_ins_error = true;
5432                    r_vci_rsp_fsm = RSP_IDLE;
5433                }
5434                else                                         // no error reported
5435                {
5436                    if ( r_vci_rsp_fifo_icache.wok())
5437                    {
5438                        vci_rsp_fifo_icache_put       = true;
5439                        vci_rsp_fifo_icache_data      = p_vci.rdata.read();
5440                        r_vci_rsp_fsm = RSP_IDLE;
5441                    }
5442                }
5443            }
5444            break;
5445        }
5446        ///////////////////
5447        case RSP_DATA_MISS:
5448        {
5449            if ( p_vci.rspval.read() )
5450            {
5451                if ( (p_vci.rerror.read()&0x1) != 0 )  // error reported
5452                {
5453                    r_vci_rsp_data_error = true;
5454                    if ( p_vci.reop.read() ) r_vci_rsp_fsm = RSP_IDLE;
5455                }
5456                else                                        // no error reported
5457                {
5458                    if ( r_vci_rsp_fifo_dcache.wok() )
5459                    {
5460                        assert( (r_vci_rsp_cpt.read() < m_dcache_words) and
5461                        "The VCI response packet for data miss is too long");
5462
5463                        r_vci_rsp_cpt                 = r_vci_rsp_cpt.read() + 1;
5464                        vci_rsp_fifo_dcache_put       = true,
5465                        vci_rsp_fifo_dcache_data      = p_vci.rdata.read();
5466                        if ( p_vci.reop.read() )
5467                        {
5468                            assert( (r_vci_rsp_cpt.read() == m_dcache_words - 1) and
5469                            "The VCI response packet for data miss is too short");
5470
5471                            r_vci_rsp_fsm     = RSP_IDLE;
5472                        }
5473                    }
5474                }
5475            }
5476            break;
5477        }
5478        //////////////////
5479        case RSP_DATA_UNC:
5480        {
5481            if (p_vci.rspval.read() )
5482            {
5483                assert( p_vci.reop.read() and
5484                "illegal VCI response packet for uncachable read data");
5485
5486                if ( (p_vci.rerror.read()&0x1) != 0 )  // error reported
5487                {
5488                    r_vci_rsp_data_error = true;
5489                    r_vci_rsp_fsm = RSP_IDLE;
5490                }
5491                else                                         // no error reported
5492                {
5493                    if ( r_vci_rsp_fifo_dcache.wok())
5494                    {
5495                        vci_rsp_fifo_dcache_put       = true;
5496                        vci_rsp_fifo_dcache_data      = p_vci.rdata.read();
5497                        r_vci_rsp_fsm = RSP_IDLE;
5498                    }
5499                }
5500            }
5501            break;
5502        }
5503        /////////////////
5504        case RSP_DATA_LL:
5505        {
5506            if ( p_vci.rspval.read() )
5507            {
5508                if ( (p_vci.rerror.read()&0x1) != 0 )  // error reported
5509                {
5510                    r_vci_rsp_data_error = true;
5511                    r_vci_rsp_fsm = RSP_IDLE;
5512                    break;
5513                }
5514                if (r_vci_rsp_cpt.read() == 0) //first flit
5515                {
5516                    if(r_vci_rsp_fifo_dcache.wok())
5517                    {
5518                        assert(!p_vci.reop.read() &&
5519                            "illegal VCI response packet for LL");
5520                        vci_rsp_fifo_dcache_put  = true;
5521                        vci_rsp_fifo_dcache_data = p_vci.rdata.read();
5522                        r_vci_rsp_cpt            = r_vci_rsp_cpt.read() + 1;
5523                    }
5524                    break;
5525                }
5526                else // last flit
5527                {
5528                    if(r_vci_rsp_fifo_dcache.wok())
5529                    {
5530                        assert(p_vci.reop.read() &&
5531                            "illegal VCI response packet for LL");
5532                        vci_rsp_fifo_dcache_put  = true;
5533                        vci_rsp_fifo_dcache_data = p_vci.rdata.read();
5534                        r_vci_rsp_fsm            = RSP_IDLE;
5535                    }
5536                    break;
5537                }
5538            }
5539            break;
5540        }
5541        ////////////////////
5542        case RSP_DATA_WRITE:
5543        {
5544            if (p_vci.rspval.read())
5545            {
5546                assert( p_vci.reop.read() and
5547                "a VCI response packet must contain one flit for a write transaction");
5548
5549                r_vci_rsp_fsm = RSP_IDLE;
5550                uint32_t   wbuf_index = p_vci.rtrdid.read();
5551                r_wbuf.completed(wbuf_index);
5552                if ( (p_vci.rerror.read()&0x1) != 0 ) r_iss.setWriteBerr();
5553            }
5554            break;
5555        }
5556    } // end switch r_vci_rsp_fsm
5557
5558    /////////////////////////////////////////////////////////////////////////////////////
5559    // The CC_SEND FSM is in charge of sending cleanups and the multicast
5560    // acknowledgements on the coherence network. It has two clients (DCACHE FSM
5561    // and ICACHE FSM) that are served with a round-robin priority.
5562    // The CC_SEND FSM resets the r_*cache_cc_send_req request flip-flops as
5563    // soon as the request has been sent.
5564    /////////////////////////////////////////////////////////////////////////////////////
5565    switch ( r_cc_send_fsm.read() )
5566    {
5567        ///////////////////////////
5568        case CC_SEND_IDLE:
5569        {
5570            ///////////////////////////////////////////////////////
5571            // handling round robin between icache and dcache :  //
5572            // we first check for the last client and listen for //
5573            // a request of the other, then update the client    //
5574            // r_cc_send_last_client : 0 dcache / 1 icache
5575            ///////////////////////////////////////////////////////
5576            bool update_last_client = r_cc_send_last_client.read();
5577            if ( r_cc_send_last_client.read() == 0 ) // last client was dcache
5578            {
5579                if (r_icache_cc_send_req.read()) // request from icache
5580                    update_last_client = 1; // update last client to icache
5581            }
5582            else // last client was icache
5583            {
5584                if (r_dcache_cc_send_req.read()) // request from dcache
5585                    update_last_client = 0; // update last client to dcache
5586            }
5587            r_cc_send_last_client = update_last_client;
5588
5589            // if there is an actual request
5590            if (r_dcache_cc_send_req.read() or r_icache_cc_send_req.read())
5591            {
5592                // the new client is dcache and has a cleanup request
5593                if      ( (update_last_client == 0) and
5594                          (r_dcache_cc_send_type.read() == CC_TYPE_CLEANUP))
5595                    r_cc_send_fsm = CC_SEND_CLEANUP_1;
5596                // the new client is dcache and has a multi acknowledgement request
5597                else if ( (update_last_client == 0) and
5598                          (r_dcache_cc_send_type.read() == CC_TYPE_MULTI_ACK))
5599                    r_cc_send_fsm = CC_SEND_MULTI_ACK;
5600                // the new client is icache and has a cleanup request
5601                else if ( (update_last_client == 1) and
5602                          (r_icache_cc_send_type.read() == CC_TYPE_CLEANUP))
5603                    r_cc_send_fsm = CC_SEND_CLEANUP_1;
5604                // the new client is icache and has a multi acknowledgement request
5605                else if ( (update_last_client == 1) and
5606                        (r_icache_cc_send_type.read() == CC_TYPE_MULTI_ACK))
5607                    r_cc_send_fsm = CC_SEND_MULTI_ACK;
5608            }
5609            break;
5610        }
5611        ///////////////////////////
5612        case CC_SEND_CLEANUP_1:
5613        {
5614            // wait for the first flit to be consumed
5615            if (p_dspin_p2m.read.read())
5616                r_cc_send_fsm = CC_SEND_CLEANUP_2;
5617
5618            break;
5619        }
5620        ///////////////////////////
5621        case CC_SEND_CLEANUP_2:
5622        {
5623            // wait for the second flit to be consumed
5624            if (p_dspin_p2m.read.read())
5625            {
5626                if (r_cc_send_last_client.read() == 0) // dcache active request
5627                    r_dcache_cc_send_req = false; // reset dcache request
5628                else // icache active request
5629                    r_icache_cc_send_req = false; // reset icache request
5630
5631                // go back to idle state
5632                r_cc_send_fsm = CC_SEND_IDLE;
5633            }
5634            break;
5635        }
5636        ///////////////////////////
5637        case CC_SEND_MULTI_ACK:
5638        {
5639            // wait for the flit to be consumed
5640            if(p_dspin_p2m.read.read())
5641            {
5642                if(r_cc_send_last_client.read() == 0) // dcache active request
5643                    r_dcache_cc_send_req = false; // reset dcache request
5644                else // icache active request
5645                    r_icache_cc_send_req = false; // reset icache request
5646                // go back to idle state
5647                r_cc_send_fsm = CC_SEND_IDLE;
5648            }
5649            break;
5650        }
5651    } // end switch CC_SEND FSM
5652
5653    ///////////////////////////////////////////////////////////////////////////////
5654    //  CC_RECEIVE  FSM
5655    // This FSM receive all coherence packets on a DSPIN40 port.
5656    // There is 5 packet types:
5657    // - CC_DATA_INVAL : DCACHE invalidate request
5658    // - CC_DATA_UPDT  : DCACHE update request (multi-words)
5659    // - CC_INST_INVAL : ICACHE invalidate request
5660    // - CC_INST_UPDT  : ICACHE update request (multi-words)
5661    // - CC_BROADCAST  : Broadcast invalidate request (both DCACHE & ICACHE)
5662    //////////////////////////////////////////////////////////////////////////////
5663    switch( r_cc_receive_fsm.read() )
5664    {
5665        /////////////////////
5666        case CC_RECEIVE_IDLE:
5667        {
5668            // a coherence request has arrived
5669            if (p_dspin_m2p.write.read())
5670            {
5671                // initialize dspin received data
5672                uint64_t receive_data = p_dspin_m2p.data.read();
5673                // initialize coherence packet type
5674                uint64_t receive_type = DspinDhccpParam::dspin_get(receive_data,
5675                                            DspinDhccpParam::M2P_TYPE);
5676                // test for a broadcast
5677                if (DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::M2P_BC))
5678                {
5679                    r_cc_receive_fsm = CC_RECEIVE_BRDCAST_HEADER;
5680                }
5681                // test for a multi updt
5682                else if (receive_type == DspinDhccpParam::TYPE_MULTI_UPDT_DATA) 
5683                {
5684                    r_cc_receive_fsm = CC_RECEIVE_DATA_UPDT_HEADER;
5685                }
5686                else if (receive_type == DspinDhccpParam::TYPE_MULTI_UPDT_INST)
5687                {
5688                    r_cc_receive_fsm = CC_RECEIVE_INS_UPDT_HEADER;
5689                }
5690                // test for a multi inval
5691                else if (receive_type == DspinDhccpParam::TYPE_MULTI_INVAL_DATA)
5692                {
5693                    r_cc_receive_fsm = CC_RECEIVE_DATA_INVAL_HEADER;
5694                }
5695                else
5696                {
5697                    r_cc_receive_fsm = CC_RECEIVE_INS_INVAL_HEADER;
5698                }
5699            }
5700            break;
5701        }
5702        ///////////////////////////////
5703        case CC_RECEIVE_BRDCAST_HEADER:
5704        {
5705            // no actual data in the HEADER, just skip to second flit
5706            r_cc_receive_fsm = CC_RECEIVE_BRDCAST_NLINE;
5707            break;
5708        }
5709        //////////////////////////////
5710        case CC_RECEIVE_BRDCAST_NLINE:
5711        {
5712            // initialize dspin received data
5713            uint64_t receive_data = p_dspin_m2p.data.read();
5714            // wait for both dcache and icache to take the request
5715            // TODO maybe we need to wait for both only to leave the state, but
5716            // not to actually post a request to an available cache => need a
5717            // flip_flop to check that ?
5718            if (not (r_cc_receive_icache_req.read()) and
5719                not (r_cc_receive_dcache_req.read()) and
5720                (p_dspin_m2p.write.read()))
5721            {
5722                // request dcache to handle the BROADCAST
5723                r_cc_receive_dcache_req = true;
5724                r_cc_receive_dcache_nline  = DspinDhccpParam::dspin_get(receive_data,
5725                                             DspinDhccpParam::BROADCAST_NLINE);
5726                r_cc_receive_dcache_type = CC_TYPE_INVAL;
5727                // request icache to handle the BROADCAST
5728                r_cc_receive_icache_req = true;
5729                r_cc_receive_icache_nline  = DspinDhccpParam::dspin_get(receive_data,
5730                                             DspinDhccpParam::BROADCAST_NLINE);
5731                r_cc_receive_icache_type = CC_TYPE_INVAL;
5732                // get back to idle state
5733                r_cc_receive_fsm = CC_RECEIVE_IDLE;
5734                break;
5735            }
5736            // keep waiting for the caches to accept the request
5737            break;
5738        }
5739        /////////////////////////////
5740        case CC_RECEIVE_DATA_INVAL_HEADER:
5741        {
5742            // sample updt tab index in the HEADER, then skip to second flit
5743            r_cc_receive_fsm = CC_RECEIVE_DATA_INVAL_NLINE;
5744            break;
5745        }
5746        /////////////////////////////
5747        case CC_RECEIVE_INS_INVAL_HEADER:
5748        {
5749            // sample updt tab index in the HEADER, then skip to second flit
5750            r_cc_receive_fsm = CC_RECEIVE_INS_INVAL_NLINE;
5751            break;
5752        }
5753        ////////////////////////////
5754        case CC_RECEIVE_DATA_INVAL_NLINE:
5755        {
5756            // sample nline in the second flit
5757            uint64_t receive_data = p_dspin_m2p.data.read();
5758            // for data INVAL, wait for dcache to take the request
5759            if (p_dspin_m2p.write.read()           and
5760                not r_cc_receive_dcache_req.read() )
5761            {
5762                // request dcache to handle the INVAL
5763                r_cc_receive_dcache_req = true;
5764                r_cc_receive_dcache_nline = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_INVAL_NLINE);
5765                r_cc_receive_dcache_type = CC_TYPE_INVAL;
5766                // get back to idle state
5767                r_cc_receive_fsm = CC_RECEIVE_IDLE;
5768                break;
5769            }
5770            break;
5771        }
5772        //////////////////////////////
5773        case CC_RECEIVE_INS_INVAL_NLINE:
5774        {
5775            // sample nline in the second flit
5776            uint64_t receive_data = p_dspin_m2p.data.read();
5777            // for ins INVAL, wait for icache to take the request
5778            if (p_dspin_m2p.write.read()           and
5779                not r_cc_receive_icache_req.read() )
5780            {
5781                // request icache to handle the INVAL
5782                r_cc_receive_icache_req = true;
5783                r_cc_receive_icache_nline = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_INVAL_NLINE);
5784                r_cc_receive_icache_type = CC_TYPE_INVAL;
5785                // get back to idle state
5786                r_cc_receive_fsm = CC_RECEIVE_IDLE;
5787                break;
5788            }
5789            break;
5790        }
5791        ////////////////////////////
5792        case CC_RECEIVE_DATA_UPDT_HEADER:
5793        {
5794            // sample updt tab index in the HEADER, than skip to second flit
5795            uint64_t receive_data = p_dspin_m2p.data.read();
5796            // for data INVAL, wait for dcache to take the request and fifo to
5797            // be empty
5798            if (not r_cc_receive_dcache_req.read())
5799            {
5800                r_cc_receive_dcache_updt_tab_idx  = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_UPDT_INDEX);
5801                r_cc_receive_fsm = CC_RECEIVE_DATA_UPDT_NLINE;
5802                break;
5803            }
5804            break;
5805        }
5806        ////////////////////////////
5807        case CC_RECEIVE_INS_UPDT_HEADER:
5808        {
5809            // sample updt tab index in the HEADER, than skip to second flit
5810            uint64_t receive_data = p_dspin_m2p.data.read();
5811            // for ins INVAL, wait for icache to take the request and fifo to be
5812            // empty
5813            if (not r_cc_receive_icache_req.read())
5814            {
5815                r_cc_receive_icache_updt_tab_idx  = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_UPDT_INDEX);
5816                r_cc_receive_fsm = CC_RECEIVE_INS_UPDT_NLINE;
5817                break;
5818            }
5819            // keep waiting for the correct cache to accept the request
5820            break;
5821        }
5822        ///////////////////////////
5823        case CC_RECEIVE_DATA_UPDT_NLINE:
5824        {
5825            // sample nline and word index in the second flit
5826            uint64_t receive_data = p_dspin_m2p.data.read();
5827            // for data INVAL, wait for dcache to take the request and fifo to
5828            // be empty
5829            if ( r_cc_receive_updt_fifo_be.empty() and
5830                 p_dspin_m2p.write.read() )
5831            {
5832                r_cc_receive_dcache_req = true;
5833                r_cc_receive_dcache_nline  = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_NLINE);
5834                r_cc_receive_word_idx  = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_WORD_INDEX);
5835                r_cc_receive_dcache_type = CC_TYPE_UPDT;
5836                // get back to idle state
5837                r_cc_receive_fsm = CC_RECEIVE_DATA_UPDT_DATA;
5838                break;
5839            }
5840            break;
5841        }
5842        ////////////////////////////
5843        case CC_RECEIVE_INS_UPDT_NLINE:
5844        {
5845            // sample nline and word index in the second flit
5846            uint64_t receive_data = p_dspin_m2p.data.read();
5847            // for ins INVAL, wait for icache to take the request and fifo to be
5848            // empty
5849            if ( r_cc_receive_updt_fifo_be.empty() and
5850                 p_dspin_m2p.write.read() )
5851            {
5852                r_cc_receive_icache_req = true;
5853                r_cc_receive_icache_nline  = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_NLINE);
5854                r_cc_receive_word_idx  = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_WORD_INDEX);
5855                r_cc_receive_icache_type = CC_TYPE_UPDT;
5856                // get back to idle state
5857                r_cc_receive_fsm = CC_RECEIVE_INS_UPDT_DATA;
5858                break;
5859            }
5860            break;
5861        }
5862        //////////////////////////
5863        case CC_RECEIVE_DATA_UPDT_DATA:
5864        {
5865            // wait for the fifo
5866            if (r_cc_receive_updt_fifo_be.wok() and (p_dspin_m2p.write.read()))
5867            {
5868                uint64_t receive_data = p_dspin_m2p.data.read();
5869                bool     receive_eop  = p_dspin_m2p.eop.read();
5870                cc_receive_updt_fifo_be   = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_BE);
5871                cc_receive_updt_fifo_data = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_DATA);
5872                cc_receive_updt_fifo_eop  = receive_eop;
5873                cc_receive_updt_fifo_put  = true;
5874                if ( receive_eop ) r_cc_receive_fsm = CC_RECEIVE_IDLE;
5875            }
5876            break;
5877        }
5878        //////////////////////////
5879        case CC_RECEIVE_INS_UPDT_DATA:
5880        {
5881            // wait for the fifo
5882            if (r_cc_receive_updt_fifo_be.wok() and (p_dspin_m2p.write.read()))
5883            {
5884                uint64_t receive_data = p_dspin_m2p.data.read();
5885                bool     receive_eop  = p_dspin_m2p.eop.read();
5886                cc_receive_updt_fifo_be   = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_BE);
5887                cc_receive_updt_fifo_data = DspinDhccpParam::dspin_get(receive_data,DspinDhccpParam::MULTI_UPDT_DATA);
5888                cc_receive_updt_fifo_eop  = receive_eop;
5889                cc_receive_updt_fifo_put  = true;
5890                if ( receive_eop ) r_cc_receive_fsm = CC_RECEIVE_IDLE;
5891            }
5892            break;
5893        }
5894
5895    } // end switch CC_RECEIVE FSM
5896
5897    ///////////////// DSPIN CLACK interface ///////////////
5898   
5899    uint64_t clack_type = DspinDhccpParam::dspin_get(r_dspin_clack_flit.read(),
5900                                                     DspinDhccpParam::CLACK_TYPE);
5901
5902    size_t clack_way  = DspinDhccpParam::dspin_get(r_dspin_clack_flit.read(),
5903                                                   DspinDhccpParam::CLACK_WAY);
5904
5905    size_t clack_set  = DspinDhccpParam::dspin_get(r_dspin_clack_flit.read(),
5906                                                   DspinDhccpParam::CLACK_SET);
5907
5908    bool dspin_clack_get      = false;
5909    bool dcache_clack_request = (clack_type == DspinDhccpParam::TYPE_CLACK_DATA);
5910    bool icache_clack_request = (clack_type == DspinDhccpParam::TYPE_CLACK_INST);
5911
5912    if (r_dspin_clack_req.read())
5913    {
5914        // CLACK DATA: Send request to DCACHE FSM
5915        if (dcache_clack_request and not r_dcache_clack_req.read()){
5916            r_dcache_clack_req = true;
5917            r_dcache_clack_way = clack_way & ((1ULL<<(uint32_log2(m_dcache_ways)))-1);
5918            r_dcache_clack_set = clack_set & ((1ULL<<(uint32_log2(m_dcache_sets)))-1);
5919            dspin_clack_get    = true;
5920        }
5921
5922        // CLACK INST: Send request to ICACHE FSM
5923        else if (icache_clack_request and not r_icache_clack_req.read()){
5924            r_icache_clack_req = true;
5925            r_icache_clack_way = clack_way & ((1ULL<<(uint32_log2(m_dcache_ways)))-1);
5926            r_icache_clack_set = clack_set & ((1ULL<<(uint32_log2(m_icache_sets)))-1);
5927            dspin_clack_get    = true;
5928        }
5929    }
5930    else
5931    {
5932        dspin_clack_get = true;
5933    }
5934
5935    if (dspin_clack_get)
5936    {
5937        r_dspin_clack_req  = p_dspin_clack.write.read();
5938        r_dspin_clack_flit = p_dspin_clack.data.read();
5939    }
5940
5941    ///////////////// Response FIFOs update  //////////////////////
5942    r_vci_rsp_fifo_icache.update(vci_rsp_fifo_icache_get,
5943                                 vci_rsp_fifo_icache_put,
5944                                 vci_rsp_fifo_icache_data);
5945
5946    r_vci_rsp_fifo_dcache.update(vci_rsp_fifo_dcache_get,
5947                                 vci_rsp_fifo_dcache_put,
5948                                 vci_rsp_fifo_dcache_data);
5949
5950    ///////////////// updt FIFO update  //////////////////////
5951    //TODO check this
5952    r_cc_receive_updt_fifo_be.update(cc_receive_updt_fifo_get,
5953                                 cc_receive_updt_fifo_put,
5954                                 cc_receive_updt_fifo_be);
5955    r_cc_receive_updt_fifo_data.update(cc_receive_updt_fifo_get,
5956                                 cc_receive_updt_fifo_put,
5957                                 cc_receive_updt_fifo_data);
5958    r_cc_receive_updt_fifo_eop.update(cc_receive_updt_fifo_get,
5959                                 cc_receive_updt_fifo_put,
5960                                 cc_receive_updt_fifo_eop);
5961
5962} // end transition()
5963
5964///////////////////////
5965tmpl(void)::genMoore()
5966///////////////////////
5967{
5968
5969    // VCI initiator command on the direct network
5970    // it depends on the CMD FSM state
5971
5972    bool is_sc_or_cas  = (r_vci_cmd_fsm.read() == CMD_DATA_CAS) or
5973                         (r_vci_cmd_fsm.read() == CMD_DATA_SC );
5974
5975    p_vci.pktid  = 0;
5976    p_vci.srcid  = m_srcid;
5977    p_vci.cons   = is_sc_or_cas; 
5978    p_vci.contig = not is_sc_or_cas; 
5979    p_vci.wrap   = false;
5980    p_vci.clen   = 0;
5981    p_vci.cfixed = false;
5982
5983    if ( m_monitor_ok ) {
5984        if ( p_vci.cmdack.read() == true and p_vci.cmdval == true) {
5985            if (((p_vci.address.read()) >= m_monitor_base) and
5986                ((p_vci.address.read()) < m_monitor_base + m_monitor_length) ) {
5987                std::cout << "CC_VCACHE Monitor " << name() << std::hex
5988                          << " Access type = " << vci_cmd_type_str[p_vci.cmd.read()] 
5989                          << " Pktid type = " << vci_pktid_type_str[p_vci.pktid.read()]
5990                          << " : address = " << p_vci.address.read()
5991                          << " / be = " << p_vci.be.read(); 
5992                if ( p_vci.cmd.read() == vci_param::CMD_WRITE ) {
5993                    std::cout << " / data = " << p_vci.wdata.read();
5994                }
5995                std::cout << std::dec << std::endl;
5996            }
5997        }
5998    }
5999
6000    switch ( r_vci_cmd_fsm.read() ) {
6001
6002    case CMD_IDLE:
6003        p_vci.cmdval  = false;
6004        p_vci.address = 0;
6005        p_vci.wdata   = 0;
6006        p_vci.be      = 0;
6007        p_vci.trdid   = 0;
6008        p_vci.pktid   = 0;
6009        p_vci.plen    = 0;
6010        p_vci.cmd     = vci_param::CMD_NOP;
6011        p_vci.eop     = false;
6012        break;
6013
6014    case CMD_INS_MISS:
6015        p_vci.cmdval  = true;
6016        p_vci.address = r_icache_vci_paddr.read() & m_icache_yzmask;
6017        p_vci.wdata   = 0;
6018        p_vci.be      = 0xF;
6019        p_vci.trdid   = 0;
6020        p_vci.pktid   = TYPE_READ_INS_MISS;
6021        p_vci.plen    = m_icache_words<<2;
6022        p_vci.cmd     = vci_param::CMD_READ;
6023        p_vci.eop     = true;
6024        break;
6025
6026    case CMD_INS_UNC:
6027        p_vci.cmdval  = true;
6028        p_vci.address = r_icache_vci_paddr.read() & ~0x3;
6029        p_vci.wdata   = 0;
6030        p_vci.be      = 0xF;
6031        p_vci.trdid   = 0;
6032        p_vci.pktid   = TYPE_READ_INS_UNC;
6033        p_vci.plen    = 4;
6034        p_vci.cmd     = vci_param::CMD_READ;
6035        p_vci.eop     = true;
6036        break;
6037
6038    case CMD_DATA_MISS:
6039        p_vci.cmdval  = true;
6040        p_vci.address = r_dcache_vci_paddr.read() & m_dcache_yzmask;
6041        p_vci.wdata   = 0;
6042        p_vci.be      = 0xF;
6043        p_vci.trdid   = 0;
6044        p_vci.pktid   = TYPE_READ_DATA_MISS;
6045        p_vci.plen    = m_dcache_words << 2;
6046        p_vci.cmd     = vci_param::CMD_READ;
6047        p_vci.eop     = true;
6048        break;
6049
6050    case CMD_DATA_UNC_READ:
6051        p_vci.cmdval  = true;
6052        p_vci.address = r_dcache_vci_paddr.read() & ~0x3;
6053        p_vci.wdata   = 0;
6054        p_vci.be      = r_dcache_vci_unc_be.read();
6055        p_vci.trdid   = 0;
6056        p_vci.pktid   = TYPE_DATA_UNC;
6057        p_vci.plen    = 4;
6058        p_vci.cmd     = vci_param::CMD_READ;
6059        p_vci.eop     = true;
6060        break;
6061
6062    case CMD_DATA_UNC_WRITE:
6063        p_vci.cmdval  = true;
6064        p_vci.address = r_dcache_vci_paddr.read() & ~0x3;
6065        p_vci.wdata   = r_dcache_vci_wdata.read();
6066        p_vci.be      = r_dcache_vci_unc_be.read();
6067        p_vci.trdid   = 0;
6068        p_vci.pktid   = TYPE_DATA_UNC;
6069        p_vci.plen    = 4;
6070        p_vci.cmd     = vci_param::CMD_WRITE;
6071        p_vci.eop     = true;
6072        break;
6073
6074    case CMD_DATA_WRITE:
6075        p_vci.cmdval  = true;
6076        p_vci.address = r_wbuf.getAddress(r_vci_cmd_cpt.read()) & ~0x3;
6077        p_vci.wdata   = r_wbuf.getData(r_vci_cmd_cpt.read());
6078        p_vci.be      = r_wbuf.getBe(r_vci_cmd_cpt.read());
6079        p_vci.trdid   = r_wbuf.getIndex();
6080        p_vci.pktid   = TYPE_WRITE;
6081        p_vci.plen    = (r_vci_cmd_max.read() - r_vci_cmd_min.read() + 1) << 2;
6082        p_vci.cmd     = vci_param::CMD_WRITE;
6083        p_vci.eop     = (r_vci_cmd_cpt.read() == r_vci_cmd_max.read());
6084        break;
6085
6086    case CMD_DATA_LL:
6087        p_vci.cmdval  = true;
6088        p_vci.address = r_dcache_vci_paddr.read() & ~0x3;
6089        p_vci.wdata   = 0;
6090        p_vci.be      = 0xF;
6091        p_vci.trdid   = 0;
6092        p_vci.pktid   = TYPE_LL;
6093        p_vci.plen    = 8;
6094        p_vci.cmd     = vci_param::CMD_LOCKED_READ;
6095        p_vci.eop     = true;
6096        break;
6097
6098    case CMD_DATA_SC:
6099        p_vci.cmdval  = true;
6100        p_vci.address = r_dcache_vci_paddr.read() & ~0x3;
6101        if ( r_vci_cmd_cpt.read() == 0 ) p_vci.wdata = r_dcache_llsc_key.read();
6102        else                             p_vci.wdata = r_dcache_vci_sc_data.read();
6103        p_vci.be      = 0xF;
6104        p_vci.trdid   = 0;
6105        p_vci.pktid   = TYPE_SC;
6106        p_vci.plen    = 8;
6107        p_vci.cmd     = vci_param::CMD_NOP;
6108        p_vci.eop     = (r_vci_cmd_cpt.read() == 1);
6109        break;
6110
6111    case CMD_DATA_CAS:
6112        p_vci.cmdval  = true;
6113        p_vci.address = r_dcache_vci_paddr.read() & ~0x3;
6114        if ( r_vci_cmd_cpt.read() == 0 ) p_vci.wdata = r_dcache_vci_cas_old.read();
6115        else                             p_vci.wdata = r_dcache_vci_cas_new.read();
6116        p_vci.be      = 0xF;
6117        p_vci.trdid   = 0;
6118        p_vci.pktid   = TYPE_CAS;
6119        p_vci.plen    = 8;
6120        p_vci.cmd     = vci_param::CMD_NOP;
6121        p_vci.eop     = (r_vci_cmd_cpt.read() == 1);
6122        break;
6123    } // end switch r_vci_cmd_fsm
6124
6125    // VCI initiator response on the direct network
6126    // it depends on the VCI_RSP FSM
6127
6128    switch (r_vci_rsp_fsm.read() )
6129    {
6130        case RSP_DATA_WRITE : p_vci.rspack = true; break;
6131        case RSP_INS_MISS   : p_vci.rspack = r_vci_rsp_fifo_icache.wok(); break;
6132        case RSP_INS_UNC    : p_vci.rspack = r_vci_rsp_fifo_icache.wok(); break;
6133        case RSP_DATA_MISS  : p_vci.rspack = r_vci_rsp_fifo_dcache.wok(); break;
6134        case RSP_DATA_UNC   : p_vci.rspack = r_vci_rsp_fifo_dcache.wok(); break;
6135        case RSP_DATA_LL    : p_vci.rspack = r_vci_rsp_fifo_dcache.wok(); break;
6136        case RSP_IDLE       : p_vci.rspack = false; break;
6137    } // end switch r_vci_rsp_fsm
6138
6139   
6140    // Send coherence packets on DSPIN P2M
6141    // it depends on the CC_SEND FSM
6142
6143    uint64_t dspin_send_data = 0;
6144    switch ( r_cc_send_fsm.read() )
6145    {
6146        //////////////////
6147        case CC_SEND_IDLE:
6148        {
6149            p_dspin_p2m.write = false;
6150            break;
6151        }
6152        ///////////////////////
6153        case CC_SEND_CLEANUP_1:
6154        {
6155            // initialize dspin send data
6156//            DspinDhccpParam::dspin_set(dspin_send_data,
6157//                                       0,
6158//                                       DspinDhccpParam::P2M_EOP);
6159            DspinDhccpParam::dspin_set(dspin_send_data,
6160                                       m_cc_global_id,
6161                                       DspinDhccpParam::CLEANUP_SRCID);
6162            DspinDhccpParam::dspin_set(dspin_send_data,
6163                                       0,
6164                                       DspinDhccpParam::P2M_BC);
6165
6166            if(r_cc_send_last_client.read() == 0) // dcache active request
6167            {
6168                uint64_t dest = (uint64_t) r_dcache_cc_send_nline.read() 
6169                                >> (m_nline_width - m_x_width - m_y_width) 
6170                                << (DspinDhccpParam::GLOBALID_WIDTH - m_x_width - m_y_width);
6171 
6172                DspinDhccpParam::dspin_set(dspin_send_data,
6173                                           dest,
6174                                           DspinDhccpParam::CLEANUP_DEST);
6175
6176                DspinDhccpParam::dspin_set(dspin_send_data,
6177                                           (r_dcache_cc_send_nline.read() & 0x300000000ULL)>>32,
6178                                           DspinDhccpParam::CLEANUP_NLINE_MSB);
6179
6180                DspinDhccpParam::dspin_set(dspin_send_data,
6181                                           r_dcache_cc_send_way.read(),
6182                                           DspinDhccpParam::CLEANUP_WAY_INDEX);
6183
6184                DspinDhccpParam::dspin_set(dspin_send_data,
6185                                           DspinDhccpParam::TYPE_CLEANUP_DATA,
6186                                           DspinDhccpParam::P2M_TYPE);
6187            }
6188            else                                // icache active request
6189            {
6190                uint64_t dest = (uint64_t) r_icache_cc_send_nline.read() 
6191                                >> (m_nline_width - m_x_width - m_y_width) 
6192                                << (DspinDhccpParam::GLOBALID_WIDTH - m_x_width - m_y_width);
6193
6194                DspinDhccpParam::dspin_set(dspin_send_data,
6195                                           dest,
6196                                           DspinDhccpParam::CLEANUP_DEST);
6197
6198                DspinDhccpParam::dspin_set(dspin_send_data,
6199                                           (r_icache_cc_send_nline.read() & 0x300000000ULL)>>32,
6200                                           DspinDhccpParam::CLEANUP_NLINE_MSB);
6201
6202                DspinDhccpParam::dspin_set(dspin_send_data,
6203                                           r_icache_cc_send_way.read(),
6204                                           DspinDhccpParam::CLEANUP_WAY_INDEX);
6205
6206                DspinDhccpParam::dspin_set(dspin_send_data,
6207                                           DspinDhccpParam::TYPE_CLEANUP_INST,
6208                                           DspinDhccpParam::P2M_TYPE);
6209            }
6210            // send flit
6211            p_dspin_p2m.data  = dspin_send_data;
6212            p_dspin_p2m.write = true;
6213            p_dspin_p2m.eop   = false;
6214            break;
6215        }
6216        ///////////////////////
6217        case CC_SEND_CLEANUP_2:
6218        {
6219            // initialize dspin send data
6220//            DspinDhccpParam::dspin_set(dspin_send_data,
6221//                                       1,
6222//                                       DspinDhccpParam::P2M_EOP);
6223
6224            if(r_cc_send_last_client.read() == 0) // dcache active request
6225            {
6226                DspinDhccpParam::dspin_set(dspin_send_data,
6227                                           r_dcache_cc_send_nline.read() & 0xFFFFFFFFULL,
6228                                           DspinDhccpParam::CLEANUP_NLINE_LSB);
6229            }
6230            else                                  // icache active request
6231            {
6232                DspinDhccpParam::dspin_set(dspin_send_data,
6233                                           r_icache_cc_send_nline.read() & 0xFFFFFFFFULL,
6234                                           DspinDhccpParam::CLEANUP_NLINE_LSB);
6235            }
6236            // send flit
6237            p_dspin_p2m.data  = dspin_send_data;
6238            p_dspin_p2m.write = true;
6239            p_dspin_p2m.eop   = true;
6240            break;
6241        }
6242        ///////////////////////
6243        case CC_SEND_MULTI_ACK:
6244        {
6245            // initialize dspin send data
6246//            DspinDhccpParam::dspin_set(dspin_send_data,
6247//                                       1,
6248//                                       DspinDhccpParam::P2M_EOP);
6249            DspinDhccpParam::dspin_set(dspin_send_data,
6250                                       0,
6251                                       DspinDhccpParam::P2M_BC);
6252            DspinDhccpParam::dspin_set(dspin_send_data,
6253                                       DspinDhccpParam::TYPE_MULTI_ACK,
6254                                       DspinDhccpParam::P2M_TYPE);
6255
6256            if(r_cc_send_last_client.read() == 0) // dcache active request
6257            {
6258                uint64_t dest = (uint64_t) r_dcache_cc_send_nline.read() 
6259                                >> (m_nline_width - m_x_width - m_y_width) 
6260                                << (DspinDhccpParam::GLOBALID_WIDTH - m_x_width - m_y_width);
6261 
6262                DspinDhccpParam::dspin_set(dspin_send_data,
6263                                           dest,
6264                                           DspinDhccpParam::MULTI_ACK_DEST);
6265
6266                DspinDhccpParam::dspin_set(dspin_send_data,
6267                                           r_dcache_cc_send_updt_tab_idx.read(),
6268                                           DspinDhccpParam::MULTI_ACK_UPDT_INDEX);
6269            }
6270            else                                    // icache active request
6271            {
6272                uint64_t dest = (uint64_t) r_icache_cc_send_nline.read() 
6273                                >> (m_nline_width - m_x_width - m_y_width) 
6274                                << (DspinDhccpParam::GLOBALID_WIDTH - m_x_width - m_y_width);
6275 
6276
6277                DspinDhccpParam::dspin_set(dspin_send_data,
6278                                           dest,
6279                                           DspinDhccpParam::MULTI_ACK_DEST);
6280
6281                DspinDhccpParam::dspin_set(dspin_send_data,
6282                                           r_icache_cc_send_updt_tab_idx.read(),
6283                                           DspinDhccpParam::MULTI_ACK_UPDT_INDEX);
6284            }
6285            // send flit
6286            p_dspin_p2m.data  = dspin_send_data;
6287            p_dspin_p2m.write = true;
6288            p_dspin_p2m.eop   = true;
6289
6290            break;
6291        }
6292    } // end switch CC_SEND FSM
6293
6294    // Receive coherence packets
6295    // It depends on the CC_RECEIVE FSM
6296    switch( r_cc_receive_fsm.read() )
6297    {
6298        /////////////////////
6299        case CC_RECEIVE_IDLE:
6300        {
6301            p_dspin_m2p.read = false;
6302            break;
6303        }
6304        ///////////////////////////////
6305        case CC_RECEIVE_BRDCAST_HEADER:
6306        {
6307            p_dspin_m2p.read = true;
6308            break;
6309        }
6310        //////////////////////////////
6311        case CC_RECEIVE_BRDCAST_NLINE:
6312        {
6313            // TODO maybe we need to wait for both only to leave the state, but
6314            // not to actually post a request to an available cache => need a
6315            // flip_flop to check that ?
6316            if (not (r_cc_receive_icache_req.read()) and not (r_cc_receive_dcache_req.read()))
6317                p_dspin_m2p.read = true;
6318            else
6319                p_dspin_m2p.read = false;
6320            break;
6321        }
6322        /////////////////////////////
6323        case CC_RECEIVE_DATA_INVAL_HEADER:
6324        case CC_RECEIVE_INS_INVAL_HEADER:
6325        {
6326            p_dspin_m2p.read = true;
6327            break;
6328        }
6329        ////////////////////////////
6330        case CC_RECEIVE_DATA_INVAL_NLINE:
6331        {
6332            p_dspin_m2p.read = not r_cc_receive_dcache_req.read();
6333            break;
6334        }
6335        case CC_RECEIVE_INS_INVAL_NLINE:
6336        {
6337            p_dspin_m2p.read = not r_cc_receive_icache_req.read();
6338            break;
6339        }
6340        ///////////////////////////
6341        case CC_RECEIVE_DATA_UPDT_HEADER:
6342        {
6343            if (not r_cc_receive_dcache_req.read())
6344                p_dspin_m2p.read = true;
6345            else
6346                p_dspin_m2p.read = false;
6347            break;
6348        }
6349        ////////////////////////////
6350        case CC_RECEIVE_INS_UPDT_HEADER:
6351        {
6352            if ( not r_cc_receive_icache_req.read()) 
6353                p_dspin_m2p.read = true;
6354            else
6355                p_dspin_m2p.read = false;
6356            break;
6357        }
6358        ///////////////////////////
6359        case CC_RECEIVE_DATA_UPDT_NLINE:
6360        case CC_RECEIVE_INS_UPDT_NLINE:
6361        {
6362            if(r_cc_receive_updt_fifo_be.empty())
6363                p_dspin_m2p.read = true;
6364            else
6365                p_dspin_m2p.read = false;
6366            break;
6367        }
6368        ///////////////////////////
6369        case CC_RECEIVE_DATA_UPDT_DATA:
6370        case CC_RECEIVE_INS_UPDT_DATA:
6371        {
6372            if (r_cc_receive_updt_fifo_be.wok())
6373                p_dspin_m2p.read = true;
6374            else
6375                p_dspin_m2p.read = false;
6376            break;
6377        }
6378    } // end switch CC_RECEIVE FSM
6379
6380
6381    int clack_type = DspinDhccpParam::dspin_get(r_dspin_clack_flit.read(),
6382                                                DspinDhccpParam::CLACK_TYPE);
6383
6384    bool dspin_clack_get      = false;
6385    bool dcache_clack_request = (clack_type == DspinDhccpParam::TYPE_CLACK_DATA);
6386    bool icache_clack_request = (clack_type == DspinDhccpParam::TYPE_CLACK_INST);
6387
6388    if (r_dspin_clack_req.read())
6389    {
6390        // CLACK DATA: wait if pending request to DCACHE FSM
6391        if (dcache_clack_request and not r_dcache_clack_req.read())
6392        {
6393            dspin_clack_get = true;
6394        }
6395
6396        // CLACK INST: wait if pending request to ICACHE FSM
6397        else if (icache_clack_request and not r_icache_clack_req.read())
6398        {
6399            dspin_clack_get = true;
6400        }
6401    }
6402    else
6403    {
6404        dspin_clack_get = true;
6405    }
6406
6407    p_dspin_clack.read = dspin_clack_get; 
6408} // end genMoore
6409
6410tmpl(void)::start_monitor(paddr_t base, paddr_t length)
6411// This version of monitor print both Read and Write request
6412{
6413    m_monitor_ok        = true;
6414    m_monitor_base      = base;
6415    m_monitor_length    = length;
6416}
6417
6418tmpl(void)::stop_monitor()
6419{
6420    m_monitor_ok        = false;
6421}
6422
6423}}
6424
6425// Local Variables:
6426// tab-width: 4
6427// c-basic-offset: 4
6428// c-file-offsets:((innamespace . 0)(inline-open . 0))
6429// indent-tabs-mode: nil
6430// End:
6431
6432// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=4:softtabstop=4
Note: See TracBrowser for help on using the repository browser.