Interview question on constraints

I want to generate a pattern such that valid should be high for 3 out of 5 clocks. consider in 5 clocks
valid can be {1_1_1_0_0},{1_0_1_1_0},{1_0_1_0_1} … any way. Requirement is every five clocks slicing 3 clocks valid should be high. can you help me with the constraint for it ?

//-------------------------------------------------------------------------
//						www.verificationguide.com
//-------------------------------------------------------------------------
class packet;
  rand bit [31:0] valid [65];
  rand bit [31:0] valid5 [5];
//  rand byte data [];

  constraint avalues { foreach( valid[i]) {
    if(i % 5 == 0 ){
      foreach(valid5[j]){
        valid5[j] dist {0:=40,1:=60};}}
        valid [i] == valid5[i%5];}}
    
      
  

endclass

module constr_iteration;
  initial begin
    packet pkt;
    pkt = new();

    $display("------------------------------------");
//    repeat(2) begin
      pkt.randomize();
//      $display("\taddr-size = %0d data-size = %0d",pkt.addr.size(),pkt.data.size());
    foreach(pkt.valid[i]) $display("\tvalid[%d] = %0d ",i,pkt.valid[i],);
      $display("------------------------------------");
  
  end
endmodule

output :
Compiler version Q-2020.03-SP1-1; Runtime version Q-2020.03-SP1-1; Aug 17 10:23 2021

valid[          0] = 1  
valid[          1] = 0  
valid[          2] = 1  
valid[          3] = 0  
valid[          4] = 1  
valid[          5] = 1  
valid[          6] = 0  
valid[          7] = 1  
valid[          8] = 0  
valid[          9] = 1  
valid[         10] = 1  
valid[         11] = 0  
valid[         12] = 1  
valid[         13] = 0  
valid[         14] = 1  
valid[         15] = 1  
valid[         16] = 0  
valid[         17] = 1  
valid[         18] = 0  
valid[         19] = 1  
valid[         20] = 1  
valid[         21] = 0  
valid[         22] = 1  
valid[         23] = 0  
valid[         24] = 1  
valid[         25] = 1  
valid[         26] = 0  
valid[         27] = 1  
valid[         28] = 0  
valid[         29] = 1  
valid[         30] = 1  
valid[         31] = 0  
valid[         32] = 1  
valid[         33] = 0  
valid[         34] = 1  
valid[         35] = 1  
valid[         36] = 0  
valid[         37] = 1  
valid[         38] = 0  
valid[         39] = 1  
valid[         40] = 1  
valid[         41] = 0  
valid[         42] = 1  
valid[         43] = 0  
valid[         44] = 1  
valid[         45] = 1  
valid[         46] = 0  
valid[         47] = 1  
valid[         48] = 0  
valid[         49] = 1  
valid[         50] = 1  
valid[         51] = 0  
valid[         52] = 1  
valid[         53] = 0  
valid[         54] = 1  
valid[         55] = 1  
valid[         56] = 0  
valid[         57] = 1  
valid[         58] = 0  
valid[         59] = 1  
valid[         60] = 1  
valid[         61] = 0  
valid[         62] = 1  
valid[         63] = 0  
valid[         64] = 1  

could you please help me on this ?

In reply to sdeepak.v:

I am not sure i understand your questions completely. Based on what I understood, I would just use the array.sum == 3 for the constraint.

module test;
  bit clk_helper[5];
  
  initial begin
    repeat(5) begin
      std::randomize(clk_helper) with {clk_helper.sum() with (int'(item)) == 3  ; } ;
      $display("%p", clk_helper);
    end
    
    
  end
endmodule

In reply to sdeepak.v:

I’m not sure if I’m following your question but assuming you want every 5 elements of the valid array to have no more than 3 1’s, you can do something like this (I probably not the optimal solution), also I’m not sure why you need 32-bit variables if you want only 1 or 0 values


class packet;
   rand bit valid [$];
   rand bit [4:0] valid5;
   constraint valid_c {
      valid.size() == 65;
      $countones(valid5) == 3;
   }
   function void post_randomize();
      bit temp[$];
      foreach(valid5[j]) begin
         temp.push_back(valid5[j]);
      end
      for(int i = 0; i < valid.size(); i = i+5) begin
         valid[i:i+4] = temp;
         temp.shuffle();
      end
   endfunction

endclass

module tb;
initial begin
packet pkt;
pkt = new();

$display("------------------------------------");
// repeat(2) begin
  if(!pkt.randomize()) $fatal(1, "Randomize Failed");
// $display("\taddr-size = %0d data-size = %0d",pkt.addr.size(),pkt.data.size());
  foreach(pkt.valid[i]) $display("valid[%0d] = %b ", i, pkt.valid[i]);
$display("------------------------------------");

end
endmodule

HTH,
-R

1 Like

In reply to rgarcia07:

Hey is there any requirement that makes this not valid

// Code your testbench here
// or browse Examples
class packet;
  rand bit [4:0] valid [$];
   constraint valid_c {
      valid.size() == 65;
     foreach(valid[ii])
       $countones(valid[ii]) == 3;
   }
 
endclass

Is uniqueness?

Regards

c

lass test; 
   rand bit valid; 
   bit [5:0] cnt;
   int unsigned rand_cnt; 
  
   constraint valid_c { 
            valid dist {1:=3,0:=2}; 
            cnt >= 3 -> valid ==0;
   }
     
   function void post_randomize(); 
       if(valid)
         cnt++;
        rand_cnt++; 
     if(rand_cnt %5 == 0) 
       	cnt=0;
   endfunction 
endclass 

module tb;
  
  initial begin 
     test t; 
    t = new(); 
    repeat(20) begin 
    	t.randomize(); 
      	$display("t value = %0d",t.valid); 
    end 
  end 
endmodule