用std ramdomization编写求和约束

时间:2015-08-01 10:34:37

标签: constraints system-verilog

我有以下两个队列,它们具有相同的大小并已分配值。我想发帖随机en_q。但是我不知道有效写它是否热。

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

1 个答案:

答案 0 :(得分:2)

您可能需要将队列重新组织为单个队列。您可以根据需要复制它们以分离它们。以下对我有用。

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