一个sc_port实例
我们利用3.3节介绍的随机读写接口ram_if来引出一个端口实例。这里我们首先需要定义一个通道ram。来看下面的代码:
- // By Chenxi,2003.2.25
- // ----------------------
- // CHANNEL : ram
- // ----------------------
- #ifndef _RAM_H
- #define _RAM_H
- #include "syst.h"
- #include "mem_if.h"
- template <class T>
- class ram : publ sc_module,ram_if<T>
- {
- public:
- ram(sc_module_name name_, unsigned int start_address, unsigned int end_address)
- :sc_module(name_)
- ,m_start_address(start_address)
- ,m_end_address(end_address){
- sc_assert(end_address>=start_address);
- mem =new T[end_address-start_address];
- }
- ~ram(){
- if(mem) {delete mem; mem=0;}
- }
- transfer_status read( unsigned address, T& data )
- {
- if( address<m_start_address || address>m_end_address){
- data=0;
- return TRANSFER_ERROR;
- }
- data = mem[address - m_start_address];
- return TRANSFER_OK;
- }
- transfer_status write( unsigned address, T& data )
- {
- if( address<m_start_address || address>m_end_address){
- return TRANSFER_ERROR;}
- mem[address-m_start_address]=data;
- return TRANSFER_OK;
- }
- bool reset(){
- for (int i=0;i<(m_end_address-m_start_address);i++)
- mem[i]=(T)0;
- return true;
- }
- inline unsigned int start_address()const{
- return m_start_address;
- }
- inline unsigned int end_address() const {
- return m_end_address;
- }
- private:
- T* mem;
- unsigned int m_start_address,m_end_address;
- };
- #endif
定义好了通道ram,接下来就可以定义一个模块通过端口来访问它了。我们接下来定义一个Master模块来访问RAM。下面的代码是一段准代码。
- SC_MODULE(Master){
- sc_in_clk clk;
- sc_port<ram_if<int>> ram_port;//端口实例
- void main_action;
- int data;
- unsigned int address;
- SC_CTOR(){
- SC_CTHREAD(main_action,clk.pos());
- }
- };
- void Master::main_action(){
- wait();
- int i=0;
- while(i++<100){
- address=0;//any valid address
- if(transfer_status status=ram_port->write(address,data)){//端口操作
- cout<<”Write RAM at address ”<<address<<”to RAM,Data is ”<<data<<endl;
- }
- else cout<<”RAM write fail,possibly caused by address out of range”<<endl;
- if(transfer_status status=ram_port->read(address, data)){
- cout<<”Read RAM at address ”<<address<<”from RAM,Data is ”<<data<<endl;
- }
- else cout<<”RAM read fail,possibly caused by address out of range”<<endl;
- wait();
- address++;
- }
- sc_stop();
- }
- 上一篇:SystemC-sc_port<IF, N
- 下一篇:SystemC-sc_port