System Verilog Interview Questions
System Verilog Interview Questions
u
2. What is the difference between a packed and unpacked struct?
ur
- A packed structure consists of a bit fields , which are
packed together in a memory without gap. They are easily
converted to and form bit vectors. An unpacked structure has
an implementation-dependent packing normally matching the C
G
compiler.
- Packed Structure can be used as a whole with arithmetic and
logical operator. The structures are declared using the
sic
int a;
shortint b;
byte c;
bit [7:0] d;
} pack1 ; //signed, 2 states packed
structure
u
4. Which of the array types: dynamic array or associative array,
are good to model really large arrays?
ur
- To model really large arrays in system Verilog , associative
arrays are generally better than dynamic array as it is
efficient for sparse data and enables flexible indexing.
G
5. Given a dynamic array of size 100, how can the array be re-
sized to hold 200 elements while the lower 100 elements are
sic
preserved as original?
- bit dyn_arr [];
dyn_arr = new[100]; // dynamic array of size 100
dyn_arr = new[200](dyn_arr); //resizing of dynamic array to
hold 200 elements preserving lower 100 elements
A
7. What is OOPS?
- OOPS stands for Object Oriented Programming , it brings
object-oriented features into hardware verification, making
testbenches more modular, reusable and scalable.
u
class Animal;
function void speak();
endfunction
endclass
ur
$display("Animal speaks");
G
class Dog extends Animal;
function void speak();
$display("Dog barks");
sic
endfunction
endclass
// Dog inherits from Animal and overrides the speak()
method.
A
module test;
Animal a;
Dog d = new();
initial begin
a = d; // base class handle to derived class
object
a.speak(); // dynamic dispatch → Dog's speak() is called
end
endmodule
u
- A class is a type that includes data and subroutines that
operate on that data.
ur
- A class defines a data type. An object is an instance of
that class. An object is used by first declaring a variable
of that class type and then creating an object of that
class(using the new function) and assigning it to the
G
variable.
- Ex: Packet p; // declare a variable of class packet
P = new; // initialize variable to a new allocated
sic
task do_work();
$display("Child: do_work()");
endtask
endclass
module tb;
Parent p;
Child c;
u
initial begin
p = new();
p = c;
p.show_msg();
p.do_work();
end
ur
// Calls Child's function
// Calls Child's task
G
endmodule
sic
u
endfunction
endclass
module tb;
Base obj;
ur
G
initial begin
obj = new Derived();
obj.show(); // Calls Derived::show()
sic
// Derived class 1
class Circle extends Shape;
function void draw();
$display("Drawing Circle");
endfunction
endclass
// Derived class 2
class Square extends Shape;
function void draw();
$display("Drawing Square");
endfunction
endclass
// Test
module test;
u
Shape s;
Circle c = new();
Square sq = new();
initial begin
s = c;
ur
G
s.draw(); // Outputs: Drawing Circle
s = sq;
sic
initial begin
u
int a = 5;
int b = 5;
pass_by_value(a);
ur
$display("After pass_by_value: a = %0d", a);
// a remains 5
G
pass_by_ref(b);
$display("After pass_by_ref: b = %0d", b);
sic
// b becomes 15
end
endmodule
A
initial begin
u
int a = 5;
modify_ref(a);
ur
$display("After modify_ref: a = %0d", a);
// a becomes 15
G
read_constref(a);
$display("After read_constref: a = %0d", a);
// a remains 15
sic
end
endmodule
A
u
the declaration/definition of variables, functions,
classes, or modules before they are referenced.
ur
Use forward declarations:In SystemVerilog, one can use
forward declarations for classes or functions to let
the compiler know about their existence before full
definition.
G
- CODE EXAMPLE:
// forward declarations of functions to avoid forward
referencing
sic
class MyClass;
function void display();
// function prototype (declaration)
endfunction
endclass
A
class ClassB;
ClassAa; // ClassA used here → circular reference!
endclass
u
Fix by Forward Declaration:
class ClassB; // Forward declaration
endclass
class ClassA;
ur
ClassBb; // Now ClassB is known
G
endclass
class ClassB;
sic
u
t1.
ur
To do a full (deep) copy, where everything (including nested
objects) are copied, custom code is typically needed.
For example:
Packet p1 = new;
G
Packet p2 = new;
p2.copy(p1)
sic
u
- // named union type
num n;
21.
-
What is "this"?
ur
Here i and f share the same memory.
u
properties) from outside the class hierarchy.
Access to public or protected class members of a
ur
superclass from within the derived classes.
Access to type declarations and enumeration named
constants declared inside the class from outside the
G
class hierarchy or from within derived classes.
- Ex. class Base;
typedef enum {bin,oct,dec,hex} radix;
static task print( radix r, integer n ); ... endtask
sic
endclass
...
Base b = new();
int bin = 123;
A
u
- This is done in two steps. Declare, within the class body,
the method prototypes, the full argument specification plus
-
the extern qualifier. ur
The extern qualifier indicates that the body of the method
(its implementation) is to be found outside the declaration.
Then, outside the class declaration, declare the full
G
method. And, to tie the method back to its class, qualify
the method name with the class name and a pair of colons:
- Ex. class MyClass;
sic
Packet p;
initial begin
if (p == null)
$display("Handle p is null — no object allocated.");
else
$display("Handle p is valid.");
// Allocate object
p = new();
if (p != null)
$display("Now handle p points to an object.");
end
u
protected data members of a System Verilog class?
- Public:
ur
Class data members and methods are accessible from
anywhere: inside the class, outside the class, and in
derived classes. It is a default access type in
SystemVerilog.
G
Ex. class A;
int a = 10; // public by default
endclass
sic
initial begin
Aobj = new;
$display("a = %0d", obj.a); // ✅ Allowed
end
A
- Private:
Class data members and methods are accessible only inside
the class where it is defined and not accessible from
outside the class or in derived classes.
Ex. class A;
private int x = 42;
function void show();
$display("x = %0d", x); // ✅ Allowed
endfunction
endclass
class B extends A;
function void try_access();
$display("x = %0d", x); ❌ Error: private member not
accessible
endfunction
endclass
- Protected:
Class data members and methodsare accessible inside the
class and in derived classes and not accessible from outside
the class hierarchy.
Ex. class A;
protected int y = 7;
endclass
class B extends A;
function void show();
u
$display("y = %0d", y); // ✅ Allowed
endfunction
endclass
initial begin
B b = new();
ur
G
$display("%0d", b.y); ❌ Error: protected member not
accessible from outside
end
sic
u
- Ex. interface i2;
wire a, b, c, d;
-
endinterface
ur
modport master (input a, b, output c, d);
modport slave (output a, b, input c, d);
u
- Virtual interfaces provide a reference (handle) to a normal
interface, so that classes (like drivers, monitors, etc.)
ur
can access and control design signals dynamically at
runtime.
G
35. What are Semaphores? When are they used?
- A semaphore is a SystemVerilog built-in class used to
sic
initial begin
sema.get(1); // Wait to acquire
// Critical section
sema.put(1); // Release
end
u
- Ex. mailbox mbx = new();
initial begin
end
initial begin
ur
mbx.put(100); // Producer sends value
G
int val;
mbx.get(val); // Consumer receives value
$display("Received: %0d", val);
end
sic
u
int q[$]; // Dynamic queue
initial begin
q.push_back(100); ur
// Add item
int val = q.pop_front(); // Remove item
end
G
39. What are the advantages of linkedlist over the queue ?
- Advantages of linkedlist over the queue :
sic
initial begin
//Queue Initialization:
queue = {7,3,1,0,8};
u
$display("----- Queue elements with index -----");
foreach(queue[i])
ur
$display("\tqueue[%0d] = %0d",i,queue[i]);
$display("-------------------------------------\n");
temp_var = queue[index];
$display("Value of Index %0d in Queue is
%0d",index,temp_var);
end //}
$display("After Queue size is %0d",queue.size());
A
end
endmodule
41. What data structure is used to store data in your
environment and why ?
- Data structure used to store data in environment are:
Queue (queue[$]) :can grow and shrink during
simulation and elements can be accessed by index.
CODE EXAMPLE:
module simple_queue_example;
int queue[$]; // Declare a queue of integers
initial begin
// Add elements to the queue
queue.push_back(5);
queue.push_back(10);
queue.push_back(15);
u
endmodule
ur
Associative Array: Useful when keys are not sequential
integers or are strings, enums, etc. Useful for sparse
or random access with custom keys.
CODE EXAMPLE:
G
module assoc_array_example;
// Declare associative array with int key and int
value
sic
int assoc_array[string];
initial begin
assoc_array["apple"] = 10;
assoc_array["banana"] = 20;
A
assoc_array["cherry"] = 30;
foreach (assoc_array[key])
$display("Key = %s, Value = %0d", key,
assoc_array[key]);
end
endmodule
foreach(dyn_array[i])
$display("dyn_array[%0d] = %0d", i,
dyn_array[i]);
// Resize to 5
dyn_array = new[5];
dyn_array[3] = 40;
u
dyn_array[4] = 50;
ur
$display("After resizing:");
foreach(dyn_array[i])
$display("dyn_array[%0d] = %0d", i,
dyn_array[i]);
G
end
endmodule
sic
u
initial begin
result
end
ur
randomize(data) with { data>0 }; // Randomize data > 0
$display("data = 0x%0h", data); // Display the
G
endmodule
// randomization applies to variables in the current
procedural scope
sic
module tb;
initial begin
packet p = new();
if (p.randomize())
u
$display(">> Randomization SUCCESS");
else
end
endmodule
ur
$display(">> Randomization FAILED");
G
45. What is the difference between rand and randc?
- Variables declared with the rand keyword are standard random
sic
u
if (queue_of_unique_values[i] == val)
return 1;
end
return 0;
endfunction
ur
G
initial begin
// Goal: Generate 10 unique random values in the range
0–19
sic
end
u
// Constrain size of dynamic array between 5 and 10
constraint c_array{ d_array.size() > 5; d_array.size() <
10; } ur
// Constrain value at each index to be equal to the index
itself
G
constraint c_val { foreach (d_array[i])
d_array[i] == i;
}
sic
endfunction
endclass
module tb;
Packet pkt;
u
- The production main is defined in terms of three
ur
nonterminals: first, second, and done. When main is chosen,
it generates the sequence, first, second, and done. When
first is generated, it is decomposed into its productions,
which specifies a random choice between add and dec.
G
Similarly, the second production spec ifies a choice between
pop and push. All other productions are terminals; they are
completely specified by their code block, which in the
sic
u
join
end
- ur
Here each thread will generate independent random values.
G
What are pre_randomize() and post_randomize() functions?
- Every class contains built-in pre_randomize() and
post_randomize() functions, that are automatically called by
sic
u
the solver finds values for all variables together to
satisfy all constraints without conflict.
- CODE EXAMPLE:
class seq_item;
ur
rand bit [7:0] val1, val2, val3, val4;
rand bit t1, t2;
G
constraint val_c {val2 > val1;
val3 == val2 - val1;
sic
module constraint_example;
seq_item item;
initial begin
item = new();
repeat(5) begin
item.randomize();
$display("val1 = %0d, val2 = %0d, val3 = %0d, val4 =
%0d", item.val1, item.val2, item.val3, item.val4);
$display("t1 = %0h, t2 = %0h", item.t1, item.t2);
end
end
endmodule
u
- Ex. class Example;
rand int a, b, c;
constraint unique_c {
endclass
}
ur
unique {a, b, c};
G
- This ensures that a, b, and c will be different from each
other every time they are randomized.
sic
module tb;
initial begin
Packet p = new();
// Disable low_range and enable high_range
p.low_range.constraint_mode(0);
p.high_range.constraint_mode(1);
p.randomize();
$display("Addr = %0d", p.addr);
end
endmodule
u
constraint default_val{ soft a == 10; }
// Hard constraint overrides soft
-
ur
constraint limit_val{ a inside {[5:15]}; }
endclass
If limit_val is not defined, a will be 10 due to the soft
constraint.
G
But if a hard constraint defines a different allowed value,
it takes precedence over the soft one.
sic
u
Valid range for a is 0 to (b-1).
Then it randomly assigns values satisfying both.
56.
-
ur
What is the use of packagess?
SystemVerilog packages provide an additional mechanism for
G
sharing parameters, data, type, task, function, sequence,
and property declarations amongst multiple SystemVerilog
modules, interfaces and programs.
- Packages are explicitly named scopes appearing at the
sic
u
class Derived extends Base; endclass
Derived d = new();
Base b = d; ur // Upcasting (implicit)
Derived d2 = Derived'(b); // Downcasting (explicit)
3. Dynamic Cast (using cast() method) :Safe runtime
casting for class handles.
G
Ex. Derived d3;
if (!$cast(d3, b)) //converting base class
handle to derived class handle at runtime.
sic
$display("Cast failed");
4. Wildcard Cast (type'()) :Similar to explicit cast but
used mainly for built-in types like bit, logic, int.
A
u
Input sampling
Synchronous drives
- Ex.
ur
interface intf (input logic clk);
logic req, gnt;
clocking cb@(posedge clk);
input gnt;
G
output req;
endclocking
endinterface
sic
- Here, req will be driven on the posedge of clk, and gnt will
be sampled on the posedge of clk, making testbench timing
predictable.
A
u
- This mimics always @(posedge clk) without using always.
62.
-
ur
Why always block is not allowed in program block?
Always block is not allowed in program block because:
always is used to model hardware behavior, typically
G
inside a module.
program blocks execute in a reactive region, after
hardware logic (RTL) finishes executing.
sic
u
advances.
Procedural Assertion is Useful for simple, single-time
ur
checks while Concurrent Assertion is Ideal for
protocol and timing checks.
G
65. Difference between assert and expect statements?
- Assert:
Checks a condition and immediately reports a failure
sic
if false.
Does not block the current thread (non-blocking).
Failure usually marks the test as failed or triggers
an error.
A
initial begin
// After 15 time units, signal becomes 1
#15 signal = 1;
u
$finish;
end
endmodule
ur
G
66. What is the difference between assumes and assert?
u
Ex( Posedge) :module posedge_example;
ur
logic clk, signal;
always @(posedge signal) begin
$display("posedge detected on
signal at time %0t", $time);
G
end
endmodule
Here the always @(posedge signal) block waits for a
sic
Option Description
join The parent process blocks until all the
processes spawned by this fork complete. .
join_any The parent process blocks until any one of
the processes spawned by this fork complete.
join_none The parent process continues to execute
concurrently with all the processes spawned
by the fork. The spawned processes do not
start executing until the parent thread
executes a blocking statement.
u
CODE EXAMPLE:
initial begin
fork
#10 $display("Task
ur
1");
G
#20 $display("Task 2");
#30 $display("Task 3");
join // Waits for all 3 to complete
end
sic
initial begin
fork
#10 $display("Task A");
#20 $display("Task B");
A
initial begin
fork
#10 $display("X");
#20 $display("Y");
#30 $display("Z");
join_none // Does not wait
$display("Continue immediately");
end
69. How to kill a process in fork/join?
- To kill a process in a fork...join, we can use the process
class in SystemVerilog, specifically the kill() method, by
getting a handle to the process using process::self().
- CODE EXAMPLE:
module kill_fork_example;
process p1; // process handle
initial begin
fork
begin : TASK1
p1 = process::self(); // get handle to current
process
u
$display("[%0t] TASK1 started", $time);
#20;
end
join_none
ur
$display("[%0t] TASK1 completed", $time);
G
#5; // wait for a bit
if (p1.status() != process::FINISHED) begin
$display("[%0t] Killing TASK1", $time);
sic
#10;
$display("[%0t] Simulation finished", $time);
A
end
endmodule
#5;
disable fork; // disable both T1 and T2 threads
u
- CODE EXAMPLE:
task do_test;
fork
join_any
exec1();
exec2();
ur
G
fork
exec3();
exec4();
sic
join_none
wait fork; // block until exec1 ... exec4 complete
endtask
- The disable fork statement terminates all active descendants
(sub-processes) of the calling process.
A
- CODE EXAMPLE:
fork
begin : T1
#10 $display("Thread 1");
end
begin : T2
#20 $display("Thread 2");
end
join_none
#5;
disable fork; // disable both T1 and T2 threads
int count = 0;
initial begin
repeat (5) begin
#10 count++;
end
end
u
final begin
$display("Simulation ended. Final count = %0d", count);
end
endmodule
ur
G
73. What is the difference between initial block and final
block?
sic
reset = 1;
#10 reset = 0;
end
- This sets reset high at time 0 and then de-asserts it after
10 time units.
- The final block executes once at the end of the simulation,
right before the simulation finishes and all processes
terminate.
- CODE EXAMPLE:
final begin
$display("Simulation finished at time %0t", $time);
end
- This prints a message just before the simulator stops.
74. What is callback?What is factory pattern ?
- Callbacks are a general programming concept available in
almost any language — basically, passing a function or
method to be called later in response to an event.
- Factory pattern overrides are a more specific Object-
Oriented Programming (OOP) design technique used to
dynamically instantiate different subclasses at runtime,
allowing flexible object creation without changing the
client code.
75. Explain the difference between data types logic, reg and
wire in system Verilog?
u
- Wire:
Represents a net data type. It models physical wires used
ur
for connecting components.It cannot store values by itself;
it reflects the driven value.Cannot be assigned inside
procedural blocks (always, initial).
CODE EXAMPLE:
G
wire a;
assign a = b &c; // continuous assignment driving wire 'a'
- Reg:
sic
u
2.var logic data_2;
3.wire logic data_3j;
4.bit data_4;
5.var bit data_5;
- logic data_1:
ur
Here data_1 can be assigned in procedural blocks (always,
G
initial) or continuous
assignments. It is of 4-state type.
- var logic data_2:
sic
- bit data_4:
Variable of 1-bit data type. It is 0f 2 – state type having
default value of 0.
- var bit data_5:
Explicit 1-bit variable declaration. Usage is same as bit
data_4
78. What is the difference between bit and logic data type?
- Bit is a 2-state SystemVerilog data type having user-defined
vector size while logic is a 4-state SystemVerilog data type
also having user-defined vector size.
- Default value for bit data type is 0 , logic data type have
default value X(unknown state).
- Bit data type simulates faster than logic data type in
SystemVerilog.
- Ex. bit a; //here a can hold 0 or 1 values
logic a; //here a can hold 0,1,X or Z values
u
code (e.g., lines, branches executed).
Functional Coverage:Measures how much of the design
-
ur
specification and scenarios have been exercised.
CDV improves verification efficiency by providing feedback
to focus test generation, uncover corner cases, and reduce
G
redundant testing, thereby increasing confidence in design
quality.
- SystemVerilog offers powerful language features to easily
specify and control functional coverage, enabling automated,
sic
u
cross mode, enable; // Cross coverage of mode and
enable
82.
endgroup ur
Describe the difference between Code Coverage and
G
Functional Coverage Which is more important and Why we need
them.
sic
u
Program block:
Executes in the reactive region of simulation (after
RTL).Ensures
ur
Testbench never races with RTL.
Clocking block:
G
Provides synchronized sampling and driving of signals based
on a clock.
Has input, output, and inout directions, and optional skew
sic
u
Preponed
Pre-active
Active
Inactive
Pre-NBA
ur
G
NBA
Post-NBA
Observed
sic
Post-observed
Reactive
Postpone
A
initial begin
#5;
$display("Triggering event at time %0t", $time);
->my_event;
end
endmodule
- This code defines and triggers my_event after 5 time units.
u
event is triggered (->) after the @ has been executed.
If -> happens before @, the event is missed, and the
- CODE EXAMPLE:
event done;
ur
process stays blocked.
G
initial begin
// This will miss the trigger if ->done executes first
@(done);
sic
$display("Event caught");
end
initial begin
->done; // If this executes before @(done), the event is
A
missed
end
- The waiting process never unblocks, because it started after
the trigger.
- Wait event control:
wait(event.triggered), allowing the process to always
unblock if the event was triggered in the same timestep—even
if wait executes after the ->. It prevents missed events due
to timing/race conditions.
- CODE EXAMPLE:
event blast;
initial fork
-> blast; // Triggers event
wait (blast.triggered); // Will unblock, even if this
executes after ->blast
join
u
sensi tive to changes to the arguments of a function.
Variables on the left-hand side of assignments within
ur
an always_comb procedure, including variables from the
contents of a called function, shall not be written to
by any other processes, whereas always @* permits
multiple processes to write to the same variable.
G
Statements in an always_comb shall not include those
that block, have blocking timing or event controls, or
fork...join statements.
sic
89. Explain how the timescale unit and precision are taken
when a module does not have any t imescalerdeclaration in RTL.
- When a Verilog/SystemVerilog module doesn't explicitly
define a timescale, it inherits the timescale from the
enclosing module or the default tool-specific timescale. If
a module is nested, it inherits from the closest enclosing
module's timescale. If a module is not nested and doesn't
have a timescale declaration, it defaults to the tool-
specific default, which is often 1 ns / 1 ps.
u
exp_mb.get(exp);
act_mb.get(act);
ur
if (!exp.compare(act))
$error("Mismatch: expected = %0p, actual = %0p",
exp, act);
else
G
$display("Match: %0p", act);
end
endfunction
sic
endclass
91. How parallel case and full cases problems are avoided in
SV?
A
unique case(sel)
2'b00: $display("Option A");
2'b00: $display("Option B"); // Warning: multiple
matches
endcase
priority case(mode)
2'b00: $display("IDLE");
2'b01: $display("RUN");
endcase // Warning: not full if mode = 2'b10 or 2'b11
u
92.
“casez” in System Verilog?
- case :
ur
What is the difference between “case”, “casex” and
case(sel)
2'b00: y = a;
2'b01: y = b;
default: y = d; // If sel = 2'bx1, none of the cases
match — goes to default.
A
endcase
- casex :
Treats both x, z, and ? in both expression and case
items as wildcards.
CODE EXAMPLE:
casex(sel)
4'b1x0z: action();
endcase
// Here, x and z are wild — even sel = 4'b1000
would match.
- casez :
treats z and ? as wildcards (i.e., don't care).
CODE EXAMPLE:
casez(opcode)
4'b1???: action1(); // matches any value starting
with 1
4'b01??: action2();
endcase
// Only z and ? in case items are wildcards — x is
still treated as-is in expressions
u
94. What is the need of alias in SV?
- ur
In SystemVerilog, the alias keyword is used to create
bidirectional aliases between two or more nets or variables,
so that all names refer to the same physical signal. This
ensures that any change to one alias reflects immediately in
G
the others.
- CODE EXAMPLE:
logic a, b;
sic
alias a = b;
This meansa and b are now two names for the same
wire.Changing a changes b, and vice versa.
u
initial begin
clk = 0;
end
ur
forever #5 clk = ~clk; // Toggles every 5 time units
G
endmodule
sic
u
Call a SystemVerilog task/function from C.
CODE EXAMPLE:
u
build_phase
connect_phase
ur
end_of_elaboration_phase
start_of_simulation_phase
run_phase
G
extract_phase
check_phase
report_phase
sic
final_phase
System Description
Task
$display Display strings, variables and expressions
immediately and append newline at the end of
the message.
$write Display strings, variables and expressions
without appending the newline at the end of
the message.
$monitor Monitor signal values upon its changes and
display
$strobe Display strings, variables and expressions at
the end of the current time slot.
- CODE EXAMPLE:
module system_tasks_demo;
reg [3:0] a = 4'd5;
reg [3:0] b = 4'd10;
initial begin
$display("DISPLAY: a=%0d, b=%0d", a, b);
$write("WRITE: a=%0d, b=%0d\n", a, b);
$strobe("STROBE: a=%0d, b=%0d", a, b);
$monitor("MONITOR: a=%0d, b=%0d", a, b);
#5 a = 7;
#5 b = 12;
#5 a = 3;
end
endmodule
u
- SIMULATION OUTPUT:
Time 0:
DISPLAY: a=5, b=10
WRITE: a=5, b=10
(printed immediately
ur
(After all statements at time 0 complete)
G
STROBE: a=5, b=10 ← Printed at end of time 0
(Then due to $monitor)
Time 5: MONITOR: a=7, b=10 ← printed when a changes
sic
104. What are system tasks and functions? Give some example of
A
u
ur
G
sic
A