SystemC-sc_export
分层通道是例化了其它通道的通道。有时我们需要用到分层通道,如下图的例子。模块E是一个通道,它例化了通道C1和通道D,而通道D又例化了通道C2。设计者希望将模块D中的模块C2和模块C1都导出到模块E中,从而对与模块E连接的模块X可见。这里,我们需要的功能是将一个模块所定义的接口导出到其父模块中,比如模块C2的接口需要导出到模块D中,进而导出到模块E中,而模块C1的接口也需要导出到模块E中,而模块C1和C2可能实现的是相同的接口。sc_export允许一个通道将接口导出到其父模块,从而使得连接到父模块的其它模块可以调用该接口的方法。
图3-8 导出端口的实例
读者可能会问,为什么要定义sc_export呢,直接通过模块E中的通道C1的名字调用通道C1不就行了么?直接通过模块E中的模块D中通道C2的名字调用通道C2不就行了么?能够这样做的前提是:如果直接通过模块E中的通道C1的名字调用通道C1,通道C1在模块E中必须是公有的,类似的,模块D在模块E中也必须是公有的,通道C2在模块D中必须是公有的。对于一个IP提供者,它可能不希望用户了解更多内部信息或者给用户增加麻烦,这就需要sc_export了。
sc_export的定义如下:
sc_export的定义如下:
namese sc_core {
class sc_export_base: publ sc_object;
template<class IF> class sc_export
: public sc_export_base
{
public:
sc_export();
explicit sc_export( const char* );
virtual const char* kind() const;
void operator() ( IF& );
void bind( IF& );
operator IF& ();
IF* operator-> ();
const IF* operator-> () const;
virtual sc_interface* get_interface();
virtual const sc_interface* get_interface() const;
……
}
sc_export()和explicit sc_export( const char* )是构造函数,sc_export()调用sc_gen_unique_name("export")生产其默认名字;kind()返回字符串"sc_export",get_interface()返回一个sc_export实例所绑定的接口的指针。函数以及操作符包括bind( IF& )、IF& ()、operator-> ()、operator-> ()const用于绑定导出端口到通道。
图3-9的例子的源码如下:
#include "syst.h"
//接口
class C_if : virtual public sc_interface
{
public:
virtual void run() = 0;
};
//通道
class C : public C_if, public sc_channel
{
public:
SC_CTOR(C) { }
virtual void run()
{
cout << sc_time_stamp() << " In Channel run() " << endl;
}
};
// --- D: 通过I导出通道C---
SC_MODULE( D )
{
sc_export<C_if> IFP;
SC_CTOR( D ): IFP("IFP"), m_C("C")
{
IFP( m_C ); // bind sc_export->interface by name
}
private:
C m_C;
};
// --- E: 具有两个导出端口的模块---
SC_MODULE( E )
{
private:
C m_C;
D m_D;
public:
sc_export<C_if> IFP1;
sc_export<C_if> IFP2;
SC_CTOR( E )
: m_C("C"),
m_D("D"),
IFP1("IFP1")
{
IFP1( m_C );
IFP2( m_D.IFP ); // bind sc_export->sc_export by name
IFP1.get_interface();
}
};
// 模块X通过E连接到通道
SC_MODULE( X )
{
sc_port<C_if> P1;
sc_port<C_if> P2;
SC_CTOR(X) {
SC_THREAD(run);
}
void run() {
wait(10, SC_NS);
P1->run();
wait(10, SC_NS);
P2->run();
}
};
int sc_main(int argc, char** argv) {
E the_E("E");
X the_X("X");
// port->IFP
the_X.P1( the_E.IFP1 );
the_X.P2( the_E.IFP2 );
sc_start(17, SC_NS);
the_E.IFP1->run();
sc_start(50, SC_NS);
return 0;
}
(
- 上一篇:动态创建SystemC进程
- 下一篇:SystemC-sc_event_queue