在验证verilog逻辑模块功能时候,我们可以从文件中读入激励,便于大规模的验证。文件中的数据我们可以用c++编写程序产生。

第一种读入文件的方法是用系统函数:$readmemb, readmemh, 第一个函数是读入二进制的字符串,第二个是读入16进制的字符串。

我们准备两个文本文件x1.txt

1111
1010
1110
0001

y1.txt

1101
0101
1010
0001

我们验证一个四位的加法器

加法器verilog代码如下:

module adder4(cout, sum, ina, inb, cin,clk);
output [3:0] sum;
output cout;
input [3:0] ina, inb;
input cin,clk;
reg[3:0] tempa, tempb, sum;
reg cout;
reg tempc; always @(posedge clk)
begin
tempa = ina;
tempb = inb;
tempc = cin;
end always @(posedge clk)
begin
{cout, sum} = tempa+ tempb + tempc;
end
endmodule

testbench代码如下,我们用readmemb函数读入激励,并在for循环中赋值给ina,inb

`timescale 1ns/1ns
`include "adder4.v" module adder_rw_tb;
reg[3:0] ina,inb;
reg cin;
reg clk = 0;
wire[3:0] sum;
wire cout;
reg[3:0] inam[0:3];
reg[3:0] inbm[0:3];
integer i;
always #10 clk =~ clk; initial
begin
$readmemb("x1.txt",inam);
$readmemb("y1.txt",inbm);
for(i=0;i<4;i=i+1)
begin
#20 ina = inam[i];
inb = inbm[i];
end
end initial
begin
cin=0;
repeat(2)
#200 cin = {$random} % 16;
end adder4 adder4_0(
.clk(clk),
.sum(sum),
.cout(cout),
.ina(ina),
.inb(inb),
.cin(cin)
);
initial
begin
$monitor($time,,,"%b + %b + %b = {%b,%b}", ina, inb, cin,cout,sum);
#400 $finish;
end initial
begin
$dumpfile("dump.vcd");
$dumpvars;
end
endmodule

用vcs编译后,run simv

Contains Synopsys proprietary information.

Compiler version M-2017.03-SP2-11; Runtime version M-2017.03-SP2-11;  Dec 21 19:34 2018
                    0  xxxx + xxxx + 0 = {x,xxxx}
                   20  1111 + 1101 + 0 = {x,xxxx}
                   30  1111 + 1101 + 0 = {1,1100}
                   40  1010 + 0101 + 0 = {1,1100}
                   50  1010 + 0101 + 0 = {0,1111}
                   60  1110 + 1010 + 0 = {0,1111}
                   70  1110 + 1010 + 0 = {1,1000}
                   80  0001 + 0001 + 0 = {1,1000}
                   90  0001 + 0001 + 0 = {0,0010}

$finish called from file "adder_rw_tb.v", line 44.

$finish at simulation time                  400
            V C S   S i m u l a t i o n   R e p o r t

在verdi中装入dump.vcd,波形如下:

修改testbench代码,用fdisplay把输出写到文件里面。

`timescale 1ns/1ns
`include "adder4.v" module adder_rw_tb;
reg[3:0] ina,inb;
reg cin;
reg clk = 0;
wire[3:0] sum;
wire cout;
reg[3:0] inam[0:3];
reg[3:0] inbm[0:3];
integer i;
integer fd;
always #10 clk =~ clk; initial
begin
$readmemb("x1.txt",inam);
$readmemb("y1.txt",inbm);
fd = $fopen("z1.txt");
for(i=1;i<4;i=i+1)
begin
#20 ina = inam[i];
inb = inbm[i];
$fdisplay(fd,"%b %b %b %b",ina, inb,sum,cout);
end
#20
$fdisplay(fd,"%b %b %b %b",ina, inb,sum,cout);
$fclose(fd);
end initial
begin
cin=0;
repeat(2)
#200 cin = {$random} % 16;
end adder4 adder4_0(
.clk(clk),
.sum(sum),
.cout(cout),
.ina(ina),
.inb(inb),
.cin(cin)
);
initial
begin
$monitor($time,,,"%b + %b + %b = {%b,%b}", ina, inb, cin,cout,sum);
#400 $finish;
end initial
begin
$dumpfile("dump.vcd");
$dumpvars;
end
endmodule

编译执行程序后,可以得到z1.txt

1111 1101 xxxx x

1010 0101 1100 1

1110 1010 1111 0

0001 0001 1000 1

0001 0001 0010 0

我们也可以把两个输入数据合在一个文件xy.txt里面,

1111 1101
1010 0101
1110 1010
0001 0001

用fscanf来读入激励数据,代码如下:

`timescale 1ns/1ns
`include "adder4.v" module adder_rw_tb;
reg[3:0] ina,inb;
reg cin;
reg clk = 0;
wire[3:0] sum;
wire cout;
reg[3:0] inam[0:3];
reg[3:0] inbm[0:3];
integer i;
integer fd,fd_r;
always #10 clk =~ clk; initial
begin
fd_r = $fopen("xy.txt","r");
fd = $fopen("z.txt","w");
for(i=0;i<4;i=i+1)
begin
#20
$fscanf(fd_r, "%d %d",ina,inb);
$fwrite(fd,"%b %b %b %b\n",ina, inb,sum,cout);
end
#20
$fwrite(fd,"%b %b %b %b\n",ina, inb,sum,cout);
$fclose(fd);
end initial
begin
cin=0;
repeat(2)
#200 cin = {$random} % 16;
end adder4 adder4_0(
.clk(clk),
.sum(sum),
.cout(cout),
.ina(ina),
.inb(inb),
.cin(cin)
);
initial
begin
$monitor($time,,,"%b + %b + %b = {%b,%b}", ina, inb, cin,cout,sum);
#400 $finish;
end initial
begin
$dumpfile("dump.vcd");
$dumpvars;
end
endmodule

