Skip to content

Interview question and answers

Mahadevaswamy B N edited this page Mar 7, 2025 · 5 revisions

Interview question and answers cheat sheet

sr. no. Interview question and answers
1. Macros and Parameters
2. Data Type Interview QA
3. Choosing an array Interview QA
4. Control flow Interview QA
5. Processes Interview QA
6. Interface Interview QA

1.Differences between macros and parameters

Macros Parameters
macros are replaceable parameters are like variables
macros works in pre-compilation state parameters works in Elaboration state
syntax : `define macro_name value syntax : parameter parameter_name=value
We can use `define in any file we can use parameter within the file
we can't give datatypes in macros we can use and change datatype in parameters
Macros can have multiple lines multiple lines cannot be possible here because parameter is just like declaring variables
We can give value for a macro in command line parameters value can't be changed in command line

Execution stage of macros and parameters

Untitled Diagram drawio (44)

Parameters used in macros

parameter data = 5; // data will be replaced by value 5 in Elaboration
`define  DATA data // in pre-compilation `DATA will be replaced with data
module tb();
  int a,b;
  initial begin
   
    $display("DATA=%0d",`DATA);
    
     
     b= `DATA + 2;  

    $display(" b=%0d",b);
  end 
endmodule
  • Output Snap
    The below output, fig -1 shows The macro DATA is replaced with the text data during preprocessing, and the parameter data is evaluated to 5 during elaboration, The output shows that the macro DATA resolves to the parameter data, which has the value 5. The calculation b = DATA + 2results inb = 7`.

image

                         fig -1 Output - Parameters used in macros  

Github lab link: https://github.com/mbits-mirafra/SystemVerilogCourse/blob/production/macros_and_parameters/macros/parameters_used_macros/parameters_used_macros.sv
Github lab link: https://github.com/mbits-mirafra/SystemVerilogCourse/blob/production/macros_and_parameters/macros/parameters_used_macros/parameters_used_macros.log


Macros used in parameters

`define DATA 5  

parameter data = `DATA;  

module tb();
  int a, b; 

  initial begin
    $display("data=%0d", data); 
    $display("DATA=%0d", `DATA); 

    a = data + 5;  
    b = `DATA + 2; 

    $display("a=%0d b=%0d", a, b); 
  end
endmodule 
  • Output Snap
    The below output, fig -2 shows The macro (DATA) is replaced by 5 before compilation like a pre-compilation stage. The parameter (data) is also assigned the value 5, which is resolved during elaboration. The values are printed using $display and arithmetic operations are performed.

image

                         fig -2 Output - macros used Parameters  

Github lab link: https://github.com/mbits-mirafra/SystemVerilogCourse/blob/production/macros_and_parameters/parameters/macros_used_parameters/macros_used_parameters.sv
Github lab link: https://github.com/mbits-mirafra/SystemVerilogCourse/blob/production/macros_and_parameters/parameters/macros_used_parameters/macros_used_parameters.log


Macros(`define) can be used in command line

 
-timescale 1ns/1ns +define+name=20
`define name 10

module hi;
  int a;

  initial begin
    $display("[%0t] a = %0d", $time, a);
    #1 a = `name;
    $display("[%0t] a = %0d", $time, a);
  end
endmodule
  • Output Snap
    The below output, fig -3 shows We can give inputs to macros in the command line interface using -timescale 1ns/1ns +define+name=20 in the compile options.

image

                         fig -3 Output - macros in command line  

Github lab link: https://github.com/mbits-mirafra/SystemVerilogCourse/blob/production/macros_and_parameters/macros/macros_commandline/macros_commandline.sv
Github lab link: https:https://github.com/mbits-mirafra/SystemVerilogCourse/blob/production/macros_and_parameters/macros/macros_commandline/macros_commandline.log

image

vlog +define+name=20 -sv macros_commandline.sv
using this command we do the override the macro values from the command line like 10 to 20. You can see my output below; 20 will be there.

image

                         fig -4 Output - after override from the command line

2. Data type Interview questions

