Professional Documents
Culture Documents
Code
Examples
1. Randomize the below variable such as
class randvar;
rand bit [7:0] var1, var2, var3, var4;
endclass
// Define a class called randvar that has four random variables of 8 bits
each
class randvar;
rand bit [7:0] var1, var2, var3, var4;
endclass
// Display the rand_mode values for each variable in pkt using the
$display function
$display("\t var1.rand_mode() = %0d \t var2.rand_mode() = %0d \t
var3.rand_mode() = %0d \t var4.rand_mode()= %0d ", pkt.var1.rand_mode(),
pkt.var2.rand_mode(), pkt.var3.rand_mode(), pkt.var4.rand_mode());
end
endmodule
// Call the 'randomize' method to generate random values for 'pkt' object
pkt.randomize();
end
endmodule
iv) Randomize var1, var3, var4.
// Define a class named randvar
class randvar;
rand bit [7:0] var1, var2, var3, var4;
endclass
end
endmodule
2. There are two constraints applied to same variable ‘A’. one will
generate the value within the range of [25:50] and another expression
say variable value should be greater than 40. What should be the value
generated, and what is the reason?
class packet;
rand bit [8:0] val;
module constr_inside;
initial
begin
packet pkt;
pkt = new();
repeat(3)
begin
pkt.randomize();
end
end
endmodule
If there are two constraints applied to the same variable 'A', one generating a value within the
range of [25:50] and another stating that the variable value should be greater than 40, the value
generated for variable 'A' will be between 40 and 50.
This is because the second constraint that 'A' should be greater than 40 already restricts the range
of 'A' to [40:50]. Therefore, the first constraint, which generates a value in the range of [25:50],
will only apply to the overlapping range of [40:50], resulting in the final value of 'A' being between
40 and 50.
It is important to note that constraints should be designed carefully to avoid conflicts and to
ensure that they work together to generate valid and meaningful results.
3. What is wrong with the below code? What is the correct process to
write the constraint?
Class const;
rand bit [7:0] low, mid, high;
constraint Const_1 {low <mid<high;}
end class
In the given code, the constraint body is {low <mid<high;}, which checks if low is less than mid
and mid is less than high. This constraint specifies a valid order of the variables, but it does not
restrict the range of the variables.
To restrict the range of the variables, you need to define a range constraint. The correct way to
define the constraint would be:
class const;
rand bit [7:0] low, mid, high;
constraint Const_1 {
endclass
module constr_inside;
initial
begin
packet pkt; // instantiate packet object pkt
pkt = new(); // allocate memory for pkt
constraint c1_range { val > 34; } // Define a constraint that "val" must
be greater than 34
constraint c2_range { val < 43; } // Define a constraint that "val" must
be less than 43
endclass
module constr_inside;
initial begin
packet pkt; // Create an instance of "packet" class called "pkt"
pkt = new(); // Initialize "pkt"
endclass
module constr_inside;
initial
begin
packet pkt; // Create an instance of the packet class called pkt
pkt = new(); // Initialize pkt object
repeat(10)
begin
pkt.randomize(); // Call the randomize method on the pkt object
$display("\t VAR1 = %0d \t VAR2 = %0d",pkt.var1,pkt.var2); // Display
the values of var1 and var2 after randomization
end
end
endmodule
initial begin
// Use the "foreach" loop to initialize each element of the array
foreach (a[i])
begin
a[i] = i*i; // Set the value of each element to the square of its
index
end
endmodule
8. Generate unique elements in an array without using the keyword
unique.
// Define a class "packet"
class packet;
rand bit [5:0] array[]; // Define a dynamic array of random bits with size
unknown initially
randc int c; // Define a random cyclic integer "c"
module test;
initial begin
addr2power addr_pow; // create an instance of addr2power
addr_pow = new(); // initialize the instance
repeat(10) begin
addr_pow.randomize(); // randomly generate values for addr and add2
11. Having 32-bit of variable, only single bit high values need to be
accessed. Write a constraint for that.
// Define a constraint named "c2" that ensures c1 has only one bit set to 1
constraint c2{ $countones(c1)==1;}
endclass
endmodule
12. Write a constraint with array size 5 to 10 values & the array values
should be in ascending order/descending order.
// Define a constraint named 'c1' that restricts the size of array 'a' to
be within [4:20]
constraint c1 { a.size() inside {[4:20]};}
initial begin
packet pkt = new; // Instantiate an object of 'packet' class named 'pkt'
pkt.randomize(); // Randomize the 'a' array of the 'pkt' object
$display("a = %p", pkt.a); // Print the values of 'a' array of the 'pkt'
object
end
endmodule
15. Constraint to generate unique numbers between 99 to 100.
endclass
endmodule
module top;
packet pkt;
initial begin
pkt = new(); // Create an instance of the packet class
pkt.check(); // Call the check task of the packet instance
end
endmodule // end of module definition
17. Derive odd numbers within the range of 10 to 30 using SV constraint.
class packet;
rand bit[7:0] b[]; // dynamic array
module test;
packet v;
initial begin
v = new();
repeat(10) begin
v.randomize();
$display("%p", v.b);
end
end
endmodule
module top;
prime_numbers prime;
initial begin
prime = new();
repeat (20) begin
prime.randomize();
$display("the prime: %d", prime.i);
end
end
endmodule
19. What is circular dependency and how to avoid this problem?
// Define the class to generate the factorial of the first 5 even numbers
class factt;
rand int num[]; // Declare dynamic array of random integers
initial
begin
assert(f.randomize); // Randomize the values of the class constraints
$display("%p", f.num); // Display the generated factorials of even numbers
end
endmodule
21. How can you generate a random number within a specified range
using SystemVerilog? Write a code that generates a random number
between 1.35 to 2.57 using SystemVerilog.
// Define a module named "tb"
module tb;
// End of module
endmodule
22. What is the constraint to generate the pattern 9 7 5 3 1 8 6 4 2 0 ?
class packet;
rand int a[]; // dynamic array to hold the numbers
constraint size { a.size() == 10; } // constrain the size of array to 10
constraint pattern { // constrain the pattern of numbers
foreach (a[i]) {
if (i < 5) // first five numbers
a[i] == 10 - ((i * 2) + 1); // 9, 7, 5, 3, 1
else // last five numbers
a[i] == 18 - (i * 2); // 8, 6, 4, 2, 0
}
}
endclass
module tb;
initial begin
packet pkt = new(); // create an instance of packet class
assert(pkt.randomize()); // randomize the packet
$display("OUTPUT = %p", pkt.a); // print the generated pattern
end
endmodule
23. Write a constraint sunch that array size between 5 to 10 values of the
array are in asscending order ?
Write a constraint sunch that array size between 5 to 10 values of the
array are in descending order ?
class eve;
rand bit[6:0] a[]; // Declare an array of random bit values
endclass
initial
begin
eve p1=new; // Create an instance of the class
repeat(5)
begin
assert(p1.randomize()); // Randomize the values in the array using
constraints
// Display the generated even numbers with their indices
foreach(p1.a[i])
$display( " [%0d] %0d " ,i , p1.a[i]);
endmodule
24. Can you provide a code example of how to use constraints to access
a single bit of a 16-bit variable? For instance, how can you generate 16-
bit numbers with only a single bit set, such as 4, 8, or 16, using
constraints?
// Defining a class called 'abc'
class abc;
rand bit [31:0] var1; // Declaring a 32-bit variable called 'var1'
rand bit [31:0] c1; // Declaring a 32-bit variable called 'c1'
// First view - Uncomment this constraint to generate values for 'c1' using
left shift operation
//constraint C11{c1==1<<var1;}
endclass
initial begin
repeat(10) begin
ab_h = new(); // Creating a new instance of 'abc' class each time
ab_h.randomize(); // Randomizing the values of the variables in 'abc' class
instance
$display("values are %0d %b", ab_h.c1, ab_h.c1); // Displaying the generated
values of 'c1' in binary format
end
end
endmodule
// Defining a constraint named "abc" to set the first 32 bits to 1 and the
next 30 bits to 0
constraint abc {
foreach(num[i])
if(i>=0&&i<32)
num[i]==1'b1;
else if(i>31&&i<62)
num[i]==1'b0;
}
endclass
initial begin
v=new;
v.randomize();
end
endmodule
26. How to generate a sequence of 10 random single-bit values that
alternate between 0 and 1 like 101010101010?
//Defining a class called "packet"
class packet;
rand bit a;
static bit b = 0;
// Defining a constraint called "abc" to ensure that the random bit toggles
between 0 and 1
constraint abc { a != b; }
// Define a constraint named "abc" to randomize only the 12th bit of "a"
constraint abc {
foreach (a[i]) {
if (i == 12)
a[i] inside {0, 1}; // Randomize the 12th bit of "a" to either 0 or 1
else
a[i] == 0; // Set all other bits of "a" to 0
}
}
// Define an initial block to randomize "a" for 10 times and display the
values
initial begin
v = new(); // Allocate memory for object "v"
repeat (10) begin
v.randomize(); // Randomize "a" using the constraint "abc"
$display("a= %d a=%b", v.a, v.a);
end
end
endmodule
28. How can you define a constraint in SystemVerilog to ensure that even
locations in a random array contain odd numbers, and odd locations
contain even numbers?
// Define a class with a random array of 4-bit values
class a;
rand bit[3:0] k[];
module test;
initial begin
a a1 = new();
repeat (5) begin
assert(a1.randomize());
foreach (a1.k[i])
$display(" the value of locations even and odd %0d %0d", i, a1.k[i]);
$display("%p", a1.k);
end
end
endmodule