112855Sgabeblack@google.com/*****************************************************************************
212855Sgabeblack@google.com
312855Sgabeblack@google.com  Licensed to Accellera Systems Initiative Inc. (Accellera) under one or
412855Sgabeblack@google.com  more contributor license agreements.  See the NOTICE file distributed
512855Sgabeblack@google.com  with this work for additional information regarding copyright ownership.
612855Sgabeblack@google.com  Accellera licenses this file to you under the Apache License, Version 2.0
712855Sgabeblack@google.com  (the "License"); you may not use this file except in compliance with the
812855Sgabeblack@google.com  License.  You may obtain a copy of the License at
912855Sgabeblack@google.com
1012855Sgabeblack@google.com    http://www.apache.org/licenses/LICENSE-2.0
1112855Sgabeblack@google.com
1212855Sgabeblack@google.com  Unless required by applicable law or agreed to in writing, software
1312855Sgabeblack@google.com  distributed under the License is distributed on an "AS IS" BASIS,
1412855Sgabeblack@google.com  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
1512855Sgabeblack@google.com  implied.  See the License for the specific language governing
1612855Sgabeblack@google.com  permissions and limitations under the License.
1712855Sgabeblack@google.com
1812855Sgabeblack@google.com *****************************************************************************/
1912855Sgabeblack@google.com
2012855Sgabeblack@google.com/*****************************************************************************
2112855Sgabeblack@google.com
2212855Sgabeblack@google.com  sc_report -- test error reporting, sepcifically which actions are selected
2312855Sgabeblack@google.com
2412855Sgabeblack@google.com  An important part of the sc_report functionality is to translate
2512855Sgabeblack@google.com  an reported tupel (id,severity) into a set of actions. This test
2612855Sgabeblack@google.com  is dedicated to this functionality. It uses user-defined actions and
2712855Sgabeblack@google.com  user-defined handler.
2812855Sgabeblack@google.com
2912855Sgabeblack@google.com  Original Author: Ulli Holtmann, Synopsys, Inc., Jan 2003
3012855Sgabeblack@google.com
3112855Sgabeblack@google.com *****************************************************************************/
3212855Sgabeblack@google.com
3312855Sgabeblack@google.com/*****************************************************************************
3412855Sgabeblack@google.com
3512855Sgabeblack@google.com  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
3612855Sgabeblack@google.com  changes you are making here.
3712855Sgabeblack@google.com
3812855Sgabeblack@google.com      Name, Affiliation, Date:
3912855Sgabeblack@google.com  Description of Modification:
4012855Sgabeblack@google.com
4112855Sgabeblack@google.com *****************************************************************************/
4212855Sgabeblack@google.com
4312855Sgabeblack@google.com#include <systemc.h>
4412855Sgabeblack@google.com
4512855Sgabeblack@google.com
4612855Sgabeblack@google.com/*
4712855Sgabeblack@google.com  Allocate user-defined action:
4812855Sgabeblack@google.com
4912855Sgabeblack@google.com  Allocate as many user-defined actions as possible.
5012855Sgabeblack@google.com  Note: We need to have at least usr1..usr6 action which are used in
5112855Sgabeblack@google.com  subsequent tests.
5212855Sgabeblack@google.com
5312855Sgabeblack@google.com*/
5412855Sgabeblack@google.com
5512855Sgabeblack@google.comconst unsigned num_usr_actions = 6;
5612855Sgabeblack@google.comsc_actions usr_actions[num_usr_actions];
5712855Sgabeblack@google.com
5812855Sgabeblack@google.comsc_actions& usr1 = usr_actions[0];
5912855Sgabeblack@google.comsc_actions& usr2 = usr_actions[1];
6012855Sgabeblack@google.comsc_actions& usr3 = usr_actions[2];
6112855Sgabeblack@google.comsc_actions& usr4 = usr_actions[3];
6212855Sgabeblack@google.comsc_actions& usr5 = usr_actions[4];
6312855Sgabeblack@google.comsc_actions& usr6 = usr_actions[5];
6412855Sgabeblack@google.com
6512855Sgabeblack@google.comconst char* id1 = "ID1";
6612855Sgabeblack@google.comconst char* id2 = "ID2";
6712855Sgabeblack@google.comconst char* id3 = "ID3";
6812855Sgabeblack@google.comconst char* id4 = "ID4";
6912855Sgabeblack@google.com
7012855Sgabeblack@google.com
7112855Sgabeblack@google.comvoid allocate_user_actions( )
7212855Sgabeblack@google.com{
7312855Sgabeblack@google.com    sc_actions usr;
7412855Sgabeblack@google.com    for (unsigned int n=0; n<1000; n++) {
7512855Sgabeblack@google.com	usr = sc_report_handler::get_new_action_id();
7612855Sgabeblack@google.com	if ( usr == SC_UNSPECIFIED ) {
7712855Sgabeblack@google.com	    cout << "We got " << n << " user-defined actions\n";
7812855Sgabeblack@google.com	    break;
7912855Sgabeblack@google.com	}
8012855Sgabeblack@google.com	// make sure we don't get the same usr action again and that it
8112855Sgabeblack@google.com	// is really new
8212855Sgabeblack@google.com	sc_assert (usr!=SC_UNSPECIFIED && usr!=SC_DO_NOTHING && usr!=SC_THROW &&
8312855Sgabeblack@google.com		usr!=SC_LOG         && usr!=SC_DISPLAY    && usr!=SC_CACHE_REPORT &&
8412855Sgabeblack@google.com		usr!=SC_STOP        && usr!=SC_ABORT );
8512855Sgabeblack@google.com	if ( n < num_usr_actions ) {
8612855Sgabeblack@google.com	    // save for later use
8712855Sgabeblack@google.com	    usr_actions[n] = usr;
8812855Sgabeblack@google.com	    for (unsigned int i=0; i<n; i++) {
8912855Sgabeblack@google.com		// lso check that is is new
9012855Sgabeblack@google.com		sc_assert( usr!=usr_actions[i]);
9112855Sgabeblack@google.com	    }
9212855Sgabeblack@google.com	}
9312855Sgabeblack@google.com    }
9412855Sgabeblack@google.com}
9512855Sgabeblack@google.com
9612855Sgabeblack@google.com
9712855Sgabeblack@google.comstatic const char* severity2str[] = {
9812855Sgabeblack@google.com    "INFO", "WARNING", "ERROR", "FATAL", "UNKNOWN_SEVERITY"
9912855Sgabeblack@google.com};
10012855Sgabeblack@google.com
10112855Sgabeblack@google.com// custom handler which is used to dump out reports and actions
10212855Sgabeblack@google.comvoid dump_all_handler( const sc_report& report, const sc_actions& actions)
10312855Sgabeblack@google.com{
10412855Sgabeblack@google.com    // dump out report
10512855Sgabeblack@google.com    cout << "report: " << report.get_msg_type()
10612855Sgabeblack@google.com	 << " " << severity2str[ report.get_severity() ];
10712855Sgabeblack@google.com    cout << " --> ";
10812855Sgabeblack@google.com    for (int n=0; n<32; n++) {
10912855Sgabeblack@google.com	sc_actions action = actions & 1<<n;
11012855Sgabeblack@google.com	if (action) {
11112855Sgabeblack@google.com	    cout << " ";
11212855Sgabeblack@google.com	    switch(action) {
11312855Sgabeblack@google.com	    case SC_UNSPECIFIED:  cout << "unspecified"; break;
11412855Sgabeblack@google.com	    case SC_DO_NOTHING:   cout << "do-nothing"; break;
11512855Sgabeblack@google.com	    case SC_THROW:        cout << "throw"; break;
11612855Sgabeblack@google.com	    case SC_LOG:          cout << "log"; break;
11712855Sgabeblack@google.com	    case SC_DISPLAY:      cout << "display"; break;
11812855Sgabeblack@google.com	    case SC_CACHE_REPORT: cout << "cache-report"; break;
11912855Sgabeblack@google.com	    case SC_INTERRUPT:    cout << "interrupt"; break;
12012855Sgabeblack@google.com	    case SC_STOP:         cout << "stop"; break;
12112855Sgabeblack@google.com	    case SC_ABORT:        cout << "abort"; break;
12212855Sgabeblack@google.com	    default:
12312855Sgabeblack@google.com		bool found=false;
12412855Sgabeblack@google.com		for (unsigned int u=0; u<num_usr_actions; u++)
12512855Sgabeblack@google.com		    if (action == usr_actions[u]) {
12612855Sgabeblack@google.com			cout << "usr" << u+1;
12712855Sgabeblack@google.com			found=true;
12812855Sgabeblack@google.com			break;
12912855Sgabeblack@google.com		    }
13012855Sgabeblack@google.com		if (!found)
13112855Sgabeblack@google.com		    cout << "UNKNOWN";
13212855Sgabeblack@google.com	    }
13312855Sgabeblack@google.com	}
13412855Sgabeblack@google.com    }
13512855Sgabeblack@google.com    cout << endl;
13612855Sgabeblack@google.com    cout << " msg="      << report.get_msg()
13712855Sgabeblack@google.com	 << " file="     << report.get_file_name()
13812855Sgabeblack@google.com	 << " line "     << report.get_line_number()
13912855Sgabeblack@google.com	 << " time="     << report.get_time();
14012855Sgabeblack@google.com	 const char* name = report.get_process_name();
14112855Sgabeblack@google.com    cout << " process=" << (name ? name : "<none>") << endl;
14212855Sgabeblack@google.com}
14312855Sgabeblack@google.com
14412855Sgabeblack@google.com
14512855Sgabeblack@google.com/*
14612855Sgabeblack@google.com  Test selection schema  id x severity :
14712855Sgabeblack@google.com
14812855Sgabeblack@google.com            ID
14912855Sgabeblack@google.com  Severity   1       2       3
15012855Sgabeblack@google.com
15112855Sgabeblack@google.com  info      usr2   usr1     usr5*
15212855Sgabeblack@google.com  warning   usr3   usr1     usr3
15312855Sgabeblack@google.com  error     usr4   usr1     usr1*
15412855Sgabeblack@google.com  fatal     usr5   usr1     usr5
15512855Sgabeblack@google.com
15612855Sgabeblack@google.com  usr1..usr5 are user-defined actions
15712855Sgabeblack@google.com
15812855Sgabeblack@google.com  ID 1 selects by severity rule which has lowest priority,
15912855Sgabeblack@google.com  ID 2 selects by ID rule,
16012855Sgabeblack@google.com  ID 3 selects by  individual severity x ID rules (highest priority)
16112855Sgabeblack@google.com
16212855Sgabeblack@google.com*/
16312855Sgabeblack@google.comvoid set_rules()
16412855Sgabeblack@google.com{
16512855Sgabeblack@google.com    // set rule 1: by severity
16612855Sgabeblack@google.com    sc_report_handler::set_actions( SC_INFO,    usr2 );
16712855Sgabeblack@google.com    sc_report_handler::set_actions( SC_WARNING, usr3 );
16812855Sgabeblack@google.com    sc_report_handler::set_actions( SC_ERROR,   usr4 );
16912855Sgabeblack@google.com    sc_report_handler::set_actions( SC_FATAL,   usr5 );
17012855Sgabeblack@google.com
17112855Sgabeblack@google.com    // set rule 2: by id
17212855Sgabeblack@google.com    sc_report_handler::set_actions( id2, usr1 );
17312855Sgabeblack@google.com
17412855Sgabeblack@google.com    // set rule 3: by (id,severity)
17512855Sgabeblack@google.com    sc_report_handler::set_actions ( id3, SC_INFO,  usr5 );
17612855Sgabeblack@google.com    sc_report_handler::set_actions ( id3, SC_ERROR, usr1 );
17712855Sgabeblack@google.com}
17812855Sgabeblack@google.comvoid query_rules( const char* id )
17912855Sgabeblack@google.com{
18012855Sgabeblack@google.com    sc_report_handler::report( SC_INFO,    id, "extra_msg_for_info",   "no_specific_file", 0);
18112855Sgabeblack@google.com    sc_report_handler::report( SC_WARNING, id, "extra_msg_for_warning","no_specific_file", 1);
18212855Sgabeblack@google.com    sc_report_handler::report( SC_ERROR,   id, "extra_msg_for_error",  "no_specific_file", 2);
18312855Sgabeblack@google.com    sc_report_handler::report( SC_FATAL,   id, "extra_msg_for_fatal",  "no_specific_file", 3);
18412855Sgabeblack@google.com    cout << endl;
18512855Sgabeblack@google.com}
18612855Sgabeblack@google.comvoid query_rules()
18712855Sgabeblack@google.com{
18812855Sgabeblack@google.com    query_rules( id1 );
18912855Sgabeblack@google.com    query_rules( id2 );
19012855Sgabeblack@google.com    query_rules( id3 );
19112855Sgabeblack@google.com}
19212855Sgabeblack@google.com
19312855Sgabeblack@google.com
19412855Sgabeblack@google.comint sc_main(int,char**)
19512855Sgabeblack@google.com{
19612855Sgabeblack@google.com    allocate_user_actions();
19712855Sgabeblack@google.com    sc_report_handler::set_handler( &dump_all_handler );
19812855Sgabeblack@google.com
19912855Sgabeblack@google.com    // disable automatic stop
20012855Sgabeblack@google.com    sc_report_handler::stop_after( SC_ERROR, 0 );
20112855Sgabeblack@google.com    sc_report_handler::stop_after( SC_FATAL, 0 );
20212855Sgabeblack@google.com
20312855Sgabeblack@google.com    // Don't emit error|fatal for ID4 because this would
20412855Sgabeblack@google.com    // terminate the simulation.
20512855Sgabeblack@google.com      cout << "Default settings for ID4\n";
20612855Sgabeblack@google.com      query_rules( id4 );
20712855Sgabeblack@google.com
20812855Sgabeblack@google.com    // check default setting of rules
20912855Sgabeblack@google.com    cout << "Specific settings for ID1..ID3\n";
21012855Sgabeblack@google.com    set_rules();
21112855Sgabeblack@google.com    query_rules();
21212855Sgabeblack@google.com
21312855Sgabeblack@google.com    // temporarily suppress usr4:
21412855Sgabeblack@google.com    // - check which actions are emitted
21512855Sgabeblack@google.com    // - check that suppress() restores old state
21612855Sgabeblack@google.com    // - check return value of suppress(.)
21712855Sgabeblack@google.com    cout << "temporarily suppress usr4\n";
21812855Sgabeblack@google.com    sc_start( 1,SC_NS );
21912855Sgabeblack@google.com    sc_report_handler::suppress( usr3 );
22012855Sgabeblack@google.com    sc_assert( sc_report_handler::suppress( usr4 ) == usr3 );
22112855Sgabeblack@google.com    query_rules( id1 );
22212855Sgabeblack@google.com    sc_assert( sc_report_handler::suppress() == usr4 );
22312855Sgabeblack@google.com    query_rules( id1 );
22412855Sgabeblack@google.com
22512855Sgabeblack@google.com    // temporarily force usr1: same checking as with suppress
22612855Sgabeblack@google.com    cout << "temporarily force usr1\n";
22712855Sgabeblack@google.com    sc_start( 1,SC_NS );
22812855Sgabeblack@google.com    sc_report_handler::force( usr2 );
22912855Sgabeblack@google.com    sc_assert( sc_report_handler::force( usr1 ) == usr2 );
23012855Sgabeblack@google.com    query_rules( id1 );
23112855Sgabeblack@google.com    sc_assert( sc_report_handler::force() == usr1 );
23212855Sgabeblack@google.com    query_rules( id1 );
23312855Sgabeblack@google.com
23412855Sgabeblack@google.com    // temporarily force usr1: same checking as with suppress
23512855Sgabeblack@google.com    cout << "temporarily suppress {usr3,usr4} and force {usr1,usr3}\n";
23612855Sgabeblack@google.com    sc_start( 1,SC_NS );
23712855Sgabeblack@google.com    sc_report_handler::force   ( usr1|usr3 );
23812855Sgabeblack@google.com    sc_report_handler::suppress( usr3|usr4 );
23912855Sgabeblack@google.com    query_rules( id1 );
24012855Sgabeblack@google.com    sc_report_handler::force();
24112855Sgabeblack@google.com    sc_report_handler::suppress();
24212855Sgabeblack@google.com    query_rules( id1 );
24312855Sgabeblack@google.com
24412855Sgabeblack@google.com    return 0;
24512855Sgabeblack@google.com}
246