0% found this document useful (0 votes)
52 views

Verilog Interview Questions

The document provides examples and explanations of various Verilog concepts and system tasks. It discusses the replication operator, instance naming conventions, common issues in tasks, nesting levels of include directives, functions of $countdrivers, $getpattern, and PATHPULSE$, conditional expression evaluation, differences between reduction/bitwise operators and blocking/non-blocking assignments, handling outputs in testbenches, file I/O, and generating random numbers.

Uploaded by

kvxrw7201
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Verilog Interview Questions

The document provides examples and explanations of various Verilog concepts and system tasks. It discusses the replication operator, instance naming conventions, common issues in tasks, nesting levels of include directives, functions of $countdrivers, $getpattern, and PATHPULSE$, conditional expression evaluation, differences between reduction/bitwise operators and blocking/non-blocking assignments, handling outputs in testbenches, file I/O, and generating random numbers.

Uploaded by

kvxrw7201
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

1.

Identify the error in the following code:

b[7:0] = {2{5}};

The error in the code is the incorrect usage of the replication operator. The correct usage should
be {n{value}} where n is the number of times the value should be replicated. In the given code, it
should be:

b[7:0] = {2{5'b1}};

This would assign the value 8'b00100001 to the b signal.

Example:
module ReplicationExample;

// Declare an 8-bit reg signal named 'b'


reg [7:0] b;

// Initial block for simulation initialization


initial begin
// Using the replication operator {n{value}} to replicate 5'b1 two
times
// Resulting in the value 8'b11, which is assigned to signal 'b'
b[7:0] = {2{5'b1}};

// Display the value of signal 'b' using binary format


$display("b = %b", b);
end

endmodule

2. When are instance names optional?

In Verilog, instance names are optional when instantiating modules using the module's positional
port connections. This means that if you instantiate a module and connect its ports based on their
position in the module's port list, you can omit the instance names.

For example, consider a module named myModule with the following port list:
module myModule (
input a,
input b,
output c
);

If you instantiate this module and connect ports based on their position, you can omit the
instance names like this:
myModule u1 (.a(input_a), .b(input_b), .c(output_c));
Here, the instance names a, b, and c are omitted, and only the port connections are provided.

"In Verilog, instance names are optional when instantiating modules using positional port
connections. This means that if you connect the module ports in the instantiation based on their
position in the module's port list, you can omit specifying the instance names in the instantiation
statement. This can be helpful in simplifying the instantiation code, especially when the port
names are self-explanatory. However, using named connections is generally recommended for
clarity and maintainability."

3. In the following program, what is the problem and how to avoid it?

task idriver;

input iread;

input i[7:0] write_data;

begin

#30 idate_valid = 1'b1;

wait(read == 1'b1);

#20 icpu_data = write_data;

$display("End of task");

end

endtask

The given program has a couple of issues:

Undeclared Variables: In the program, the variables idate_valid and icpu_data are being used
without being declared. Verilog requires variable declarations before usage.

Non-blocking Assignment in a Task: The line #20 icpu_data = write_data; uses a non-blocking
assignment (<=) inside a task. Non-blocking assignments are typically used in procedural blocks
(always blocks) to model sequential behavior. Using them in a task can lead to unexpected
behavior.
Here's a corrected version of the program:
module TestModule;
reg idate_valid;
reg iread;
reg [7:0] icpu_data;

task idriver;
input iread;
input [7:0] write_data;
begin
#30 idate_valid = 1'b1;
wait(iread == 1'b1);
#20;
icpu_data <= write_data; // Using non-blocking assignment here
$display("End of task");
end
endtask

initial begin
// Instantiate the task and provide input values
idriver(iread, icpu_data);
end
endmodule

In this corrected version:

idate_valid and icpu_data are declared as reg variables.

The non-blocking assignment operator (<=) is used for assigning icpu_data to write_data inside
the task, as non-blocking assignments are suitable for modeling sequential behavior.

The idriver task is instantiated in the initial block with the appropriate inputs.

Please note that the instantiation of the task with actual values for iread and icpu_data is just a
demonstration for testing purposes. In a real scenario, these values would come from your design
or testbench environment.

4. How many levels can be nested using include?

The include directive in Verilog allows us to incorporate external files into our code, promoting
modularity and organization within our design. In terms of nesting, it's worth noting that the
include compiler directive can be utilized to nest files within files. Specifically, it's possible to nest
the include directive to a depth of at least 16 levels. This implies that we can include files within
other included files, forming a hierarchy of up to 16 layers. However, it's important to exercise
caution when using this approach excessively, as excessive nesting can potentially complicate
code maintenance and comprehension.
5. What is the use of $countdrivers?

The $countdrivers system function is used to determine the number of drivers connected to a
net. It can be helpful in identifying potential issues with multiple drivers on the same net, which
can lead to contention and unexpected behavior in the design.

6. What is the use of $getpattern?

The $getpattern system function is used to retrieve stimulus patterns from a pre-loaded memory,
often used for testbenches to propagate patterns to multiple scalar inputs efficiently.

Here's a small code snippet demonstrating the use of $getpattern:


module GetPatternExample;
reg [7:0] i;
reg [7:0] in_mem [0:15];
integer index;

initial begin
// Load stimulus patterns into 'in_mem' memory
in_mem[0] = 8'b10101010;
in_mem[1] = 8'b11001100;
// ... Other pattern assignments ...

index = 0; // Set initial pattern index

// Use $getpattern to retrieve patterns from 'in_mem'


i = $getpattern(in_mem[index]);

// Display the retrieved pattern


$display("Retrieved pattern: %b", i);
end
endmodule

7. What is the functionality of &&& (not && or &)?

In Verilog, there is no &&& operator. It appears there might have been a misunderstanding or
confusion. The correct operators for logical AND are && (logical AND) and & (bitwise AND). The
&& operator is used for logical AND operations, evaluating to true if both operands are true. The
& operator performs a bitwise AND operation between corresponding bits of the operands.
8. How to get a copy of all the text that is printed to the standard output in a log file?

You can redirect the standard output to a log file using the $display system function.

By using the $display system function with file I/O syntax, you can direct the output to both the
console and a log file.

Code:
initial begin
$fopen("log.txt");
$display("This will be written to the log file");
$fclose;
end

9. What is the use of PATHPULSE$?

PATHPULSE$ is used to control pulse handling within a module path.

10. In the statement (a==b) && (c==d), what is the expression coverage if a=0, b=0, c=0, d=0?

The expression (a==b) && (c==d) evaluates to 1'b1 (True).

Since both (a==b) and (c==d) are true (both are equal to 0), the entire expression is true.

11. Difference between Reduction and Bitwise operators?

Reduction operators operate on bits of a single vector operand, while bitwise operators operate
on corresponding bits of two operands.

Reduction operators (e.g., &, |, ^) reduce a vector into a single bit result. Bitwise operators (e.g.,
&, |, ^) perform operations between corresponding bits of two vectors.

Code:
wire [3:0] a, b;
wire r_and, bw_and;
assign r_and = &a; // Reduction AND
assign bw_and = a & b; // Bitwise AND
12. What is the difference between the following two lines of Verilog code?

#5 a = b;

a = #5 b;

The first line delays the assignment by 5 time units, while the second line delays the assignment
until 5 time units after b changes.

In the first line, the assignment is delayed by 5 time units from the point of execution. In the
second line, the assignment is delayed until 5 time units after b changes.

13. What is the difference between c = (foo) ? a : b; and if (foo) c = a; else c = b;?

Both lines produce the same result, but the first line uses the ternary operator for conditional
assignment.

The ternary operator (condition) ? value_if_true : value_if_false is a compact way to perform


conditional assignment, similar to an if-else statement.

Both expressions achieve conditional assignment of values to c, but they differ in how they handle
"x" and "z" values.

In (foo) ? a : b;, the ternary operator maintains "x" and "z" values, resulting in "x" or "z" if foo is
"x" or "z".

In the if-else construct, if foo is "x" or "z", it treats the condition as false and assigns the value of
b to c.

Code:
reg [7:0] a, b, c;
reg foo;

// Using ternary operator


assign c = (foo) ? a : b;

// Using if-else
always @* begin
if (foo)
c = a;
else
c = b;
end
14. How is Verilog implementation independent and why is this an advantage?

Verilog is implementation independent as it describes behavior, not specific hardware. This


portability allows designs to be simulated on different tools and technologies.

15. What level of Verilog is used in: a. Testbenches b. Synthesized designs c. Netlists

a. Testbenches use behavioral and structural Verilog.

b. Synthesized designs use structural and gate-level Verilog.

c. Netlists use gate-level Verilog.

16. What is the difference between $fopen("filename"); and $fopen("filename","w");?

The first opens the file in read mode, while the second opens the file in write mode.

$fopen("filename"); opens the file in read mode, allowing you to read from it. $fopen("filename",
"w"); opens the file in write mode, allowing you to write to it.

17. What is the difference between multi-channel descriptors (mcd) and file descriptors (fd)?

mcd is a 32-bit reg indicating opened files. fd is a file descriptor returned by $fopen.

mcd is a single-bit reg indicating opened files, while fd is a descriptor that refers to a specific
opened file.

18. How to generate a random number?

Use $random to generate a 32-bit pseudo-random number.

Code:
module RandomNumberExample;
reg [31:0] random_number;

initial begin
random_number = $random;
$display("Random Number: %d", random_number);
end
endmodule
19. How to generate a random number which is less than 100?

Use $random % 100.

Code:
module RandomNumberExample;
reg [31:0] random_number;

initial begin
random_number = $random % 100;
$display("Random Number: %d", random_number);
end
endmodule

20. How to generate a random number which is between 40 and 50?

Use MIN + {$random} % (MAX - MIN ) will_generate random numbers between MIN and MAX

Code:
module Tb();
integer add;

initial
begin
repeat(5) // Repeat the following block 5 times
begin
#1; // Wait for 1 time unit before proceeding

// Generate a random number between 40 and 49


add = 40 + {$random} % (50 - 40);

// Display the generated random number


$display("add = %0d", add);
end
end
endmodule
1. How to generate a random number between 0.5 to 0.9?
module Tb();
real address;

initial
begin
repeat (5)
begin
#1 address = 0.5 + ($urandom % 5) / 10.0;
$display("address = %0.2f;", address);
end
end

endmodule

2. How to get different random numbers in different simulations?

To achieve different random numbers in different simulations in Verilog, you need to ensure that
the random number generator is seeded differently for each simulation run. Here's a general
approach to achieve this:
module Tb();
real address;
time seed;

initial
begin
// Set a unique seed value for each simulation run
//seed = $time;

// Seed the random number generator


// $random(seed);

repeat (5)
begin
seed = $time;
$random(seed);
#1 address = 0.5 + ($random % 5) / 10.0;
$display("address = %0.2f;", address);
end
end

endmodule

In this code, the $time system function gives the current simulation time in picoseconds. By using
this value as the seed for the $random function, you're setting a different seed for each simulation
run, which will result in a different sequence of random numbers.
Keep in mind that the effectiveness of this approach can depend on the simulation tool you are
using and its support for these system functions. If you encounter any issues, consult your
simulation tool's documentation or support resources to understand how to correctly set the
seed for generating different random numbers in different simulations.

3. What is the difference between $sformat and $swrite?

The system task $sformat is similar to the system task $swrite, with one major difference. Unlike
the display and write family of output system tasks, $sformat always interprets its second
argument, and only its second argument, as a format string. This format argument can be a static
string, such as "data is 0", or it can be a reg variable whose content is interpreted as the format
string. No other arguments are interpreted as format strings. $sformat supports all the format
specifiers supported by $display.

4. What is the difference between wire and reg?

Net types:

wire, tri: Physical connection between structural elements. Value assigned by continuous
assignment or gate output.

Register type:

reg, integer, time, real, real time: Represents abstract data storage element. Assigned values only
within an always statement or an initial statement.

The main difference between wire and reg is that wire cannot hold (store) the value when there's
no connection between a and b like "a-------------b". If there is no connection between a and b, a
wire loses its value. On the other hand, a reg can hold the value even if there is no connection.

Default values: wire is 'Z', reg is 'x'.


5. What happens if a port is unconnected?

Unconnected input ports initialize to 'z' and if that value is fed into the component, it can cause
problems. More commonly, redundant or unwanted outputs are left unconnected to be
optimized away in synthesis.

6. What is the difference between === and ==?

The output of "==" can be 1, 0, or 'x'. The output of "===" can only be 0 or 1. When comparing
two numbers using "==", if one or both of the numbers have 'x' bits, the output would be 'x'. But
when using "===", the output would be 0 or 1.

For example:

A === B will give 0 as output.

A == B will give 'x' as output.

7. What is the difference between case, casex, and casez?

case treats only 0 or 1 values in case alternatives and does not deal with don't-care conditions.

casex treats all 'x' and 'z' values in case alternatives as don't-care conditions.

casez treats all 'z' values in case alternatives as don't-care conditions.

8. What is the difference between @(a or b) and @(a | b)?

In Verilog, @(a or b) and @(a | b) are two different ways to trigger a simulation event based on
the values of signals a and b. However, there is no practical difference between them; they both
achieve the same result.

The @(a or b) syntax uses the logical operator or to specify that the simulation event will be
triggered when either signal a or signal b changes value. Similarly, the @(a | b) syntax uses the
bitwise OR operator |, but in Verilog, the bitwise OR operation is the same as the logical OR
operation for single-bit signals, so it's equivalent to @(a or b).
Here's an example to illustrate the equivalence:
module EventExample;
reg a, b;
integer time_counter;

always @(a or b) begin


$display("@(a or b) event triggered at time %0t", $time);
time_counter = time_counter + 1;
end

always @(a | b) begin


$display("@(a | b) event triggered at time %0t", $time);
time_counter = time_counter + 1;
end

initial begin
a = 0; b = 0;
time_counter = 0;

// Simulate changing values of a and b


repeat (10) begin
#10;
a = !a;
#15;
b = !b;
end

// Display the total number of triggered events


$display("Total triggered events: %d", time_counter);
$finish;
end
endmodule

9. What is a full case and a parallel case?

In Verilog, "full case" and "parallel case" are terms used to describe different ways of writing case
statements to describe the behavior of a specific input condition. These terms pertain to the
structure and style of the case statement.

Full Case:

A "full case" style of writing a case statement includes all possible input conditions explicitly. In
other words, each possible value of the control expression is listed along with its corresponding
set of actions. If any value is not explicitly listed, it is assumed to have no effect.

case (ctrl)

2'b00: action_00;

2'b01: action_01;
2'b10: action_10;

2'b11: action_11;

endcase

In this example, all four possible 2-bit values of ctrl (00, 01, 10, and 11) are listed along with their
respective actions.

Parallel Case:

A "parallel case" style of writing a case statement groups together actions that share common
behavior for certain input conditions. In other words, multiple input values may result in the same
set of actions, and those actions are grouped together.

Here's an example of a parallel case statement:

case (ctrl)

2'b00, 2'b01: action_0x; // Both 00 and 01 cases trigger action_0x

2'b10: action_10;

2'b11: action_11;

endcase

In this example, both 2'b00 and 2'b01 cases trigger the action_0x behavior.

In summary:

"Full case" style includes all possible input conditions explicitly.

"Parallel case" style groups together input conditions that have the same behavior.

Example that demonstrates both "full case" and "parallel case" styles using a case statement:

module CaseExample;
reg [1:0] ctrl;

initial begin
ctrl = 2'b01;

// Full Case Style


case (ctrl)
2'b00: $display("Full Case: Control is 00");
2'b01: $display("Full Case: Control is 01");
2'b10: $display("Full Case: Control is 10");
2'b11: $display("Full Case: Control is 11");
endcase

// Parallel Case Style


case (ctrl)
2'b00, 2'b01: $display("Parallel Case: Control is 00 or 01");
2'b10: $display("Parallel Case: Control is 10");
2'b11: $display("Parallel Case: Control is 11");
endcase
end
endmodule

Full Case Style:

Each possible value of ctrl is listed explicitly along with a corresponding display message.

When ctrl is 2'b00, it triggers the message "Full Case: Control is 00".

Similarly, for other values of ctrl.

Parallel Case Style:

Here, we group together values 2'b00 and 2'b01 under one case condition.

When ctrl is either 2'b00 or 2'b01, it triggers the message "Parallel Case: Control is 00 or 01".

Similarly, for other values of ctrl.

10. What is the difference between compiled, interpreted, event-based, and cycle-based
simulators?
Compiled Simulators:
 Compiled simulators convert the Verilog code into an optimized binary representation before
simulation.
 The conversion process involves translating the code into machine code or an intermediate
representation for efficient execution.
 Compiled simulators tend to be faster than interpreted simulators since they avoid
interpreting the source code directly during simulation.
 They are well-suited for larger designs and extensive simulations.
 Examples include Synopsys VCS and Cadence Incisive.
Interpreted Simulators:
 Interpreted simulators execute the Verilog code directly without prior compilation.
 They read the source code line by line during simulation and execute corresponding actions.
 Interpreted simulators are generally slower than compiled simulators due to the lack of pre-
compiled optimization.
 They are useful for smaller designs and quick debugging since they don't require compilation.
 Examples include ModelSim and QuestaSim.

Event-Based Simulators:
 Event-based simulators focus on simulating changes in signal values that trigger events.
 They maintain an event queue and simulate hardware behavior based on the order of these
events.
 Event-based simulators are more accurate in modeling signal-level interactions and timing
delays.
 They handle events occurring at different times and in different orders.
 Examples include ModelSim and QuestaSim.

Cycle-Based Simulators:
 Cycle-based simulators focus on simulating at a higher level of abstraction, considering signal
values only at discrete time steps (cycles).
 Each cycle represents a unit of simulation time during which signals can change.
 They are faster than event-based simulators because they eliminate the need to track and
process individual events.
 Timing accuracy can be lower compared to event-based simulators.
 Examples include NCSim and XSIM.

In summary:

Compiled simulators are optimized and fast but require an initial compilation step.

Interpreted simulators are flexible for small designs and debugging but can be slower.

Event-based simulators offer accurate signal-level simulation, handling events efficiently.

Cycle-based simulators provide higher-level abstraction for faster simulation but with potentially
lower timing accuracy.
11. What data types can be used for input ports, output ports, and inout ports?

Input ports, output ports, and inout ports of a module can use various data types to represent
the signals being passed between different modules. Here are the commonly used data types for
each type of port:

Input Ports:

wire: Wires are used for unidirectional communication from the calling module to the called
module. They can be used for any digital signal, such as control signals, data signals, or clock
signals.

Output Ports:

wire: Just like with input ports, wires can be used for unidirectional communication from the
called module to the calling module. They are commonly used for transmitting digital signals.

Inout Ports:

wire: Inout ports are used for bidirectional communication, allowing the calling and called
modules to communicate in both directions. Wires are often used for inout ports when signals
can flow in either direction.

reg: Registers can also be used for inout ports, typically when the inout signal needs to be latched
or stored. This is common for bidirectional buses where data is read or written in specific clock
cycles.

tri: Tri-state buffers are used in conjunction with wire or reg to create bidirectional
communication. Tri-state signals can be actively driven low, driven high, or put in a high-
impedance (Z) state.

Remember that the choice of data type depends on the nature of the signal being passed and the
requirements of the design. For basic digital communication, wire is the most commonly used
data type for input, output, and inout ports.
12. What is the functionality of a trireg?

A trireg in Verilog is a data type used to model the behavior of bidirectional buses. It represents
a signal that can be actively driven low (0), actively driven high (1), or put into a high-impedance
(Z) state. Tri-state signals are often used to connect multiple devices to a common bus, allowing
only one device to drive the bus at a time while others are in a high-impedance state, preventing
conflicts on the bus.

13. What is the functionality of tri1 and tri0?


tri1 Functionality:
 Represents a bidirectional signal that can be actively driven low (0) or put into a high-
impedance (Z) state.
 When actively driven low, it provides a low logic value.
 Used for signals that are actively pulled low and can be shared among multiple devices using
tri-state behavior.

tri0 Functionality:
 Represents a bidirectional signal that can be actively driven high (1) or put into a high-
impedance (Z) state.
 When actively driven high, it provides a high logic value.
 Used for signals that are actively pulled high and can be shared among multiple devices using
tri-state behavior.

14. Difference between conditional compilation and $plusargs?

Conditional Compilation:
 Conditional compilation involves including or excluding parts of the code during compilation
based on predefined conditions.
 It's controlled by preprocessor directives (ifdef, ifndef, else, elsif, endif) that evaluate compile-
time constants or macros.
 It allows you to create different versions of the code for different scenarios without affecting
runtime behavior.
 Used for enabling or disabling code sections based on compile-time conditions.

$plusargs:
 $plusargs is a system function in Verilog used to provide runtime configuration inputs to the
simulation.
 It allows users to pass command-line arguments to the simulation tool, which can then be
read by the Verilog code.
 $plusargs provides flexibility to modify simulation behavior without recompiling the code.
 Used for passing runtime parameters to the simulation, such as test configurations or initial
values.

15. What is the benefit of using Behavioral modeling style over RTL modeling?

Benefits of Using Behavioral Modeling Style over RTL Modeling in Verilog:

 Abstraction Level: Higher-level, abstract representation of functionality.


 Design Exploration: Enables quick iteration and exploration of design options.
 Ease of Modification: Easier to modify and adapt to changing requirements.
 Readability and Maintainability: More readable and manageable code.
 Simulation Efficiency: Faster simulation due to higher-level abstraction.
 Rapid Prototyping: Allows rapid prototyping and early functional verification.
 Portability and Reusability: More portable and reusable across implementations

16. What is the difference between a task and a function?

Task:
 Tasks are capable of enabling a function as well as enabling other versions of a task
 Tasks also run with a zero simulation however they can if required be executed in a non
zero simulation time.
 Tasks are allowed to contain any of these statements.
 A task is allowed to use zero or more arguments which are of type output, input or
inout.
 A Task is unable to return a value but has the facility to pass multiple values via the
output and inout statements .

Function:
 A function is unable to enable a task however functions can enable other functions.
 A function will carry out its required duty in zero simulation time. ( The program time will
not be incremented during the function routine)
 Within a function, no event, delay or timing control statements are permitted
 In the invocation of a function their must be at least one argument to be passed.
 Functions will only return a single value and can not use either output or inout
statements.
17. What is the difference between a static function and an automatic function?

Static function has module scope, can't access local variables outside its module. Can use
$Monitor and $Strobe on local variables.
Automatic function has function scope, is more memory efficient, and cannot be used with
$Monitor and $Strobe.

18. What is the advantage of wired AND and wired OR over wire?

Wired AND: This construct is used for open collector or open emitter designs. It allows multiple
drivers to pull the line low, but the line is left floating (high-impedance state) when none of the
drivers are active. Wired AND doesn't directly provide logical conflict resolution but is more about
combining multiple pull-down paths.

Wired OR: Wired OR allows multiple drivers to pull the line high. When none of the drivers are
active, the pull-up resistor connected to the wire ensures a defined high state. Wired OR is often
used for open drain designs. Similarly, like Wired AND, Wired OR doesn't provide logical conflict
resolution but enables multiple pull-up paths.

It's important to note that logical conflict resolution is generally not achieved through wired AND
or wired OR constructs themselves, but through additional logic or design practices in more
complex circuitry.

19. Identify the error in the following code.


a[7:0] = {4{'b10}};

The error in the provided Verilog code is the incorrect usage of the replication operator ({}). The
replication operator is used to replicate a pattern a specified number of times. In this case, it
seems like you want to create a 4-bit value where each 2-bit chunk is 2'b10.

Here's the corrected code:


a[7:0] = {4{2'b10}};

In this corrected version, the replication operator {4{2'b10}} replicates the 2-bit pattern 2'b10
four times to create an 8-bit value.
module ReplicationExample;
reg [7:0] a;

initial begin
a = {4{2'b10}};
$display("a = %b", a);
$finish;
end
endmodule
20. What is the difference between initial and always blocks?

Initial Block:

 Functionality: An initial block is used to specify initial conditions and execute statements at
the beginning of simulation.
 Execution Timing: The statements inside an initial block are executed only once at the start
of the simulation.
 Usage: Typically used for initializing variables, displaying messages, or setting up initial
testbench conditions.

Always Block:

 Functionality: An always block is used to define continuous or sequential behavior based on


sensitivity lists.
 Execution Timing: The statements inside an always block are executed repeatedly based on
the conditions specified in the sensitivity list.
 Usage: Used for defining sequential logic, combinational logic, or modeling hardware
behavior based on clock edges or signal changes.

In summary, initial blocks are used for simulation setup and one-time actions at the beginning of
simulation, while always blocks are used to model ongoing behavior based on specific conditions
or signal changes.
1. What is the difference between $stop and $finish tasks in functions?
The $stop task immediately terminates the simulation and can be used to halt the simulation at
a particular point. The $finish task also terminates the simulation, but it allows any remaining
simulation time to be completed before the simulation stops. This can affect the execution of
other scheduled tasks and processes.

2. Difference between parameter and define?


Parameters are used to declare values that can be set when instantiating modules, and they can
be overridden at different levels of hierarchy. Defines, on the other hand, are simple text
substitutions and are often used to declare constants that are replaced during preprocessing.
Parameters offer more flexibility and can be redefined at different levels, while defines are global
and lack this hierarchical scoping.

3. What is the difference between the following two statements?


@(val == 2)

wait(val == 2)
@(val == 2) is a procedural statement that waits for the condition val to become equal to 2 before
proceeding. It's typically used in testbenches for synchronization.
wait(val == 2) is also a procedural statement used in SystemVerilog's wait statement construct. It
suspends the execution of the current process until the condition val is satisfied.

4. What are the differences while specifying parameters using the defparam construct vs.
specifying during instantiation?
The defparam construct is used to override parameter values after module instantiation, and it
applies to all instances of the module. Specifying parameters during instantiation sets the
parameter values during instantiation and is more modular, allowing different instances to have
different parameter values.
5. Difference between vectored and scalar nets?
Vectored nets represent multiple signals as a single entity, allowing efficient representation of bus
signals. Scalar nets represent a single signal, making them suitable for individual signal
connections. Vectored nets are denoted with [ ] brackets, while scalar nets are not.

6. Difference between real and realtime?

real is a data type used to represent floating-point values. realtime is a SystemVerilog type used
for simulation time expressions. It ensures that the operations involving realtime values are
performed in the order specified by simulation time, regardless of the order of execution in the
procedural code.

7. What is the difference between arithmetic and logical shift registers?


Arithmetic shift registers perform shifts while considering the sign bit, which means they preserve
the sign of the number during shifts. Logical shift registers perform shifts without considering the
sign bit, effectively shifting in zeros.

Let's consider the binary number 1101 (13 in decimal) and perform both arithmetic and logical
right shifts by 1 bit.

Arithmetic Right Shift:


In an arithmetic right shift, the sign bit is preserved. If the original number is positive (sign bit 0),
zeros are shifted in from the left. If the original number is negative (sign bit 1), ones are shifted in
from the left.
Original: 1101
After Arithmetic Right Shift by 1: 1110

Logical Right Shift:


In a logical right shift, only zeros are shifted in from the left, regardless of the sign bit.
Original: 1101
After Logical Right Shift by 1: 0110
8. What is the difference between the following two registers?
reg [1:n] rega;
reg mema [1:n];

reg [1:n] rega; declares a single register named rega that is n bits wide. This single register can
hold an n-bit binary value.

reg mema [1:n]; declares an array of registers named mema, where each element of the array is
1 bit wide. There are n separate 1-bit registers named mema[1], mema[2], ..., mema[n].

9. How are the above two handled in assignments, ports, functions, and tasks?
The way the two types of register declarations (reg [1:n] rega; and reg mema [1:n];) are handled
in assignments, ports, functions, and tasks depends on their nature as a single register and an
array of registers. Here's how they are treated in different contexts:

 Assignments:
For reg [1:n] rega;: You can assign an n-bit value to the entire register rega using standard
assignment operators, e.g., rega = 4'b1100;.

For reg mema [1:n];: You can assign 1-bit values to individual elements of the array, like mema[1]
= 1'b1; or mema[2] = 1'b0;.

 Ports:
For reg [1:n] rega;: In module ports, rega can be used as a single port of width n.
For reg mema [1:n];: Each element of the array mema can be used as separate ports in module
declarations.

 Functions and Tasks:

For reg [1:n] rega;: You can use rega as an argument to functions or tasks, treating it as a single
value.
For reg mema [1:n];: You can pass individual elements of the array mema as arguments to
functions or tasks.
10. What is the difference between parameters and specparams?
Parameters are used for general values that can be overridden during instantiation. Specparams
are used in assertion constructs like SystemVerilog's cover property to provide specific values for
coverage analysis, separate from regular parameter overrides.

11. Is it possible to synthesize a for loop?

Yes, a for loop with fixed limits can be synthesized into hardware. It represents a repeated
operation that can be unrolled during synthesis to generate multiple instances of the loop's body.

12. How is time advanced in simulation?


In simulation, time is advanced by executing events and processes in chronological order. Events
scheduled at different simulation times are executed in the correct sequence to simulate the
passage of time.

13. Name three methods of timing control?


Three methods of timing control are:

 Blocking Assignments (=): In a blocking assignment, the right-hand side expression is


evaluated immediately, and the value is assigned to the left-hand side variable. The simulation
process is blocked until the assignment is complete. This method is often used for
combinational logic modeling.
Example:

a = b & c;
 Non-blocking Assignments (<=): Non-blocking assignments schedule the assignment to occur
at the end of the time step. Multiple non-blocking assignments within a procedural block are
executed in parallel, allowing modeling of sequential behavior. This method is often used for
modeling flip-flops and sequential logic.

Example:
q <= d;
 Delay Statements: Verilog provides delay statements to control the timing of events. The delay
value specifies how much time should elapse before the statement is executed. The delay can
be specified using a numerical value followed by a time unit (such as #10ns for a 10
nanosecond delay).
Example:
#5ns a = b & c;

14. What is behavioral modeling used for?


Behavioral modeling is used to describe the functionality and behavior of a digital system without
specifying the low-level implementation details. It focuses on what the system does rather than
how it's implemented.

15. How do you define the states for an FSM?


Finite State Machines (FSMs) have states defined as discrete values. They are usually declared
using an enumerated data type or binary encoding. Each state represents a unique condition that
the FSM can be in.

16. What is the difference between force/release and assign/deassign?


force and release are used to drive values onto nets in a continuous assignment, and they are
mainly used for testbench purposes. assign and deassign are used for modeling combinational
logic and allow signals to be assigned or deassigned at different points in time.

17. What is the difference between posedge and negedge?


In edge-sensitive constructs like always @(posedge clk) and always @(negedge rst), posedge
triggers on the rising edge (transition from 0 to 1), while negedge triggers on the falling edge
(transition from 1 to 0).

18. What is the difference between $display and $write?


$display adds a newline character to its output by default, while $write does not. When used
without arguments, $display prints a newline character, but $write without parameters prints
nothing.
19. What is the difference between $display and $monitor?
Both $display and $monitor are used for printing information during simulation. However,
$monitor automatically monitors variables and expressions for changes and displays them
whenever they change, while $display requires explicit calls and arguments to display values.

20. What is the difference between $display and $strobe?

$display is the normal display, which executes its parameters wherever it is present in the code.
$strobe executes only once in a time instant, when all processes in that time instant have
executed.
1. What is the difference between $finish and $stop?

$finish is a system task that simply makes the simulator exit and passes control back to the host
operating system.

$stop is a system task that causes the simulation to be suspended.

2. What hardware structure is inferred by both case and if statements in Verilog?

Both case and if statements are typically synthesized into hardware multiplexers or combinational
logic circuits in Verilog.

3. How could you change a case statement in order that its implementation does not result in
a priority structure?

You can change a case statement into a "don't care" priority structure by using the default clause
without specifying any other condition inside the case. This will prevent the synthesis tool from
inferring a priority structure.

Example:
module PriorityExample(input [1:0] sel, output reg [3:0] out);
always @(*)
begin
case (sel)
2'b00: out = 4'b0001; // Condition 1
2'b01: out = 4'b0010; // Condition 2
2'b10: out = 4'b0100; // Condition 3
default: out = 4'b1000; // Default or "don't care" condition
endcase
end
endmodule

4. If you are not using a synthesis attribute "full case," how can you assure coverage of all
conditions for a case statement?

You can ensure coverage of all conditions for a case statement by adding a default clause (default)
at the end of the case block. This way, any condition not explicitly covered by other cases will be
handled by the default clause.
5. How do you infer tristate gates for synthesis?

Tristate gates can be inferred in Verilog by using the tri data type and assigning values using the
assign statement with conditional logic to enable or disable the tristate condition based on
specific control signals.

6. Can a task be synthesized?

No, tasks are not synthesized. Tasks are used for specifying simulation-level behavior and are not
part of the hardware description that gets synthesized into hardware circuits.

7. What is the difference between ! and ~?

! is the logical NOT operator, which inverts the value of a Boolean expression. For example, !A will
be true if A is false, and vice versa.

~ is the bitwise NOT operator, which inverts each bit of a binary number.

8. What is the difference between the two statements? Do a and b values have to be equal?

a = edata[0] || edata[1];

b = |edata;

a and b are not necessarily equal. a performs a logical OR operation between edata[0] and
edata[1], while b performs a bitwise OR operation on all bits of edata.

9. What is the difference between the two programs?

a) initial
#10 a = 0;

always @(a)
a <= ~a;

b) initial
#10 a = 0;

always @(a)
a = ~a;
The key difference between the two programs is the use of blocking (=) and non-blocking (<=)
assignments:

In the first program, a is assigned the value of ~a using non-blocking assignment (<=). This means
that a is updated at the end of the current time step, and the assignment does not affect the
order of execution within the always block.

In the second program, a is assigned the value of ~a using blocking assignment (=). This means
that the assignment occurs immediately, potentially affecting the order of execution within the
always block.

10. Why is it recommended not to mix blocking and non-blocking assignments in the same
block?

Mixing blocking and non-blocking assignments in the same block can lead to simulation errors
and unexpected behavior. It can create race conditions and make the code difficult to understand
and maintain. Keeping them separate helps ensure predictable and reliable behavior in your
Verilog code.

11. Declare parameters for representing state machine states using one-hot encoding.

module OneHotStateMachine (
input wire [2:0] state,
output wire stateA,
output wire stateB,
output wire stateC
);

// Define parameters for one-hot encoded states


parameter STATE_A = 3'b001; // State A
parameter STATE_B = 3'b010; // State B
parameter STATE_C = 3'b100; // State C

// Output assignments based on the current state


assign stateA = (state == STATE_A);
assign stateB = (state == STATE_B);
assign stateC = (state == STATE_C);

endmodule
12. What does a function synthesize into?

A function in Verilog typically synthesizes into combinational logic. It is used to describe a Boolean
function, and the synthesis tool will map it to gates and wires that implement the desired logic.

13. How to change the value of width to 3 in the following code?


`define width 7

To change the value of width to 3 in the Verilog code using a define macro, you can do the
following:
`define width 3

This will define width as 3, and you can use it in your code as width wherever needed.

14. What is the functionality of $input?

The $input system task allows command input text to come from a named file instead of from the
terminal. At the end of the command file, the input is switched back to the terminal.

15. What is the difference between blocking and non-blocking?

Blocking Assignments Non-Blocking Assignments


Sequential Execution: Blocking assignments Parallel Execution: Non-blocking assignments
(=) are executed sequentially in the order they (<=) are executed in parallel within the same
appear within a procedural block. simulation time step, regardless of their order
in the code.

Immediate Update: A blocking assignment Deferred Update: A non-blocking assignment


causes the value of the variable or signal on schedules the update of the variable or signal
the left-hand side to be updated immediately on the left-hand side to occur at the end of the
within the current simulation time step. current simulation time step.

Typically used for describing combinational Typically used for modeling clocked sequential
logic or concurrent assignments where order logic, like flip-flops and state machines, to
of execution matters. ensure proper simulation behavior and avoid
race conditions.
Race Conditions: Can potentially lead to race Preventing Race Conditions: Non-blocking
conditions if multiple assignments to the same assignments inherently prevent race
variable occur within the same block. conditions within the same block because all
assignments are scheduled to happen
simultaneously at the end of the time step.

Example: Example:
a = b; // Value of 'a' is immediately a <= b; // Value of 'a' is scheduled
updated to 'b' to be updated to 'b' at the end of
the time step

16. What are the two types of race conditions?

Data Race Condition: It occurs when two or more processes or threads access shared data
concurrently, and at least one of them modifies the data. Data races can lead to unpredictable
behavior and incorrect results.

Control Race Condition: It occurs when the control flow or sequence of execution in a program or
system is not deterministic due to multiple possible paths. Control race conditions can lead to
unexpected outcomes and are often associated with asynchronous events.

17. How can race conditions between the DUT and the testbench be avoided?

To avoid race conditions between the DUT (Design Under Test) and the testbench, you can follow
these practices:

 Synchronize clock domains properly if multiple clocks are involved.


 Use non-blocking assignments (<=) for signal updates in testbench processes.
 Ensure that the testbench generates stimuli and checks results in a controlled and
synchronized manner.
 Use proper synchronization constructs like wait statements or events to coordinate testbench
and DUT activities.
 Avoid creating race conditions by ensuring that signals are sampled or modified at the
appropriate times based on clock edges.
18. How can race conditions between the DUT and the testbench be avoided?

Guidelines to avoid race conditions:

 Use non-blocking (<=) assignments for signal updates in synchronous processes.


 Properly synchronize signals when crossing clock domains.
 Use synchronization constructs like wait statements to coordinate activities in testbenches.
 Be aware of signal setup and hold times in synchronous designs.
 Avoid multiple drivers for the same signal.
 Ensure that signals are sampled and modified at the right clock edges.

19. Identify the bug in the following code:

always @(posedge clk)

a = b;

always @(posedge clk)

b = a;

The bug in the code you provided is that it results in multiple drivers for signals a and b, which
can lead to a race condition. To fix it, you should use a single always block for both assignments:
always @(posedge clk) begin
a <= b;
b <= a;
end

This code will ensure that both a and b are updated synchronously on the rising edge of the clock,
preventing any race condition.

20. Identify the bug in the following code:

if (a = b)

match = 1;

else

match = 0;

The bug in the code is that you are using a single equals sign (=) for comparison instead of a
double equals sign (==).
Here's the corrected code:

if (a == b)

match = 1;

else

match = 0;

In Verilog, == is used for equality comparison, while = is used for signal assignment.

21. Find the bug in the following code.

for (............);

begin

.....

end

Misplaced semicolons in for-loops.

22. Find the bug in the following code.

automatic itasko entry_assign();

begin

a <= b; // Comment

end

Intra-assignment non-blocking statements are not allowed in automatic tasks.


23. Find the bug in the following code.

always @(in)

if (ena)

out = in;

else

out = 1'b1;

Simulation mismatch might occur. To assure the simulation will match the synthesized logic, re-
add "ena" to the event list so the event list reads: always @ (in or ena)

24. Find the bug in the following code.

always @(in1 or in2 or sel)

begin

out = in1;

if (sel)

out <= in2;

end

Not supported; cannot mix blocking and non-blocking assignments in an always statement.

25. Find the bug in the following code.

reg [1:0] select;

always @(select)

begin

case (select)

00: yo = 1;

01: yo = 2;

10: yo = 3;
11: yo = 4;

endcase

end

Branches 01 and 11 are considered as integers, and they will never be selected.

26. Fill the ????

fd = $fopen("filename", "r");

if (????)

$display("File cannot be opened");

// Attempt to open the file


fd = $fopen("filename", "r");

// Check if the file opened successfully


if (!fd)
$display("File could not be opened");

27. How to model a perfect buffer of 10 units of delay?

a) #10 a = b;

b) a = #10 b;

c) #10 a <= b;

d) a <= #10 b;

c) #10 a <= b;

This line of Verilog code correctly uses non-blocking assignment (<=) and introduces a 10-unit
delay specified by #10 between signal b and signal a.
28. Write code for a clock generator.
module clk1();
parameter clk_period = 10;
reg clk;

initial begin
clk = 0;
end

always #(clk_period/2) clk = ~clk;


endmodule

29. Write code for a clock generator that can generate a clock frequency of 156MHz.
module clk_gen;

`timescale 1ns/1ps

bit a, b;
bit clk = 0;

initial begin
#100 $finish();
end

initial begin
//forever #5 ns clk = !clk; // 100MHz
forever #1.6 ns clk = !clk; // 156MHz
//forever #2 ns clk = !clk; // 200MHz
end

endmodule

30. Write a Verilog code to generate a 40MHz clock with a 50% duty cycle.
//DESIGN
`timescale 1ns/1ps

module clock_gen ( input enable,


output reg clk);

parameter FREQ = 40000; // in kHZ


parameter PHASE = 0; // in degrees
parameter DUTY = 50; // in percentage

real clk_pd = 1.0/(FREQ * 1e3) * 1e9; // convert to ns


real clk_on = DUTY/100.0 * clk_pd;
real clk_off = (100.0 - DUTY)/100.0 * clk_pd;
real quarter = clk_pd/4;
real start_dly = quarter * PHASE/90;

reg start_clk;
initial begin
$display("FREQ = %0d kHz", FREQ);
$display("PHASE = %0d deg", PHASE);
$display("DUTY = %0d %%", DUTY);

$display("PERIOD = %0.3f ns", clk_pd);


$display("CLK_ON = %0.3f ns", clk_on);
$display("CLK_OFF = %0.3f ns", clk_off);
$display("QUARTER = %0.3f ns", quarter);
$display("START_DLY = %0.3f ns", start_dly);
end

initial begin
clk <= 0;
start_clk <= 0;
end

always @ (posedge enable or negedge enable) begin


if (enable) begin
#(start_dly) start_clk = 1;
end else begin
#(start_dly) start_clk = 0;
end
end

always @(posedge start_clk) begin


if (start_clk) begin
clk = 1;

while (start_clk) begin


#(clk_on) clk = 0;
#(clk_off) clk = 1;
end

clk = 0;
end
end
endmodule

//TB
module tb;
wire clk1;
wire clk2;
wire clk3;
wire clk4;
reg enable;
reg [7:0] dly;

clock_gen u0(enable, clk1);

initial begin
enable <= 0;

for (int i = 0; i < 10; i= i+1) begin


dly = $random;
#(dly) enable <= ~enable;
$display("i=%0d dly=%0d", i, dly);
#50;
end

#50 $finish;
end

initial begin
$dumpvars;
$dumpfile("dump.vcd");
end
endmodule

31. What are the >>> and <<< operators?

The >>> and <<< operators in Verilog are used for logical right shift and logical left shift operations,
respectively. They are typically used for bitwise shifting of values.

Example:

result = value >>> 2; // Right shift "value" by 2 positions, filling with zeros

result = value <<< 3; // Left shift "value" by 3 positions, filling with zeros

32. What does the following code mean?

reg [22:0] sig;

always @(|sig) begin

// ...

end

The Verilog code snippet declares a 23-bit wide register named sig and sets up an always block
that triggers whenever any of the bits in the sig register change. Inside this always block, you
would typically include logic that operates based on changes in the sig register, making it suitable
for implementing combinational logic based on the register's value changes.
33. What is the function of the force and release?

The force and release statements are used to override assignments on both registers and nets.
They are typically used in the interactive debugging process, where certain registers or nets are
forced to a value, and the effect on other registers and nets is noted. They should occur only in
simulation blocks.

34. What is the purpose of declaring tasks or functions as automatic?

Declaring tasks and functions as automatic will create dynamic storage for each task or function
call.

35. What is Synthesis?

Synthesis is the stage in the design flow concerned with translating your Verilog code into gates.
It involves converting your Verilog design into a netlist representing the chip that can be
fabricated through an ASIC or FPGA vendor.

You might also like