1
\$\begingroup\$

This is my first attempt at learning Verilog HDL testbench for an AND gate:

 '
 ' 
initial
 begin
 //case 0
 A_t <= 0; B_t <= 0;
 #1 $display("F_t = %b", F_t);
 //case 1
 A_t <= 0; B_t <= 1;
 #1 $display("F_t = %b", F_t);
 //case 2
 A_t <= 1; B_t <= 0;
 #1 $display("F_t = %b", F_t);
 // case 3
 A_t <= 1; B_t <= 1;
 #1 $display("F_t = %b", F_t);
 end
endmodule

My question is since this is for a two input we had only four test cases, lets say we have 2000 cases, then can we use a for loop as shown below:

 '
 '
initial
 begin
 for (i=0;i<2000;i++)
{
 for (j=0;j<2000;j++)
 {
A_t <= i; B_t <= j;
 #1 $display("F_t = %b", F_t);
 }
}
 end
 endmodule 

Is this legally correct to use loops like this? If not then please suggest me the correct method for automating the inputs.

asked Dec 17, 2012 at 9:02
\$\endgroup\$

3 Answers 3

2
\$\begingroup\$

'for' loops exist in verilog, but they look like this:

for(i = 0; i < 2000; i = i + 1) begin
 A_t <= i;
 #1 $display("F_t = %b", F_t);
end

There is no ++ operator and you have to use begin and end.

It's a very reasonable way of automating the inputs. Of course you have to get the timing right, which in your case is done with that #1 there.

answered Dec 17, 2012 at 10:13
\$\endgroup\$
0
\$\begingroup\$

It is a quite good approach since the testbench isn't supposed to be synthesised. (Small syntactic changes still has to be made as suggested by the previous answer)

You can also extend the testbench by having the test vectors in an external file, that would make it more flexible.

Another solution would be to use do-files for modelsim and generate stimuli that way.

answered Dec 17, 2012 at 10:14
\$\endgroup\$
0
\$\begingroup\$

Easier way to apply all combinations is to use concatenation operator instead of nested for loops. It looks like this,

integer i; //Represents range of input values
for(i = 0; i < 2000; i = i + 1)
begin
{A_t, B_t} = i; //Nonblocking assignment "<=" is non needed here.
$display($time, "A = %b, B = %b", A_t, B_t);
#1;
end
answered Oct 26, 2015 at 5:37
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.