Constraint correction help

It would be much more helpful to provide more detailed information than simply stating, “This is not working.”

Your code contains syntax errors, but I believe that’s merely a copy error. The actual issue is that you didn’t declare strm_array with the rand modifier. This is necessary when you have class objects contained within the object you’re randomizing, ensuring that the constraint solver can descend into these contained objects and solve the rand members and constraints within them.

I’m assuming you meant to have C and S be random variables, otherwise they must have been set in some other code you did not show.

There is no need for inside expression constraints when the entire range is limited by the bit width of the random variable.

You should be using iterative constructs rather than accessing each individual array element.

module top;

class stream;

  rand bit [13:0] B = 16;
  rand bit [10:0] A = 16;
  rand bit [2:0] C = 1;
  rand bit S = 0;
  // Constraint for C to be the ceiling of B/A and within [0:7]
  constraint const_C {
    C == ((B % A == 0) ? (B / A) : ((B / A) + 1));
  }
  // improve probability of S being 1
  constraint dist_S {
    S dist {0:=1,1:=1}; 
  }
endclass

class tx_config;

  rand stream strm_array[4];

  constraint max_payload {
    strm_array.sum() with (item.S == 1 ? item.B : 0) <= 2047;
  }

  function new(string name = "");
    foreach(strm_array[i])
      strm_array[i] = new();
  endfunction
endclass

  tx_config tx_cfg = new;
  initial repeat(10) begin
    assert(tx_cfg.randomize());
    foreach(tx_cfg.strm_array[i]) $write("%p ",tx_cfg.strm_array[i]);
    $display("\n %d", tx_cfg.strm_array.sum() with (item.S == 1 ? item.B : 0));
  end
endmodule