1.Is there any difference between reg and logic in SystemVerilog?

Answer:
Before system verilog ,verilog is mainly used for synthesis and verification purposes. In verilog there are two main data type

  1. reg
    2)net
    Reg is mainly used when we want to store values in the variable .It is mainly used inside procedural block .mainly it is used for designing of sequential circuit .net variable are mainly continuously driven .In net type variable it is not possible to store value .

So ,there is lots of confusion for designer ,where to use reg and where to use net.This problem is solved in system verilog .System verilog introduced a new data type called as “logic”. This data type can be use at both the places. It can be use for net as well as reg.

2.What is the difference between logic[7:0] and byte variable in System Verilog?

Answer:

  • The 'byte' is a signed variable which means it can only be used to count values till 127.
  • A logic[7:0] variable can be used for an unsigned 8 bit variable that can count up to 255.

3.What are the difference between 2 state and 4 state?

Answer:

  • 4 state includes logic 0, logic 1, logic X where it is related to registers, in real world it is declared as 0 or 1 it is based on the simulators we're using and logic Z, which is related to wires.
  • 2 state includes of logic 1 and logic 0.

4.Difference between packed and unpacked arrays

Answer: Refer the below link
https://github.com/mbits-mirafra/SystemVerilogCourse/wiki/02.Array#static-arrays-has-two-types

5.Comparision between fixed, dynamic, associative array and queue.

Answer:

Refer the below link https://github.com/mbits-mirafra/SystemVerilogCourse/wiki/Choosing-an-array

6.Given a dynamic array of size 100, how can the array be re-sized to hold 200 elements while the lower 100 elements are preserved as original?

Answer: A dynamic array needs memory allocation using new[] to hold elements. Here is an example with an integer array that grows from an initial size of 100 elements to 200 elements. // Declare the dynamic array.

  
    // Declare the dynamic array.
    integer addr[]; 
    // Create a 100-element array.
     addr = new[100]; 
    ………
    // Double the array size, preserving previous values.
    addr = new[200](addr);

7.Difference between packed and unpacked structure

Answer:
Refer the below link
https://github.com/mbits-mirafra/SystemVerilogCourse/wiki/03.Structure-and-Union#types-of-structure

8.What happens if we give same value for different names in enumeration data type?

Answer:
code:

    
      module set_value_enum;    
      //set same value for MONDAY and TUESDAY.  
       enum {MONDAY=2, 
             TUESDAY=2, 
             WEDNESDAY=5, 
             THURSDAY, 
             FRIDAY=10, 
             SATURDAY, 
             SUNDAY }days;

output :

      Error: Enum member 'TUESDAY' does not have unique value.

9.What is the output if you declare bit as signed and assign value as 11xz11x1 ?

Answer:
code:

      module data_type_bit;   
        // declare bit signed type 8 bit variable 
        bit signed [7:0] data = 8'b11xz11x1;   

        initial begin
           $display("\nValue of bit signed data in binary = %0b",data);
           $display("Value of bit signed data in decimal = %0d\n",data);
        end
      endmodule: data_type_bit

output :

      Value of bit signed data in binary = 11001101
      Value of bit signed data in decimal = -51

In the above code, we declared bit as 11xz11x1, here bit is 2 state, so x and z are converted to 0 and it will become 11001101. Since it is signed bit it gives -51 in decimal format.

10.What is the output if you declare byte as unsigned and assign value as 11101001 ?

Answer:
code:

      module data_type_byte;   
         // declare byte unsigned type 8 bit variable 
         byte unsigned data = 8'b11101001;

         initial begin
           $display("\nValue of byte unsigned data in binary = %0b",data);
           $display("Value of byte unsigned data in decimal = %0d\n",data);
         end
      endmodule: data_type_byte

output :

      Value of byte unsigned data in binary = 11101001
      Value of byte unsigned data in decimal = 233

In the above code, we declared byte as 11101001. Since it is unsigned byte it gives 233 in decimal format.


3. Choosing an array Interview questions and answers

Fixed Arrays/Static Arrays:

