Verilog Interview Questions
Verilog Interview Questions
1. Explain Verilog
Verilog is a text-based hardware description language used to describe electronic systems and
circuits. Verilog is designed to be used in electronic design for timing analysis, test analysis (fault
grading and testability analysis), logic synthesis, and verification through simulation.
The following three levels of abstraction are supported by Verilog most frequently in a design:
Behavioural level
Register-transfer level
Gate level
1
VERILOG INTERVIEW QUESTIONS
VHDL Verilog
It was established in 1980, making it an older It was formed between late 1983 and early
language. 1984.
It uses the base languages, Ada and Pascal The base language is C.
VHDL analyses and simulates the behavior of the They are only applied to describe digital
digital system in addition to describing its structure. systems.
Blocking is the simple term for when one Non-blocking allows assignments to be executed
register assignment prevents another concurrently regardless of the sequence of
assignment from being executed dependencies.
simultaneously.
Assignments will only be blocked in order Non-blocking will plan the execution of
until the one before them is finished. In sequential assignments so that concurrent
execution of tasks is possible.
2
VERILOG INTERVIEW QUESTIONS
10. What do you know about Verilog parallel and full case statements?
In Verilog, there are two different kinds of case statements.
Verilog parallel case statements: A statement that matches just one case item in a case
expression is referred to as a parallel case statement. The matching case items are known as
"overlapping case items," The case statement would not be parallel if you could find a case
expression that would fit more than one case item.
Verilog full case statements: Every case expression in a Verilog full case statement must
have binary patterns that match either a case item or the default. The case statement would
not be regarded as complete if it did not include a case default and was likely to come across
a binary case expression that did not match any of the specified case elements.
3
VERILOG INTERVIEW QUESTIONS
== ===
The key distinction between == and === The output of ===, on the other hand, can only
in Verilog is that the result of == can be either 0 or 1.
either be 1, 0 or X.
If two numbers are compared using == and one or The result of a === comparison between two
both numbers have one or more bits set to X, the numbers would be either 0 or 1.
result will be X.
Since it cannot compare Xs, == can only be used Xs can also be compared using ===.
to compare 1s and 0s.
4
VERILOG INTERVIEW QUESTIONS
13. What are the primary differences between Verilog's Task and Function?
Another most frequently asked Verilog interview question. The key distinctions between a Task and
a Function in Verilog are as follows:
Task Function
In Verilog, tasks can activate both additional A function in Verilog can enable other functions
tasks and functions. but cannot allow a task.
A task is permitted to contain any event, delay, Event, delay, or timing control statements are
or timing control statement. not allowed. Hence they cannot be contained in
a function.
The simulation for tasks is also zero. However, Because the program time is not increased
they can also be carried out in a simulation time during the function routine, a function can do its
that is not zero if necessary. required task in zero simulation time.
A task may employ zero or more arguments of A function must receive at least one parameter
the output or input types arguments. when it is invoked.
Using the output and input statements, a task Functions cannot employ either output or input
can pass multiple values but cannot return a statements and only return a single value.
value.
5
VERILOG INTERVIEW QUESTIONS
18. What are the key differences between Reg and Wire?
Reg and wire are often an interviewer’s favourite in the Verilog interview questions, so it is important
to prepare them thoroughly. Essential differences between Reg and Wire are:
Reg Wire
Reg can get the output without a driver. The wire needs drivers to obtain output values.
The reg components can be used for both The only type of logic that the wire elements can
sequential and combinational logic. model is combinational logic.
Reg cannot be used on an assigned statement's Wire can be used on the left-hand side of a given
left-hand side. statement.
6
VERILOG INTERVIEW QUESTIONS
20. Which will be updated first between the variable and the signal?
Variables can only be used inside processes, whereas signals are used inside and outside. As a result,
signals are updated before variables are updated.
21. Is it required to list every input in the sensitivity disc for a pure combinational circuit? If
so, why then?
Yes. If you don't list every input in the sensitivity disc for a pure combinational circuit, the outcome
will have a pre- and post-synthesis mismatch.
22. Explain the Verilog concepts of freeze, deposit, force, and drive.
It is important to know the various concepts of Verilog used as they are often asked in Verilog
interview questions.
Freeze: For unresolved signals, use freeze. Throughout the simulation, the signal's value is frozen.
You cannot modify this value.
Deposit: Deposit ensures that a signal value is kept until it is altered or replaced by the simulation.
Throughout the simulation, it serves as value initialization.
Force: It can drive signals at any point during the simulation.
Drive: When the drive command gives a signal a value, it will "resolve" to a new value if the
simulation updates the signal.
7
VERILOG INTERVIEW QUESTIONS
$monitor $display
Changes to the signal's value are made via the It is used to display a signal's value.
monitor. An expression, string, or variable can be
a signal.
There can be only one execution of the monitor You can use the display command more than
command. once.
26. Write a Verilog program to switch the contents of two registers with and without a
temporary register.
An example of a Verilog code that switches the contents of two registers with a temporary register:
always @ (posedge clock)
begin
temp=y;
y=x;
8
VERILOG INTERVIEW QUESTIONS
x=temp;
end.
Without a temporary register, a Verilog program can swap the contents of two registers:
always @ (posedge clock)
begin
x <= y;
y <= x;
end
9
VERILOG INTERVIEW QUESTIONS
Verilog is versatile enough to model digital circuits using various logic levels; however, it is not
specifically designed to support analog circuit descriptions, as it is primarily focused on digital logic
modelling. For analog circuit design, another language called Verilog-A is typically used.
Very High-speed integrated circuit HDL describes and simulates the system before making it into a
digital one. Its features are
Complexity management
Design data portability
Independent of technology
Efficient and less time consuming
Readability
A factory creates different objects for a prototype. It is done By calling the corresponding constructor
when different classes are registered with the factory.
The Factory pattern directly creates an object without calling the constructor method. It allows the
use of polymorphism for object creation.
35.What is Callback?
A callback is when a function calls another function taking the first one as an argument. It is used
when an event happens. It includes calling back a function
10
VERILOG INTERVIEW QUESTIONS
The base class does not need to implement a Any derivative class must
virtual function implement the function
Typedef Enables users to craft unique names for type definitions for frequent use in their
codes. They are easily used while building technical array definitions.
11
VERILOG INTERVIEW QUESTIONS
12
VERILOG INTERVIEW QUESTIONS
in a case statement if all the possible combinations are not compared and default is also not
specified like in example above a latch will be inferred ,a latch is inferred because to reproduce
the previous value when unknown branch is specified.
For example in above case if {s1,s0}=3 , the previous stored value is reproduced for this storing
a latch is inferred.
To avoid inferring latches make sure that all the cases are mentioned if not default condition is
provided.
// timescale directive tells the simulator the base units and precision of the simulation
`timescale 1 ns / 10 ps
module name (input and outputs);
// parameter declarations
parameter parameter_name = parameter value;
// Input output declarations
input in1;
input in2; // single bit inputs
output [msb:lsb] out; // a bus output
13
VERILOG INTERVIEW QUESTIONS
// internal signal register type declaration – register types (only assigned within always
statements). reg register
variable 1;
reg [msb:lsb] register variable 2;
// internal signal. net type declaration – (only assigned outside always statements) wire net
variable 1;
// hierarchy – instantiating another module
reference name instance name (
.pin1 (net1),
.pin2 (net2),
.
.pinn (netn)
);
// synchronous procedures
always @ (posedge clock)
begin
.
end
// combinatinal procedures
always @ (signal1 or signal2 or signal3)
begin
.
end
assign net variable = combinational logic;
endmodule
14
VERILOG INTERVIEW QUESTIONS
Verilog 2001 allows us to use star in sensitive list instead of listing all the variables in RHS of
combo logics . This removes typo mistakes and thus avoids simulation and synthesis
mismatches, Verilog 2001 allows port direction and data type in the port list of modules as
shown in the example below
module memory (
input r,
input wr,
input [7:0] data_in,
input [3:0] addr,
output [7:0] data_out
);
48. There Is a Triangle and On It There Are 3 Ants One on Each Corner and Are Free to
Move Along Sides of Triangle What Is Probability That They Will Collide?
Ants can move only along edges of triangle in either of direction, let us say one is represented
by 1 and another by 0, since there are 3 sides eight combinations are possible, when all ants are
going in same direction, they won’t collide that is 111 or 000 so probability of not collision is
2/8=1/4 or collision probability is 6/8=3/4
49. Will Case Infer Priority Register If Yes How Give an Example?
yes, case can infer priority register depending on coding style
reg r;
// Priority encoded mux,
always @ (a or b or c or select2)
begin
r = c;
case (select2)
2’b00: r = a;
2’b01: r = b;
endcase
end
50. In the given Following Verilog Code, What Value Of “a” Is Displayed?
15
VERILOG INTERVIEW QUESTIONS
This is a tricky one! Verilog scheduling semantics basically imply a four-level deep queue for
the current simulation time:
The “a <= 1” is a non-blocking event, so it’s placed into the 3rd queue.
Finally, the display statement is placed into the 4th queue. Only events in the active queue are
completed this sim cycle, so the “a = 0” happens, and then the display shows a = 0. If we were
to look at the value of a in the next sim cycle, it would show 1.
51. What Is the Difference Between the Following Two Lines of Verilog Code?
#5 a = b;
a = #5 b;
#5 a = b; Wait five-time units before doing the action for “a = b;”.
a = #5 b; The value of b is calculated and stored in an internal temp register, after five-time
units, assign this stored value to a.
// Port Declaration
16
VERILOG INTERVIEW QUESTIONS
input oe;
input clk;
input [7:0] inp;
output [7:0] outp;
inout [7:0] bidir;
reg [7:0] a;
reg [7:0] b;
assign bidir = oe ? a : 8’bZ ;
assign outp = b;
// Always Construct
begin
b <= bidir;
a <= inp;
end
endmodule
The case statement walks down the list of cases and executes the first one that matches. So
here, if the lowest 1-bit of x is bit 2, then something3 is the statement that will get executed (or
selected by the logic).
17
VERILOG INTERVIEW QUESTIONS
54. Why Is It That “if (2’b01 & 2’b10)…” Doesn’t Run the True Case?
This is a popular coding error. You used the bit wise AND operator (&) where you meant to
use the logical AND operator (&&).
Event Driven
Cycle Based
Event-based Simulator:
This Digital Logic Simulation method sacrifices performance for rich functionality: every
active signal is calculated for every device it propagates through during a clock cycle. Full
Event-based simulators support 4-28 states; simulation of behavioural HDL, RTL HDL, gate,
and transistor representations; full timing calculations for all devices; and the full HDL
standard. Event-based simulators are like a Swiss Army knife with many different features but
none are particularly fast.
Cycle Based Simulator:
This is a Digital Logic Simulation method that eliminates unnecessary calculations to achieve
huge performance gains in verifying Boolean logic:
Results are only examined at the end of every clock cycle; and
The digital logic is the only part of the design simulated (no timing calculations). By
limiting the calculations, Cycle based Simulators can provide huge increases in
performance over conventional Event-based simulators.
Cycle based simulators are more like a high-speed electric carving knife in comparison because
they focus on a subset of the biggest problem: logic verification.
Cycle based simulators are almost invariably used along with Static Timing verifier to
compensate for the lost timing information coverage.
18
VERILOG INTERVIEW QUESTIONS
The ? merges answers if the condition is "x", so for instance if foo = 1'bx, a = 'b10, and b =
'b11, you would get c = 'b1x. On the other hand, if treats Xs or Zs as FALSE, so you would
always get c = b.
19
VERILOG INTERVIEW QUESTIONS
PART-1
1. Write a Verilog code using behavioral modeling for a 4-bit shift register?
2. Write a Verilog code for positive edge triggered D-flip flop with (a) synchronous reset
and (b) asynchronous reset.
3. Describe the difference between the system tasks $monitor and $display?
4. List the differences between tasks and functions.
5. Is there any difference between "==" and "===" in behavioral modeling of verilog?
6. What is the use of non-blocking assignment in behavioral modeling of verilog? How is
it different from blocking- assignment?
7. Given the following Verilog code, what value of "a" is displayed?
always @(clk) begin
a = 0;
a <= 1;
$display(a);
end
8. Is there any problem in the following code: (a) For simulations (b) For synthesis
and for post synthesis.
always @(cntrl,a,b)
begin
if(cntrl)
a <= b;
end
9. Implement following logic's using minimum number of D Flip-Flops:
a) Clock Divide by 2
b) Clock Divide by 4
10. What is the difference between a Mealy and a Moore finite state machine?
11. Write a Verilog code to swap contents of two registers a and b without any
temporary register?
12. Write a Verilog code for a flip-flop and latch?
13. Write a Verilog code to detect a pattern 10110 from an input stream of bits.
14. What is the difference between reg and wire?
15. Write a Verilog code to print nth Fibonacci number?
20
VERILOG INTERVIEW QUESTIONS
PART-2
1.If a net has no driver, it gets the value.
a)0
b)X
c)Z
d)None of the above
2.Which logic level is not supported by verilog?
a)U
b)X
c)Z
d)None of the above
3.Which level of abstraction level is available in Verilog but not in VHDL?
a)Behavioral level
b)Gate level
c)Dataflow level
d)Switch level
4.Default value of reg is
a)X
b)Z
c)0
d)None of the above
5.If A= 4`b011 and B= 4b`0011, then the result of A**B will be.
a)9
b)6
c)27
d)None of the above
6.If A= 4b`001x and B= 4b`1011, then result of A+B will be.
a)110X
b)1100
c)XXXX
d)None of the above
21
VERILOG INTERVIEW QUESTIONS
22
VERILOG INTERVIEW QUESTIONS
d)Conditional
12.What will be the value of c in following case (after 5 sim units)?
initial begin
a=0; b=1;
c = #5 a+b;
end
a)0
b)1
c)2
d)None of the above
13.What is equivalent hardware to meet the following behavior?
always@(a)
if(a==1'b1)
output =1;
else
output=0;
a)Mux
b)Flipflop
c)Latch
d)None of the above
14.Which of the following statement are correct?
a)UDPs can have only one output
b)UDPs can have 1 to 10 inputs
c)The Z logic value is not supported
d)All of the above
15.The task $stop is provided to
a)End simulation
b)Suspend simulation
c)Exit simulator
d)None of the above
16.Turn off delays means,gate output transition to
a)0
b)1
23
VERILOG INTERVIEW QUESTIONS
c)X
d)Z
17.If there is mismatch in connecting wire such as
W1[7:0]=W2[15:0]
a)The end result is W1[7:0]=W2[15:0]
b)The end result is W1[7:0]=W2[7:0]
c)The end result is W1[15:0]=W2[7:0]
d)None of the above
18.Given the following Verilog code, what value of "a" is displayed?
always @ (clk)
begin
a = 0;
a <= 1;
$display(a);
end
a)0
b)1
c)Both a and b
d)None of the above
19.The LHS of procedural continuous assignments can be
a)Registers or concatenations of registers
b)Nets or concatenation of nets
c)Arrays of nets
d)None of the above
20.For the segment is given below choose the correct answers
bufif0 #(5,6,7) c1(out,in,cntrl)
a)5=rise 6=turnoff 7=fall
b)5=fall 6=rise 7=turnoff
c)5=rise 6=fall 7=turnoff
d)5=turnoff 6=rise 7=fall
21.Which procedural assignments should be used to model a perfect combinational logic
buffer?
a)always@(In)
24
VERILOG INTERVIEW QUESTIONS
# 5 output<=In;
b)always@(In)
output= #5 In;
c)always@(In)
# 5 output=In;
d)always@(In)
output<= #5 In;
22.Which of the following statement is incorrect?
a)Tasks are part of functions
b)Functions are part of tasks
c)Function are always synthesizable
d)Functions have some inputs
23.Which is legal negative number?
a)4'd-3
b)6'-d3
c)-6'd3
d)None of the above
24.In a pure combinational circuit is it necessary to mention all the inputs in sensitivity
list?
a)No
b)Yes
c)It depends on the coding style
d)None of the above
25.Which is not a correct method of specifying time scale in verilog?
a)1ns/1ps
b)10ns/1ps
c)100ns/100ps
d)100ns/110ps
Answers:
1.c 2.a 3.d 4.a 5.c 6.c 7.d 8.a 9.b 10.c 11.a 12.b 13.a 14.d 15.b 16.d 17.b 18.a
19.a 20.c 21.c 22.a 23.c 24.b 25.d
25
VERILOG INTERVIEW QUESTIONS
PART-3
1. What logic is inferred when there are multiple assign statements targeting the same
wire?
It is illegal to specify multiple assign statements to the same wire in a synthesizable code that
will become an output port of the module. The synthesis tools give a syntax error that a net is
being driven by more than one source.
2. What do conditional assignments get inferred into?
Conditionals in a continuous assignment are specified through the “?:” operator. Conditionals
get inferred into a multiplexer. For example, the following is the code for a simple Multiplexer:
wire wire1;
assign wire1 = (sel == 1'b1) ? a : b;
3. Why should a non-blocking assignment be used for sequential logic, and what would
happen if a blocking assignment were used? Compare it with the same code in a
combinational block.
The main difference between the blocking and non-blocking assignment is that, in the blocking
assignment, the RHS immediately gets assigned to the LHS, whereas for the nonblocking
assignment, the assignment to the LHS is scheduled after the RHS is evaluated.
Using blocking statements in a sequential logic
The following is an example of a Verilog module in which the blocking assignments have been
used in the sequential block.
26
VERILOG INTERVIEW QUESTIONS
In the above example, the assignments to the reg1, reg2, reg3, out1 have been made as blocking
assignments. The synthesized result is a single FF, with the d input of in1, and q output of reg3,
as shown in the following figure:
This is because the intermediate results between in1 and out1 were stored in reg1, reg2, and
reg3 in a blocking format. As a result, the evaluation of the final result to out1 didn’t require
waiting for all the events of the RHS to be completed. Rather, they were immediately assigned
to the LHS in the order specified. Observe that the signals reg1, reg2, and reg3 have been
optimized away by synthesis.
Using nonblocking statements in a sequential logic
The following illustration of code uses the nonblocking assignments in a sequential block:
27
VERILOG INTERVIEW QUESTIONS
In the above example, the assignments to the reg1, reg2, reg3, out1 have been made as
nonblocking assignments. The synthesized result is the inference of as many FFs as specified
in the always block [in this case, 4 FFs].
This is because the intermediate results between in1 and out1 were stored in reg1, reg2, and
reg3 in a nonblocking format. As a result, the evaluation of the result to each individual reg
required waiting for all the events of the RHS to be completed. In this case, it was the output
of the previous register controlled by the clk event. As a result, the output is a shift register.
Using blocking statements in a combinational logic
The following example illustrates the use of blocking statements in combinational logic:
In the above example, the blocking assignments are made in a combinational block. Note the
absence of posedge and “<=”, being replaced with “=”, in the assignments. The logic
synthesized out of this is a simple wire between in1 to out1.
28
VERILOG INTERVIEW QUESTIONS
This is because all the assignments have been immediate, and there is no event to wait upon.
4. What are some reusable coding practices for RTL Design?
A reusable design mainly helps in reducing the design time of larger implementations using
IPs.
Register all the outputs of crucial design blocks. This will make the timing interface
easy during system level integration.
If an IP is being developed in both Verilog and VHDL, try to use the constructs that
will be translatable later into VHDL.
Partition the design considering the clock domains and the functional goals.
Avoid instantiation of technology specific gates.
Use parameters instead of hard-coded values in the design.
Avoid clocks and resets that are generated internal to the design.
Avoid glue logic during top level inter-module instantiations.
5. Can the generate construct be nested?
No. The generate construct cannot be nested. It is a syntax error to try to nest the
generate-endgenerate construct.
However, the if, case, and for constructs within the generate endgenerate can be nested.
The constructs can also be used within one another, too, that is, if within case, for within
if etc.
You can also use multiple non-nested generate-endgenerate constructs within the
module.
6. Why is one-hot encoding preferred for FSMs designed for high-speed designs?
Since there is one explicit FF per stage of a one-hot encoded state machine, there is no need
of output state decoding. Hence, the only anticipated delay is the clock to q delay of the FF.
This makes the one-hot encoding mechanism preferable for high-speed operation.
7. Discuss the main differences between $strobe and $monitor.
The differences between $strobe and $monitor are summarized in the following points:
$strobe can be used to create new simulation events, simply by encapsulating the
$strobe system call within a simulation construct that moves simulation time, such as
@(posedge clock), @(negedge clock),@(any_signal) etc.There can exist multiple
$strobe system calls at the same time, with identical or different arguments.
29
VERILOG INTERVIEW QUESTIONS
$monitor stands alone. A given set of arguments of $monitor form their own unique
sensitivity list. Only one $monitor call can be active at any time. Each call to $monitor
replaces any previous call(s) to $monitor.
8. How can I selectively enable or disable monitoring?
$monitor can be selectively enabled or disabled by the $monitoron and the $monitoroff system
calls, respectively. The $monitoron and $monitoroff system calls affect only the most recent
call to $monitor.
30
VERILOG INTERVIEW QUESTIONS
Following is Verilog code for the flip-flop with a positive-edge clock and
synchronous set.
module flop (clk, d, s, q);
input clk, d, s;
output q;
reg q;
always @(posedge clk)
begin
if (s)
q <= 1’b1;
else
q <= d;
end
endmodule
Following is Verilog code for the flip-flop with a positive-edge clock and clock
enable.
module flop (clk, d, ce, q);
input clk, d, ce;
output q;
reg q;
always @(posedge clk)
begin
if (ce)
q <= d;
end
endmodule
31
VERILOG INTERVIEW QUESTIONS
Following is the Verilog code for a latch with a positive gate and an
asynchronous clear.
module latch (g, d, clr, q);
input g, d, clr;
output q;
reg q;
always @(g or d or clr)
begin
if (clr)
q <= 1’b0;
else if (g)
q <= d;
end
endmodule
Following is Verilog code for a 4-bit latch with an inverted gate and an
asynchronous preset.
module latch (g, d, pre, q);
input g, pre;
input [3:0] d;
output [3:0] q;
reg [3:0] q;
always @(g or d or pre)
begin
if (pre)
q <= 4’b1111;
else if (~g)
q <= d;
end endmodule
32
VERILOG INTERVIEW QUESTIONS
Following is the Verilog code for a 4-bit unsigned down counter with
synchronous set.
module counter (clk, s, q);
input clk, s;
output [3:0] q;
reg [3:0] tmp;
always @(posedge clk)
begin
if (s)
tmp <= 4’b1111;
else
tmp <= tmp - 1’b1;
end
assign q = tmp;
endmodule
33
VERILOG INTERVIEW QUESTIONS
Following is the Verilog code for a 4-bit unsigned up/down counter with an
asynchronous clear.
module counter (clk, clr, up_down, q);
input clk, clr, up_down;
output [3:0] q;
34
VERILOG INTERVIEW QUESTIONS
35
VERILOG INTERVIEW QUESTIONS
end
assign q = tmp;
endmodule
Following is the Verilog code for an 8-bit shift-left register with a positive-
edge clock, serial in and serial out.
module shift (clk, si, so);
input clk,si;
output so;
reg [7:0] tmp;
always @(posedge clk)
begin
tmp <= tmp << 1;
tmp[0] <= si;
end
assign so = tmp[7];
endmodule
Following is the Verilog code for an 8-bit shift-left register with a negative-
edge clock, a clock enable, a serial in and a serial out.
module shift (clk, ce, si, so);
input clk, si, ce;
output so;
reg [7:0] tmp;
always @(negedge clk)
begin
if (ce) begin
tmp <= tmp << 1;
tmp[0] <= si;
end
end
assign so = tmp[7];
endmodule
Following is the Verilog code for an 8-bit shift-left register with a positive-
edge clock, asynchronous clear, serial in and serial out.
module shift (clk, clr, si, so);
input clk, si, clr;
output so;
reg [7:0] tmp;
always @(posedge clk or posedge clr)
begin
if (clr)
tmp <= 8’b00000000;
else
tmp <= {tmp[6:0], si};
end
assign so = tmp[7];
endmodule
Following is the Verilog code for an 8-bit shift-left register with a positive-
edge clock, a synchronous set, a serial in and a serial out.
module shift (clk, s, si, so);
input clk, si, s;
output so;
reg [7:0] tmp;
always @(posedge clk)
begin
36
VERILOG INTERVIEW QUESTIONS
if (s)
tmp <= 8’b11111111;
else
tmp <= {tmp[6:0], si};
end
assign so = tmp[7];
endmodule
Following is the Verilog code for an 8-bit shift-left register with a positive-
edge clock, a serial in and a parallel out.
module shift (clk, si, po);
input clk, si;
output [7:0] po;
reg [7:0] tmp;
always @(posedge clk)
begin
tmp <= {tmp[6:0], si};
end
assign po = tmp;
endmodule
Following is the Verilog code for an 8-bit shift-left register with a positive-
edge clock, an asynchronous parallel load, a serial in and a serial out.
module shift (clk, load, si, d, so);
input clk, si, load;
input [7:0] d;
output so;
reg [7:0] tmp;
always @(posedge clk or posedge load)
begin
if (load)
tmp <= d;
else
tmp <= {tmp[6:0], si};
end
assign so = tmp[7];
endmodule
Following is the Verilog code for an 8-bit shift-left register with a positive-
edge clock, a synchronous parallel load, a serial in and a serial out.
module shift (clk, sload, si, d, so);
input clk, si, sload;
input [7:0] d;
output so;
reg [7:0] tmp;
always @(posedge clk)
begin
if (sload)
tmp <= d;
else
tmp <= {tmp[6:0], si};
end
assign so = tmp[7];
endmodule
37
VERILOG INTERVIEW QUESTIONS
Following is the Verilog code for a 4-to-1 1-bit MUX using an If statement.
module mux (a, b, c, d, s, o);
input a,b,c,d;
input [1:0] s;
output o;
reg o;
always @(a or b or c or d or s)
begin
if (s == 2’b00)
o = a;
else if (s == 2’b01)
o = b;
else if (s == 2’b10)
o = c;
else
o = d;
end
endmodule
Following is the Verilog Code for a 4-to-1 1-bit MUX using a Case statement.
module mux (a, b, c, d, s, o);
input a, b, c, d;
input [1:0] s;
output o;
reg o;
always @(a or b or c or d or s)
begin
case (s)
2’b00 : o = a;
2’b01 : o = b;
2’b10 : o = c;
default : o = d;
endcase
end
endmodule
Following is the Verilog code for a 3-to-1 1-bit MUX with a 1-bit latch.
module mux (a, b, c, d, s, o);
input a, b, c, d;
38
VERILOG INTERVIEW QUESTIONS
input [1:0] s;
output o;
reg o;
always @(a or b or c or d or s)
begin
if (s == 2’b00)
o = a;
else if (s == 2’b01)
o = b;
else if (s == 2’b10)
o = c;
end
endmodule
39
VERILOG INTERVIEW QUESTIONS
if (sel[0])
code = 3’b000;
else if (sel[1])
code = 3’b001;
else if (sel[2])
code = 3’b010;
else if (sel[3])
code = 3’b011;
else if (sel[4])
code = 3’b100;
else if (sel[5])
code = 3’b101;
else if (sel[6])
code = 3’b110;
else if (sel[7])
code = 3’b111;
else
code = 3’bxxx;
end
endmodule
Following is the Verilog code for an unsigned 8-bit adder with carry in.
module adder(a, b, ci, sum);
input [7:0] a;
input [7:0] b;
input ci;
output [7:0] sum;
Following is the Verilog code for an unsigned 8-bit adder with carry out.
module adder(a, b, sum, co);
input [7:0] a;
input [7:0] b;
output [7:0] sum;
output co;
wire [8:0] tmp;
assign tmp = a + b;
assign sum = tmp [7:0];
assign co = tmp [8];
40
VERILOG INTERVIEW QUESTIONS
endmodule
Following is the Verilog code for an unsigned 8-bit adder with carry in and
carry out.
module adder(a, b, ci, sum, co);
input ci;
input [7:0] a;
input [7:0] b;
output [7:0] sum;
output co;
wire [8:0] tmp;
endmodule
endmodule
assign res = a * b;
endmodule
41
VERILOG INTERVIEW QUESTIONS
42
VERILOG INTERVIEW QUESTIONS
43
VERILOG INTERVIEW QUESTIONS
do <= RAM[addr];
end
44
VERILOG INTERVIEW QUESTIONS
end
endmodule
Following templates show a single-port RAM in write-first mode.
module raminfr (clk, we, en, addr, di, do);
input clk;
input we;
input en;
input [4:0] addr;
input [3:0] di;
output [3:0] do;
reg [3:0] RAM [31:0];
reg [4:0] read_addr;
always @(posedge clk)
begin
if (en) begin
if (we)
RAM[addr] <= di;
read_addr <= addr;
end
end
assign do = RAM[read_addr];
endmodule
Following is the Verilog code for a single-port RAM with asynchronous read.
module raminfr (clk, we, a, di, do);
input clk;
input we;
input [4:0] a;
input [3:0] di;
output [3:0] do;
reg [3:0] ram [31:0];
always @(posedge clk)
begin
if (we)
ram[a] <= di;
end
assign do = ram[a];
endmodule
45
VERILOG INTERVIEW QUESTIONS
Following is the Verilog code for a single-port RAM with "false" synchronous
read.
module raminfr (clk, we, a, di, do);
input clk;
input we;
input [4:0] a;
input [3:0] di;
output [3:0] do;
reg [3:0] ram [31:0];
reg [3:0] do;
always @(posedge clk)
begin
if (we)
ram[a] <= di;
do <= ram[a];
end
endmodule
Following is the Verilog code for a single-port RAM with synchronous read
(read through).
module raminfr (clk, we, a, di, do);
input clk;
input we;
input [4:0] a;
input [3:0] di;
output [3:0] do;
reg [3:0] ram [31:0];
reg [4:0] read_a;
always @(posedge clk)
begin
if (we)
ram[a] <= di;
read_a <= a;
end
assign do = ram[read_a];
endmodule
Following is the Verilog code for a single-port block RAM with enable.
module raminfr (clk, en, we, a, di, do);
input clk;
input en;
input we;
input [4:0] a;
input [3:0] di;
output [3:0] do;
reg [3:0] ram [31:0];
reg [4:0] read_a;
always @(posedge clk)
begin
if (en) begin
if (we)
ram[a] <= di;
read_a <= a;
end
end
assign do = ram[read_a];
endmodule
46
VERILOG INTERVIEW QUESTIONS
Following is the Verilog code for a dual-port RAM with asynchronous read.
module raminfr (clk, we, a, dpra, di, spo, dpo);
input clk;
input we;
input [4:0] a;
input [4:0] dpra;
input [3:0] di;
output [3:0] spo;
output [3:0] dpo;
reg [3:0] ram [31:0];
always @(posedge clk)
begin
if (we)
ram[a] <= di;
end
assign spo = ram[a];
assign dpo = ram[dpra];
endmodule
Following is the Verilog code for a dual-port RAM with false synchronous
read.
module raminfr (clk, we, a, dpra, di, spo, dpo);
input clk;
input we;
input [4:0] a;
input [4:0] dpra;
input [3:0] di;
output [3:0] spo;
output [3:0] dpo;
reg [3:0] ram [31:0];
reg [3:0] spo;
reg [3:0] dpo;
always @(posedge clk)
begin
if (we)
ram[a] <= di;
spo = ram[a];
dpo = ram[dpra];
end
endmodule
Following is the Verilog code for a dual-port RAM with synchronous read
(read through).
module raminfr (clk, we, a, dpra, di, spo, dpo);
input clk;
input we;
input [4:0] a;
input [4:0] dpra;
input [3:0] di;
output [3:0] spo;
output [3:0] dpo;
reg [3:0] ram [31:0];
reg [4:0] read_a;
reg [4:0] read_dpra;
always @(posedge clk)
begin
if (we)
47
VERILOG INTERVIEW QUESTIONS
Following is the Verilog code for a dual-port RAM with enable on each port.
module raminfr (clk, ena, enb, wea, addra, addrb, dia, doa, dob);
input clk, ena, enb, wea;
input [4:0] addra, addrb;
input [3:0] dia;
output [3:0] doa, dob;
reg [3:0] ram [31:0];
reg [4:0] read_addra, read_addrb;
always @(posedge clk)
begin
if (ena) begin
if (wea) begin
ram[addra] <= dia;
end
end
end
48
VERILOG INTERVIEW QUESTIONS
always @(raddr)
begin
if (en)
case(raddr)
4’b0000: data = 4’b0010;
4’b0001: data = 4’b0010;
4’b0010: data = 4’b1110;
4’b0011: data = 4’b0010;
4’b0100: data = 4’b0100;
4’b0101: data = 4’b1010;
4’b0110: data = 4’b1100;
4’b0111: data = 4’b0000;
4’b1000: data = 4’b1010;
4’b1001: data = 4’b0010;
4’b1010: data = 4’b1110;
4’b1011: data = 4’b0010;
4’b1100: data = 4’b0100;
4’b1101: data = 4’b1010;
4’b1110: data = 4’b1100;
4’b1111: data = 4’b0000;
default: data = 4’bXXXX;
endcase
end
endmodule
49
VERILOG INTERVIEW QUESTIONS
50
VERILOG INTERVIEW QUESTIONS
51
VERILOG INTERVIEW QUESTIONS
1.What value is inferred when multiple procedural assignments made to the same reg
variable in an always block ?
When multiple procedural assignments are made to the same reg (register) variable within the
same always block in a hardware description language like Verilog or VHDL, the last
assignment made in that block will take precedence. In other words, the value assigned to the
register variable in the last procedural assignment statement will be inferred as the final value
of that register.
4.What are the difference between Module and Task in Reusable logic ?
In Verilog, both tasks and modules are used to encapsulate and create reusable logic. However,
they serve different purposes and have distinct characteristics:
1. Modules:
Modules are used to define reusable hardware components.
Modules describe the behaviour and structure of a part of your design.
Modules can have input and output ports, internal logic (combinational and sequential),
and hierarchical instances.
Modules are instantiated multiple times in your design hierarchy to create instances of
the hardware component.
Modules are intended for structural representation and instantiation.
2. Tasks:
Tasks are used to define reusable procedural blocks of code.
Tasks do not have ports like modules; instead, they have input and output arguments.
Tasks are called with arguments and execute a sequence of procedural statements.
Tasks are useful for encapsulating sequences of operations or algorithms that need to
be reused in different parts of your design.
Tasks are intended for behavioural representation.
In summary, modules are used to represent reusable hardware components with structural and
hierarchical characteristics, while tasks are used to encapsulate and reuse procedural blocks of
code with behavioural characteristics. Depending on your design needs, you can use both
modules and tasks to create efficient and maintainable Verilog code.
An SSD has 7 segments and theoretically we can use it to display 2^7 = 128 combinations of
characters. But most of these combinations, does not make sense to a human eye. Decimal
numbers can be displayed correctly on a 7-segment panel as shown below:
The image on the right indicates the order in which the panels are accessed normally. This is
done using a 7-bit vector.
Here, I have written a Verilog code which takes in a BCD number and converts it into a 6-bit
vector format, which the seven-segment panel understands. Note that, to light up an individual
panel, we must switch it OFF(pass '0' through it).
//Verilog module.
module segment7(
bcd,
seg
);
4 : seg = 7'b1001100;
5 : seg = 7'b0100100;
6 : seg = 7'b0100000;
7 : seg = 7'b0001111;
8 : seg = 7'b0000000;
9 : seg = 7'b0000100;
//switch off 7 segment character when the bcd digit is not a decimal number.
default : seg = 7'b1111111;
endcase
end
endmodule
Testbench:
module tb_segment7;
//Apply inputs
initial begin
for(i = 0;i < 16;i = i+1) //run loop for 0 to 15.
begin
bcd = i;
#10; //wait for 10 ns
end
end
endmodule
initial begin
mynumber = $random;
end
endmodule
VERILOG INTERVIEW QUESTIONS
If random numbers in a certain range are required, this can be achieved with the following
code:
module rand();
integer mynumber;
initial begin
mynumber = {$random} %10 ; // random numbers between 0 and 9.
end
endmodule
Simulations are required to operate on a given timescale that has a limited precision as specified
by the timescale directive. Hence it is important that the precision of timescale is good enough
to represent a clock period. For example, if the frequency of the clock is set to 640000 kHz,
then its clock period will be 1.5625 ns for which a timescale precision of 1ps will not suffice
because there is an extra point to be represented. Hence simulation will round off the last digit
to fit into the 3 point timescale precision. This will bump up the clock period to 1.563 which
actually represents 639795 kHz !
The following Verilog clock generator module has three parameters to tweak the three different
properties as discussed above. The module has an input enable that allows the clock to be
disabled and enabled as required. When multiple clocks are controlled by a
common enable signal, they can be relatively phased easily.
`timescale 1ns/1ps
reg start_clk;
initial begin
$display("FREQ = %0d kHz", FREQ);
$display("PHASE = %0d deg", PHASE);
$display("DUTY = %0d %%", DUTY);
VERILOG INTERVIEW QUESTIONS
// Achieve duty cycle by a skewed clock on/off time and let this
// run as long as the clocks are turned on.
always @(posedge start_clk) begin
if (start_clk) begin
clk = 1;
clk = 0;
end
end
endmodule
VERILOG INTERVIEW QUESTIONS
typedef enum logic [2:0] {S0, S1, S2, S3, S4} state_t;
state_t state;
if(~din)
state <= S3;
else
state <= S1;
end
S3: begin
dout <= 1'b0;
if(din)
state <= S4;
else
state <= S0;
end
S4: begin
dout <= 1'b1;
if(din)
state <= S1;
else
state <= S0;
end
endcase
end
end
endmodule
end
endmodule
typedef enum logic [2:0] {S0, S1, S2, S3, S4} state_t;
state_t state;
endmodule
end
end
endmodule
Sequence Detector 110 (Moore Machine + Mealy Machine)
1) Moore Machine
endmodule
2) Mealy Machine
endmodule