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

Last change on this file since 473 was 473, checked in by alain, 11 years ago

cosmetic

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