1) Fixed Arrays Over All Arrays:

  • Fixed Arrays execute faster than all other types of arrays (i.e Associative arrays, dynamic arrays, queues) because it will store in uninitialized data segment (bss segment) of memory, so it will consume less simulation time to execute. And the heap is the segment where dynamic, associative and queue memory allocation takes place.
    The below figure shows the memory layout.

memory_segments

                                            Figure.1. Memory Layout
  • Size is known previously then we can choose fixed arrays over all other types of arrays.

Dynamic Arrays:

1) Dynamic Arrays Over Fixed Arrays:

  • Memories allocated at run time, so memory will not be wasted but in fixed array memories would be wasted when we don't know the actual size.
  • We can easily add elements using new() method but in fixed arrays we can't add elements after the declaration.
  • We can delete entire arrays after allocating the memories to it but in fixed arrays we can't delete the memories after declaring array.

Note: In fixed arrays, there is no methods like new(),delete().

2) Dynamic Arrays Over Associative Arrays:

  • Indexing is in continues manner but in associative arrays it's non continuous. it's show in below figure.

indexing

                             Figure.2. continuous and non continuous indexing
  • We can find relation between indexing so travelling through arrays with ease using loops but in associative arrays keys are required.

3) Dynamic Arrays Over Queues:

  • According to footprint, dynamic array executes faster and it will required less simulation time but queues will required more time to execute.

dynamic-1

                         Figure.3. dynamic array execution time

queue-1

                         Figure.4. queue execution time
  • Dynamic array used to trace the each and every element in the array using the loops but in queue with pop and push operation. we can't trace the all element of array using fooreach loop. for pop operation the size of the array is decrement and i is increment and for push operation the size of the array is increment and i is also increment it result in Killing process.

code:

      module dynamic;
        int dyn [];
        initial begin
          dyn = new[8];
          dyn = '{2,7,0,6,1,9,9,7};
          foreach(dyn[i])begin
            $display("verifying the values of each index, dyn[%0d] = %0d", i, dyn[i]);
          end
        end
      endmodule 

OUTPUT:

     run -all
     verifying the values of each index, dyn[0] = 2  
     verifying the values of each index, dyn[1] = 7  
     verifying the values of each index, dyn[2] = 0  
     verifying the values of each index, dyn[3] = 6  
     verifying the values of each index, dyn[4] = 1  
     verifying the values of each index, dyn[5] = 9  
     verifying the values of each index, dyn[6] = 9  
     verifying the values of each index, dyn[7] = 7  
     exit  

code: using pop operation

     module queue;
       int que[$];
       initial begin
         que = '{2,7,0,6,1,9,9,7};
         foreach(que[i])begin
           $display("verifying the values of each index, que[%0d] = %0d", i, que[i]);
           que.pop_back();
         end
       end 
     endmodule

OUTPUT:

     run -all  
     verifying the values of each index, que[0] = 2  
     verifying the values of each index, que[1] = 7  
     verifying the values of each index, que[2] = 0  
     verifying the values of each index, que[3] = 6  
     exit  

code: using push operation in queue

     module queue;
       int que[$];
       initial begin
         que = '{2,7,0,6,1,9,9,7};
         foreach(que[i])begin
           $display("verifying the values of each index, que[%0d] = %0d", i, que[i]);
           que.push_back(6);
         end
       end
     endmodule 

OUTPUT:

     run -all
     verifying the values of each index, que[0] = 2
     verifying the values of each index, que[1] = 7
     verifying the values of each index, que[2] = 0
     verifying the values of each index, que[3] = 6
     verifying the values of each index, que[4] = 1
     verifying the values of each index, que[5] = 9
     verifying the values of each index, que[6] = 9
     verifying the values of each index, que[7] = 7
     verifying the values of each index, que[8] = 6
     verifying the values of each index, que[9] = 6
     verifying the values of each index, que[10] = 6
     verifying the values of each index, que[11] = 6
     verifying the values of each index, que[12] = 6
     Result reached the maximum of 5000 lines. Killing process.
     Execution interrupted or reached maximum runtime.

