当前位置: 代码迷 >> 综合 >> SystemVerilog rand Variables
  详细解决方案

SystemVerilog rand Variables

热度:95   发布时间:2024-01-26 19:44:56.0

使用rand或randc关键字将变量声明为随机变量。它们可以用于普通变量,数组,动态数组或队列。

rand

class Packet;rand int       count;rand byte      master [$];rand bit [7:0]  data [];...
endclass

让我们以一个简单的类为例,该类具有一个称为data的3位变量,该变量被随机分配了10次。 函数randomize()作为类对象的一部分被调用,以使该类对象内的所有rand类型变量随机化。

class Packet;rand bit [2:0] data;
endclassmodule tb;initial beginPacket pkt = new ();for (int i = 0 ; i < 10; i++) beginpkt.randomize ();$display ("itr=%0d data=0x%0h", i, pkt.data);endend
endmodulerun -all;
# KERNEL: itr=0 data=0x7
# KERNEL: itr=1 data=0x2
# KERNEL: itr=2 data=0x2
# KERNEL: itr=3 data=0x1
# KERNEL: itr=4 data=0x2
# KERNEL: itr=5 data=0x4
# KERNEL: itr=6 data=0x0
# KERNEL: itr=7 data=0x1
# KERNEL: itr=8 data=0x5
# KERNEL: itr=9 data=0x0
# KERNEL: Simulation has finished. There are no more test vectors to simulate.

声明为rand的变量是标准随机变量,其值在其范围内均匀分布。 例如,上面代码片段中的变量数据是一个8位无符号整数,范围从0->255。如果此变量是随机的,没有任何约束,则该范围内的任何值将被分配给具有相等值的变量。 可能性。 在连续随机尝试中,变量最终可能具有相同的值,但概率为1/256。

randc

class Packet;randc int     count;randc byte      master [$];randc bit [1:0]   data [];...
endclass

声明为randc的变量是随机循环的,因此在重复任何特定值之前会循环遍历其范围内的所有值。 例如,上面代码片段中的变量数据的范围是0->3。如果对该变量进行了随机化而没有任何限制,则该范围内的任何值都将分配给该变量,但是对于连续随机化,则将使用相同的值 仅在所有值至少分配一次后重复。 randc变量先于其他随机变量求解。

我们将在上面使用相同的示例,但进行一些小的更改-将数据设为randc变量。

class Packet;randc bit [2:0] data;
endclassmodule tb;initial beginPacket pkt = new ();for (int i = 0 ; i < 10; i++) beginpkt.randomize ();$display ("itr=%0d data=0x%0h", i, pkt.data);endend
endmodule  run -all;
# KERNEL: itr=0 data=0x6
# KERNEL: itr=1 data=0x3
# KERNEL: itr=2 data=0x4
# KERNEL: itr=3 data=0x7
# KERNEL: itr=4 data=0x0
# KERNEL: itr=5 data=0x1
# KERNEL: itr=6 data=0x5
# KERNEL: itr=7 data=0x2
# KERNEL: itr=8 data=0x5              // Start of a new sequence
# KERNEL: itr=9 data=0x0
# KERNEL: Simulation has finished. There are no more test vectors to simulate.

参考文献:
【1】https://www.chipverify.com/systemverilog/systemverilog-random-variables

  相关解决方案