第四章 验证与 VCS 使用
本章将讲述的内容:
第一节 验证
。什么是验证
。为什么需要验证
。验证的重要性
。如何进行验证
第二节 VCS 简单使用方法
2.1 什么是 VCS
2.2VCS 可以做什么
2.3 怎样进行验证
2.4VCS 的工作方式
2.5VCS 使用方法 举个简单例子
2.6VirSim 的图形方式和每个窗口的介绍
附录 A. VCS 的参数
附录 B. virsim 简明帮助
附录 C. simv 简明帮助
第一节 验证
当代码编写完之后,怎么确定是正确的呢,代码能不能符合设计要求,能不能完成所需
要的功能,这就是验证所要做的工作。验证在设计中有很重要的地位,从设计流程中可以看
到,几乎设计工作每前进一步,都要进行验证。
对验证的要求,大多数人认为只要编译通过之后,能实现功能就可以了,其实决不仅仅
这么简单,验证的目的应该是尽量多的找到代码中的错误,不管是编写错误还是功能错误,
找出的错误越多,验证工作就做的越好越好。
既然验证这么重要,如何进行验证呢?对于验证来说,不同等级的验证,它的方法是不
一样的。什么是验证的等级,从设计流程(下图)可以看到,验证可以大致分为单独子模块
验证、功能模块验证、系统顶级验证。
。单独子模块验证,需要做的工作是验证它的功能和逻辑是否符合设计要求
。功能模块验证,需要验证这个模块的功能可不可以满足要求,是否会有非法数据或不
该有的输出,错误的状态等。
。系统顶级验证,更关注于系统整体的行为方式,模块间的联系和通讯,总线信号,数
据流路径是否满足设计要求,数据处理或时序正确与否等。
验证需要一个支持的平台,这就是 test_bench,在这个测试平台上,有激励信号产生器、
被测模块、响应分析和监测器,(下图)
激励与控制:输入端口设置,测试向量,测试模式设置,同步。
响应分析器和监测器:可以及时监控输出信号变化,可以判断输出信号是正确、合法、错误、
非法等等。
testbench 可以用 verilog 描述语言搭建,也可以用 C 语言编写,如果用 C 语言编写,还
需要相关的编译器并和与 verilog 的接口。
第二节 VCS 的简单使用方法
2.1 什么是 VCS
VCS的全称是Verilog Compile Simulator,是Synopsys公司的强有力的电路仿真工具,可
以进行电路的时序模拟。
2.2 VCS的工作方式
VCS运行首先把输入的verilog源文件编译,然后生成可执行的模拟文件,也可以生成
VCD或者VCD+记录文件。然后运行这个可执行的文件,可以进行调试与分析;或者查看
生成的 VCD 或者 VCD+记录文件。还生成了一些供分析和查看的文件,以便于调试。
2.3 怎样进行仿真和验证
仿真测试一个模块的大致步骤如下:
(1) 首先需要编写好模块的 verilog 代码。
(2) 搭建 testbench,充分了解被测模块的特性,编写测试向量,输入端口的激励,
编写响应分析和监测部分。
(3) 运行 VCS 进行模拟,查看输出或者波形。
(4) 若发现错误,分析错误类型和原因,修改代码或者修正测试方法,直到符合测
试要求。
2.4 VCS 的运行方式
VCS 的运行方式有两种,一种是交互模式(interactive mode),一种是批处理模式(batch
mode),两种方式各有优劣,具体用在不同的情况下。在测试小模块或者底层模块,情况不
太复杂的时候,而又需要很详细信息的时候,可以采用交互模式,交互性能更好,显示更直
观;当进行复杂测试而关注于整体性能,而不必去查看每个信号的时候,只需要查看所需要
关心的信号即可,这种情况可以用批处理模式。
2.5 VCS 简单使用例子
Module
Monitor
&
Analyzer
Stimulus
&
Control
下面用一个简单的例子来说明如何使用 VCS
这是个四位全加器的 verilog 代码,存储为 add4.v,
module addr4 (clk,in1, in2, sum, carry);
output [3:0] sum;
output carry;
input clk;
input [3:0] in1, in2;
reg [3:0] sum;
reg carry;
integer temp;
initial begin
sum = 0;
carry = 0;
end
always @(posedge clk) begin
temp = in1+in2;
sum=temp;
if (temp > 15)
carry = 1;
else
carry = 0;
end
endmodule
然后再根据这个模块写一个测试模块,也称之为 testbench,存为 top.v,
module top;
reg clk_reg;
reg [3:0] in1_reg, in2_reg;
wire [3:0] sum;
wire carry;
addr4 a4 (clk_reg,in1_reg, in2_reg, sum, carry);
parameter d = 100;
initial begin
clk_reg=0;
in1_reg = 0;
in2_reg = 0;
repeat (16*100) begin
#d in1_reg = in1_reg+1; in2_reg = in2_reg+1;
// $display($stime,,"in1_reg +%d in2_reg+ %d = sum %d carry is %d", in1_reg,
in2_reg, sum, carry);
// $display($stime,," %b + %b = %b and carry is %b", in1_reg, in2_reg, sum,
carry);
end
// $strobe($stime,,"in1_reg %b in2_reg %b sum %b carry %b", in1_reg, in2_reg,
sum, carry);
#1
$finish(2);
end
always
begin
#50 clk_reg= ~clk_reg;
end
always @(sum) begin
//$display($stime,,"in1_reg +%d in2_reg+ %d = sum %d carry_reg is %d", in1_reg,
in2_reg, sum, carry_reg);
$display($stime,,"now at a clock posedge,the operation is :: %d + %d = %d and carry is
%d", in1_reg, in2_reg, sum, carry);
//$stop;
end
endmodule
最简单的仿真,只要运行 vcs filename 即可,filename 可以有很多个,
比如上面的例子:
vcs top.v add4.v
然后,如果没有发现编译错误,将会出现 VCS 的说明和一些信息,接下来的就是它的
执行情况,翻译 top.v 和 add4.v,可以自动发现顶层模块,如果定义了 timescale 将显示用的
timescale 信息,如果没有,就显示 No TimeScale specified。然后将产生一个名为 simv 的可
执行文件,这个就是模拟仿真文件。
下面是运行结果:
Parsing design file 'top.v'
Parsing design file 'add4.v'
Top Level Modules:
top
No TimeScale specified
1 of 2 unique modules to generate
1 of 1 modules done
Invoking loader...
simv generation successfully completed
下面我们来运行一下这个 simv 可执行文件,
simv <cr>
结果将显示:
Chronologic VCS simulator copyright 1991-2001
Contains Synopsys proprietary information.
Compiler version 6.0; Runtime version 6.0; Jan 8 15:37 2002
0 now at a clock posedge,the operation is :: 0 + 0 = 0 and carry is 0
150 now at a clock posedge,the operation is :: 1 + 1 = 2 and carry is 0
250 now at a clock posedge,the operation is :: 2 + 2 = 4 and carry is 0
350 now at a clock posedge,the operation is :: 3 + 3 = 6 and carry is 0
450 now at a clock posedge,the operation is :: 4 + 4 = 8 and carry is 0
550 now at a clock posedge,the operation is :: 5 + 5 = 10 and carry is 0
650 now at a clock posedge,the operation is :: 6 + 6 = 12 and carry is 0
750 now at a clock posedge,the operation is :: 7 + 7 = 14 and carry is 0
850 now at a clock posedge,the operation is :: 8 + 8 = 0 and carry is 1
950 now at a clock posedge,the operation is :: 9 + 9 = 2 and carry is 1
1050 now at a clock posedge,the operation is :: 10 + 10 = 4 and carry is 1
1150 now at a clock posedge,the operation is :: 11 + 11 = 6 and carry is 1
1250 now at a clock posedge,the operation is :: 12 + 12 = 8 and carry is 1
1350 now at a clock posedge,the operation is :: 13 + 13 = 10 and carry is 1
1450 now at a clock posedge,the operation is :: 14 + 14 = 12 and carry is 1
1550 now at a clock posedge,the operation is :: 15 + 15 = 14 and carry is 1
1650 now at a clock posedge,the operation is :: 0 + 0 = 0 and carry is 0
1750 now at a clock posedge,the operation is :: 1 + 1 = 2 and carry is 0
1850 now at a clock posedge,the operation is :: 2 + 2 = 4 and carry is 0
1950 now at a clock posedge,the operation is :: 3 + 3 = 6 and carry is 0
2050 now at a clock posedge,the operation is :: 4 + 4 = 8 and carry is 0
2150 now at a clock posedge,the operation is :: 5 + 5 = 10 and carry is 0
2250 now at a clock posedge,the operation is :: 6 + 6 = 12 and carry is 0
2350 now at a clock posedge,the operation is :: 7 + 7 = 14 and carry is 0
2450 now at a clock posedge,the operation is :: 8 + 8 = 0 and carry is 1
…………(略)
$finish at simulation time 160001
V C S S i m u l a t i o n R e p o r t
Time: 160001
CPU Time: 0.100 seconds; Data structure size: 0.0Mb
Tue Jan 8 15:37:31 2002
验证输出的这些信息,就可以发现,模块的设计是正确的,满足和全加器的设计要求。
注意到 top.v 文件中的注释掉的几行,也可以用其他方式显示,特殊情况需要特殊处理。
2.6 图形方式的 VCS
下面用图形方式启动 VCS,可以进行更方便的控制和监测,
运行 VCS –RI top.v add4.v (-RI 的选项就是打开交互式图形界面,是指 Run Interactive)
运行上面的命令会打开 VirSim 主程序,Interactive 窗口会自动打开。