Chunyi Lin
Chunyi Lin

Reputation: 1

Summation constraint writing with std::randomize

I have two queues; they have the same size and have been assigned value. I want to randomize en_q, but I have no idea about how to write it efficiently.

function void rand ()
int val_q[$];
bit en_q[$];
std::randomize(en_q[i]) with {
  
  // pseudo-code

  sum(val_q[i]*en_q[i]) < 100;  
  // I'm wondering how to convert the above condition into system verilog language? 
  // I know en_q.sum() < 100; but with multiply summation, I have no any idea.
}

endfunction

Upvotes: 0

Views: 367

Answers (1)

dave_59
dave_59

Reputation: 42698

You may need to re-structure your queues into a single queue. You could copy them as needed to seperate them. The following worked for me.

module top;

   typedef struct {
      int     val;
      rand bit    en;
   } field_t;

   field_t q[$];

   initial begin
      q = '{ '{1,0},'{2,0},'{3,0},'{4,0},'{5,0},'{6,0},'{7,0},'{8,0},'{9,0} };
      repeat (10) begin
     if (!std::randomize(q) with {
                 q.sum(x) with (x.val*x.en) <10;
                 }) $error("randomize failed");
         $display("%p",q);
     end
   end
endmodule

Upvotes: 2

Related Questions