SystemVerilog Topics PPT [Autosaved]
SystemVerilog Topics PPT [Autosaved]
Blocking
Contexts
• Blocking Statements and Non-Blocking statements
• Examples
• Verilog Regions
• System Verilog Regions
• System tasks
Blocking and Non-Blocking
Assignment
Example of blocking assigments
Example of Non-blocking
Assignment
Example
Verilog Regions
• Active region Continuous assignments
Procedural assignments
Active region
• Inactive region $display and $write
Evaluate the RHS of Non-blocking
• NBA region
(Non-blocking Assignments) Inactive region #0 blocking assignment
• $monitor region
Updation LHS value of non-
NBA blocking Assign
Module Active
Inactive
NBA
Re-NBA
Postponed
System tasks
$display:-executed in the 0 simulation time
$monitor:-executes every time whenever the value changes
$strobe:-executes at the end of the simulation
$write:- To display strings, variables, and expressions without
appending the newline at the end of the message
$finish:- terminate the current simulation
$stop:- The $stop task suspends the simulation at the point where it is called
Example of $display and $write
Example of $display and $strobe
Example of $monitor
Events
Introduction
• Events are used to synchronize the execution of the process in
conjunction with wait statements to control the stimulus time.
Wait Order
• Wait for events to be triggered in the given order and issue an error if
any event executes out of order.
Merge Events
• When one events variable is assigned
to another, all processes waiting for the
first event to trigger will wait until the
second variable if triggered.
Threads
Types of fork join
Fork join
Fork-join :- In fork-join, all processes start simultaneously,
and join will wait for all processes to be completed.
Fork_join any
• In fork-join_any, all processes start simultaneously and
join_any will wait for any one process to be completed
Fork_join none
• In fork-join_none, all processes start simultaneously and join_none will not wait for any
process to be completed.
• So, we can say that fork-join and fork-join_any is blocked due to process execution time,
whereas fork-join_none is not blocked due to any process.
Disable fork join
Wait fork
Randomization
Introduction
• Random variables
• Randomization methods
• Scope randomization
• Disable randomization
• Advantages
Random variables
• Variables are declared random using the rand or randc keyword.
• randc is random-cyclic. For the variables declared with the randc
keyword, on randomization variable values don’t repeat a random
value until every possible value has been assigned.
• Example:
Randomization methods
• Pre randomization: Function is defines within the same class whose
object will be randomization before.
• Post randomization: Function is also defines within the same class
whose object will be randomization after.
Scope randomization
• The scope randomize function, std::randomize(), enables users to
randomize data in the current scope without the need to define a
class or instantiate a class object.
Disable randomization
• Randomization of variable in a class can be disable by using
rand_mode method call.
Advantages
Syntax:-
Constraint <Constraint_name> {<variable> inside {….};}
Inline Constraint
Syntax:-
Class_handle.randomize( ) with {….;};
Soft constraint
Syntax:-
Constraint <Constraint_name> {soft <variable> inside {….};}
Distribution constraint/weighted constraint
Syntax:-
constraint constraint_name {<variable> dist {[1:5]:=10 }; }
constraint constraint_name {<variable> dist {[1:5]:/10}; }
Implication constraint
Syntax:-
Constraint constraint_name {<expression> -> <constraint>;}
constraint scale_c { (scale == LOW) -> value <50; }
Bi-directional constraint
Syntax:-
Constraint constraint_name1 {variable_1 > value, variable_2< value;
Constraint constraint_name2 {variable_1 < variable_2;}
Unique constraint & randc
Syntax:-
constraint <constraint name> {unique {array or variable};}
Foreach loop constraint
Syntax:-
constraint <constraint_name> { foreach(variable[i]) variable[i] <condition>}
Solve before Constraint
• Solve before is the constraint property. solve before is used inside the
constraint block to specify the order of constraint solving.
• If the variables are dependent, due to the bidirectional nature of constraints
value of one variable will influence the value of another variable.
Syntax:-
Constraint constraint_name {solve <variable> before {range};}
Disable modes
Syntax :-
Covergroup covergroup_name;
Coverpoint_name: cover point variable_name
Endgroup
Cg Cg_inst=new();
Cross coverage
• The cross-coverage allows having a cross-product between two or more variables or
coverage points within the same covergroup.
• In simple words, cross-coverage is nothing but a set of cross-products of variables or
coverage points.
Syntax:-
<cross_coverage_label> : cross <coverpoint_1>, <coverpoint_2>,..., <coverpoint_n>
Cross coverage Example
class parent;
rand bit [3:0] a,b;
bit [7:0] c;
bit wr_rd;
endclass
module crosscoverage;
parent p1=new();
covergroup cov;
option.per_instance = 1;
l1: coverpoint p1.a;
l2: coverpoint p1.b;
l1xl2: cross l1,l2; //Cross Coverage
endgroup
cov c1=new();
initial begin
repeat(500) begin
assert(p1.randomize());
c1.sample();
end
end
endmodule
Implicit /automatic bins
module eg();
covergroup cg;
option.per_instance = 1;
Y : coverpoint addr;
X : coverpoint data {
option.auto_bin_max = 8;
}
endgroup: cg
initial begin: B1
cg c = new();
repeat(50) begin: B2
{data,addr} = $urandom();
c.sample();
end: B2
end: B1
initial begin: B3
#100;
$stop();
end: B3
endmodule: eg
Explicit bins
module eg();
covergroup cg;
option.per_instance = 1;
Y : coverpoint addr;
endgroup: cg
initial begin: B1
cg c = new();
repeat(50) begin: B2
{data,addr} = $urandom();
c.sample();
end: B2
end: B1
initial begin: B3
#100;
$stop();
end: B3
endmodule: eg
Illegal bins:- Illegal bins are used to capture values that are not expected, such as those that should
never occur in a particular scenario.
program main;
bit [0:2] y;
bit [0:2] values[$]= '{1,6,3,7,3,4,3,5};
covergroup cg;
option.per_instance = 1;
cover_point_y : coverpoint y {
illegal_bins ib = {7};
}
endgroup
cg cg_inst = new();
initial
foreach(values[i]) begin
y = values[i];
cg_inst.sample();
end
endprogram
Ignore bins:- Ignore bins are used to exclude certain values from the coverage model, meaning the
coverage model will not track those excluded values
program main;
bit [0:2] y;
bit [0:2] values[$]= '{1,6,3,7,3,4,3,5};
covergroup cg;
cover_point_y : coverpoint y {
ignore_bins ig = {1,2,3,4,5};
}
endgroup
cg cg_inst = new();
initial
foreach(values[i])
begin
y = values[i];
cg_inst.sample();
end
endprogram
Wildcard bins:- The wildcard bins definition causes all X, Z, or ? to be treated as wildcards for 0 or 1
(similar to the ==? operator).
program main;
reg [0:3] y;
reg [0:3] values[$]= '{ 4'b1100,4'b1101,4'b1110,4'b1111};
covergroup cg;
option.per_instance = 1;
cover_point_y : coverpoint y {
wildcard bins g12_15 = { 4'b11?? } ;
}
endgroup
cg cg_inst = new();
initial
foreach(values[i])
begin
y = values[i];
cg_inst.sample();
end
endprogram
Sampling methods
They are 2 sampling methods
2. Clock method
• covergroup cg @(posedge clk);
Modports and
clocking blocks
Modports
• Within an interface to declare port directions for signals modport
is used.
• The modport also put some restrictions on interface access.
Syntax:-
Modport <name> (input <port_list>, output <port_list>) ;
Clocking block
• To specify the synchronization scheme and timing requirements for an
interface, a clocking block is used.
• The testbench can have multiple clocking blocks but only one clocking block
per clock.
• The clocking block can be declared in the program, module, or interface.
Syntax:-
clocking <clocking_name> (<clocking event>);
Default input <delay> output <delay>;
<signals>
endclocking
SEMAPHORE
Semaphores are typically used for mutual exclusion, access control to
shared resources, and basic synchronization.
Semaphore_name.new()/get()/put()/try_get();
Semaphore Example:
MAILEBOX
A SystemVerilog mailbox is a way of communication between different
processes to exchange data. One process can put data into a mailbox
that stores data internally and can be retrieved by another process.
Mailbox behaves as first-in, first-out (FIFO).
•unbounded mailbox : Size is not specified and number of entries are unlimited
Generic mailbox
The generic mailbox can be put or get data of any data_type like int, bit, byte, string, etc. By default,
the mailbox is a typeless or generic mailbox.
Syntax:
mailbox <mailbox_name>
Parameterized mailbox
The parameterized mailbox can be put or get data of particular data_type. The parameterized mailbox is
useful when data_type needs to be fixed.For differences in data_type, a compilation error is expected.
Syntax:
mailbox #(<type>) <mailbox_name>
Example: https://www.edaplayground.com/x/2PQL
SV Assertions
ASSERTIONS :-
Assertions are used to check the property of a signal.
• It improves the debugging time.
• It improves error detection.
IMMEDIATE ASSERTIONS:
Immediate assertions checks for a condition at the current simulation time.
• In Immediate assertions all the signals will be evaluated at a time.
Syntax : assert (expression)
These Immediate assertions we will use in Combinational Circuits.
EXAMPLE:
Sequence: Property:
sequence <sequence_name>; property <property_name>;
Boolean Expression; test expression or
endsequence sequence_name;
endproperty
Example :
sequence seq;
@(posedge clk) a ##2 b;
endsequence
property p;
seq;
endproperty
assert property(p);
sequence seq_1; property p;
@(posedge clk) a==1; seq_1;
endsequence endproperty
assert property(p);
Reason for once clock cycle delay.
Sequences with timing relationship:
sequence seq;
@(@(posedge clk) <expression>;a ##2 b;
endsequence
property p;
seq;
endproperty
a_1 : assert property(p););
Clock defined in the property definition :
sequence seq;
a ##2 b;
endsequence
property p;
@(posedge clk) seq;
endproperty
• If we want the sequence to be checked only after “a” is high, this can be achieved by
using the implication operator.
• The left-hand side of the implication is called the “antecedent” and the right-hand side
is called as “consequent.”
• If the antecedent succeeds, then the consequent is evaluated.
• If antecedent fails we will call it as a vacious success.
Antecedent --> Consequent.
• The implication construct can be used only in property definitions. It cannot be used in
sequences.
• Below property checks that, if signal “a” is high on a given positive clock edge,
then signal “b” should be high on the next clock edge. And the output will be
given on the next posedge clk due to sv event Schedulers.
property p;
@(posedge clk) a |=> b;
Endproperty
a: assert property(p);
The implication with a fixed delay on the consequent:
• Below property checks that, if signal “a” is high on a given positive clock edge, then
signal “b” should be high after 2 clock cycles.
property p;
@(posedge clk) a |-> ##2 b;
endproperty
a: assert property(p);
Using two sequences to write conditions:
Below property checks that, if the sequence seq_1 is true on a given positive edge of
the clock, then start checking the seq_2 (“d” should be low, 2 clock cycles after
seq_1 is true).
sequence seq_1;
(a && b);
endsequence
sequence seq_2;
##2 !c;
endsequence
property p;
@(posedge clk) seq_1 |-> seq_2;
endpeoperty
a: assert property(p);
EXAMPLE:
Below property checks that, if signal “a” is high on a given positive clock edge, then
within 1 to 4 clock cycles, the signal “b” should be high.
property p;
@(posedge clk) a |-> ##[1:4] b;
endproperty
a: assert property(p);
EXAMPLE:
Below property checks that, if signal “a” is high on a given positive clock edge, then
signal “b” should be high in the same clock cycle or within 4 clock cycles.
property p;
@(posedge clk) a |-> ##[0:4] b;
endproperty
a: assert property(p);
Indefinite timing window:
• The upper limit of the timing window specified in the right-hand side can be defined
with a “$” sign which implies that there is no upper bound for timing.
• The checker will keep checking for a match until the end of the simulation.
• Below property checks that, if signal “a” is high on a given positive clock edge,
then signal “b” will be high eventually starting from the next clock cycle.
property p;
@(posedge clk) a |-> ##[1:$] b;
endproperty
a: assert property(p);
Repetition Operators:
property p;
@(posedge clk) a |-> ##1 b ##1 b ##1 b;
endproperty
a: assert property(p);
• The above property checks that, if the signal “a” is high on given posedge of the clock,
the signal “b” should be high for 3 consecutive clock cycles.
• The Consecutive repetition operator is used to specify that a signal or a sequence will
match continuously for the number of clocks specified.
• Syntax
signal [*n] or sequence [*n]
$rose : returns true if the signal changed from 0 to 1. Otherwise, it returns false.
Syntax : $rose(boolean expression or signal name)
sequence seq_rose;
@(posedge clk) $rose(a);
endsequence
Sequence seq_rose checks that the signal “a” transitions to a value of 1 on every
positive edge of the clock. If the transition does not occur, the assertion will fail.
$fell: returns true if the signal changes from 1 to 0. Otherwise, it returns
false.
• RESULTS
• I is equal to 20
Case and repeat:
• EXAMPLE : case and repeat
• program main ;
• integer i;
• initial begin
• repeat(10)begin
• i = $random();
• case(1) begin
• (i<0) :$display(" i is less than zero i==%d\n",i);
• (i>0) :$display(" i is grater than zero i=%d\n",i);
• (i == 0):$display(" i is equal to zero i=%d\n",i);
• end
• end
• end
• endprogram
• RESULTS
• Value i = 0
• Value i = 1
• Value i = 2
• Value i = 3
• Value i = 4
• Value i = 5
• Value i = 6
• Value i = 7
While-loop
• EXAMPLE : whileloop
• program while_loop;
• integer operator=0;
• initial begin
• while (operator<5)begin
• operator += 1;
• $display("Operator is %0d\n", operator);
• end
• end
• endprogram
• RESULTS
• Operator is 1
• Operator is 2
• Operator is 3
• Operator is 4
• Operator is 5
Do-while
• EXAMPLE : dowhile
• program test;
• integer i = 0;
• initial begin
• do
• begin
• $display("i = %0d \n", i);
• i++;
• end
• while (i < 10);
• end
• endprogram
• RESULTS
• i=0
• i=1
• i=2
• i=3
• i=4
• i=5
• i=6
• i=7
• i=8
• i=9
For-each
• The foreach construct specifies iteration over the elements of an array. Its argument is an identifier that
designates any type of array (fixed-size, dynamic, or associative) followed by a list of loop variables enclosed in
square brackets. Each loop variable corresponds to one of the dimensions of the array. The foreach construct is
similar to a repeat loop that uses the array bounds to specify the repeat count instead of an expression.
• The mapping of loop variables to array indexes is determined by the dimension cardinality, as described in
multidimentional topic.
• The foreach arranges for higher cardinality indexes to change more rapidly.
• EXAMPLE : foeach
• program example;
• string names[$]={"Hello", "SV"};
• int fxd_arr[2][3] = '{'{1,2,3},'{4,5,6}};
• initial begin
• foreach (names[i])
• $display("Value at index %0d is %0s\n", i, names[i]);
• foreach(fxd_arr[,j])
• $display(fxd_arr[1][j]);
• end
• endprogram
• RESULTS
• Value at index 1 is SV
•4
•5
•6