3.9 端口与通道的关联
首先我们来看看如何将端口与通道相互连接起来,以允许模块内的进程通过端口来调用所连接的通道提供的方法。
我们来看下面的例子。如图1,这是一个信源与信宿通过FIFO通信的简单系统框图。
图1 信源与信宿通过通道FIFO通信
我们首先来定义一个FIFO接口。FIFO的接口可以分为读接口和写接口,它们是分开的,我们分别定义。如下面的代码
- #include <syst.h>
- template<class T>
- class fifo_write_if:publ sc_interface{
- //write a piece of data to FIFO
- bool fifo_write(T data)=0;
- //How many free place avaiable in FIFO?
- unsigned int number_free()=0;
- };
- template<class T>
- class fifo_read_if:public sc_interface{
- //将一个数据单位写入FIFO
- bool fifo_read(T& data)=0;
- //FIFO中有多少个数据单位?
- unsigned int number_avaiable()=0;
- };
假设我们已经设计好了一个FIFO通道fifo<T, D>,其中T代表存储的数据类型,D代表FIFO的深度。那么在source模块中就可以通过下面的方式调用FIFO了。
- SC_MODULE(Source){
- sc_port<fifo<int>> fifo_write_port;
- void write_process(){
- int data=100;
- if (fifo_write_port->number_free()!=0)
- fifo_write_port->fifo_write(data);
- }
- ......
- };
write_process()是Source模块的一个进程,它通过端口fifo_write_port来检查fifo的状态。如果FIFO中还有空位置,就将数据data写入FIFO。
在这里我们已经假设Top的设计如下:
- #define DEPTH 64
- SC_MODULE(Top){
- fifo<int,DEPTH> fifo1;
- Source Source1;
- Sink Sink1;
- ......
- SC_CTOR(Top){
- Source1.fifo_write_port(fifo1);
- Sink1.fifo_read_port(fifo1);
- ......
- }
- };
这里语句Source1.fifo_write_port(fifo1)将Source1的fifo_write_port端口与fifo1连接。这样当fifo_write_port->fifo_write(data)执行的时候,data的值就会被写入fifo1的存储单元中。
- 上一篇:SystemC通道的同步规则
- 下一篇:SystemC Channel