使用系统 Verilog 生成随机枚举

Generate random enum using system Verilog

typedef enum int { IPV4_VERSION = 0, IPV4_IHL = 1, IPV4_TOTAL_LENGTH = 2,IPV4_CHECKSUM = 3 } ipv4_corrupton;

ipv4_corrupton ipv4_corrupt;

std::randomize(ipv4_corrupt) with {ipv4_corrupt dist { IPV4_VERSION :=2,IPV4_IHL := 4,IPV4_TOTAL_LENGTH := 4,IPV4_CHECKSUM := 2}; };

我运行上面的代码10次,总是得到IPV4_CHECKSUM。我犯了什么错误?

我稍微修改了你的代码并得到了预期的输出。

module test;

  typedef enum int { IPV4_VERSION = 0, IPV4_IHL = 1, IPV4_TOTAL_LENGTH = 2,IPV4_CHECKSUM = 3 } ipv4_corrupton;

  ipv4_corrupton ipv4_corrupt;

  initial begin

    repeat(10) begin

      #1

      std::randomize(ipv4_corrupt) with {ipv4_corrupt dist { 0 :=2,1 := 4,2:= 4,3:= 2}; };

      $display ("Value is %s",ipv4_corrupt);

    end

   end

endmodule

输出:

Value is IPV4_VERSION

Value is IPV4_IHL

Value is IPV4_TOTAL_LENGTH

Value is IPV4_IHL

Value is IPV4_VERSION

Value is IPV4_TOTAL_LENGTH

Value is IPV4_CHECKSUM

Value is IPV4_TOTAL_LENGTH

Value is IPV4_IHL

Value is IPV4_IHL