Associative Arrays:

1) Advantages of Associative array over all arrays:

  • The function exists(input index); method is used in the associative array to find the index element where this method is not used in associative and dynamic array.

code:

    module associative;           
      int id[int];    
      int name[string];    
      int value[int];     
      initial begin    
        id = '{4275:25, 4278:12};    
        name = '{"Dilip":12};    
        value = '{50000:25};  
        //check whether the name exist  
        name.exists("Dilip");  
        $display("the element exist= %0p", name);  
      end  
    endmodule 

OUTPUT:

     run -all    
     the element exist= {"Dilip":12}   
     exit 
  • In Dynamic array & queue there is no such method like function exists(input index) to find the array element if we use function exists(input index); in dynamic array the following error is occur

Dynamic array:

code:

      module dynamic;
        int dyn [];
        initial begin
          dyn = new[8];
          dyn = '{2,7,0,6,1,9,9,7};
          dyn.exists(0);
          $display("the element of the dyn=%0p", dyn);
        end
      endmodule 

OUTPUT:

       -- Compiling module dynamic
        ** Error (suppressible): (vlog-13276) testbench.sv(7): Could not find field/method name (exists) in 'dyn' of 'dyn.exists'.  
        ** Error (suppressible): (vlog-13276) testbench.sv(7): Could not find field/method name (exists) in 'dyn' of 'dyn.exists.$0'.  
       End time: 02:35:57 on Sep 03,2022, Elapsed time: 0:00:00  
       Errors: 2, Warnings: 0  

Queue :

code:

    module queue;
      int que[$];
      initial begin
        que = '{2,7,0,6,1,9,9,7};
        que.exists(0);
        $display("the element of the que=%0p", que);
      end
    endmodule

OUTPUT:

    -- Compiling module queue    
     ** Error (suppressible): (vlog-13276) testbench.sv(6): Could not find field/method name (exists) in 'que' of 'que.exists'.  
     ** Error (suppressible): (vlog-13276) testbench.sv(6): Could not find field/method name (exists) in 'que' of 'que.exists.$0'.  
     End time: 02:43:56 on Sep 03,2022, Elapsed time: 0:00:00
     Errors: 2, Warnings: 0

2) Associative array over dynamic array and queue:

  • In Associative array the index type is of any data_type can used but while in dynamic and queue we can't use different data types for indexing.

code:

    module associative;
      int id[int];
      int name[string];
      int value[int];
      initial begin
        id = '{4275:25, 4278:12};
        name = '{"Dilip":12};
        value = '{50000:25};
        $display("The id is =  {%0p}", id);
        $display("The name is =  %0p", name);
        $display("The value is =  %0p", value);
      end 
    endmodule

OUTPUT:

     run -all  
     The id is =  {{4275:25} {4278:12} }  
     The name is =  {"Dilip":12}   
     The value is =  {50000:25}   
     exit  

3) Associative array over dynamic array and fixed array:

  • We can delete the particular index element in Associative array by using function void delete(input index); but in dynamic and fixed type we can't use function void delete(input index); to delete the particular index.

code:

    module associative;
      int id[int];
      int name[string];
      int value[int];
      initial begin
      
        id = '{4275:25, 4278:12};
        name = '{"Dilip":12};
        value = '{50000:25};
        $display("The id is =  {%0p}", id);
        $display("The name is =  %0p", name);
        $display("The value is =  %0p", value);
        $display("The size of the id is=%0d", id.size());
        id.delete(4278);
        $display("The size of 'id' after deleting = %0d", id.size());
        $display("The id is = {%0p}", id);
      end
    endmodule 

OUTPUT:

     run -all  
     The id is =  {{4275:25} {4278:12} }  
     The name is =  {"Dilip":12}   
     The value is =  {50000:25}   
     The size of the id is=2  
     The size of 'id' after deleting = 1  
     The id is = {{4275:25} }  
     exit  
  • The Associative array is memory friendly we can store the value of the array in any memory location.

Queues:

