Testbench
As we have seen before, HDL will code be divided into synthesizable and testbench. A testbench is an HDL module that is used to test another module, called the device under test (DUT).
HDL example 4.38 shows how to write a self-checking testbench,
module testbench2();
logic a, b, c, y;
// instantiate device under test
sillyfunction dut(a, b, c, y);
// apply inputs one at a time
// checking results
initial begin
a = 0; b = 0; c = 0; #10;
assert (y === 1) else $error("000 failed.");
c = 1; #10;
assert (y === 0) else $error("001 failed.");
b = 1; c = 0; #10;
assert (y === 0) else $error("010 failed.");
c = 1; #10;
assert (y === 0) else $error("011 failed.");
a = 1; b = 0; c = 0; #10;
assert (y === 1) else $error("100 failed.");
c = 1; #10;
assert (y === 1) else $error("101 failed.");
b = 1; c = 0; #10;
assert (y === 0) else $error("110 failed.");
c = 1; #10;
assert (y === 0) else $error("111 failed.");
end
endmoduleCode Explanation
The
initialstatement executes the statements in its body at the start of simulation.initialstatement should be used only in testbenches for simulation, not in modules intended to be synthesized into actual hardware.The blocking statements (
=) and delays (#) are used to apply the inputs in the appropriate order.Testbenches uses
===and!==(instead of==and!=) for comparisons of equality and inequality, respectively, because these operators work correctly with operands that could bexorz.
More on Delay
What does delay actually mean? In essence, during the delay time, all signal values in the block where delay (#) is used remain unchanged, so in the waveform viewer it looks flat (no change), unless there are other blocks which has some signal assignments.
For example,
So basically, at time t=0, a=0 and b=1. Then from time t=0 to t=10, because of delay, nothing happens and signals stay same as t=0. Similar to the remaining, and the waveform looks like below,

However, the delay (#) only applies to the block the delay statement is in, if we have another block, for example, an always block to generate the clock signal, that block won't be affected by the delay in the initial block.
And thus, the waveform will look like as follows,

Last updated