systemC构建格雷码和二进制的转换

废话不多说,直接上实现:

simulus是gray码信号发生器的实现:

simulus.h:
include "base.h" 
 
#ifndef SIMULUS 
#define SIMULUS 
 
const unsigned int size=4; 
 
SC_MODULE(simulus){ 
    //   signal drivers  
    sc_out<sc_uint<size> > gray ; 
     
    void prc_simulus(); 
    sc_uint<size> convert( sc_uint<size> bin ); 
     
    SC_CTOR(simulus){ 
        SC_THREAD(prc_simulus); 
    } 
}; 
 
#endif
simulus.cpp:

#include "simulus.h"
//    使用2进制转化为格雷码
void simulus::prc_simulus(){    
//  signal generates process    
sc_uint<size> temp(0) ;        
for(;;){        
    gray = (convert(temp));        
    temp++ ;        
    wait(100,SC_NS);    
    }
}

sc_uint<size> simulus::convert( sc_uint<size> bin )
{    
    sc_uint<size> gray ;    
    gray = bin^(bin>>1);        
    //    在此中所有的方法都不支持,    
    //    只有使用逻辑符号进行操作        
    
    return gray ;
}

dut是对格雷码转转二进制的实现:

dut.h:
#include "../base.h" 
 
#ifndef DUT 
#define DUT 
 
/*********************************   
   在这个systemC版本中无法使用模版!!!
   可能是由于下载的版本过老,很多操作
   都没有支持。
   (1)模版无法使用,只有通过常数变量来代替
   (2)sc内置类型的一些方法无法使用,
       只能使用基本的位操作。。。。。
**********************************/
 
const unsigned int gsize = 4 ;       //   gray to bin convertor ....
SC_MODULE(gb_convertor){ 
     
    sc_in<sc_uint<gsize> >gray; 
    sc_out<sc_uint<gsize> > bin ; 
     
    void prc_gb(); 
     
    SC_CTOR(gb_convertor){ 
        SC_METHOD(prc_gb); 
        sensitive<<gray ; 
    }; 
     
}; 
 
#endif
dut.cpp:
#include "dut.h"
void gb_convertor::prc_gb()
{
    sc_uint<gsize>  bin_temp = gray.read(), gray_temp = gray.read() ;
    
    while(gray_temp>>=1)
        bin_temp ^= gray_temp ;
    bin = bin_temp^1 ;
   
}

主要的实现部分已给出,至于其他主函数部分和monitor部分博主就不写了。。。。


猜你喜欢

转载自blog.51cto.com/13824643/2137220
今日推荐