Chronologic VCS simulator copyright 1991-2017

Contains Synopsys proprietary information.

Compiler version M-2017.03-SP2-11; Runtime version M-2017.03-SP2-11;  Dec 21 20:13 2018
                    0  xxxx + xxxx + 0 = {x,xxxx}
                   20  0111 + 1101 + 0 = {x,xxxx}
                   30  0111 + 1101 + 0 = {1,0100}
                   40  0010 + 0101 + 0 = {1,0100}
                   50  0010 + 0101 + 0 = {0,0111}
                   60  0110 + 0010 + 0 = {0,0111}
                   70  0110 + 0010 + 0 = {0,1000}
                   80  0001 + 0001 + 0 = {0,1000}
                   90  0001 + 0001 + 0 = {0,0010}

写入z.txt数据为:

0111 1101 xxxx x

0010 0101 0100 1

0110 0010 0111 0

0001 0001 1000 0

0001 0001 0010 0

在testbench从文件读入激励的更多相关文章

  1. VHDL TestBench基础(转)

    TestBench的主要目标是: 实例化DUT-Design Under Test 为DUT产生激励波形 产生参考输出,并将DUT的输出与参考输出进行比较 提供测试通过或失败的指示 TestBench ...

  2. curl 命令 从文件读取参数

    -d @filename 从文件读入内容-d @- 从stdin读入内容 -x localhost:8888   加上fiddler代理 一个sample curl -K api.conf -d @b ...

  3. Testbench学习笔记

    Testbench学习笔记(一) 书写testbench是数字电路设计中不可或缺的一项设计方法,主要是提供的是激励.尽管现在各种开发工具都通过绘制波形图的方法生成测试激励,测试书写的代码,但是其不可移 ...

  4. 基于UVM的verilog验证

    Abstract 本文介绍UVM框架,并以crc7为例进行UVM的验证,最后指出常见的UVM验证开发有哪些坑,以及怎么避免. Introduction 本例使用环境:ModelSim 10.2c,UV ...

  5. 基于UVM的verilog验证(转)

    reference:https://www.cnblogs.com/bettty/p/5285785.html Abstract 本文介绍UVM框架,并以crc7为例进行UVM的验证,最后指出常见的U ...

  6. Windows API 函数列表 附帮助手册

    所有Windows API函数列表,为了方便查询,也为了大家查找,所以整理一下贡献出来了. 帮助手册:700多个Windows API的函数手册 免费下载 API之网络函数 API之消息函数 API之 ...

  7. 我的SQL总结---未完待续

    我的SQL总结---未完待续 版权声明:本文为博主原创文章,未经博主允许不得转载. 总结: 主要的SQL 语句: 数据操作(select, insert, delete, update) 访问控制(g ...

  8. 使用Microsoft Roslyn提取C#和VB.NET源代码中的字符串常量

    Microsoft Roslyn是微软.NET“编译器即服务(Compiler as a Service)”的主要产品,它提供了开放的编译器API,并为源代码产生.分析和重构提供了新一代的语言对象模型 ...

  9. awk sed 总结

    Awk总结笔记 介绍 90年代 new awk :nawk Linux 的是gawk 我们简化awk 用法 #  awk [options ] ‘scripts’ file1 file2 .... # ...

随机推荐

  1. 图片圆角显示与手机版文章页面CSS布局

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 研发环境 chrome谷歌浏览器和firefox火狐浏览器解决跨域问题

    1 火狐浏览器 (1).先在地址栏输入about:config,然后单击“我了解此风险”. (2).找到security.fileuri.strict_origin_policy,然后在值下面的tru ...

  3. 学习MVC之租房网站(十二)-缓存和静态页面

    在上一篇<学习MVC之租房网站(十一)-定时任务和云存储>学习了Quartz的使用.发邮件,并将通过UEditor上传的图片保存到云存储.在项目的最后,再学习优化网站性能的一些技术:缓存和 ...

  4. Oracle表字段的增删改和重命名

    增加字段语法:alter table tablename add (column datatype [default value][null/not null],….); 说明:alter table ...

  5. oracle instr函数(oracle 用instr 来代替 like)

    oracle instr函数 对于instr函数,我们经常这样使用:从一个字符串中查找指定子串的位置.例如: SQL> select instr('Oracle','or') position ...

  6. 从零自学Java-7.使用数组存储信息

    1.创建数组: 2.设置数组的大小: 3.为数组元素赋值: 4.修改数组中的信息: 5.创建多维数组: 6.数组排序. 程序SpaceRemover:显示输入字符串,并将其中所有的空格字符替换为句点字 ...

  7. Oracle EBS INV 获取现有量等值

    DECLARE L_api_return_status VARCHAR2(1); l_qty_oh NUMBER; l_qty_res_oh NUMBER; l_qty_res NUMBER; l_q ...

  8. xp_readerrorlog与sp_readerrorlog

    SQL SERVER 可以使用xp_readerrorlog 或者sp_readerrorlog来查看错误日志. xp_readerrorlog 一共有七个参数: 1. 存档编号 2. 日志类型(1为 ...

  9. spring-bean 版本的问题(报错:org.xml.sax.SAXParseException; lineNumber: 14; columnNumber: 75;)

    当XML中配置的xsd是4.0,而引用的包是4以下的spring-bean.jar时,当服务器能连网时没问题,不能连网时,就报以下类似错误: org.xml.sax.SAXParseException ...

  10. 第 14 章 结构和其他数据形式(enum枚举)

    /*----------------------------- enum.c -- 使用枚举类型的值 -----------------------------*/ #include <stdi ...