A notion of time

1、simulation kernel用sc_time数据类型来跟踪仿真时间,指定delay和timeouts。

sc_time是用一个64bit的无符号整型数来表示。

SC_SEC  seconds   10^0 秒

SC_MS    milliseconds 10^-3  毫秒

SC_US    microseconds  10^-6   微秒

SC_NS    nanoseconds   10^-9   纳秒     

SC_PS    picoseconds  10^-12   皮秒

SC_FS    femtoseconds  10^-15  飞秒

sc_time_stamp()  用来获取当前的仿真时间,返回值是一个sc_time对象

sc_simulation_time()  returns time as a double in the current default time unit.

Info: (I804) /IEEE_Std_1666/deprecated: sc_simulation_time() is deprecated use sc_time_stamp()

To establish the default time unit, call sc_set_default_time_unit(). you must call this routine prior to call time specifications and prior to

the initiation of sc_start(). 

sc_start() 启动仿真,括号中可带参数,指定的时间流逝后,仿真停止。

If you provide a time argument, simulation stops after the specified simulation time has elapsed.

#include "stdafx.h"
#include <systemc.h>
#include <iostream>
using namespace std;

SC_MODULE(My_Module)
{
	SC_CTOR(My_Module)
	{
		SC_THREAD(mytime);
	}
	void mytime();
	
};
void My_Module::mytime()
	{
		int a = 5,b=3,c;
		c = a + b;
		wait(5,SC_NS);		
		cout<<"The time is now "<<sc_time_stamp()<<"!"<<endl;
		sc_time t_delay(2,SC_MS);
		t_delay *= 2;
		cout<<"Delaying "<<t_delay<<endl;
		wait(t_delay);
		cout<<"The time is now "<<sc_time_stamp()<<"!"<<endl;
	}
int sc_main(int sc_argc,char * argv[])
{		
	My_Module md("");
	sc_time m_time(5,SC_SEC);
	//sc_start();
	//sc_start(10.0,SC_MS); //limit sim to 10 ms
	sc_start(m_time);//start sim and maximum simulation time	
	system("pause");
	return 0;
}

输出:

#include "stdafx.h"
#include <systemc.h>
#include <iostream>
using namespace std;

int sc_main(int sc_argc,char * argv[])
{	
	sc_set_time_resolution(1.0,SC_MS);
	sc_start(7200,SC_SEC);//limit simulation to 2 hours(or 7200 secs)
	sc_time st = sc_time_stamp();//获取当前仿真时刻和时间单位 
	double t = sc_simulation_time();//仅仅获取当前仿真时刻而无时间单位
	//sc_cycle、sc_initialize:用于完成周期级仿真。 
	unsigned hours = int(t/3600.0);
	t -= 3600*hours;
	unsigned minutes = int(t/60.0);
	t -= 60.0*minutes;
	double seconds = t;
	cout<< hours << " hours "
		<< minutes << " minutes "
		<< seconds << " seconds "
		<< endl;
	//system("pause");
	return 0;
}

输出:

上面时间输出错误,没有考虑到time units。

sc_report_handler::set_actions("/IEEE_Std_1666/deprecated", SC_DO_NOTHING); 加入该句可以关闭有关deprecated 的warnings。

#include <systemc.h>
#include <iostream>
#include <math.h>
using namespace std;

int sc_main(int sc_argc,char * argv[])
{
	sc_report_handler::set_actions("/IEEE_Std_1666/deprecated", SC_DO_NOTHING); 
	cout<<"=== my time resolution is "<<sc_get_time_resolution()<<endl;//ps 10^-12s
	double t1 = sc_get_time_resolution().to_seconds();
	cout<<"=== my time resolution is "<<t1<<"s."<<endl;
	sc_start(7296,SC_SEC);
	sc_time t = sc_time_stamp();
	cout<<"Current time is "<<t<<endl;
	double t2 = sc_simulation_time();//ns?
	long long index = pow((double)10,(double)9);
	double ts = t2/index; // ns --> s
	unsigned hours = (int)(ts/3600.0);
	ts -= 3600 * hours;
	unsigned minutes = (int)(ts/60.0);
	ts -= 60 * minutes;
	double seconds = ts;
	cout <<"Time is "<< hours <<" hours "
		         << minutes << " minutes "
			 << seconds << " seconds "
			 << endl;
	system("pause");
	return 0;
}

输出:


#include <systemc.h>
#include <iostream>
using namespace std;

int sc_main(int sc_argc,char * argv[])
{
	sc_report_handler::set_actions("/IEEE_Std_1666/deprecated", SC_DO_NOTHING); 
	/* for example,if the specified time resolution is 100ps,
	   then coding 20ps will result in an effective value of 0 ps */
	sc_set_time_resolution(1,SC_NS);
	sc_set_default_time_unit(1,SC_MS); //establish the default time unit
	cout<<"=== my time resolution is "<<sc_get_time_resolution()<<endl;//ns 10^-9s
	double t1 = sc_get_time_resolution().to_seconds();
	cout<<"=== my time resolution is "<<t1<<"s."<<endl;
	sc_start(7296,SC_SEC);
	sc_time t = sc_time_stamp();
	cout<<"Current time is "<<t<<endl;
	double t2 = sc_simulation_time();//default time unit
	double ts = t2/1000; // ms --> s
	unsigned hours = (int)(ts/3600.0);
	ts -= 3600 * hours;
	unsigned minutes = (int)(ts/60.0);
	ts -= 60 * minutes;
	double seconds = ts;
	cout <<"Time is "<< hours <<" hours "
		         << minutes << " minutes "
			 << seconds << " seconds "
			 << endl;
	system("pause");
	return 0;
}

输出:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值