Mình đang mới học viết testbench cho verilog bằng Modelsim và có dính đến lệnh Readmemb, mọi người cho mình hỏi lệnh này cách sử dụng như thế nào? Ví dụ như đoạn chương trình sau thì file "example.txt" mình đặt nó ở đâu? Có sai gì thì nhờ mọi người chỉ dùm
Code module
Code testbench
File example.txt
Code module
module example(a, b, c, y);
input a, b, c;
output y;
assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c;
endmodule
input a, b, c;
output y;
assign y = ~a & ~b & ~c | a & ~b & ~c | a & ~b & c;
endmodule
module testbench3();
reg clk, reset;
reg a, b, c, yexpected;
wire y;
reg [31:0] vectornum, errors;
reg [3:0] testvectors[10000:0];
// instantiate device under test
example DUT(.a(a), .b(b), .c(c), .y(y));
// generate clock
always #5 clock = ~clock;
// at start of test, load vectors
// and pulse reset
initial
begin
$readmemb("example.txt", testvectors);
vectornum = 0; errors = 0;
reset = 1; #27; reset = 0;
end
// apply test vectors on rising edge of clk
always @(posedge clk)
begin
#1; {a, b, c, yexpected} = testvectors[vectornum];
end
// check results on falling edge of clk
always @(negedge clk)
if (~reset) begin // skip cycles during reset
if (y !== yexpected) begin // check result
$display("Error: inputs = %b",
{a, b, c});
$display(" outputs = %b (%b expected)",
y, yexpected);
errors = errors + 1;
end
vectornum = vectornum + 1;
if (testvectors[vectornum] === 4'bx) begin
$display("%d tests completed with %d errors",
vectornum, errors);
//$finish;
end
end
endmodule
reg clk, reset;
reg a, b, c, yexpected;
wire y;
reg [31:0] vectornum, errors;
reg [3:0] testvectors[10000:0];
// instantiate device under test
example DUT(.a(a), .b(b), .c(c), .y(y));
// generate clock
always #5 clock = ~clock;
// at start of test, load vectors
// and pulse reset
initial
begin
$readmemb("example.txt", testvectors);
vectornum = 0; errors = 0;
reset = 1; #27; reset = 0;
end
// apply test vectors on rising edge of clk
always @(posedge clk)
begin
#1; {a, b, c, yexpected} = testvectors[vectornum];
end
// check results on falling edge of clk
always @(negedge clk)
if (~reset) begin // skip cycles during reset
if (y !== yexpected) begin // check result
$display("Error: inputs = %b",
{a, b, c});
$display(" outputs = %b (%b expected)",
y, yexpected);
errors = errors + 1;
end
vectornum = vectornum + 1;
if (testvectors[vectornum] === 4'bx) begin
$display("%d tests completed with %d errors",
vectornum, errors);
//$finish;
end
end
endmodule
000_1
001_0
010_0
011_0
100_1
101_1
110_0
111_0
001_0
010_0
011_0
100_1
101_1
110_0
111_0
Comment