Constraints on dynamic array

I want to constraint a dynamic array so that its values on some of the indexes are equal to a constant value.


class some_class;

  rand bit array[];

  constraint array_c{
    array.size == 20;
    soft array[7:0] == 'b10110100;
  }
endclass

But it gives error. How can I solve the issue?

In reply to emin:

It always helps to show the error. It explains the problem

array is an unpacked array of 1-bit elements. 'b10110100 represents a packed array of bits, an integral value. You can’t compare an unpacked array with a packed array. Also, constraints cannot involve unpacked array expression unless you iterate over their elements with a foreach or an unpacked array reduction method.

class some_class;
 
  rand bit array[];
  const bit [0:7] constant_value = 'b10110100;
 
  constraint array_c{
    array.size == 20;
    foreach (array[i])
      if (i inside {[0:7]})
        soft array[i] == constant_value[i];
  }
endclass

Keep in mind that dynamic arrays have index bounds from 0 to size-1. So be careful if you want array[0] to have the value 1 or 0.

randomize queue such that it should contain elements of another queue.
output is giving not as expected .
qu size is = 5
qu = '{'hd8, 'h85, 'h82, 'h95, ‘haf} //expected is qu =’{'h1,'h2,'h3,'h4,'h5}//or any values of qu_1 queue

class queue_rand;
  rand bit [7:0] qu[$];
  rand bit [7:0] qu_1[$] = {1,2,3,4,5,11,23};
  
  constraint size_c  { qu.size() inside {[4:10]}; }
  constraint constraint_3{foreach(qu[i]) (qu[i] inside {qu_1[i]});}
  
  function void display();
    $display("qu size is = %0d",qu.size());
    $display("qu = %p",qu);
  endfunction
endclass

module queue_randomization;
  queue_rand pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
endmodule
constraint constraint_3{foreach(qu[i]) (qu[i] inside {qu_1});}

Hi Dave,Still the output is incorrect.
qu = '{'h2, 'h5a, 'h45, 'h45, 'h5a}

queue randomization(1) - EDA Playground

qu_1 is also random, which is causing issue.

got it.Thanks Rajratna.