概述
sc_port bind
port 绑定时,有如下这两个函数,其中,operator()的实现需要通过调用虚成员函数bind来实现。函数参数可以为 interface,也可以是sc_export (如spec中的解释,其实是调用了 隐式类型转换sc_export<IF>::operator IF&)。
void operator() ( IF& );
virtual void bind( IF& );
Each of these two functions shall bind the port instance for which the function is called to the channel instance passed as an argument to the function. The actual argument can be an export, in which case the C++ compiler will call the implicit conversion sc_export<IF>::operator IF&. The implementation of operator() shall achieve its effect by calling the virtual member function bind.
port 绑定时,如果参数依然是一个port,其实就是层次结构的透传。比如 Module A的一个sub-Module B 中有信号需要与 外部Module C进行交互,那么 可以在Module A和sub-Module B中各定义一个port,这两个port之间绑定起来,就是用的如下的两个函数。
void operator() ( sc_port_b<IF>& );
virtual void bind( sc_port_b<IF>& );
Port绑定时,如果出现 延迟类型的,需要在end_of_elaboration函数中完成。
Since a port may be bound to another port that has not yet itself been bound, the implementation may defer the completion of port binding until a later time during elaboration, whereas exports shall be bound immediately. Such deferred port binding shall be completed by the implementation before the callbacks to function end_of_elaboration.
sc_port call & useage
operator-> shall return a pointer to the first channel instance to which the port was bound during elaboration.
operator[] shall return a pointer to a channel instance to which a port is bound. The argument identifies which channel instance shall be returned.
operator-> is key to the interface method call paradigm in that it permits a process to call a member function, defined in a channel, through a port bound to that channel.
The member functions size and get_interface can be called during elaboration or simulation, whereas operator-> and operator[] should only be called from end_of_elaboration or during simulation.
重载操作符-> 和[] 是 实现上述解耦、访问对端sc_interface中API的关键。如果port只绑定了一个interface,那么-> 和 [0] 的效果是一样的。如果绑定了多个interface,其中->只能访问instance为0的首个sc_interface,[]加下标可以访问全部instance的sc_interface。
如sc_port 和 sc_signal bind的一个奇怪用法_123axj的博客-CSDN博客 中的代码,bool t_sig = m_port_in->read(); 就是调用了operator->。
查看源码,我们会发现,operator[] 实际上是调用了get_interface(int index)。
IF* operator [] ( int index_ )
{ return get_interface( index_ ); }
const IF* operator [] ( int index_ ) const
{ return get_interface( index_ ); }
get_interface 有带参和不带参两种重载函数,不带参数,返回的是instance 为0的首个sc_interface的指针,带int index 参数的,返回的就是instance 为参数值的指针。
get_interface is intended for use in implementing specialized port classes derived from sc_port. In general, an application should call operator-> instead. However, get_interface permits an application to call a member function of the class of the channel to which the port is bound, even if such a function is not a member of the interface type of the port.
operator->和get_interface (operator[])作用类似。区别在于,operator->只能访问本sc_port子类中定义好的API,而get_interface得到对应sc_interface的指针,可以访问所有的public 函数。Spec 中给出了一个非常好的示例:使用 get_interface,从 sc_in<bool> clock中获取 外部clock的period。
SC_MODULE(Top)
{
sc_in<bool> clock;
void before_end_of_elaboration()
{
sc_interface* i_f = clock.get_interface();
sc_clock* clk = dynamic_cast<sc_clock*>(i_f);
sc_time t = clk->period(); // Call method of clock object to which port is bound
其中,sc_clock继承于sc_signal,而sc_signal 又最终继承于sc_interface 和sc_prim_channel。sc_clock中有period() 函数来得到clock的时钟。sc_in<> 是sc_port的子类,提供了 read() / pos() /neg() 等API,并没有提供sc_clock的period函数 (也不能提供,因为sc_in<>可以与非sc_clock的其他sc_signal类型进行绑定)。上述示例中,由于我们知道sc_in<bool> clock这个变量肯定是与一个sc_clock绑定,故就可以使用get_interface()来获取到sc_clock的指针,然后再调用 period()函数。
最后
以上就是丰富衬衫为你收集整理的sc_port / sc_interface / sc_export (part 2)sc_port bindsc_port call & useage的全部内容,希望文章能够帮你解决sc_port / sc_interface / sc_export (part 2)sc_port bindsc_port call & useage所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复