SystemC中的信息和差错报告机制
在 Syst 仿真中,常常需要打印和报告仿真进度信息,发生错误时打印错误原因并停止仿真。 Syst 中定义了5个默认宏用于打印和报告事件信息以及事件的严重程度,它们是:
- SC_REPORT_INFO( msg_type , msg )
- SC_REPORT_WARNING( msg_type , msg )
- SC_REPORT_ERROR( msg_type , msg )
- SC_REPORT_FATAL( msg_type , msg )
- sc_assert( expr )
msg_type和msg都是常量字符串,msg_type通常是一个识别ID,而msg则是用户自己定义的打印信息。其中,sc_assert的严重程度FATAL。这几个函数可以用在 SystemC 模块的任何地方。
在 SystemC 中,信息所对应事件的严重程度分为5级,如下面的代码所定义:
- enum sc_severity {
- SC_INFO = 0 ,
- SC_WARNING ,
- SC_ERROR ,
- SC_FATAL ,
- SC_MAX_SEVERITY
- };
对于不同严重程度的信息所对应的事件,仿真器采取的行动不同,共分为9个级别,如下面的代码所定义:
- enum {
- SC_UNSPECIFIED = 0x0000 ,
- SC_DO_NOTHING = 0x0001 ,
- SC_THROW = 0x0002 ,
- SC_LOG = 0x0004 ,
- SC_DISPLAY = 0x0008 ,
- SC_CACHE_REPORT = 0x0010 ,
- SC_INTERRUPT = 0x0020 ,
- SC_STOP = 0x0040 ,
- SC_ABORT = 0x0080
- };
不同严重程度的信息所对应的缺省行动为:
- #define SC_DEFAULT_INFO_ACTIONS ( SC_LOG | SC_DISPLAY )
- #define SC_DEFAULT_WARNING_ACTIONS ( SC_LOG | SC_DISPLAY )
- #define SC_DEFAULT_ERROR_ACTIONS \
- ( SC_LOG | SC_CACHE_REPORT | SC_THROW )
- #define SC_DEFAULT_FATAL_ACTIONS \
- ( SC_LOG | SC_DISPLAY | SC_CACHE_REPORT | SC_ABORT )
我们可以看到,对于信息和警告,缺省的情况只是打印出来,对于出错(ERROR),SystemC仿真器抛出异常,让用户定义的异常处理代码去处理。对于严重错误(FATAL),则直接放弃,并停止仿真。
sc_report_handler和sc_report是SystemC信息和差错报告机制中最重要的两个类。SC_REPO RT_INFO、SC_REPORT_WARNING、SC_REPORT_ERROR、SC_REPORT_FATAL和sc_assert( expr )的定义依赖于sc_report_handler,它们的定义如下:
- #define SC_REPORT_INFO( msg_type , msg ) \
- sc_report_handler::report( SC_INFO , msg_type
- , msg , __FILE__ , __LINE__ )
- #define SC_REPORT_WARNING( msg_type , msg ) \
- sc_report_handler::report( SC_WARNING , msg_type
- , msg , __FILE__ , __LINE__ )
- #define SC_REPORT_ERROR( msg_type , msg ) \
- sc_report_handler::report( SC_ERROR , msg_type
- , msg , __FILE__ , __LINE__ )
- #define SC_REPORT_FATAL( msg_type , msg ) \
- sc_report_handler::report( SC_FATAL , msg_type
- , msg , __FILE__ , __LINE__ )
- #define sc_assert( expr ) \
- ( ( void ) ( ( expr ) ? 0 : ( SC_REPORT_FATAL(
- implementation-defined , #expr ) , 0 ) ) )
在项目最初的调试阶段,总是希望打印出更多的信息,而在后来的调试阶段,总是希望只打印一些关键信息从而达到加快仿真速度和快速定位错误的目的,因此,常常需要修改信息报告函数的行为。SC_REPORT_INFO、SC_REPORT_WARNING、SC_REPORT_ERROR、SC_REPORT _FATAL和sc_assert( expr )的缺省行为可以通过set_actions进行修改,该函数的定义如下:
- class sc_report_handler
- {
- publ:
- static sc_actions set_actions( sc_severity ,
- sc_actions = SC_UNSPECIFIED );
- static sc_actions set_actions( const char * msg_type
- , sc_actions = SC_UNSPECIFIED );
- static sc_actions set_actions( const char * msg_type ,
- sc_severity , sc_actions =SC_UNSPECIFIED );
- ……
- }
如下面的代码:
- sc_report_handler::set_actions(SC_WARNING, SC_DO_NOTHING);
- sc_report_handler::set_actions("/Acme_IP", SC_DISPLAY);
- sc_report_handler::set_actions("/Acme_IP",SC_INFO,
- SC_DISPLAY| SC_CACHE_REPORT );
- SC_REPORT_WARNING("", "1"); // 不输出任何信息
- SC_REPORT_WARNING("wb_sram", "2"); // 打印“2”到标准输出
- SC_REPORT_INFO("/sram8x256", "3"); //打印“3”到标准输出并且缓存。
- 下文给出一个使用SC_REPORT_INFO的例子。该例子的全部代码见前面章节中的stimulus_gen:
- #include "systemc.h"
- SC_MODULE(stimulus_gen)
- {
- ……
- void main()
- {
- ……
- SC_REPORT_INFO(“stimulus_gen_main”,” stimulus_gen started”);
- while(true) {
- ……
- }
- }
- SC_CTOR(stimulus_gen)
- {
- SC_THREAD(main);
- sensitive<<clk_i.pos();
- };
- };