1) Advantages of Queue over all arrays

  • In queue the main advantage is push & pop operation.
  • The push used to insert the element in to queue.
  • The push as two methods function push_back(input element_t); & function push_front(input element_t);
  • The pop as two methods function pop_front(); & function pop_back();

code:

     module queue_data_type;
       string queue1[$];
       initial begin
         queue1 = {"manipal", "banglaore", "udupi"};
         $display("\nRemove the array element at first index position of queue1:%p",queue1.pop_front());
         $display("\n After removing the 'manipal' from queue1 is :%p", queue1);
         $display("\nRemove the array element at last index position of queue1: %p", queue1.pop_back());
         $display("\n After removing the 'udupi' from queue1 is :%p", queue1);
         queue1.push_front("Yelahanka");
         $display("\ninsert the array element at first index position of queue1:output");
         $display("\n queue1: %p", queue1);
         queue1.push_back("udupi");
         $display("\ninsert the array element at last index position of queue1: ouptut");
         $display("\n queue1 : %p", queue1);
       end 
     endmodule

OUTPUT:

     run -all  
     // Remove the array element at first index position of queue1:"manipal"  

     // After removing the 'manipal' from queue1 is :'{"banglaore", "udupi"}  
 
     // Remove the array element at last index position of queue1: "udupi"  
  
    // After removing the 'udupi' from queue1 is :'{"banglaore"}  
 
    // insert the array element at first index position of queue1:output  
 
    // queue1: '{"Yelahanka", "banglaore"}  
 
    // insert the array element at last index position of queue1: ouptut  

    // queue1 : '{"Yelahanka", "banglaore", "udupi"}  
    exit

2) Advantages of Queue over Dynamic & Associative Array:

  • In queue we can insert the element at any index position using the method function insert(index , queue_element);. So we can say inserting is easy. Thus the queue length increases and shrinks even if we remove or add elements from the middle, but in dynamic and associative array we can't insert the element in the array.

code:

     module queue_data_type;
       string queue1[$];
       int queue2[$];
       initial begin
         queue1 = {"banglaore", "udupi"};
         queue1.insert(0, "manipal");
         $display("Insert the array element at 0 index position of queue1: {%0p}", queue1);
       end 
     endmodule

OUTPUT:

      run -all      
      // Insert the array element at 0 index position of queue1: {{manipal} {banglaore} {udupi}}  
      exit  

4. Control Flow Interview questions and answers

1. How to call the task inside a function? giving example with code.

Answer: Normally a function can be called inside the task.
but calling the task inside the function is illegal because task has delay elements and the function does not have any delay elements but there is an exceptional case for it, by using the fork and join_none inside the function to call the task is possible.

Please refer to the below code for a better understanding.

module test;
initial begin
  void'(function_name);
end

function function_name;
  $display($time, "\t I'm in function");
  fork
    $display("\t calling task inside a function");
    task_name;
  join_none
endfunction

task task_name;
  #5 $display($time,"\t Now I'm in task");
endtask

endmodule

The below figure shows the output of calling a task inside a function.
Untitled Diagram drawio (9)

                  Figure.1. output of calling a function inside a task. 

In the above screenshot calling a task inside a function. In the initial module, function is called and printed I'm in function and called task inside fork join_none and in task printed I'm in task after 5 ns and returned back to function and then to initial block.


2. What is the output of the following snippet?

         int x=2;
         repeat(x) begin;
           x++;
           $display("The value of x is %0d", x);
         end

Answer: Repeat loop is used for repeating statements or operations for a fixed number of times.
Here initially they give the value of x is 2 and later x value changed inside repeat, In repeat once x is taken and doesn't bother if x value changes i.e., initially x value evaluates and and doesn't evaluate again.
So Statements will be repeated 2 times

Please refer to the below code for a better understanding.

   module test;    
     int x=2;      
     initial begin      
       $display("output of repeat(x=2) is ");     
       repeat(x)     
       begin    
         $display("The value of x is %0d", x);   
         x++;  
       end    
     end  
   endmodule

