软约束不适用于位变量

soft constraints not working properly for a bit variable

我有一个可变数据,我希望最后一位为 1,其余位为 0,我使用软约束编写了它。

class ABC;
  rand bit [3:0] data;

  // This constraint is defined as "soft" 
  constraint c_data {
                       soft data == 0;
                            data[0] == 1;
                    }
endclass

module tb;
  ABC abc;

  initial begin
    abc = new;
    abc.randomize();
    $display ("data = %0b", abc.data);
  end
endmodule

我希望输出为 'b0001,但实际输出为 data = 1101”

您的问题是由您定义软约束的方式引起的 soft data == 0

软约束只有在不与其他(硬)约束相矛盾时才会得到满足。您的软约束表示 data[3:0]all 位必须是 0。然而,在你的硬约束中,你说 data[0] 必须是 1。因此,软约束无法满足并被忽略。

您可以通过将 data == 0 定义为硬约束来验证这一点:然后随机化将失败。

要获得您期望的行为,请尝试按如下方式定义 class:

class ABC;
    rand bit [3:0] data;

    // This constraint is defined as "soft" 
    constraint c_data {
                        soft data[3:1] == 0;
                        data[0]        == 1;
                       }
endclass