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

Last change on this file since 1064 was 1063, checked in by alain, 4 years ago

Improve the print_trace() function to display CP2 registers.

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