The below figure shows the output of the Repeat loop.
Untitled Diagram drawio (4)

                      Figure.2. the output of the Repeat loop. 

3. What is a dead code in System Verilog?
Answer: Dead code is a piece of code that doesn't run at all & which doesn't affect your simulation or synthesis.
The code which has code coverage of 0% i.e., the final product chip there will be some hardware generated in the chip which has never been used in the lifetime.
Examples of dead code are signals that are never used or conditions that are never triggered in code.


4. Can we use a local variable of the foreach loop outside of the loop as shown in following snippet ? why?

   int array[5]  
   foreach(array[i]) begin  
     array[i]=i;  
     $display("\tarray[%0d]=%0d",i,array[i]);  
   end  
   $display("value of i out of loop: %0d",i);
   $display(" out of loop ");  

Answer : No

In the foreach loop we can use some random variable let's say i as a variable and i variable can't be used outside the inside module because i refers to only foreach block itself (using the scope resolution operator is also not possible). It will show the error as shown in the following fig.

for error1


5.How to use the forever loop using for loop ?
Normally As the name says forever loop will execute the statements inside the loop forever.
It can be said as infinite iterations. so if we use that simulator will hang.
But there are 2 ways to stop the forever loop.

  1. using if conditions and break statements inside the for a loop.
  2. $finish.
  • using if conditions and break statements to terminate the loop
                        module forever_for;  
                          initial begin  
                            $display("-----forever loop output ---");  
                            for (int i=0;1;i++) begin  
                              #4  $display("\t value of i = %0d", i);  
                              if(i==5)  
                              break;  
                            end 
                          end    
                        endmodule    

In the above code we use the for loop syntax to do the forever loop and inside the for loop i=0;1;i++ The condition is 1 which means true all the time. so for loop never terminate means for is acting as forever loop. so to stop it we are using the if condition and break statements in if condition when i=5 then break so it will work from 0 to 5 iterations and then exits the loop.

  • using $finish to terminate the forever loop
module forever_for;
  initial begin
    $display("-----forever loop output ---");
    for (int i = 0; 1; i++) begin
      #4 $display("\t value of i = %0d", i);
    end
  end

  initial begin
    #21 $finish;
  end
endmodule

Here also the same example as we used in the previous example but here we are using a $finish statement to terminate the loop.

The below figure shows the output of the forever loop
Untitled Diagram drawio (6)

                      Figure.3. the output of the forever loop.  

6.What is difference between pre-increment(++i) and post-increment(i++) in SV ?
Answer :
let's say we are assigning a variable j with the use of i variable incrementing by post and pre-increment methods.

In pre-increment(++i) will increment the value of i and then the assignment will happen to the LHS i.e., to j.

In post-increment(i++) will increment the value of i and after the assignment to LHS.

for a better understanding look at the below code

  • pre-increment code
module pre_increment;
 int i = 1;
 int j;

 initial begin
   $display("\t the value of i is %0d", i);
   $display("\t the value of j is %0d", j);
   $display("\t assigning i value to j");
   j = ++i;
   $display("\t after pre-increment the value j is %0d", j);
 end
endmodule

In the above code, i value is initially 1 and we assign i value to j with pre-incrementing (++i).

  • post increment code
module post_pre;
  int i = 1;
  int j;

  initial begin
    $display("\t the value of i is %0d", i);
    $display("\t the value of j is %0d", j);
    $display("\t assigning i value to j");
    j = i++;
    $display("\t after post-increment the value j is %0d", j);
  end
endmodule

In the above code, i value is initially 1 and we assign i value to j with post-incrementing (i++);

The below figure shows the output of both pre-increment and post-increment

post pre

                 Figure.4. the output of both pre-increment and post-increment.  

7. How will the break keyword work inside the repeat loop?

Answer :
we use the break keyword inside the repeat loop it will terminate the loop. and based on certain conditions.

for a better understanding look at the below code

module repeat_break;
  int b;

  initial begin
    repeat (5) begin
      $display("\tValue of b = %0d", b);
      b++;
      if (b == 3)
        break;
    end
  end
