Constraints Question
Constraints Question
ष्ट्र
CONSTRAINTS
व:
रा
Chanchal Tiwari
Linkedin
Randomization
Constraints :-
always@* begin
gray[3] = binary[3] ^ 1'b0 ;
gray[2] = binary[2] ^ binary[3] ;
gray[1] = binary[1] ^ binary[2] ;
gray[0] = binary[0] ^ binary[1] ;
end
endmodule
class converter ;
rand logic [3:0] binary ;
logic [3:0] gray ;
constraint size{ binary inside{[0:10]};}
endclass
module top ;
converter con;
reg [3:0] binary ;
wire [3:0] gray;
Randomization
binary_to_gray dut(.binary(binary),.gray(gray));
initial begin
con = new();
repeat(10) begin
con.randomize();
binary = con.binary ;
#1;
if(gray == con.gray ) begin
$display("binary : %b, con.gray :%b, gray
:%b",con.binary,con.gray,gray);
$display("constraint passed ");
end else begin
$display("binary : %b, con.gray :%b, gray
:%b",con.binary,con.gray,gray);
$display("constraint failed");
end;
end
end
endmodule
module gray_to_binary (
input bit [3:0] gray ,
output logic [3:0] binary
);
class converter ;
rand logic [3:0] gray ;
bit [3:0] binary ;
constraint cons{
gray inside{[0:15] };
}
endclass
module test;
reg [3:0] gray ;
wire [3:0] binary ;
gray_to_binary dut(.gray(gray),.binary(binary));
converter con ;
initial begin
con = new();
repeat(10)begin
con.randomize();
gray <= con.gray;
#1;
if(binary == con.binary) begin
$display("gray=%0d, con.binary=%0d,
binary=%0d",con.gray,con.binary,binary);
Randomization
module test;
packet pkt ;
initial begin
Randomization
pkt = new();
repeat(5)begin
pkt.randomize();
$display("num : %0d",pkt.num);
#1;
end
end
endmodule
# num : 126
# num : 224
# num : 210
# num : 154
B= 0 1 2 3 4 5 6 7 8 9
A 0 1 0 2 0 3 0 4 0 5
class packet;
rand int a ;
int b= 0;
constraint c{a==gen_pattern(b);}
endclass
module test ;
packet pkt;
initial begin
pkt = new();
repeat(10)begin
pkt.randomize();
Randomization
$write("%0d\t",pkt.a);
#1;
end
end
endmodule
0 1 0 2 0 3 0 4 0
class pkt ;
rand int a;
int b = 1;
constraint c{ a==b ;}
module packet ;
pkt p ;
initial begin
p= new();
for(int i =0 ; i<10; i++)begin
if(i%2==1)begin
p.randomize();
$display("a=%0d",p.a);
end else begin
p.a = 0 ;
$display("a=%0d",p.a);
end
end
end
endmodule
# a=0
# a=1
# a=0
# a=2
Randomization
# a=0
# a=3
# a=0
# a=4
# a=0
# a=5
class sample ;
rand bit [2:0] a ;
bit [2:0] temp ;
constraint ac{
if(temp == 7 ) a == 0 ;
else a == temp +1 ;}
module test ;
sample s ;
initial begin
s = new();
$display("########output##########");
repeat(10)begin
s.randomize();
$display("the value of a :%0d",s.a);
end
$display("########END#######");
end
endmodule
########output##########
# the value of a :1
# the value of a :2
# the value of a :3
# the value of a :4
# the value of a :5
# the value of a :6
# the value of a :7
# the value of a :0
Randomization
# the value of a :1
# the value of a :2
########END#######
constraint c{
arr.size() == 7 ;
arr.sum() == 100 ;
foreach(arr[i]){
arr[i] <= 20 ;
arr[i] >= 0;
}
}
endclass
module test;
packet pkt ;
initial begin
pkt = new();
repeat(10)begin
pkt.randomize();
$display("arr :%0p",pkt.arr);
end
end
endmodule
# arr :7 19 19 18 8 15 14
# arr :20 18 17 12 3 19 11
# arr :17 14 20 10 12 19 8
# arr :17 17 1 20 15 13 17
# arr :15 17 19 12 16 16 5
# arr :10 18 17 11 14 17 13
# arr :14 19 19 12 20 0 16
# arr :2 13 15 16 18 18 18
# arr :17 3 12 20 13 16 19
# arr :15 8 10 14 16 17 20
Randomization
`define width 3
class packet ;
rand logic[(`width - 1):0]a;
int queue[$];
constraint c1 { !(a inside {queue});}
module rand_gen;
packet pkt ;
initial begin
pkt = new();
repeat(10)begin
pkt.randomize();
$display("a :%0d",pkt.a);
end
end
endmodule
# a :0
# a :2
# a :3
# a :6
# a :1
# a :5
# a :7
# a :4
# a :1
# a :5
Randomization
//how to generator and check even and odd parity using constraints?.
class packet ;
rand bit [3:0] data ;
rand bit even_parity , odd_parity ;
endclass
module test;
packet pkt ;
initial begin
pkt = new();
repeat(5)begin
pkt.randomize();
$display("data=%b,even =%0d,
odd=%0d",pkt.data,pkt.even_parity,pkt.odd_parity);
#1;
end
end
endmodule
module test;
generate_1 gen;
initial begin
gen = new();
repeat(5) begin
gen.randomize();
$display("a = %0d " , gen.a);
#1;
end
end
endmodule
#a=1
#a=z
#a=x
#a=x
#a=z