Verilog Interview Question
Verilog Interview Question
Answer:
• wire is used for combinational logic and represents a connection between circuit
elements. It is continuously driven by an assignment (assign statement).
• reg holds values and is updated in procedural blocks (always or initial blocks).
Example Code:
module wire_vs_reg;
wire w; // A wire (for combinational logic)
reg r; // A reg (for sequential logic)
initial begin
r = 1'b1; // reg can hold values inside procedural blocks
end
endmodule
Key Takeaway:
Answer:
• The initial block executes only once at the start of the simulation.
• The always block runs continuously, responding to changes in specified signals.
Example Code:
module initial_vs_always;
reg x, y;
initial begin
x = 1; // Executes once at time 0
#10 x = 0;
end
Key Takeaway:
Answer:
Example Code:
module blocking_vs_nonblocking;
reg a, b, c;
Key Takeaway:
Code:
module test;
reg a;
initial begin
a = 1;
#5 a = 0;
#5 $display("a = %b", a);
end
endmodule
Answer:
a = 0
Explanation:
• a = 1; executes at time 0.
• a = 0; executes after 5 time units.
• $display prints the value after 10 time units, so a is 0 at that moment.
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
• The timescale directive defines the unit of time and simulation precision, written
as: `timescale 1ns/1ps
Answer:
• Combinational Circuits: The output depends only on the present inputs. There is
no memory or feedback.
• Sequential Circuits: The output depends on both present and past inputs (i.e., they
have memory).
Key Takeaway:
module test;
reg [3:0] a;
initial begin
a = 4'b1010;
#5 a = a + 1;
#5 $display("a = %b", a);
end
endmodule
Answer:
Explanation:
Answer:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
• A race condition occurs when two or more signals update simultaneously, leading
to unpredictable behavior.
• It can be avoided by:
o Using non-blocking (<=) assignments for sequential logic.
o Ensuring a proper sensitivity list in always blocks.
Solution:
Answer:
Example Code:
Key Takeaway:
• <= ensures non-blocking assignments for sequential circuits.
Answer:
Example Code:
module generate_example;
genvar i;
generate
for (i = 0; i < 4; i = i + 1) begin : loop
assign out[i] = in1[i] & in2[i]; // Generates multiple AND
gates
end
endgenerate
endmodule
Key Takeaway:
Answer:
initial begin
force clk = 0; // Overrides clk value
#10 force clk = 1;
end
Key Takeaway:
Answer:
• typedef allows custom data types for better readability and maintainability.
Example Code:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Answer:
Answer:
• Clock gating reduces power consumption by disabling unnecessary clock signals.
• Example:
assign clk_gated = enable & clk;
Answer:
Example Code:
Key Takeaway:
module test;
reg [3:0] a;
initial begin
a = 4'b1001;
#5 a = ~a;
#5 $display("a = %b", a);
end
endmodule
Answer:
Output:
a = 0110
Explanation:
• Initially, a = 1001.
• ~a inverts all bits → 0110.
• Displayed after 10 time units.
Key Takeaway:
Answer:
Example:
always @(posedge clk) begin
a = b; // Blocking
b = a; // Issue: Race condition
end
always @(posedge clk) begin
a <= b; // Non-blocking
b <= a; // No race condition
end
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Key Takeaway:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
• Metastability occurs when a signal changes too close to a clock edge, leading
to an unpredictable state.
• Solutions:
o Use synchronizers (flip-flop chains).
o Ensure correct setup and hold times.
42. How do you design a priority encoder in Verilog?
Answer:
Example Code:
Key Takeaway:
Answer:
Answer:
Answer:
FSM Example:
61. What is the difference between case, casex, and casez statements in
Verilog?
Answer:
Example Code:
case (sel)
2'b00: out = a;
2'b01: out = b;
default: out = 0;
endcase
Key Takeaway:
module test;
reg [3:0] a = 4'b1010;
initial begin
#5 a = a >> 1;
#5 $display("a = %b", a);
end
endmodule
Answer:
Output:
a = 0101
Explanation:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
always @(a or b)
if (a)
y = b; // No else block → Latch inferred
Solution:
always @(a or b)
if (a)
y = b;
else
y = 0; // Ensures no latch
Key Takeaway:
Answer:
Example Code:
initial begin
a = 0; // Runs once
end
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
initial begin
wait (signal == 1);
$display("Signal is now 1");
end
Key Takeaway:
Answer:
Example Code:
module alu(input [3:0] a, b, input [1:0] op, output reg [3:0] result);
always @(*) begin
case (op)
2'b00: result = a + b;
2'b01: result = a - b;
2'b10: result = a & b;
2'b11: result = a | b;
endcase
end
endmodule
Key Takeaway:
76. What is the difference between a generate block and a for loop in
Verilog?
Answer:
module gen_example;
genvar i;
generate
for (i = 0; i < 4; i = i + 1) begin : loop_name
some_module inst (.in(i), .out(out[i]));
end
endgenerate
endmodule
Key Takeaway:
module test;
reg a, b;
initial begin
a = 1;
#5 a = 0;
b = a;
$display("b = %b", b);
end
endmodule
Answer:
Output:
b = 1
Explanation:
Answer:
Example Code:
module top;
defparam my_inst.WIDTH = 16; // Overrides parameter
my_module my_inst(.out(out));
endmodule
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
wire a, b;
assign b = a; // Continuous assignment
reg c;
always @(posedge clk)
c = b; // Procedural assignment
Key Takeaway:
• Use wire for combinational logic and reg inside always blocks.
Answer:
Answer:
• A tri-state buffer allows a signal to be driven high, low, or left floating (Z).
Example Code:
Key Takeaway:
• Each clock cycle, the value of d is shifted into the q register, discarding the oldest
bit.
Answer:
Example Code:
initial begin
force my_signal = 1'b1;
#10 release my_signal;
end
Key Takeaway:
Answer:
Type Example
Synthesizable always, assign, case, if-else
Non- initial, #delay, $display,
Synthesizable $monitor
Answer:
Example Code:
always @(posedge clk or posedge reset)
if (reset)
q <= 0;
else
q <= d;
Key Takeaway:
module comb;
reg a, b, c;
wire y;
assign y = (a & b) | c;
endmodule
Answer:
Answer:
Example Code:
Answer:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
module example #(parameter WIDTH = 8) ();
localparam DEPTH = 16; // Cannot be overridden
endmodule
Key Takeaway:
module test;
reg a;
initial begin
a = 1'bx;
#5 a = 1'b0;
#5 $display("a = %b", a);
end
endmodule
Answer:
Output:
a = 0
Explanation:
Answer:
• Defines time unit and precision for delays and simulation timing.
Example:
`timescale 1ns / 1ps // 1 ns time unit, 1 ps precision
Key Takeaway:
Answer:
Example Code:
Answer:
module test;
reg [3:0] a;
initial begin
a = 4'b1101;
#10 a = a << 1;
$display("a = %b", a);
end
endmodule
Answer:
Output:
a = 1010
Explanation:
Answer:
Example Code:
initial begin
$display("Runs only once at time 0");
end
always @(posedge clk) begin
$display("Runs on every clock edge");
end
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Example Code:
// Synthesizable
always @(posedge clk) begin
q <= d;
end
// Non-synthesizable
initial begin
#10 q = 1'b1; // Delay not synthesizable
$display("Simulation message");
end
2. Write a Verilog code for a 4-bit full adder using structural modeling.
Answer:
Key Takeaway:
3. What is a clock divider, and how can you implement one in Verilog?
Answer:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Key Takeaway:
6. What are metastability issues in Verilog, and how can they be avoided?
Answer:
• Metastability occurs when a signal changes too close to the clock edge.
• Solution: Use synchronizer flip-flops for clock domain crossing.
Example Code:
Answer:
Key Takeaway:
Answer:
module alu(input [3:0] a, input [3:0] b, input [1:0] op, output reg
[3:0] result);
always @(*) begin
case (op)
2'b00: result = a + b; // Addition
2'b01: result = a - b; // Subtraction
2'b10: result = a & b; // AND
2'b11: result = a | b; // OR
endcase
end
endmodule
Key Takeaway:
Answer:
10. Write Verilog code for a Mealy FSM that detects the sequence "101".
Answer:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
module dual_port_ram(
input clk, input we, input [4:0] addr_r, input [4:0] addr_w,
input [7:0] data_in, output reg [7:0] data_out
);
reg [7:0] mem [31:0];
Key Takeaway:
Answer:
module uart_tx(input clk, input rst, input start, input [7:0] data,
output reg tx);
reg [3:0] bit_count;
reg [9:0] shift_reg;
Key Takeaway:
Answer:
Example Code:
module tb;
reg clk, rst;
wire out;
initial begin
rst = 1; #10 rst = 0;
#100 $finish;
end
Key Takeaway:
20. Write a Verilog code for a 4:1 multiplexer using case statements.
Answer:
Key Takeaway:
• Multiplexers select one input based on control signals.
Answer:
Example Code:
// Non-Synthesizable Delay
initial begin
#5 a = 1; // Adds a 5-time unit delay in simulation
end
// Synthesizable Delay
always @(posedge clk) begin
a <= b; // Registers provide delay in hardware
end
Key Takeaway:
Answer:
Key Takeaway:
• Used in sequential circuits for storing data.
23. What are the main differences between combinational and sequential
circuits?
Answer:
Answer:
module counter(input clk, input rst, input en, output reg [3:0]
count);
always @(posedge clk or posedge rst) begin
if (rst) count <= 0;
else if (en) count <= count + 1;
end
endmodule
Key Takeaway:
Answer:
Answer:
Answer:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Answer:
Key Takeaway:
Answer:
• generate is used for conditional instantiation and loop-based hardware
generation.
• parameter is a constant used to make the design configurable.
Key Takeaway:
• Use parameter for fixed values and generate for conditional instantiation.
Answer:
Example Code:
Key Takeaway:
Answer:
module shift_reg(input clk, input rst, input d_in, output reg [7:0]
q);
always @(posedge clk or posedge rst) begin
if (rst) q <= 8'b0;
else q <= {q[6:0], d_in}; // Shift left
end
endmodule
Key Takeaway:
Answer:
• Metastability occurs when a flip-flop is triggered near its setup/hold time, causing
an undefined output.
• To prevent it, use synchronization registers.
Key Takeaway:
35. What is a state machine in Verilog, and how do you implement it?
Answer:
module fsm(input clk, input rst, input in, output reg [1:0] state);
parameter S0 = 2'b00, S1 = 2'b01, S2 = 2'b10;
Key Takeaway:
Answer:
Key Takeaway:
• Ripple Carry Adder is a basic arithmetic unit but slow due to carry propagation.
Answer:
• Gray Code changes only one bit per transition, reducing glitches.
Example Code:
module gray_counter(input clk, input rst, output reg [3:0] gray);
reg [3:0] binary;
always @(posedge clk or posedge rst) begin
if (rst) binary <= 0;
else binary <= binary + 1;
end
always @(*) gray = binary ^ (binary >> 1);
endmodule
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
• A Watchdog Timer (WDT) resets the system if the software fails to respond in time.
Example Code:
Key Takeaway:
Answer:
Answer:
44. How do you implement an XOR gate using only NAND gates?
Answer:
assign y = ~(~(a & ~(a & b)) & ~(b & ~(a & b)));
Answer:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Example Code:
always_comb begin
y = a & b; // Combinational logic
end
Key Takeaway:
49. What is a Look-Ahead Carry Adder, and how is it better than a Ripple
Carry Adder?
Answer:
assign sum = p ^ c;
endmodule
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Key Takeaway:
Answer:
module uart_tx(input clk, input rst, input [7:0] data, input start,
output reg tx);
reg [3:0] bit_count;
always @(posedge clk or posedge rst) begin
if (rst) begin
bit_count <= 0;
tx <= 1; // Idle state
end else if (start) begin
tx <= 0; // Start bit
repeat (8) begin
tx <= data[bit_count];
bit_count <= bit_count + 1;
end
tx <= 1; // Stop bit
end
end
endmodule
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Example Code:
Key Takeaway:
Answer:
Answer:
Answer:
Answer:
module i2c_master(
input clk, input rst, input start,
input [7:0] data_in, output reg sda, output reg scl
);
reg [3:0] state;
Key Takeaway:
Answer:
• SPI is a full-duplex serial protocol using SCLK, MOSI, MISO, and CS.
• The Master controls the clock and selects the Slave using Chip Select (CS).
Key Takeaway:
Answer:
Key Takeaway:
Answer:
Answer:
module multi_core_comm (
input clk, input [31:0] core1_data, input core1_wr,
input core2_rd, output reg [31:0] core2_data
);
reg [31:0] shared_mem;
Key Takeaway:
Answer:
Answer:
Answer:
• AXI Stream is used for high-speed data transfer without addressing overhead.
• Uses handshaking signals like tvalid, tready, and tdata.
Answer:
Answer:
Answer:
Key Takeaway:
• RISC-V uses simple and modular instruction sets for efficient CPU design.
Answer:
• Convolutional Neural Networks (CNNs) are used in deep learning for image
processing.
• FPGA-based accelerators use parallel computation for fast inference.
• Key components:
o Convolution Engine: Performs matrix multiplications.
o Activation Unit: Applies ReLU, Sigmoid, etc.
o Pooling Unit: Reduces feature map dimensions.
23. How do you implement a NoC (Network-on-Chip) in Verilog?
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
Answer:
module tmr (
input logic a, b, c,
output logic result
);
assign result = (a & b) | (b & c) | (c & a);
endmodule
Key Takeaway:
Answer:
Answer:
Answer:
• PCI Express (PCIe) is a high-speed serial communication interface.
• Key features: lane configuration, packet-based transfer, and error handling.
Answer:
Answer:
Answer:
Answer:
• Techniques:
o Clock Gating: Disables clock for unused circuits.
o Power Gating: Disconnects power to idle sections.
o Dynamic Voltage Scaling: Adjusts voltage based on workload.
39. How do you design a Real-Time Embedded System in Verilog?
Answer:
Answer: