You are on page 1of 24

Constraints

Code
Examples
1. Randomize the below variable such as
class randvar;
rand bit [7:0] var1, var2, var3, var4;
endclass

i) Randomize all variable.

// 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

// Define a module called rand_methods


module rand_methods;
initial begin
// Create an object of the randvar class called pkt
randvar pkt;
// Allocate memory for the pkt object
pkt = new();

// Call the randomize method to assign random values to the variables


in pkt
pkt.randomize();

// Display the randomly generated values of the variables in pkt


using the $display function
$display("\t VAR1 = %0d \t VAR2 = %0d \t VAR3 = %0d \t VAR4 = %0d ",
pkt.var1, pkt.var2, pkt.var3, pkt.var4);
end
endmodule

ii) Randomize only var2.


// 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

// Define a module called rand_methods


module rand_methods;
initial begin
// Create an object of the randvar class called pkt
randvar pkt;
// Allocate memory for the pkt object
pkt = new();

// Set the rand_mode for var1, var3, and var4 to 0 (sequential)


pkt.var1.rand_mode(0);
pkt.var3.rand_mode(0);
pkt.var4.rand_mode(0);

// Call the randomize method to assign random values to the variables


in pkt
pkt.randomize();

// Display the randomly generated values of the variables in pkt


using the $display function
$display("\t VAR1 = %0d \t VAR2 = %0d \t VAR3 = %0d \t VAR4 = %0d ",
pkt.var1, pkt.var2, pkt.var3, pkt.var4);

// 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

iii) Randomize var1, var4.

// Define a new class called 'randvar'


class randvar;
// Declare four random variables of 8-bit width
rand bit [7:0] var1, var2, var3, var4;
endclass

// Define a new module called 'rand_methods'


module rand_methods;
initial
begin
// Create an object of type 'randvar'
randvar pkt;
pkt = new();

// Set the random mode of 'var2' and 'var3' to 0 (uniform distribution)


pkt.var2.rand_mode(0);
pkt.var3.rand_mode(0);

// Call the 'randomize' method to generate random values for 'pkt' object
pkt.randomize();

// Print the values of 'var1', 'var2', 'var3', and 'var4'


$display("\t VAR1 = %0d \t VAR2 = %0d \t VAR3 = %0d \t VAR4 = %0d
",pkt.var1,pkt.var2, pkt.var3,pkt.var4);

// Print the random mode of 'var1', 'var2', 'var3', and 'var4'


$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
iv) Randomize var1, var3, var4.
// Define a class named randvar
class randvar;
rand bit [7:0] var1, var2, var3, var4;
endclass

// Define a module named rand_methods


module rand_methods;
// Define an initial block that executes only once at the beginning of
simulation
initial
begin
// Create an instance of randvar class named "pkt"
randvar pkt;
// Initialize pkt using the new operator
pkt = new();

// Set the randomization mode of pkt.var2 to 0


pkt.var2.rand_mode(0);

// Call randomize method to randomize the values of pkt's variables


pkt.randomize();

// Display the values of pkt's variables using $display function


