I am trying write a constraint to generate this pattern 2, 33, 222, 5555, 22222, 777777, 2222222, 99999999, 2222222222 and so on
This is my constraint code
rand longint a;
int count=0;
constraint value {
//ODD
if(count%2 == 1)
a == odd(count);
//EVEN
else
a == even(count);
}
function void post_randomize();
count++;
endfunction
//ODD
function longint odd(int x);
longint num = 0;
int value = x+2;
for(int i=0;i <= x;i++) begin
num = num*10+value;
end
return num;
endfunction
//EVEN
function longint even(int x);
longint num=0;
for(int i=0;i <= x;i++) begin
num = num*10+1;
end
return 2*num;
endfunction
This is my output
#2
33
222
5555
22222
777777
2222222
99999999
222222222
12222222221
22222222222
1444444444443
2222222222222
166666666666665
222222222222222
The even pattern of 2s are coming properly.
But odd pattern is coming wrong
i think we can use hexadecimal format and shift operations to print this pattern
take the input patterns like “3”,“5”,“7”,“9”,“11”,“13” …
convert it to hex decimal numbers
longint value = str.atohex();
using below logic we can get format(num = 0 )
for (int i = 1; i <= x; i++)
num = (num << no_of_bits) + value;
Note: number of bits to shift → no of bits needed to represent a input number in hexadecimal
if ( ip_value < 16 ) no_of_bits = 4;
else if ( (ip_value < 256) && ( ip_value >= 16)) no_of_bits = 8;
/*
2, 33, 222, 5555, 22222, 777777 --> a
1 2 3 4 5 6 --> count
*/
class pattern;
int count=1;
rand bit [255:0] a;
constraint c1 {
if(count%2==1) a==print2(count);
else a==even(count);
}
function bit [255:0] print2(int c);
bit [255:0] num=0;
for (int i=0; i<c; i++) begin
num = num*10+2;
end
return num;
endfunction
function bit [255:0] even(int c);
bit [255:0] num=0;
int shift = (c>9) ? 100 : 10;
for (int i=0; i<c; i++) begin
num = num*shift+(c+1);
end
return num;
endfunction
function void post_randomize();
$display("%0d -> %0d", count, a);
count++;
endfunction
endclass