endmodule

In the above code is repeat loop with a break keyword, we are declaring the repeat loop it will be executed 5 times but using the break keyword to terminate the loop in a 3rd iteration and comes out of the repeat loop completely.

The below figure shows the output of the repeat loop with the break keyword.
Untitled Diagram drawio (8)

                            Figure.5. the output of the repeat loop with the break keyword. 

8.what is the difference between If and unique if statements?
Answer :

SL.No condition if unique if
1. only one conditional expression is true execute the set of statements inside the true conditional block execute the set of statements inside the true conditional block
2. more than one conditional expression is true executes the first true conditional block statements executes the first true conditional block statements and also displays a warning that multiple conditions were true which leads to a dead code
3. none of the conditional expressions is true without else comes out of the if-else block comes out of the unique if block and shows a warning that none of the conditions is true

9. what is the difference between If and priority if statements?
Answer :

SL.No condition if priority if
1. only one conditional expression is true execute the set of statements inside the true conditional block execute the set of statements inside the true conditional block
2. more than one conditional expression is true executes the first true conditional block statements, no warning display executes the first true conditional block statements, no warning display
3. none of the conditional expressions is true without else execute the statements out from the conditional block, no warning display execute the statements out from the conditional block, a warning display

5. Processes Interview Questions and answers

1.Difference between fork_join,fork_join_any and fork_join_none ?

fork_join fork_join_any fok_join_none
In fork_join the main(parent) threads will execute after all the threads(child threads) in the fork_join is executed In fork_join_any the main(parent) threads executes if any one of the child threads excecutes In fork_join_none child threads and main(parent) threads are executed parallely
fork-join  fork_join_any fork-join_none

2.Can we use wait_fork in fork_join ?

We know that in fork_join the main thread is executed only when all the threads in fork_join is executed so here no need of using wait_fork. We can use wait fork after the fork_join_any or fork_join_none statement to wait for all the threads in the fork-join_any or fork_join_none to complete. So here in fork_join wait_fork is not required.

3.Difference between Blocking and Non-Blocking Assignments ?

blocking Non-Blocking
In Blocking Assignment one statement is executed then the next statement will executed i.e first expression of right hand side is evaluated and immediately assigned to the left hand side variable In Non-Blocking assignment evaluated all the right hand side expression for the current time unit and assigned to left-hand side variable at the end of the time unit
Represented by " = " Represented by " <= "
It executes sequentially It executes parallely
Blocking is used in combinational Non-Blocking is used in sequential
bloc non-b

4.Difference between wait event and @ event ?

If we triggered wait and @ in the same delay then wait statement is executed because the wait catches fast then @ event.

Example:

    module tb;  
      event e;  
      initial begin  
        #20 ->e;  
        $display($time,"thread1");  
      end  
      initial begin  
        #20 @e;  
        $display($time,"thread2");  
      end  
      initial begin  
        #20 wait(e.triggered);  
        $display($time,"thread3");  
      end 
    endmodule   

In the above example we can see that we have same delay for the event,wait and @ but after executing the event wait is executed eventhough we declared '@' in between, its because wait catches the fast then the @ so as a output we can only see thread1 and thread3.

Untitled Diagram drawio (6)

5.Can we execute wait and @ with different delays ?

    module tb;    
      event e;  
      initial begin  
        #20 ->e;  
        $display($time,"thread1");  
      end  
      initial begin  
        #25 @e;  
        $display($time,"thread2");  
      end  
      initial begin  
        #15 wait(e.triggered);  
        $display($time,"thread3");  
      end 
    endmodule  

