在验证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",i