sc_port / sc_interface / sc_export (part 2)

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()函数。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

123axj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值