?? modelsim下用systemc做設計驗證方法與示例.txt
字號:
如何在ModelSim下編譯和仿真SystemC的設計?
如何在ModelSim下用SystemC的做驗證?
SystemC作為一種系統級設計與驗證語言,非常適合做復雜IC的驗證,而不是用于RTL描述。很多人問我如何將SystemC綜合和編譯為可以下載的CPLD/FPGA的比特文件或者綜合為ASIC網表,我的回答是用SystemC做RTL設計還為時過早。可以想象將來可能將SystemC的行為級的描述綜合為網表,即所謂高層次綜合,這是一個很美好的未來,但未來不是現在。Verilog/SystemVerilog依然是最好的RTL設計語言。未來的RTL設計屬于SystemVerilog。關于SystemC和SystemVerilog在設計中的地位問題,我認為在驗證方面,SystemC有明顯的優勢。如果你設計純粹的ASIC,那么用SystemVerilog可能就足夠了。但是在很多場合,軟硬件同時存在,SystemC的代碼很多部分可以之間用于設計軟件,這個是很明顯的優勢。大家同時也可以看到,現在在ModelSim等仿真軟件中,SystemC使用起來跟Verilog/VHDL一樣,非常方便。舉一個例子,我們假如想做DVB-S2的LDPC,我們一定會先用C++(M atlab也可以)寫仿真程序,驗證算法的正確性。然后假設我們已經確定了目標ASIC的架構,打算用Verilog做RTL設計。現在既然C++代碼的驗證部分可以幾乎不加改變的用于基于SystemC的驗證模塊的設計,我們為什么還要費力的用SystemVerilog重新寫一遍驗證代碼呢?
下面步入正題,講一講如何在ModelSim下編譯和仿真SystemC的設計。我們設計一個一位移位寄存器模塊(Verilog代碼):
1.shifter.v
`timescale 1ns/100ps
module shifter(clk,nrst,din,dout);
input clk,nrst;
input din;
output reg dout;
always(posedge clk or negedge nrst) begin:shifter_with_nreset
if(~nrst) dout<=1'b0;
else dout<=din;
end
endmodule
頂層設計為驗證模塊加shifter模塊的例化:
2.tb.v
`timescale 1ns/100ps
module tb;
wire clk,nrst,data,data_fd_bk;
shifter_test tester(.clk(clk),.nrst(nrst),.data(data),.data_fd_bk(data_fd_bk));
shifter uut(.clk(clk),.nrst(nrst),.din(data),.dout(data_fd_bk));
endmodule
其中shifter_test用SystemC描述。這個例子實際上不能顯示SystemC的好處。
下面是SystemC的代碼:
3.Shifter_test.h
#ifndef __shifter_test_h
#define __shifter_test_h
#include <systemc.h>
#include <assert.h>
SC_MODULE(shifter_test)
{
public:
// Module ports
sc_out<bool> clk,nrst;
sc_out<bool> data;
sc_in<bool> data_fd_bk;
bool data_reg;
bool err;
sc_clock internal_clk;
void st_behaviour()
{
nrst=0;
data=0;
wait(5);
data=1;
wait(2);
nrst=1;
wait(2);
while(1)
{
data=0;
wait(2);
data=1;
wait(3);
data=0;
wait(4);
if(err) printf("Test failed");
else printf("Test passed\n");
}
}
void gen_clk(){clk=internal_clk.read();}
void disp_data(){
printf("nrst=%d,data input=%d,data output=%d\n",nrst.read(),data_reg,data_fd_bk.read());
if((nrst.read()==1) && (data_reg!=data.read()))
{
err=1;
assert(false);
}
data_reg=data.read();
}
SC_CTOR(shifter_test)
:clk("clk"),nrst("nrst"),data("data"),data_fd_bk("data_fd_bk"),internal_clk("internal_clk",1000,0.5,SC_NS)
{
SC_METHOD(gen_clk);
sensitive<<internal_clk;
dont_initialize();
SC_CTHREAD(st_behaviour, clk.pos());
SC_METHOD(disp_data);
sensitive<<clk.neg();
err=0;
}
};
#endif
4.shifter_test.cpp
#include "shift_test.h"
SC_MODULE_EXPORT(shifter_test);
只有兩行代碼。注意這里SC_MODULE_EXPORT的作用是將systemc的模塊對其它語言可見。
將以上4個文件加入到ModelSim的Project中,之后輸入編譯命令如下:
sccom –g *.cpp sccom –link vlog *.v vsim tb
就可以根據需要看一些信號的仿真波形了。
這里只有 sccom –g *.cpp sccom –link 與SystemC有關。 在ModelSim中選擇Compile all之后,再執行sccom –link,其效果等價于sccom –g *.cpp;vlog *.v;sccom –link。
大家可以看到,在ModelSim中使用SystemC是如此簡單。很多人比較熟悉VC,而不熟悉gcc,可能對于gcc的編譯錯誤信息不是十分理解,這是在ModelSim中使用SystemC的一個大障礙。有兩個問題需要提醒。
一是好像ModelSim對于sc_clock的參數理解有些問題。 比如 sc_clock internal_clk("internal_clk",1000,0.5,SC_NS) 的仿真波形顯示的周期是100ns,我將Verilog的`timescale 設置為1ns/1ns仍然是100ns,不知道是不是Bug.
二是sc_clock必須初始化,否則在vsim tb時就會出現類似下面的錯誤
vsim tb # Loading work.tb # Loading work.shifter # Loading work/systemc.so # Loading work.shifter_test # ** Error: (vsim-6504) sc_clock low time is zero: increase the period or decrease the duty cycle Also check the simulator resolution and time-unit settings in the modelsim.ini file. The default simulator resolution and time-unit used by ModelSim is 1ns.: clock 'tester/internal_clk' # ** Fatal: Fatal SystemC error detected, exiting... # Time: 0 ps Iteration: 0 Instance: /tb/tester/internal_clk file: C:\Modeltech_6.0c\include\systemc\sc_clock.h # FATAL ERROR while loading design
好像有人問過類似問題。
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -