KEMBAR78
SV Constraints | PDF | Computer Programming | Applied Mathematics
0% found this document useful (0 votes)
42 views15 pages

SV Constraints

The document discusses constraints in SystemVerilog, highlighting their importance in defining valid ranges and relationships for random variables in constrained randomization. It outlines the need for constraints to generate realistic test scenarios, improve test coverage, and enhance test quality, while also detailing various types of constraints such as simple, distribution, conditional, and unique constraints. Each type of constraint is explained with examples to illustrate their application in randomization processes.

Uploaded by

Shaik Rufiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views15 pages

SV Constraints

The document discusses constraints in SystemVerilog, highlighting their importance in defining valid ranges and relationships for random variables in constrained randomization. It outlines the need for constraints to generate realistic test scenarios, improve test coverage, and enhance test quality, while also detailing various types of constraints such as simple, distribution, conditional, and unique constraints. Each type of constraint is explained with examples to illustrate their application in randomization processes.

Uploaded by

Shaik Rufiya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Constraints in SV

By: B. Deepthi
Contents:

1. What are constraints


2. Need for constraints
3. Types of constraints
What are constraints??

In SystemVerilog, constraints are a powerful mechanism used


to define the valid ranges and relationships between random
variables. They play a crucial role in constrained
randomization, a technique that generates test scenarios that
meet specific criteria.
Need for constraints:
Constraints in SystemVerilog are essential for effective and efficient
verification, particularly within the context of constrained randomization.

1. Generating Realistic Test Scenarios

2. Improving Test Coverage

3. Reducing Test Development Time and Effort

4. Enhancing Test Quality:


Types of constraints in SV:

1. Simple Constraints 6. Unique Constraints


2. Distribution Constraints 7. Solve-Before Constraints
3. Conditional(if-else) Constraints 8.Implication Constraints
4. Soft Constraints 9. Inline Constraints
5. Iterative Constraints
class Packet;
1. Simple Constraints: rand int size;

// Size must be between 10 and 100


endclass
These directly specify constraint size_c { size >= 10 && size <= 100; }
the valid range of
values for a single module test;
random variable. initial begin
Packet pkt = new();
repeat(3) begin
pkt.randomize();
Output: $display("Random Size: %0d", pkt.size);
Random Size: 54 // Random value between 10 and 100
# Random Size: 30 end
# Random Size: 91 end
endmodule
2.Distribution class Packet;
Constraints: rand int size;
constraint dist_c { size dist { [1:10] := 10, [11:20] :=
90 }; }
Distribution constraints allow // 10% chance for 1-10, 90% for 11-20
you to define the probability endclass
distribution for random values.
module test;
This enables control over the initial begin
likelihood of certain values Packet pkt = new();
occurring more often than repeat(5) begin
others. assert(pkt.randomize());
$display("Random Size: %0d", pkt.size);
output: // Random value with weighted distribution
Random Size: 20 end
# Random Size: 15 end
# Random Size: 12 endmodule
# Random Size: 18
# Random Size: 20
3. Conditional(if-else) Constraints: class Packet;
rand bit[8:0] size;
rand bit enable;
constraint cond_c {
Conditional constraints are applied if (enable) size > 50;
using an if-else block. These // If enable is true, size must be greater than 50
else size < 50;
constraints are only enforced when // If enable is false, size must be less than 50
specific conditions are met, allowing for }
endclass
more flexible randomization.
module test;
initial begin
output:
Packet pkt = new();
Enable: 0, Size: 6
repeat(5) begin
# Enable: 1, Size: 368
assert(pkt.randomize());
# Enable: 0, Size: 10
$display("Enable: %0b, Size: %0d", pkt.enable,
# Enable: 1, Size: 480
pkt.size); end
# Enable: 0, Size: 11
// Display size based on enable
end
endmodule
class Packet;
4. Soft Constraints: rand bit[9:0] size;
constraint soft_c { soft size <= 50; }
// Soft constraint, size is preferred to
Soft constraints are optional and be <= 50
constraint hard_c { size > 50; }
allow for more flexible randomization.
endclass
They do not need to be satisfied by
the solver, and the solver can choose module test;
to ignore them if they conflict with initial begin
other constraints. Packet pkt = new();
repeat(5) begin
assert(pkt.randomize());
$display("Random Size: %0d",
pkt.size); // Size <= 50, but can be
Random Size: 877
overridden
# Random Size: 911
# Random Size: 68 end
# Random Size: 552 end
# Random Size: 864 endmodule
class Packet;
rand int array[5];
5. Iterative Constraints: constraint foreach_c {
foreach (array[i]) array[i] >= 0 && array[i]
<= 100;
Iterative constraints are used when
// All elements should be between 0 and
applying constraints to all or some 100
elements in an array. The foreach }
construct is commonly used to define endclass
such constraints.
module test;
initial begin
Packet pkt = new();
Array[0]: 14 assert(pkt.randomize());
# Array[1]: 62 foreach (pkt.array[i])
# Array[2]: 83 $display("Array[%0d]: %0d", i, pkt.array[i]);
# Array[3]: 33 end
# Array[4]: 81 endmodule
class Packet;
rand bit [2:0]addr1, addr2, addr3;
6. Unique Constraints:
constraint unique_c { unique { addr1,
addr2, addr3 }; }
// All addresses must be unique
Unique constraints ensure that the
endclass
values of certain variables or array
elements are unique. The unique module test;
keyword ensures that each value initial begin
within a set or array is distinct. Packet pkt = new();
repeat(5) begin
assert(pkt.randomize());
$display("Addr1: %0d, Addr2: %0d,
Addr1: 2, Addr2: 5, Addr3: 1 Addr3: %0d", pkt.addr1, pkt.addr2,
# Addr1: 3, Addr2: 5, Addr3: 6 pkt.addr3); // All values are unique
# Addr1: 2, Addr2: 0, Addr3: 3
# Addr1: 2, Addr2: 1, Addr3: 7 end
# Addr1: 0, Addr2: 5, Addr3: 1 end
endmodule
class Packet;
rand bit [5:0] size, addr;
constraint solve_before_c {
7. Solve-Before Constraints: solve size before addr;
// Solve addr after size is solved
addr == size * 2;
The solve-before constraint specifies // addr is twice the size
}
the order in which variables are solved
endclass
during randomization. It allows control
over which variables are solved first, module test;
initial begin
especially when interdependencies
Packet pkt = new();
exist between variables. repeat(5) begin
assert(pkt.randomize());
$display("Size: %0d, Addr: %0d", pkt.size,
Size: 14, Addr: 28 pkt.addr);
# Size: 12, Addr: 24 // addr is twice the size
# Size: 29, Addr: 58 end
# Size: 16, Addr: 32 end
# Size: 11, Addr: 22 endmodule
class Packet;
rand bit enable;
8.Implication Constraints: rand bit [4:0] size;
constraint impl_c {
(enable == 1) -> (size < 10);
// If enable is true, size must be greater than 10
Implication constraints use the - }
> operator to apply constraints endclass
based on conditions. The
constraint is enforced only if the module test;
initial begin
condition preceding the -> is
Packet pkt = new();
true. repeat(5) begin
assert(pkt.randomize());
Enable: 0, Size: 29 $display("Enable: %0b, Size: %0d", pkt.enable,
Enable: 1, Size: 6 pkt.size);
Enable: 1, Size: 3 // If enable is true, size > 10
Enable: 1, Size: 9 end
Enable: 0, Size: 18 end
endmodule
class Packet;
rand bit[5:0] size;
endclass
9. Inline Constraints:
module test;
Inline constraints are temporary initial begin
constraints defined during the Packet pkt = new();
randomize() call. repeat(5) begin
assert(pkt.randomize() with {
pkt.size == 20; });
Random Size: 20 // Inline constraint
$display("Random Size: %0d",
# Random Size: 20
pkt.size);
# Random Size: 20 // Size will be 20
end
# Random Size: 20
end
# Random Size: 20 endmodule
THANK YOU!!

You might also like