github地址:https://github.com/albertxie/iverilog-tutorial
先安装iverilog和gtkwave。
sudo apt-get install iverilog
sudo apt-get install gtkwave
从github克隆代码,进行编译,编译后生成simple.vvp文件。
iverilog -o simple.vvp simple.v simple_tb.v
其中,simple.v代码内容为:
// very standard Verilog module
module simple(A, B);
input [3:0] A;
output [3:0] B;
// mix up the input bits
assign B = { A[0], A[2], A[1], A[3] };
endmodule
simple_tb.v内容为:
module simple_tb;
// initial values
reg [3:0] A = 4'b1010;
wire [3:0] B;
initial
begin
// vcd dump
$dumpfile("simple.vcd"); //指定VCD文件的名字为simple.vcd,仿真信息将记录到此文件
// the variable 's' is what GTKWave will label the graphs with
$dumpvars(0, s);
$monitor("A is %b, B is %b.", A, B); //监控A、B的值。
// setting each elements values at each time interval, must finish with $finish
#50 A = 4'b1100;
#100 A = 4'b0000;
#100 $finish;
end
// stap of module
simple s(A, B);
endmodule
输入下列代码进行仿真:
vvp simple.vvp
输出结果为:
输出结果说明:
当A=4'b1010,B = { A[0], A[2], A[1], A[3] }时,B[3]=A[0]=0,B[2]=A[2]=0,B[1]=A[1]=1,B[0]=A[3]=1,即B=4'0011,以此类推。
然后执行下列代码查看波形:
gtkwave simple.vcd
得到波形:
通过图像可以观察到,在时间范围为100~150sec时,A=4'b1100,B=4'b0101,这与输出结果一致。