1. 程式人生 > >systemC構建格雷碼和二進制的轉換

systemC構建格雷碼和二進制的轉換

sha process 進行 itl text 信號 版本 分享 進制

廢話不多說,直接上實現:

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;
   
}

主要的實現部分已給出,至於其他主函數部分和monitor部分博主就不寫了。。。。

技術分享圖片

systemC構建格雷碼和二進制的轉換