I tried writing it following way .
But writing the following is not valid=>
port == port_0 -> {foreach(addr[i]}
if (i<=22){
{a[i],a[i+1],a[i+2],a[i+3]} == 4'b1011;
}}
class packet;
typedef enum {port_0, port_1} port_num;
rand port_num port;
rand bit [25:0] addr;
constraint c1 {
port dist {port_0 :=70, port_1 :=30};
port == port_0 -> {foreach(addr[i]}
if (i<=22){
{a[i],a[i+1],a[i+2],a[i+3]} == 4'b1011;
}}
port == port_1 -> {foreach(addr[i]}
if (i<=22){
{a[i],a[i+1],a[i+2],a[i+3]} != 4'b1011;
}};
}
endclass
// Define a module named 'test'
module test;
// Instantiate an object 'pkt' of class 'packet'
packet pkt = new();
initial begin // Repeat the randomization process 10 times
repeat(10) begin
// Randomize the object 'pkt'
pkt.randomize();
// Display the randomized values of 'port' and 'addr'
$display("%p", pkt);
end
end
endmodule
You need to write the code in a way that it has one or more matches. Use the reduction .or() method to achieve this. Additionally, you’ll need to create a dummy unpacked array to use as an iterator.
rand bit [25:0] addr;
const bit itr[22];
constraint c1 { a.or() with (addr[item.index+:4] == 4'b1011}; }
Hi Dave,
If one were to attach weightage to the 4-bit value using dist as part of the with clause,
what would be the correct way to write it ?
class C;
rand bit [6:0] addr; // Width reduced to 7-bits for testing the below constraint logic
bit itr[4];
// 30% probability of unconstrained 4-bit value
constraint c1 { itr.or() with ( addr[item.index+:4] dist { 4'b1011:= 70 , [4'b0:4'b1111]:/30 } ; ) }
endclass
My interpretation of the requirement was that the 4-bit value should be 4’b1011 with 70% weightage and remaining 30% of the time the 4-bit value could be unconstrained
Thanks. I changed my code to ::
module tb;
class C;
rand bit [6:0] addr; // Width reduced to 7-bits for testing the below constraint logic
bit itr[4];
// 30% probability of unconstrained 4-bit value
constraint c1 { itr.or() with ( addr[item.index+:4] ) dist { 4'b1011:= 70 , [4'b0:4'b1111]:/ 30 } ; }
endclass
C c1;
initial begin
c1 = new();
repeat(10)
if( c1.randomize() ) $display("Success with addr = %7b",c1.addr);
end
endmodule
Using random seeds, I expected that the addr would be 4’b1011 at least 5 to 7 times ( out of 10 ).
However, across tools I see it once once or twice in the 10 displays.
In certain cases I don’t observe it at all
Am I missing something ?
The result of the reduction .or() method is either 1'b0 or 1'b1.
The original poster, @SAURABH_M, needs to clarify their constraint. However, their code implies that the pattern should appear 70% of the time, regardless of how many times it appears within the address, while only 30% of the time it should not appear anywhere. That constraint would be
If the address is 28 bits wide, there are 25 possible overlapping 4-bit values sets. However, you can have at most 9 of these sets with 4’b1011. Therefore, the maximum percentage of this pattern that can be observed within a single randomization is 36%.
If I want this 70% probability over multiple randomizations.
Meaning at least one 1011 pattern in the 70% of the generated address should come.
How it can be written?