Need help to generate pattern 10110111011110111110

Can u help generate pattern 10110111011110111110 using SV constraint.

class A;
  rand bit [19:0] pattern;
  constraint c { pattern == 20'b10110111011110111110;}
endclass

Next question?

1 Like

Amazing. Thanks.
Next question is if there is any generic way to write this for extending this pattern.?

class pattern;
  rand bit [19:0] data;
  
  constraint c{
    foreach(data[i]) {
      if(i inside {1,4,8,13,19}) 
        data[19-i] == 1'b0;
      else
        data[19-i] == 1'b1;
    }
  }
endclass
// 10110111011110111110
// 10, 110, 1110, 11110, 111110
//  2   3    4     5       6

class pattern;
    rand bit   d = 1; 
    bit [31:0] i = 2;  
    bit [31:0] count=0;
    
  constraint C {
                 if(count == i-1) d == 0;
                 else  d == 1;
                }
    
    function void post_randomize();
      if(count == i) begin 
        i += 1; 
        count = 0; 
      end
      count += 1;
    endfunction
    
  endclass

Output:
# run -all
# 10110111011110111110…
# exit

The problem with your question is you haven’t explained what the pattern is as a requirement; just shown a bunch of bits.


class ch;
  
  rand bit a [][];
  rand bit arr[];
  rand bit [31:0] row_size;
  int count = 0,arr_size;
  constraint row_s {row_size inside {[3:5]};}
  constraint c1 {a.size == row_size;}
  constraint c2 {foreach(a[i]) a[i].size == i+2;}
  constraint c3 {foreach(a[i,j]) {
    if(j == a[i].size - 1) a[i][j] == 0;
    else a[i][j] == 1;  }}

    function void post_randomize();
       for(int i = 0; i< row_size; i++) arr_size += 2 + i;
          arr = new[arr_size];
          arr = {>>{a}}; // converts a 2D array to 1D array
                 
          // another method to convert 2D array to 1D array
          /*foreach(a[i,j]) begin
               arr[count] = a[i][j];
               count += 1;
          end*/

      endfunction
    
endclass
    
module m;
  
  initial begin 
    ch c;
    c = new;
    void'(c.randomize());
    $display("%p \n %p",c.a,c.arr);
  end
  
endmodule
Output : 
'{'{1, 0}, '{1, 1, 0}, '{1, 1, 1, 0}, '{1, 1, 1, 1, 0}, '{1, 1, 1, 1, 1, 0}} 
 '{1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0}
class rand_pat;
  parameter N = 10;
  rand bit [N-1:0] value;
  int count = 2;
  
  constraint c1 {//$bits(value) == count;
                 $countones(value) == count;
                  value < (1 << count);}
    
  function void post_randomize();
    value[0] = 1'b0;
    //value = value[count-1:0];
    count++;
  endfunction
endclass

module pattern_10110_;
  rand_pat pat_inst;
  
  initial begin
  	pat_inst = new();
    for(int i=0;i<10;i++) begin
      pat_inst.randomize();
      $display("Value = %b", pat_inst.value);
    end
  end
endmodule

I want to get rid of 0s to make the output look neat. I cannot use count to slice the value because it’s not a constant. Any suggestions?

Right now it looks like this
Value = 0000000010
Value = 0000000110
Value = 0000001110
Value = 0000011110
Value = 0000111110
Value = 0001111110
Value = 0011111110
Value = 0111111110
Value = 1111111110