sc_vcd_trace.cpp

来自「基于4个mips核的noc设计」· C++ 代码 · 共 2,112 行 · 第 1/4 页

CPP
2,112
字号
    fprintf(fp, "$version\n %s\n$end\n\n", sc_version());    //timescale:    if     (timescale_unit == 1e-15) sprintf(buf, "1 fs");    else if(timescale_unit == 1e-14) sprintf(buf, "10 fs");    else if(timescale_unit == 1e-13) sprintf(buf, "100 fs");    else if(timescale_unit == 1e-12) sprintf(buf, "1 ps");    else if(timescale_unit == 1e-11) sprintf(buf, "10 ps");    else if(timescale_unit == 1e-10) sprintf(buf, "100 ps");    else if(timescale_unit == 1e-9)  sprintf(buf, "1 ns");    else if(timescale_unit == 1e-8)  sprintf(buf, "10 ns");    else if(timescale_unit == 1e-7)  sprintf(buf, "100 ns");    else if(timescale_unit == 1e-6)  sprintf(buf, "1 us");    else if(timescale_unit == 1e-5)  sprintf(buf, "10 us");    else if(timescale_unit == 1e-4)  sprintf(buf, "100 us");    else if(timescale_unit == 1e-3)  sprintf(buf, "1 ms");    else if(timescale_unit == 1e-2)  sprintf(buf, "10 ms");    else if(timescale_unit == 1e-1)  sprintf(buf, "100 ms");    else if(timescale_unit == 1e0)   sprintf(buf, "1 s");    else if(timescale_unit == 1e1)   sprintf(buf, "10 s");    else if(timescale_unit == 1e2)   sprintf(buf, "100 s");    fprintf(fp,"$timescale\n     %s\n$end\n\n",buf);    running_regression = ( getenv( "SYSTEMC_REGRESSION" ) != NULL );    // Don't print message if running regression    if( ! timescale_set_by_user && ! running_regression ) {	cout << "WARNING: Default time step is used for VCD tracing." << endl;    }    // Create a dummy scope    fputs("$scope module SystemC $end\n", fp);    //variable definitions:    int i;    for (i = 0; i < traces.size(); i++) {        vcd_trace* t = traces[i];        t->set_width(); // needed for all vectors        t->print_variable_declaration_line(fp);    }    fputs("$upscope $end\n", fp);    fputs("$enddefinitions  $end\n\n", fp);    // double inittime = sc_simulation_time();    double inittime = sc_time_stamp().to_seconds();        sprintf(buf,            "All initial values are dumped below at time "            "%g sec = %g timescale units.",            inittime, inittime/timescale_unit            );    write_comment(buf);    double_to_special_int64(inittime/timescale_unit,                            &previous_time_units_high,                            &previous_time_units_low );    fputs("$dumpvars\n",fp);    for (i = 0; i < traces.size(); i++) {        vcd_trace* t = traces[i];        t->write(fp);        fputc('\n', fp);    }    fputs("$end\n\n", fp);}void vcd_trace_file::sc_set_vcd_time_unit(int exponent10_seconds){    if(initialized){        vcd_put_error_message("VCD trace timescale unit cannot be changed once tracing has begun.\n"                              "To change the scale, create a new trace file.",                              false);        return;    }     if(exponent10_seconds < -15 || exponent10_seconds >  2){        vcd_put_error_message("set_vcd_time_unit() has valid exponent range -15...+2.", false);        return;    }    if     (exponent10_seconds == -15) timescale_unit = 1e-15;    else if(exponent10_seconds == -14) timescale_unit = 1e-14;    else if(exponent10_seconds == -13) timescale_unit = 1e-13;    else if(exponent10_seconds == -12) timescale_unit = 1e-12;    else if(exponent10_seconds == -11) timescale_unit = 1e-11;    else if(exponent10_seconds == -10) timescale_unit = 1e-10;    else if(exponent10_seconds ==  -9) timescale_unit = 1e-9;    else if(exponent10_seconds ==  -8) timescale_unit = 1e-8;    else if(exponent10_seconds ==  -7) timescale_unit = 1e-7;    else if(exponent10_seconds ==  -6) timescale_unit = 1e-6;    else if(exponent10_seconds ==  -5) timescale_unit = 1e-5;    else if(exponent10_seconds ==  -4) timescale_unit = 1e-4;    else if(exponent10_seconds ==  -3) timescale_unit = 1e-3;    else if(exponent10_seconds ==  -2) timescale_unit = 1e-2;    else if(exponent10_seconds ==  -1) timescale_unit = 1e-1;    else if(exponent10_seconds ==   0) timescale_unit = 1e0;    else if(exponent10_seconds ==   1) timescale_unit = 1e1;    else if(exponent10_seconds ==   2) timescale_unit = 1e2;    char buf[200];    sprintf(buf,	    "Note: VCD trace timescale unit is set by user to 1e%d sec.\n",	    exponent10_seconds);    cout << buf << flush;    timescale_set_by_user = true;}// ----------------------------------------------------------------------------#define DEFN_TRACE_METHOD(tp)                                                 \void                                                                          \vcd_trace_file::trace( const tp& object_, const sc_string& name_ )            \{                                                                             \    if( initialized ) {                                                       \        vcd_put_error_message(                                                \	    "No traces can be added once simulation has started.\n"           \            "To add traces, create a new vcd trace file.", false );           \    }                                                                         \    sc_string temp_vcd_name;                                                  \    create_vcd_name( &temp_vcd_name );                                        \    traces.push_back( new vcd_ ## tp ## _trace( object_,                      \						name_,                        \						temp_vcd_name ) );            \}DEFN_TRACE_METHOD(bool)DEFN_TRACE_METHOD(sc_bit)DEFN_TRACE_METHOD(sc_logic)DEFN_TRACE_METHOD(float)DEFN_TRACE_METHOD(double)DEFN_TRACE_METHOD(sc_signed)DEFN_TRACE_METHOD(sc_unsigned)DEFN_TRACE_METHOD(sc_int_base)DEFN_TRACE_METHOD(sc_uint_base)DEFN_TRACE_METHOD(sc_fxval)DEFN_TRACE_METHOD(sc_fxval_fast)DEFN_TRACE_METHOD(sc_fxnum)DEFN_TRACE_METHOD(sc_fxnum_fast)#undef DEFN_TRACE_METHOD#define DEFN_TRACE_METHOD_SIGNED(tp)                                          \void                                                                          \vcd_trace_file::trace( const tp&        object_,                              \                       const sc_string& name_,                                \                       int              width_ )                              \{                                                                             \    if( initialized ) {                                                       \        vcd_put_error_message(                                                \	    "No traces can be added once simulation has started.\n"           \            "To add traces, create a new vcd trace file.", false );           \    }                                                                         \    sc_string temp_vcd_name;                                                  \    create_vcd_name( &temp_vcd_name );                                        \    traces.push_back( new vcd_signed_ ## tp ## _trace( object_,               \					               name_,                 \						       temp_vcd_name,         \                                                       width_ ) );            \}#define DEFN_TRACE_METHOD_UNSIGNED(tp)                                        \void                                                                          \vcd_trace_file::trace( const unsigned tp& object_,                            \                       const sc_string&   name_,                              \                       int                width_ )                            \{                                                                             \    if( initialized ) {                                                       \        vcd_put_error_message(                                                \	    "No traces can be added once simulation has started.\n"           \            "To add traces, create a new vcd trace file.", false );           \    }                                                                         \    sc_string temp_vcd_name;                                                  \    create_vcd_name( &temp_vcd_name );                                        \    traces.push_back( new vcd_unsigned_ ## tp ## _trace( object_,             \				                         name_,               \						         temp_vcd_name,       \                                                         width_ ) );          \}DEFN_TRACE_METHOD_SIGNED(char)DEFN_TRACE_METHOD_SIGNED(short)DEFN_TRACE_METHOD_SIGNED(int)DEFN_TRACE_METHOD_SIGNED(long)DEFN_TRACE_METHOD_UNSIGNED(char)DEFN_TRACE_METHOD_UNSIGNED(short)DEFN_TRACE_METHOD_UNSIGNED(int)DEFN_TRACE_METHOD_UNSIGNED(long)#undef DEFN_TRACE_METHOD_SIGNED#undef DEFN_TRACE_METHOD_UNSIGNEDvoidvcd_trace_file::trace( const unsigned& object_,		       const sc_string& name_,		       const char** enum_literals_ ){    if( initialized ) {        vcd_put_error_message(	    "No traces can be added once simulation has started.\n"	    "To add traces, create a new vcd trace file.", false );    }    sc_string temp_vcd_name;    create_vcd_name( &temp_vcd_name );    traces.push_back( new vcd_enum_trace( object_,					  name_,					  temp_vcd_name,					  enum_literals_ ) );}voidvcd_trace_file::write_comment(const sc_string& comment){    //no newline in comments allowed, as some viewers may crash    fputs("$comment\n", fp);    fputs((const char *) comment, fp);    fputs("\n$end\n\n", fp);}voidvcd_trace_file::delta_cycles(bool flag){    trace_delta_cycles = flag;}voidvcd_trace_file::cycle(bool this_is_a_delta_cycle){    char message[4000];    unsigned this_time_units_high, this_time_units_low;        // Just to make g++ shut up in the optimized mode    this_time_units_high = this_time_units_low = 0;    // Trace delta cycles only when enabled    if (!trace_delta_cycles && this_is_a_delta_cycle) return;    // Check for initialization    if (!initialized) {        initialize();        initialized = true;        return;    };    double now_units = sc_time_stamp().to_seconds() / timescale_unit;    unsigned now_units_high, now_units_low;    double_to_special_int64(now_units, &now_units_high, &now_units_low );    bool now_later_than_previous_time = false;    if( now_units_low > previous_time_units_low         && now_units_high == previous_time_units_high        || now_units_high > previous_time_units_high){        now_later_than_previous_time = true;    }    bool now_equals_previous_time = false;    if(now_later_than_previous_time){        this_time_units_high = now_units_high;        this_time_units_low = now_units_low;    } else {        if( now_units_low == previous_time_units_low	    && now_units_high == previous_time_units_high){	    now_equals_previous_time = true;            this_time_units_high = now_units_high;            this_time_units_low = now_units_low;	}    }    // Since VCD does not understand 0 time progression, we have to fake    // delta cycles with progressing time by one unit    if(this_is_a_delta_cycle){        this_time_units_high = previous_time_units_high;        this_time_units_low = previous_time_units_low + 1;        if(this_time_units_low == 1000000000){            this_time_units_high++;            this_time_units_low=0;        }        static bool warned = false;        if(!warned){	    cout << "Note: VCD delta cycling with pseudo timesteps (1 unit) "                    "is performed.\n" << endl;            warned = true;        }    }    // Not a delta cycle and time has not progressed    if( ! this_is_a_delta_cycle && now_equals_previous_time &&	( now_units_high != 0 || now_units_low != 0 ) ) {	// Don't print the message at time zero        static bool warned = false;        if( ! warned && ! running_regression ) {            sprintf(message,                    "Multiple cycles found with same (%u) time units count.\n"                    "Waveform viewers will only show the states of the last one.\n"                    "Use ((vcd_trace_file*)vcdfile)->sc_set_vcd_time_unit(int exponent10_seconds)\n"                    "to increase time resolution.",                    now_units_low                    );            vcd_put_error_message(message, true);            warned = true;        }    }    // Not a delta cycle and time has gone backward    // This will happen with large number of delta cycles between two real    // advances of time    if(!this_is_a_delta_cycle && !now_equals_previous_time && !now_later_than_previous_time){        static bool warned = false;        if(!warned){            sprintf(message,                    "Cycle found with falling (%u -> %u) time units count.\n"                    "This can occur when delta cycling is activated.\n"                    "Cycles with falling time are not shown.\n"                    "Use ((vcd_trace_file*)vcdfile)->sc_set_vcd_time_unit(int exponent10_seconds)\n"                    "to increase time resolution.",                    previous_time_units_low, now_units_low);            vcd_put_error_message(message, true);            warned = true;        }	// Note that we don't set this_time_units_high/low to any value only        // in this case because we are not going to do any tracing. In the        // optimized mode, the compiler complains because of this. Therefore,        // we include the lines at the very beginning of this function to make        // the compiler shut up.        return;     }    // Now do the actual printing     bool time_printed = false;    vcd_trace* const* const l_traces = traces.raw_data();    for (int i = 0; i < traces.size(); i++) {        vcd_trace* t = l_traces[i];        if(t->changed()){            if(time_printed == false){                char buf[200];                if(this_time_units_high){                    sprintf(buf, "#%u%09u", this_time_units_high, this_time_units_low);                }                else{                     sprintf(buf, "#%u", this_time_units_low);                }                fputs(buf, fp);                fputc('\n', fp);                time_printed = true;            }	    // Write the variable            t->write(fp);            fputc('\n', fp);        }    }    // Put another newline after all values are printed    if(time_printed) fputc('\n', fp);    if(time_printed){        // We update previous_time_units only when we print time because        // this field stores the previous time that was printed, not the        // previous time this function was called        previous_time_units_high = this_time_units_high;        previous_time_units_low = this_time_units_low;    }}voidvcd_trace_file::create_vcd_name(sc_string* p_destination){    const char first_type_used = 'a';    const int used_types_count = 'z' - 'a' + 1;    int result;    char char4 = (char)(vcd_name_index % used_types_count);    result = vcd_name_index / used_types_count;    char char3 = (char)(result % used_types_count);    result = result / used_types_count;    char char2 = (char)(result % used_types_count);    char buf[20];    sprintf(buf, "%c%c%c",            char2 + first_type_used,            char3 + first_type_used,            char4 + first_type_used);    *p_destination = buf;    vcd_name_index++;}// same as abovesc_stringvcd_trace_file::obtain_name(){    const char first_type_used = 'a';    const int used_types_count = 'z' - 'a' + 1;    int result;    char char4 = (char)(vcd_name_index % used_types_count);    result = vcd_name_index / used_types_count;    char char3 = (char)(result % used_types_count);    result = result / used_types_count;    char char2 = (char)(result % used_types_count);    char buf[20];    sprintf(buf, "%c%c%c",            char2 + first_type_used,            char3 + first_type_used,            char4 + first_type_used);    vcd_name_index++;    return sc_string(buf);}vcd_trace_file::~vcd_trace_file(){    int i;    for (i = 0; i < traces.size(); i++) {        vcd_trace* t = traces[i];        delete t;    }    fclose(fp);}// Functions specific to VCD tracingstatic charmap_sc_logic_state_to_vcd_state(char in_char){    char out_char;    switch(in_char){        case 'U':        case 'X':         case 'W':        case 'D':            out_char = 'x';            break;        case '0':        case 'L':            out_char = '0';            break;        case  '1':        case  'H':             out_char = '1';            break;        case  'Z':             out_char = 'z';            break;        default:            out_char = '?';    }    return out_char;}voidvcd_put_error_message(const char* msg, bool just_warning){    if(just_warning){	cout << "VCD Trace Warning:\n" << msg << "\n" << endl;    }    else{	cout << "VCD Trace ERROR:\n" << msg << "\n" << endl;    }}staticvoidremove_vcd_name_problems(sc_string& name){    char message[4000];    static bool warned = false;    bool braces_removed = false;    for (int i = 0; i< name.length(); i++) {      if (name[i] == '[') {	name.set(i, '(');	braces_removed = true;      }      else if (name[i] == ']') {	name.set(i, ')');	braces_removed = true;      }    }    if(braces_removed && !warned){        sprintf(message,                "Traced objects found with name containing [], which may be\n"                "interpreted by the waveform viewer in unexpected ways.\n"                "So the [] is automatically replaced by ().");        vcd_put_error_message(message, true);        warned = true;    }}sc_trace_file*sc_create_vcd_trace_file(const char * name){  sc_trace_file *tf;  tf = new vcd_trace_file(name);  sc_get_curr_simcontext()->add_trace_file(tf);  return tf;}voidsc_close_vcd_trace_file( sc_trace_file* tf ){    vcd_trace_file* vcd_tf = (vcd_trace_file*)tf;    delete vcd_tf; }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?