Google+ VLSI QnA: $strobe
Showing posts with label $strobe. Show all posts
Showing posts with label $strobe. Show all posts

Friday, 23 May 2014

Interview Questions on Blocking and Nonblocking Assignments

This post is continuation to our previous post on blocking and non-blocking assignments. For better understanding of how the blocking and nonblocking assignments are scheduled in Verilog, please go through this post.

Q.1) What will be the output of following code?

         module seq;
         reg clk, rst, d;
         initial
         begin
               $monitor("%g clk = %b rst = %b d = %b", $time, clk, rst, d);
               #1   clk = 0;
               #10 rst = 0;
               #5   d = 0;
               #10 $finish;
          end
          endmodule

Answer) 0     clk = x   rst = x   d = x
              1     clk = 0   rst = x   d = x
              11   clk = 0   rst = 0   d = x
              16   clk = 0   rst = 0   d = 0

Q.2) What will be the output of following code?

         module parallel;
         reg clk, rst, d;
         initial
         begin
             $monitor("%g clk = %b rst = %b d = %b", $time, clk, rst, d);
             fork
                   #1   clk = 0;
                   #10  rst = 0;
                   #5   d = 0;
             join
             #1 display("%t Terminating simulation", $time);
         end
         endmodule
(Note : fork-join block causes the statements to be evaluated in parallel, i.e. all at the same time.)

Answer) 0     clk = x    rst = x   d = x
              1     clk = 0    rst = x   d = x
              5     clk = 0    rst = x   d = 0
             10    clk = 0    rst = 0   d = 0
             11    Terminating simulation

Q.3) What will be the output of the following code ?

         blocking                                                                          nonblocking
         always @(i1 or i2)                                                          always @(i1 or i2)
         begin                                                                               begin
                i1 = 1;                                                                              i1 = 1;
                i2 = 2;                                                                              i2 = 2;
                #10;                                                                                 #10;
                i1 = i2;                                                                             i1  <= i2;
                i2 = i1;                                                                             i2  <= i1;
         end                                                                                  end
                      (a)                                                                                   (b)

Answer) In the case of (a), i.e. blocking the values of i1 and i2 will be both '2', whereas in the case of (b) (nonblocking) the values of i1 and i2 will be '2' and '1' respectively.
    

Q.4) What will be the output of the following code ?

         module tp;
                reg i1;
                initial 
                      $monitor("\$monitor: i1 = %b", i1);
                initial 
                begin
                       $strobe ("\$strobe : i1 = %b", i1);
                       i1 = 0;
                       i1 <= 1;
                       $display ("\$display: i1 = %b", i1);
                      #1 $finish;
                end
         endmodule

Answer) $display: i1 = 0
              $monitor: i1 = 1
              $strobe : i1 = 1

Q.5) What will be the output of the following code?

         module tp;
                  reg i1, i2;
                  initial 
                  begin
                           i1 = 0;
                           i2 = 1;
                           i1 <= i2;
                           i2 <= i1;
                           $monitor ("%0dns: \$monitor: i1=%b i2=%b", $stime, i1, i2);
                           $display ("%0dns: \$display: i1=%b i2=%b", $stime, i1, i2);
                           $strobe ("%0dns: \$strobe : i1=%b i2=%b\n", $stime, i1, i2);
                    #0   $display ("%0dns: #0 : i1=%b i2=%b", $stime, i1, i2);
                    #1   $monitor ("%0dns: \$monitor: i1=%b i2=%b", $stime, i1, i2);
                           $display ("%0dns: \$display: i1=%b i2=%b", $stime, i1, i2);
                           $strobe ("%0dns: \$strobe : i1=%b i2=%b\n", $stime, i1, i2);
                           $display ("%0dns: #0 : i1=%b i2=%b", $stime, i1, i2);
                     #1  $finish;
                  end
         endmodule

Answer) 0ns: $display: i1=0 i2=1
              0ns: #0 : i1=0 i2=1
              0ns: $monitor: i1=1 i2=0
              0ns: $strobe : i1=1 i2=0
              1ns: $display: i1=1 i2=0
              1ns: #0 : i1=1 i2=0
              1ns: $monitor: i1=1 i2=0
              1ns: $strobe : i1=1 i2=0

In case of any doubt regarding the above solutions, feel free to leave a comment.

Thursday, 24 April 2014

Verilog Interview Questions - v1.2

Q.1) Explain $display, $monitor and $strobe?

Answer) All these commands have the same syntax.
$display :- The $display displays once the value of parameters, when it is executed.
$strobe :- The $strobe displays the parameter at the very end of the current simulation time.
$monitor :- The $monitor displays once every time one of it's parameters changes.

The format string is same as that of C/C++.
Format characters include :
Decimal              :- %d
Hexadecimal      :- %h
Binary                :- %b
Character           :- %c
String                 :- %s
Time                   :- %t
Hierarchy Level  :- %m

Syntax :-
$display("format_string", val1,  val2,....)
$strobe("format_string", val1, val2, ...)
$monitor("format_string", val1, val2,.....)

Q.2) What is the difference between:
         y = (sel)? i1 : i2;       and                if(sel) y = i1;
                                                                  else    y = i2;

Answer) The ternary operator "?" , merges the answer.
              Example : sel = 2'bx1; i1 = 2'b00; i0 = 2'b10;
                              y = 2'bx0;

               The "if" treats "X" or "Z" as false values, so the answer always be in false condition, i.e. i2.

Q.3) What is the difference between equality operator (==) and case equality operator(===)?

Answer) The equality operator (==) can give the output 0,1 or X.
               Case equality operator (===) only gives the output TRUE (1) or FALSE (0).
               If I1 = 3'bx01 ; I2 = 3'b1x0;
               I1 == I2 gives 'X';
               I1 === I2 gives '0';

Q.4) Explain the difference between task and function.

Answer)
Difference between task and function

Q.5) Write a verilog code to swap contents of two registers with and without a temporary register.

Answer) With temporary register :
               always@(posedge clock)
                begin
                    temp = b;
                    b = a;
                    a = temp;
                end

                Without temporary register :
                 always @(posedge clock)
                 begin
                    a <= b;
                    b <= a;
                 end