## Verilog TUTORIAL | Verification

If our code is correct it will produce the result according to the following table

Table: A one bit comparator

 Input x Input y Output z 0 0 1 0 1 0 1 0 0 1 1 1

Every verilog implementation goes through extensive verification. How do we verify that the circuit behaves as expected ? We basically provide stimulus to the circuit at its input port and check its output. We change the input and check the output again. We continue doing it till we exhaust all possible inputs. If output under all conditions is as expected, the circuit stands verified.

Here is how a stimulus verification code looks like

 `// referencedesigner.com verilog tutorial// testbench for comparator module`timescale 1ns / 1psmodule stimulus; // Inputs reg x; reg y; // Outputs wire z; // Instantiate the Unit Under Test (UUT) comparator uut ( .x(x), .y(y), .z(z) );  initial begin // Initialize Inputs x = 0; y = 0;  #20 x = 1; #20 y = 1; #20 y = 0; #20 x = 1; #40; end   initial begin \$monitor("x=%d,y=%d,z=%d \n",x,y,z); end endmodule  `

We will try to make you understand the code. The code

 ``timescale 1ns / 1ps`

defines the timescale. It tells that the simulation will run in steps of 1ns and has a precision value of 1ps. We will come back to it later.

We need a way for the stimulus to simulate the input and the output pins of the comparator module. We do this by defining two register variables x and y and a wire variable z as in

 ```// Inputs reg x; reg y; // Outputs wire z;```

We will discuss more about registers and wires later. The next statement creates an instance of the comparator module.

 ```comparator uut ( .x(x), .y(y), .z(z) );```

The following section of the code defines the values of the input at different interval of time.

 ```initial begin // Initialize Inputs x = 0; y = 0;   #20 x = 1; #20 y = 1; #20 y = 1; #20 x = 0; #40 end```

Initially the value of x and y are both 0. At t = 50 ns , we assign the value of x as 1. So at t = 50 ns we have x =1 and y = 0. At t = 60 we assign the value of as 1. So at t = 60 we have x =1 and y =1. So and and so forth. At each step, the value of the out z changes in accordance with the design and this is what we need to verify.

The following code monitor the values of x, y and z at EACH change.

 ```initial begin \$monitor("x=%d,y=%d,z=%d \n",x,y,z); end```

It will basically display the values of x, y and z as follows.
``````
x=0,y=0,z=1
x=1,y=0,z=0
x=1,y=1,z=1
x=0,y=1,z=0
```
```

In the next page we will see how to test this code using the simulation with Icarus tool.