Learn SystemC - Time Notation

本文介绍了SystemC中用于仿真时间跟踪的sc_time数据类型,区分了wall-clocktime和simulatedtime的概念,展示了如何使用SC_ZERO_TIME以及如何设置和转换不同时间单位。示例代码演示了如何在仿真环境中操作sc_time并获取当前时间。
摘要由CSDN通过智能技术生成

本文为英文版本翻译,原文地址:Time Notation

两种时间测量

首先,需要理解以下两种时间测量的不同:

  1. wall-clock time(挂钟时间)
    从开始执行到结束的时间,包括等待其他系统活动及应用的时间
  2. 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值