本文为英文版本翻译,原文地址:Time Notation
两种时间测量
首先,需要理解以下两种时间测量的不同:
- wall-clock time(挂钟时间)
从开始执行到结束的时间,包括等待其他系统活动及应用的时间 - simulated time(仿真时间)
由仿真内核建模管理的时间,可能大于或小于wall-clock
sc_time
在systemC中,sc_time
是仿真内核用于跟踪仿真时间的数据类型,它定义了多个时间单位:
SC_SEC(秒)
SC_MS(毫秒)
SC_US(微秒)
SC_NS(纳秒)
SC_PS(皮秒)
SC_FS(飞秒)
每个时间单位是上一个的千分之一(1/1000)
sc_time
可以进行赋值,计算,比较等操作。
计算乘法时,需要另一个操作数为double
计算除法时,需要除数为double
SC_ZERO_TIME
SC_ZERO_TIME
是表示0时间的宏定义。建议在需要0时间时使用该宏。
例如:创建delta notification或delta time-out时
另外,可以使用sc_time_stamp()
获取当前的仿真时间
示例
下面是本节的示例:
// Learn with Examples, 2020, MIT license
#include <systemc>
using namespace sc_core;
int sc_main(int, char*[]) {
sc_core::sc_report_handler::set_actions( "/IEEE_Std_1666/deprecated",
sc_core::SC_DO_NOTHING ); // suppress warning due to set_time_resolution
sc_set_time_resolution(1, SC_FS); // deprecated function but still useful, default is 1 PS
sc_set_default_time_unit(1, SC_SEC); // change time unit to 1 second
std::cout << "1 SEC = " << sc_time(1, SC_SEC).to_default_time_units() << " SEC"<< std::endl;
std::cout << "1 MS = " << sc_time(1, SC_MS).to_default_time_units() << " SEC"<< std::endl;
std::cout << "1 US = " << sc_time(1, SC_US).to_default_time_units() << " SEC"<< std::endl;
std::cout << "1 NS = " << sc_time(1, SC_NS).to_default_time_units() << " SEC"<< std::endl;
std::cout << "1 PS = " << sc_time(1, SC_PS).to_default_time_units() << " SEC"<< std::endl;
std::cout << "1 FS = " << sc_time(1, SC_FS).to_default_time_units() << " SEC"<< std::endl;
sc_start(7261, SC_SEC); // run simulation for 7261 second
double t = sc_time_stamp().to_seconds(); // get time in second
std::cout << int(t) / 3600 << " hours, " << (int(t) % 3600) / 60 << " minutes, " << (int(t) % 60) << "seconds" << std::endl;
return 0;
}
运行结果:
1 SEC = 1 SEC
1 MS = 0.001 SEC
1 US = 1e-06 SEC
1 NS = 1e-09 SEC
1 PS = 1e-12 SEC
1 FS = 1e-15 SEC
2 hours, 1 minutes, 1seconds # 7261 seconds