cached.cpp revision 12855:588919e0e4aa
1/*****************************************************************************
2
3  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
4  more contributor license agreements.  See the NOTICE file distributed
5  with this work for additional information regarding copyright ownership.
6  Accellera licenses this file to you under the Apache License, Version 2.0
7  (the "License"); you may not use this file except in compliance with the
8  License.  You may obtain a copy of the License at
9
10    http://www.apache.org/licenses/LICENSE-2.0
11
12  Unless required by applicable law or agreed to in writing, software
13  distributed under the License is distributed on an "AS IS" BASIS,
14  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
15  implied.  See the License for the specific language governing
16  permissions and limitations under the License.
17
18 *****************************************************************************/
19
20/*****************************************************************************
21
22  sc_report -- caching of report in process context
23
24  Original Author: Ulli Holtmann, Synopsys, Inc., Jan 2003
25
26 *****************************************************************************/
27
28/*****************************************************************************
29
30  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
31  changes you are making here.
32
33      Name, Affiliation, Date:
34  Description of Modification:
35
36 *****************************************************************************/
37
38#include <systemc.h>
39
40
41/*
42
43  id1 has actions: display, cache
44  id2 has actions: display
45
46 Issue report in this order in the following contextes:
47
48   retrieve report from thread1   -> no report
49   retrieve report from method1   -> no report
50   retrieve report from global    -> no report
51
52     info/id1/a -> thread1
53  warning/id1/b -> method1
54     info/id1/c -> global
55  warning/id1/d -> thread2
56     info/id1/e -> method2
57
58     info/id2/f -> thread1
59  warning/id2/g -> method1
60     info/id2/h -> global
61  warning/id2/i -> thread2
62     info/id2/j -> method2
63
64   retrieve report from thread1   ->    info/id1/a
65   retrieve report from method1   -> warning/id1/b
66   retrieve report from global    ->    info/id1/c
67   retrieve report from thread2   -> no report
68   retrieve report from method2   -> no report
69*/
70
71
72static const char* severity2str[] = {
73    "INFO", "WARNING", "ERROR", "FATAL", "UNKNOWN_SEVERITY"
74};
75
76void dump_cached_report(const char* ctx)
77{
78    sc_report* report = sc_report_handler::get_cached_report();
79    cout << sc_time_stamp()
80	 << " from context '" << ctx << "' ";
81    if (report) {
82	cout << report->get_msg_type()
83	     << " " << severity2str[ report->get_severity() ] << endl
84	     << " msg="      << report->get_msg()
85	     << " file="     << report->get_file_name()
86	     << " line "     << report->get_line_number()
87	     << " time="     << report->get_time();
88	const char* name = report->get_process_name();
89	cout << " process=" << (name ? name : "<none>") << endl;
90    } else {
91	cout << "<no cached report>\n";
92    }
93    sc_report_handler::clear_cached_report();
94}
95
96SC_MODULE( M )
97{
98    sc_in<bool> emit;  // 1: emit, 0: dump cahced report
99    sc_in<const char*> id;
100    sc_in<bool> ofs;
101    sc_event t1, t2, m1, m2;
102
103    SC_CTOR( M ) {
104	SC_THREAD( thread1 );
105	sensitive << t1;
106	dont_initialize();
107
108	SC_THREAD( thread2 );
109	sensitive << t2;
110	dont_initialize();
111
112	SC_METHOD( method1 );
113	sensitive << m1;
114	dont_initialize();
115
116	SC_METHOD( method2 );
117	sensitive << m2;
118	dont_initialize();
119    }
120    void thread1() {
121	while(1) {
122	    if (emit)
123		sc_report_handler::report(SC_INFO, id.read(), "aa"+ofs, "file_t1", 110+ofs);
124	    else
125		dump_cached_report("t1");
126	    wait();
127	}
128    }
129    void method1() {
130	if (emit)
131	    sc_report_handler::report(SC_WARNING, id.read(), "bb"+ofs, "file_m1", 210+ofs);
132	else
133	    dump_cached_report("m1");
134    }
135    void thread2() {
136	while(1) {
137	    if (emit)
138		sc_report_handler::report(SC_WARNING, id.read(), "dd"+ofs, "file_t2", 120+ofs);
139	    else
140		dump_cached_report("t2");
141	    wait();
142	}
143    }
144    void method2() {
145	if (emit)
146	    sc_report_handler::report(SC_INFO, id.read(), "ee"+ofs, "file_m2", 220+ofs);
147	else
148	    dump_cached_report("m2");
149    }
150};
151
152
153int sc_main(int,char**)
154{
155    sc_report_handler::set_actions( "ID1", SC_DISPLAY | SC_CACHE_REPORT );
156    sc_report_handler::set_actions( "ID2", SC_DISPLAY );
157
158    sc_signal<bool> emit;
159    sc_signal<const char*> ID;
160    sc_signal<bool> ofs;
161    M uut("M");
162    uut( emit,ID,ofs );
163
164    emit = 0;
165    ID="ID3";
166    ofs=0;
167    sc_start( 1,SC_NS );
168
169    // dump initial cached reports
170    cout << "Initial status:\n";
171    uut.t1.notify();
172    uut.m1.notify();
173    uut.t2.notify();
174    uut.m2.notify();
175    dump_cached_report("global");
176    sc_start( 1, SC_NS );
177
178    // emit report ID1 everywhere
179    emit = 1;
180    ID="ID1";
181    sc_start( 1,SC_NS );
182    cout << "\n\nEmit ID1\n";
183    uut.t1.notify();
184    uut.m1.notify();
185    sc_start( 1, SC_NS );
186    sc_report_handler::report(SC_INFO, ID.read(), "cc", "file_g", 300);
187    uut.t2.notify();
188    uut.m2.notify();
189    sc_start( 1, SC_NS );
190
191    // emit report ID2 everywhere
192    cout << "\n\nEmit ID2\n";
193    emit = 1;
194    ID="ID2";
195    ofs=1;
196    sc_start( 1,SC_NS );
197    uut.t1.notify();
198    uut.m1.notify();
199    sc_start( 1, SC_NS );
200    sc_report_handler::report(SC_INFO, ID.read(), "c", "file_g", 310);
201    uut.t2.notify();
202    uut.m2.notify();
203    sc_start( 1, SC_NS );
204
205    // dump cached reports: should be all ID1
206    emit = 0;
207    sc_start( 1,SC_NS );
208    cout << "\n\nStatus:\n";
209    uut.t1.notify();
210    uut.t2.notify();
211    uut.m1.notify();
212    uut.m2.notify();
213    dump_cached_report("global");
214    sc_start( 1, SC_NS );
215
216    // dump cached reports again
217    // (should be all empty because dump clears cached report)
218    cout << "\n\nStatus:\n";
219    uut.t1.notify();
220    uut.t2.notify();
221    uut.m1.notify();
222    uut.m2.notify();
223    dump_cached_report("global");
224    sc_start( 1, SC_NS );
225
226    return 0;
227}
228