SystemVerilog constraint: unique addr across array of structs without auxiliary array

Hi Forum!
Is it possible to constrain unique struct members in a dynamic array without a helper variable? (adr_hlpr in this problem below) We can use 2 foreach loops but is that the only way?

typedef struct {
  rand bit[7:0]  addr;
  rand bit[15:0] data;
}pkt_t;

class db;
  rand pkt_t     pkt[];
  rand bit[7:0]  adr_hlpr[];

  constraint c{
    pkt.size() == 10;
    adr_hlpr.size() == 10;
    unique {adr_hlpr};
    pkt.sum() with ((item.addr == adr_hlpr[item.index]) ? 1:0) == pkt.size();
  };
endclass: db

module     tb_top();
  db db_h;

  initial begin
    db_h = new();
    db_h.randomize();

    $display("pkt : %p", db_h.pkt);
  end
endmodule: tb_top

Why are you looking for another way?

Hi Dave,

On writing the unique constraint as unique { pkt } I observe compilation error.

Invalid argument ‘this.pkt’ specified for ‘unique’ constraint

Variable ‘pkt’ needs to be numeric for participating in a constraint expression.

Seeking suggestions for the reason behind this error

LRM 18.5.4 Uniqueness constraints says

The group of variables to be constrained shall be specified using a restricted form of the range_list syntax in which each item in the comma-separated list shall be one of the following:

— A singular variable of integral or real type

— An unpacked array variable whose leaf element type is integral or real, or a slice of such an unpacked array variable

A leaf element of an unpacked array is found by descending through the array until an element is reached that is not of unpacked array type.

Would leaf element of unpacked array ‘pkt’ be pkt_t ( i.e structure type ) ?

Yes, the leaf elements of the unpacked array pkt are an unpacked struct. You could convert the leaf elements into a packed struct, but if your goal is to retain only the addr bits unique, this approach won’t give you the desired results.

You need to use the helper array and constrain the elements of the helper array to the addr field of the unpacked array

constraint helpme { foreach(adr_hlpr[i] ) adr_hlpr[i] == pkt[i].addr; }
1 Like