$display("\t VAR1 = %0d \t VAR2 = %0d \t VAR3 = %0d \t VAR4 = %0d
",pkt.var1,pkt.var2, pkt.var3,pkt.var4);

// Display the randomization mode of pkt's variables using $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
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;

constraint c1_range { val inside {[25:50]}; }


constraint c2 { val > 40;}
endclass

module constr_inside;
initial
begin
packet pkt;
pkt = new();

repeat(3)
begin
pkt.randomize();

$display("\t VALUE = %0d",pkt.val);

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 {

low inside {[0:50]};


mid inside {[25:75]};
high inside {[50:100]};
}
endclass
4. Write a single constraint to generate random values for bit [8:0]
variable in the below range, 1-34, 127, 129-156, 192-202,257-260.
class packet;
rand bit [8:0] val; // declare random variable val as a 9-bit wide bit-
vector

constraint c1_range { val inside {[1:34], 127, [129:156], [192:202],


[257:260]}; } // define constraint c1_range on val

endclass

module constr_inside;
initial
begin
packet pkt; // instantiate packet object pkt
pkt = new(); // allocate memory for pkt

repeat(10) // repeat the following block 10 times


begin
pkt.randomize(); // call the randomize method on pkt to generate a
random value for val

$display("\t VALUE = %0d",pkt.val); // print the resulting value of val


end
end
endmodule

5. Write a constraint without an inside function to generate random


values within the range of 34 to 43?
class packet;
rand bit [8:0] val; // Declare a random variable "val" of 9 bits

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"

repeat (10) begin // Generate and display 10 random values of "val"


pkt.randomize(); // Use the "randomize()" method to generate a random
value for "val" that satisfies the constraints

$display("\t VALUE = %0d", pkt.val); // Display the value of "val"


end
end
endmodule
6. Write a constraint to generate a random value for a var1 [7:0] within
50 and var2 [7:0] with the non-repeated value in every randomization?
class packet;
rand bit [7:0] var1; // Define a random 8-bit variable called var1
rand bit [7:0] var2; // Define a random 8-bit variable called var2

constraint c1_range { var1 inside {[0:50]}; } // Define a constraint on


var1
constraint c2_range { unique {var2}; } // Define a constraint on var2

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

7. Without using randomization method or rand keyword(modifiers),


generate an array of unique values.
module test;

// Declare an array "a" of 10 unsigned integers


int unsigned a[10];

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

// Shuffle the array using the "shuffle" method


a.shuffle();

// Display the contents of the array using the "$display" statement


$display("a = %p",a);
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"

// Define a constraint "size_array" that restricts the size of the array to


be between 4 and 20
// and sets the size to be equal to the value of "c"
constraint size_array {c inside {[4:20]}; array.size == c;}

// Define a constraint "elements" that restricts each element of the array


to be between 0 and 64
constraint elements {foreach (array[i]) array[i] inside {[0:64]};}

// Define a constraint "abc" that restricts each element of the array to be


unique
constraint abc {foreach(array[i]) foreach(array[j]) if (i!=j)
array[i]!=array[j];}
endclass: packet

// Define a module "foreach_constraint"


module foreach_constraint;
packet pkt = new(); // Create an instance of "packet" named "pkt"
initial
begin
repeat (15)
begin
assert(pkt.randomize()); // Randomize the values of the packet's
variables according to their constraints
$display("\nThe size of the array is %0d",pkt.array.size());
$display("Elements of the array = %0p",pkt.array);
end
end
endmodule

9. Write a constraint to generate 0, 1, x and z randomly.


// Define a packet class with two random logic variables a and b
class packet;
rand logic a;
rand logic b;

// Define a post_randomize function to assign value to b and modify a's


value based on b's value after randomize
function void post_randomize();
b = $urandom_range(0,1); // Assign a random value to b
if (b)
a = a ? 'x : 'z; // If b is true (1), then assign 'x or 'z to a based
on its original value
endfunction
endclass
// Define a test module to create an instance of packet and print its value
of a
module test;
initial begin
packet pkt = new; // Create a new instance of packet
repeat (10) begin
pkt.randomize(); // Randomize the values of a and b
$display(pkt.a); // Print the value of a after randomization
end
end
endmodule

10. Write a constraint to generate multiples of power 2.


// Define a class to represent an address and its corresponding power of 2
class addr2power;
rand bit[7:0] addr;
randc bit[2:0] add2; // the corresponding power of 2
constraint ADDR { addr == 2**add2; } // ensure that addr is equal to 2
raised to the power of add2
endclass

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

// Display the resulting address (2 raised to the power of add2)


$display("%0d", addr_pow.addr);
end
end
endmodule

11. Having 32-bit of variable, only single bit high values need to be
accessed. Write a constraint for that.

// Define a class named "abc"


class abc;
rand bit [31:0] var1; // Declare a random variable var1 of 32-bit size
rand bit [31:0] c1; // Declare a random variable c1 of 32-bit size

// Define a constraint named "c2" that ensures c1 has only one bit set to 1
constraint c2{ $countones(c1)==1;}

endclass

// Define a module named "bb"


module bb();

abc ab_h; // Instantiate an object of class "abc" named "ab_h"


initial begin
ab_h = new(); // Allocate memory for the object
repeat(10) begin
ab_h.randomize(); // Generate random values for var1 and c1 using
constraints
$display("values are %0d %b",ab_h.c1,ab_h.c1); // Display the values
of c1 in decimal and binary format
end
end

endmodule

12. Write a constraint with array size 5 to 10 values & the array values
should be in ascending order/descending order.

// Define the class "packet"


class packet;
rand bit [7:0] b[]; // Declare a dynamic array named "b"

// Define a constraint named "abc1"


constraint abc1 {
b.size() inside {[5:10]}; // Ensure that the size of array "b" is between
5 and 10
}

// Define a constraint named "odd_even_2"


constraint odd_even_2 {
foreach(b[i]) // Iterate through each element in "b"
if (i % 2 == 0) // If the index "i" is even
b[i] % 2 != 0; // Ensure that the element at "b[i]" is odd
else // If the index "i" is odd
b[i] % 2 == 0; // Ensure that the element at "b[i]" is even
}
endclass

// Define a module named "test"


module test;
packet v; // Instantiate an object of class "packet" named "v"
initial begin
v = new; // Allocate memory for the object "v"
repeat (10) begin
v.randomize(); // Randomize the object "v"
$display("%p",v.b); // Display the randomized elements of array "b" in
object "v"
end
end
endmodule
13. Write a constraint to generate a pattern 0102030405.
// Define a class named packet
class packet;

rand int a[]; // Declare a dynamic array named 'a' of integers

// Define a constraint named 'c1' that restricts the size of array 'a' to
be within [4:20]
constraint c1 { a.size() inside {[4:20]};}

// Define a constraint named 'c2' that restricts the even-indexed elements


of 'a' to be 0
// and odd-indexed elements to be equal to (i+1)/2
constraint c2 {
foreach (a[i])
{
if(i % 2 == 0)
{
a[i] == 0 ; // If the index 'i' is even, then the element should be 0
}
else
{
a[i] == (i + 1 )/2 ; // If the index 'i' is odd, then the element
should be (i+1)/2
}
}
}
endclass

// Define a module named 'test'


module test;

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.

// Define a packet class


class packet;
randc int a; // Random integer with constant weight
real b; // Real number
// Define a constraint that restricts the value of a within the range of
[990, 1000]
constraint c1 {a inside {[990:1000]};}

// Define a post_randomize function to calculate b value based on a


function void post_randomize();
b = a / 10.0; // Set b value based on the calculated value of a
endfunction

endclass

// Define a test module


module test;

// Generate random packets and display their a and b values


initial begin
packet pkt = new; // Create an instance of the packet class
repeat(10) begin // Repeat the process for 10 times
pkt.randomize(); // Randomize the packet instance
$display("a=%d, b=%f", pkt.a, pkt.b); // Display the values of a and b
end
end

endmodule

16. Write a constraint - divisible by 5.


class packet;
bit [8:0] a = 27;

// Define a task named "check"


task check();
// Check if a is divisible by 5
if (a % 5 == 0) begin
$display("The given number is divisible by 5");
return; // Exit the task
end

$display("The given number is NOT divisible by 5");


endtask // end of task definition
endclass // end of class definition

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

constraint abc1 { b.size() inside {[1:10]}; } // constrain array size


between 1 and 10
constraint odd { // constrain each element to be odd and between 10 and 30
foreach (b[i]) {
b[i] inside {[10:30]};
b[i] % 2 != 0;
}
}
endclass

module test;
packet v;
initial begin
v = new();
repeat(10) begin
v.randomize();
$display("%p", v.b);
end
end
endmodule

18. Write a constraint to generate prime numbers between the range of


1 to 100.
class prime_numbers;

rand bit [31:0] i;

// Define the range of i to be from 1 to 100


constraint a1 { i inside {[1:100]}; }

// Define constraints to generate prime numbers


constraint c {
i != 1;

// If i equals 2, then it is prime


if (i == 2) {
i == 2;
} else {
// If i is not 2, then check if it is divisible by 2
i % 2 != 0;
}

// If i equals 3, then it is prime


if (i == 3) {
i == 3;
} else {
// If i is not 3, then check if it is divisible by 3
i % 3 != 0;
}

// If i equals 5, then it is prime


if (i == 5) {
i == 5;
} else {
// If i is not 5, then check if it is divisible by 5
i % 5 != 0;
}

// If i equals 7, then it is prime


if (i == 7) {
i == 7;
} else {
// If i is not 7, then check if it is divisible by 7
i % 7 != 0;
}
}
endclass

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?

Circular dependency is a situation in SystemVerilog where two or more


modules or entities depend on each other, directly or indirectly, causing
an infinite loop or an undefined behavior during simulation.

For example, consider two modules: module A and module B, where


module A requires module B and module B requires module A. This
creates a circular dependency, as each module depends on the other to
execute successfully.

To avoid circular dependency in SystemVerilog, you can use a technique


called "forward declaration." This involves declaring the modules or
entities before they are defined, allowing the compiler to know about
the module or entity before its full definition is provided.
20. How can we generate the factorial of the first 5 even numbers using
constraints in SystemVerilog?

// Define the class to generate the factorial of the first 5 even numbers
class factt;
rand int num[]; // Declare dynamic array of random integers

constraint size {num.size == 5;} // Limit the size of the array to 5


constraint fact_num {
foreach (num[i])
num[i] == fact((i + 1) * 2); // Generate the factorial of each even number
//num[i] == fact(((i + 1) * 2) - 1); // Alternatively, generate the factorial
of each odd number
}

// Function to calculate the factorial of a number recursively


function int fact(int j);
if (j == 0)
fact = 1;
else
fact = j * fact(j - 1);
endfunction
endclass

// Test the class by generating random factorials of even numbers


module factorial;
factt f = new(); // Instantiate the class

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;

// Define a class named "real_num"


class real_num;

rand int r_num[]; // Declare a dynamic array of random integers


real num[10]; // Declare an array of real numbers

constraint size1 { r_num.size == 10; } // Set the size of dynamic array


constraint real_num {
// Set the constraint to limit the range of random integers
foreach (r_num[i]) r_num[i] inside {[1350:2570]};
}

// Define a function to calculate real numbers from random integers


function void post_randomize();
foreach (num[i]) // Loop through each real number
begin
num[i] = r_num[i] / 1000.0; // Calculate the real number from random
integer
$display("Number = %f", num[i]); // Display the real number
end
endfunction
endclass

// Create an instance of the "real_num" class


real_num rn = new();

// Define the initial block to randomize the values of the class


initial begin
rn.randomize(); // Randomize the values of the class
end

// 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 ?

// Define a class to generate a set of even numbers


module even();

class eve;
rand bit[6:0] a[]; // Declare an array of random bit values

// Specify a constraint to set the size of the array between 5 to 10


constraint a1 { a.size inside {[5:10]};}

// Specify a constraint to ensure that the values in the array are in


ascending order
constraint a3 {foreach(a[i])
if (i>0)
a[i]>a[i-1];}

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]);

