KEMBAR78
SV Constraints | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
79 views5 pages

SV Constraints

Uploaded by

shashank gupta
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)
79 views5 pages

SV Constraints

Uploaded by

shashank gupta
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/ 5

test.

sv Page 1

//SV Constraints Interview Questions and Solutions:


//1. Generate unique random values without using unique constraint.
//Ans.
class packet;
rand bit[3:0] arr[10];
constraint con1 {
foreach (arr[i]) {
foreach (arr[j]) {
if (i != j)
arr[i] != arr[j];
}
}
}
function void display();
$display("%p",arr);
endfunction
endclass

module test;
packet p1;
initial begin
p1 =new;
p1.randomize();
p1.display();
end
endmodule

//2. Write a constraint to display the Fibonacci sequence.


//Ans.
class cons;
rand int array[];
constraint size{array.size() inside {[10:15]};}
constraint fibo{
foreach(a[i])
if(i<2)
a[i]==i;
else
a[i]==a[i-1]+a[i-2];}
function void post_randomize();
$display("Fibonacci sequence --> %0p",a);
endfunction
endclass

module test;
cons c;
initial begin
c=new();
c.randomize;
end
endmodule

//3. Write a code to have the cyclic randomization behaviour without


//using the randc keyword.
//Ans.
class sample;
rand bit [2:0] a;
bit [2:0] que [$];
constraint rand_a_c {
// unique {a, que};
!(a inside {que});
}
function void post_randomize();
que.push_back(a);
endfunction
test.sv Page 2

endclass

module test;
sample s=new();
initial begin
repeat(8) begin
s.randomize();
$display(" a = %0d",s.a);
end
$display(" que = %p",s.que);
end
endmodule

//4. Write a constraint such that even locations contain odd numbers and
//odd locations contains even numbers.
//Ans.
class cons;
rand int arr[];
constraint size{arr.size inside {[10:20]};}
constraint range{foreach(arr[i])
arr[i] inside {[1:100]};}
constraint even_odd{foreach(arr[i])
if(i%2==0)
arr[i]%2==1;
else
arr[i]%2==0;}

function void post_randomize();


$display("After randomizing data is %p",arr);
endfunction
endclass

module test;
cons c;
initial begin
c=new;
c.randomize;
end
endmodule
//5. Constraint to Generate Pattern 0, 2, 1, 3, 4, 6, 5, 7, 8.
//Ans.
class packet;
rand int array[];

constraint con1 { array.size == 10;}

constraint con2 {
foreach(array[i]) {
if(i%4==0)
array[i]==i;
else{
if(i%2==0)
array[i]==i-1;
else
array[i] == array[i-1] +2;
}
} }

function void display();


$display("----------------------------------");
$display("array --> %p", array);
$display("----------------------------------");
endfunction

endclass
test.sv Page 3

module test;
packet p1;

initial begin
p1 = new();
if (!p1.randomize()) begin
$display("Randomization Failure");
end else begin
p1.display();
end
end
endmodule

//6. Write a constraint to generate a pattern 0102030405.


//Ans.
class pass;
rand int arr[];

constraint size{arr.size() inside{[10:20]};}


constraint name{foreach(arr[i])
if(i%2==0)
arr[i]==0;
else
arr[i]==(i+1)/2;}

endclass

module test;
pass p1;
initial begin
p1= new;
if(!p1.randomize())
$display("Randomization failure");
$display("array = %p",p1.arr);
end
endmodule

//7. Write a constraint in such a way that for read transaction 30% time and
//for write transaction 70% time for the signal wr_rd.
//Ans.
//wr_rd = 1 -------> write opearion
//wr_rd = 0 -------> read opearion
//Weighted Distribution Constraint using dist keyword

class packet;
rand bit wr_rd;

constraint range { wr_rd dist {1:=7,0:=3 }; }


endclass

module test;
packet pkt;
initial begin
pkt = new();
$display("------------------------------------");
repeat(10) begin
pkt.randomize();
$display("wr_rd = %b",pkt.wr_rd);
end
$display("------------------------------------");
end
endmodule
test.sv Page 4

//8. Write a constraint for a 16-bit variable such that no two consecutive (continuous
)
//ones should be generated.
//Ans.
class packet;
rand bit [15:0] a;
constraint a_value {
foreach (a[i])
if(i>0)
a[i]!=a[i-1];
}
endclass

module test;
packet pkt;
initial begin
pkt=new();
repeat(10)
begin
pkt.randomize();
$display("value of a=%0b",pkt.a);
end
end
endmodule
//9. Having 32-bit of variable, only single bit high values need to be accessed.
//Write a constraint for that.
//Ans.
class packet;
rand bit [31:0] a;
//rand bit [4:0] i;

constraint con1 {
(a & (a - 1)) == 0;
//$countones(a)==1;
//a==2 ** i;
}

function void display();


$display("a --> %b (%0d)", a, a);
endfunction
endclass

module test;
packet p1;

initial begin
p1 = new;
if (!p1.randomize()) begin
$display("Randomization Failure");
end else begin
p1.display();
end
end
endmodule

//10. Write a constraint with array size 5 to 10 values & the array values
//should be in ascending order/descending order.
//Ans.
//Ascending order
class ascending;
rand bit [5:0] array [];

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

constraint order
test.sv Page 5

{
foreach(array[i])
if(i>0)
array[i]>array[i-1];
}
function void display(string msg = "After sorting");
$display("-------------------------------------------------");
$display("%s",msg);
$display("%p",array);
$display("-------------------------------------------------");
endfunction
endclass

//Descending order
class descending;
rand bit [5:0] array [];

constraint con1{array.size inside{[5:10]};}

constraint con2
{
foreach(array[i])
if(i>0)
array[i]<array[i-1];
}
function void display(string msg = "After sorting");
$display("-------------------------------------------------");
$display("%s",msg);
$display("%p",array);
$display("-------------------------------------------------");
endfunction
endclass

module test;
ascending p1;
descending p2;

initial begin
p1 =new ;
p2 =new ;

if(!p1.randomize())
$display("Randomize Failure");
p1.display("ASCENDING ORDER");

if(!p2.randomize())
$display("Randomize Failure");
p2.display("DESCENDING ORDER");
end
endmodule

You might also like