I want to write a constraint such that in 26 bit address I get the consecutive 4bits with patter 1011 70% of the time and 30 % of the time we don't care

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

The issue with your constraint is that the foreach loop attempts to match every overlapping part select.

addr[3:0] == 4'b1011 && addr [4:1] == 4'b1011 && ...

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

I am facing a Compilation Issue with dist

The dist construct is a constraint, not a boolean expression–it can’t go inside the with() clause.

I’m not sure what you’re trying to accomplish with a distribution, so I can’t suggest a solution. Please explain your goals in more detail.

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

constraint c1 { a.or() with (addr[item.index+:4] == 4'b1011} dist {1:=70, 0 :=30}; }
1 Like

Hi,
Thanks for the clarification dave.
Someone asked me this question.
after anaylizing I realize it will pe practically impossible .

1011 0110 1101 1011 0110 1101 1011

Even if we take 28bits . possible 4 bits set =26 possible 1011 sets =9.

So maximum probability => 9/26

Let me know if my understanding is correct.

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%.

1 Like

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?

As I wrote above: