0% found this document useful (0 votes)
23 views12 pages

Constraints Question

System verilog constraints

Uploaded by

axyz014501
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)
23 views12 pages

Constraints Question

System verilog constraints

Uploaded by

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

देवो भ

ष्ट्र
CONSTRAINTS

व:
रा

Chanchal Tiwari
Linkedin
Randomization

Constraints :-

• Create code that converts a binary number to a gray integer


using a constraint.

module binary_to_gray(input bit [3:0] binary ,


output logic [3:0] gray );

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

function void post_randomize();


gray = binary ^ (binary >> 1) ;
endfunction

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

# binary : 1001, con.gray :1101, gray :1101


# constraint passed
# binary : 1010, con.gray :1111, gray :1111
# constraint passed
# binary : 0110, con.gray :0101, gray :0101
# constraint passed
# binary : 0011, con.gray :0010, gray :0010
# constraint passed
# binary : 0111, con.gray :0100, gray :0100
# constraint passed
# binary : 0110, con.gray :0101, gray :0101
# constraint passed
# binary : 0111, con.gray :0100, gray :0100
# constraint passed
# binary : 0011, con.gray :0010, gray :0010
# constraint passed
# binary : 1010, con.gray :1111, gray :1111
# constraint passed
# binary : 0011, con.gray :0010, gray :0010
# constraint passed

• Create code that converts a gray number to a binary number


using a constraint
Randomization

module gray_to_binary (
input bit [3:0] gray ,
output logic [3:0] binary
);

always @(*) begin


binary[3] = gray[3] ^ 1'b0 ;
binary[2] = gray[2] ^ binary[3] ;
binary[1] = gray[1] ^ binary[2] ;
binary[0] = gray[0] ^ binary[1] ;
end
endmodule

class converter ;
rand logic [3:0] gray ;
bit [3:0] binary ;

constraint cons{
gray inside{[0:15] };
}

function void post_randomize();


binary[3] = gray[3] ;
for (int i = 2; i>=0; i-- ) begin
binary[i] = gray[i] ^ binary[i+1] ;
end
endfunction

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

$display("test case passed");


end else begin
$display("gray=%0d, con.binary=%0d,
binary=%0d",con.gray,con.binary,binary);
$display("test case failed");
end
end
end
endmodule

# gray=8, con.binary=15, binary=15


# test case passed
# gray=2, con.binary=3, binary=3
# test case passed
# gray=3, con.binary=2, binary=2
# test case passed
# gray=6, con.binary=4, binary=4
# test case passed
# gray=8, con.binary=15, binary=15
# test case passed
# gray=8, con.binary=15, binary=15
# test case passed
# gray=12, con.binary=8, binary=8
# test case passed
# gray=5, con.binary=6, binary=6
# test case passed
# gray=11, con.binary=13, binary=13
# test case passed
# gray=15, con.binary=10, binary=10
# test case passed

How can we write a constraint to generate an even number that is also a


multiple of 7?
class packet;
rand bit [7:0] num;
constraint c{
num[0] == 0 ;
num % 7 == 0 ;
}
endclass

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

Write a constraint to generate this pattern 0,1,0,2,0,3,0,4,0,5,0…………….


Without using **,arrays, and bitwise operator.

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

function int gen_pattern(int i);


if(i%2) return 0;
else return (i+1)/2 ;
endfunction

function void post_randomize();


b = b + 1;
endfunction

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

function void post_randomize();


b = b + 1;
endfunction
endclass

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

function void post_randomize();


temp = a ;
endfunction
endclass

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

//how to create a constraint for an array with specific value


class packet ;
rand int arr[];

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

//how to implement randc behaviour without using randc keyworld?.

`define width 3
class packet ;
rand logic[(`width - 1):0]a;
int queue[$];
constraint c1 { !(a inside {queue});}

function void post_randomize();


queue.push_back(a);
if(queue.size == 2**`width)
queue.delete(); //queue = {};
endfunction
endclass

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 ;

constraint c1{(even_parity ^ (^data)== 0);}


constraint c2{(odd_parity ^ (^data)==1);}

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

# data=0000,even =0, odd=1


# data=0011,even =0, odd=1
# data=1110,even =1, odd=0
# data=0000,even =0, odd=1
# data=1000,even =1, odd=0

//write a constraint to generate random 4 states values.


class generate_1;
rand bit[1:0] ctrl;
logic a;

constraint c1{ ctrl inside{[0:3]};}

function void post_randomize();


if(ctrl==0) a=0;
else if (ctrl == 1) a=1 ;
else if (ctrl == 2) a = 1'bx ;
else a = 1'bz;
endfunction
endclass
Randomization

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

You might also like