//=================================================constraint for prime
number=========================//
class prime_numbers;
rand bit [31:0] i; // Define the range of i to be from 1 to 100
constraint a1 { i inside {[50: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
{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 (100)
begin
prime.randomize();
$display("the prime: %d", prime.i); end
end
endmodule
//===================================================== SV Code for Prime
number================//
module prime_generator;
integer i, j;
bit is_prime;
initial begin
$display("Prime numbers from 1 to 100:");
for (i = 50; i <= 100; i++) begin
is_prime = 1;
for (j = 2; j*j <= i; j++) begin
if (i % j == 0) begin
is_prime = 0;
break;
end
end
if (is_prime) begin
$display("%d", i);
end
end
end
endmodule
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];}
constraint c {foreach(array[i])
{
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 {
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: packet // Define a module "foreach_constraint"
module foreach_constraint;
packet pkt = new(); // Create an instance of "packet" named "pkt"
initial begin repeat (5) 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
//=========================================META
QUESTION============================//
/* class aishu;
rand bit [7:0] a[];
constraint c1{a.size()==10;}
// constraint c4{ foreach(a[i])
// a[i] inside {[1:50]};}
constraint c2{foreach(a[i])
if(i%2 == 0)
a[i]%2 == 0;}
constraint c3{a.sum() with (int'(item))<1000;}
function void post_randomize();
// a=a.unique;
foreach(a[i])begin
$display("value of a[%0d]=%0d",i,a[i]); end
$display("Sum=%d",a.sum());
endfunction
endclass
module akkibhai;
aishu a_h;
initial begin
a_h=new();
repeat(5)
a_h.randomize();
end
endmodule*/