In the above example we can see that delays are different for event ,wait and @.We can also see that here @ delay is more than the event delay and wait delay is less than the event delay so here it executes only the wait statement with the event delay.So in the below figure we can see that thread 1 and thread 3 is executed with same delay(#20)

Untitled Diagram drawio (7)


6. Interface Interview Questions and answers

1. How does the clocking block handles synchronous reset?

Solution:

Synchronous reset is sampled only with respect to clock event. When reset is enabled, it will be effective only after next active clock edge.

     clockingblock  TB_CB @(posedge clk)   
     default input #1step output #0;   
       input rst;
       output a,b;
       input y;

Note: The clocking block is only designed to handle synchronous reset - it should happen only be based on clock event. Reset in clocking block should be handled elsewhere. If the clocking block outputs are variables, you can procedurally assign them outside of a clocking block on a reset.


2. How does the modport handle asynchronous reset?

Solution:

Asynchronous Reset

In asynchronous reset sampling is done by independent of clk. When reset is enabled ,it will be effective immediately within the same the clock edges.

image

                                         Fig 1: Design example   

Here, in this example there are three signals a,b and c. 'a' is continuous signal and asynchronous. 'b' and 'c' are synchronous signals. Using modport we can change the asynchronous signal 'a' to synchronous signal.
modport TB_MP(TB_CB , output a);

Note: In Modport, the design can handled with asynchronous reset. The asynchronous reset will be happen at any time.


3.What is synchronous and asynchronous reset?

Solution:

Synchronous reset means reset is sampled with respect to clock. In other words, when reset is enabled, it will not be effective till the next active clock edge.

  module synchronous_reset_test (input logic clk, reset, in1, output logic out1)  
    always @(posedge clk)  
    if(reset) 
      out1 <= 1'b0;  
    else out1 <= in1;   
  endmodule 

The out1 will be changed only with the posedge of clk. To get the effect of reset, reset should be wide enough to be captured by the next posedge of clk.

image

                                               Fig 2: Synchronous Reset  

Asynchronous Reset

In asynchronous reset, reset is sampled independent of clk. That means, when reset is enabled it will be effective immediately and will not check or wait for the clock edges.

module asynchronous_reset (input logic clk, reset, in1, output logic out1);  
  always @(posedge clk or posedge reset)  
  if(reset) 
    out1 <= 1'b0;  
  else 
    out1 <= in1;  
endmodule  

image

                                                  Fig 3: Asynchronous Reset     

4.What is the usage of blocking and non blocking assignments in sampling and driving signals?

Solution:

The blocking assignment (=) used for sampling signals in active region.
The non blocking assignment (<=)used for driving signals in active-NBA region.
So we can avoid the race condition in verilog.


5. Access restriction of signals using modport

Solution:

Code Snippet:

interface.sv file:

    // interface defination for and gate
      interface and_intr;
      //input and output signals declaration for design
        logic p,q;
        logic r;
      // modport declaration for design file
        modport DUT_MP(input q,output r);
      // modport declaration for testbench file
        modport TB_MP(output p,output q,input r);
     endinterface : and_intr

tb.sv file:

   // testbench file for and gate design
   // module defination for testbench with interface instanciation
      module tb(and_intr inf);
  
        initial begin
          $display("// and gate output using modports\n");
            repeat(5) begin
              inf.TB_MP.p = $random;
              #1;
              inf.TB_MP.q = $random;
              #1;
              $display("input_p=%b\t input_q=%b\t output_r=%b",inf.TB_MP.p,inf.TB_MP.q,inf.TB_MP.r);
            end
          end
      endmodule : tb

design.sv file:

    // and gate design file
    // module defination for and gate with interface instanciation
       module and_gate(and_intr inf);
          //assign the output using continuous assignment
          assign inf.DUT_MP.r = (inf.DUT_MP.p) & (inf.DUT_MP.q);
       endmodule : and_gate

Output:

when the input signal 'p' is not there in the DUT_MP(design modport) but that signal is called in testbench to generate the output at that time it will give an error which is shown in below snapshot.

access_restricting_modports

Lab link:- https://github.com/mbits-mirafra/SystemVerilogCourse/tree/production/interface/modports/modport_error


6. What happens if we assign value to output in test module, while doing interface?

solution: We get a warning as one of the assignment is implicit , but we'll get proper output.

inter_error

Lab link: https://github.com/mbits-mirafra/SystemVerilogCourse/tree/production/interface/interface_error/interface_example


Clone this wiki locally