// Display the sum of the generated even numbers


$display(" %p %0d ", p1,p1.a.sum());
end
end

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'

// Defining constraints to generate values for 'c1'

// First view - Uncomment this constraint to generate values for 'c1' using
left shift operation
//constraint C11{c1==1<<var1;}

// Second view - Uncomment this constraint to generate values for 'c1' as a


onehot variable
//constraint c2{ $onehot(c1)==1;}

// Third view - Uncomment this constraint to generate values for 'c1' as a


variable with only one '1' bit
constraint c2{ $countones(c1)==1;}

// Fourth view - Uncomment this constraint to generate values for 'c1' as a


power of 2
//constraint c2 {(c1 & (c1-1)) == 0; c1 !=0; }

endclass

// Defining a module called 'bb'


module bb();

abc ab_h; // Creating an instance of the 'abc' class called 'ab_h'

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

25. Write a constraint to generate a variable with 0-31 bits should be 1,


32-61 bits should be 0.
// Defining a class "packet" to generate a 62-bit random number with
constraints
class packet;
rand bit [61:0] num;

// 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;
}

// Defining a function named "post_randomize" to display the value of the


random number generated
function void post_randomize();
$display("num= %d %b",num, num);
endfunction

endclass

// Defining a module named "test" to instantiate the "packet" class and


generate a random number
module test;
packet v;

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; }

// Defining a function called "post_randomize" to print the value of "a" and


update the value of "b"
function void post_randomize();
$display("a=%d, b=%d", a, b);
b = a;
endfunction
endclass

// Defining a module called "test"


module test;
packet v;

// Generating 10 random values using "repeat" loop


initial begin
v = new;
repeat (10) begin
v.randomize();
end
end
endmodule
27. Write a SystemVerilog program to randomize a 32-bit variable, but
only randomize the 12th bit.
// Define a class named "packet"
class packet;
randc bit[31:0] a; // Declare a 32-bit random variable named "a"

// 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 a function to display the randomized value of "a"


function void post_randomize();
$display("a= %b", a);
endfunction
endclass

// Define a module named "test"


module test;
packet v; // Instantiate an object of "packet" class named "v"

// 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[];

// Define a constraint on the size of the array


constraint b {k.size() inside {[4:20]};}

// Define a constraint to ensure even locations contain odd numbers,


// and odd locations contain even numbers
constraint c {
foreach (k[i]) {
if (i % 2 == 0) // even location
k[i] % 2 == 1; // odd number
else if (i % 2 == 1) // odd location
k[i] % 2 == 0; // even number
}
}
endclass

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

You might also like