Random Variables——简介

介绍

约束驱动的测试生成允许用户自动生成测试以进行功能验证。 随机测试可能比传统的定向测试方法更有效。 通过指定约束,可以轻松创建可以找到难以到达的极端案例的测试。 SystemVerilog允许用户以紧凑的声明性方式指定约束。 然后,由求解器处理约束,该求解器生成满足约束的随机值。

在Verilog中,我们使用了$ random方法生成随机整数值,这是系统任务中的Verilog构建,它返回32位随机值。 这不利于对象随机化(此处基于对象均值类)。 为了帮助基于类的对象被随机化,SystemVerilog支持rand变量和randomize()方法,我们将在下面对此进行详细介绍。

随机变量Random Variables

可以使用rand和randc修饰符将类属性声明为随机变量。

【1】Systemverilog可以随机化整数,reg和枚举类型的标量变量。 bit变量可以是Systemverilog支持的任何大小;
【2】可以将数组声明为rand或randc,在这种情况下,将其所有成员元素都视为rand或randc;
【3】关联数组,动态数组可以声明为rand或randc;
【4】关联数组可以声明为rand或randc,但是,只有数字键范围为0到n-1的元素才是随机的。
如上所示,变量可以声明为:
【1】rand
【2】randc

rand

用rand关键字声明的变量是标准随机变量。它们的值在其范围内均匀分布。

randc

用randc关键字声明的变量是随机循环变量,它们以其声明范围的随机排列循环所有值。 随机循环变量只能是reg或枚举类型,并且最大大小限制为16位,因此任何randc变量的最大范围为0到65535。

基本思想是randc随机迭代范围内的所有值,并且在迭代中不重复任何值。 迭代完成后,将自动开始新的迭代。

Example : Random Variables

 1 typedef enum { UNICAST=11, MULTICAST, BROADCAST} pkt_type;
  2 
  3 program rand_ex;
  4   class frame_t;
  5     rand pkt_type ptype;
  6     rand integer len;
  7     randc bit [1:0] no_repeat;
  8     rand bit  [7:0] payload [];
  9     // 约束成员
 10     constraint legal {
 11       len >= 2;
 12       len <= 5;
 13       payload.size() == len;
 14     }
 15     function string getType(pkt_type ltype);
 16       begin
 17         case(ltype)
 18          UNICAST   : getType = "UNICAST";
 19          MULTICAST : getType = "MULTICAST";
 20          BROADCAST : getType = "BROADCAST";
 21          default   : getType = "UNKNOWN";
 22         endcase
 23       end
 24     endfunction
 25     // Print the members of the class
 26     task print();
 27       begin
 28         integer i =0;
 29         $write("Packet type %s\n",getType(ptype));
 30         $write("Size of frame is %0d\n",len);
 31         if (payload.size() > 0) begin
 32           $write("Payload is ");
 33           for (i=0; i < len; i++) begin
 34             $write(" %2x",payload[i]);
 35           end
 36           $write("\n");
 37         end
 38         $write("no_repeat is %d\n",no_repeat);
 39       end  
 40     endtask
 41   endclass
 42 
 43   initial begin
 44     frame_t frame = new();
 45     integer j = 0;
 46     // Print frame before randomize
 47     $write("-------------------------------\n");
 48     frame.print(); 
 49     $write("-------------------------------\n");
 50     for (j = 0 ; j < 10;j++) begin
 51       if (frame.randomize() == 1) begin
 52         // Print frame after randomize
 53         frame.print(); 
 54       end else begin
 55         $write("Failed to randomize frame\n");
 56       end
 57       $write("-------------------------------\n");
 58     end
 59   end
 60 endprogram

 	 	


 -------------------------------
 Packet type UNKNOWN
 Size of frame is x
 no_repeat is 0
 -------------------------------
 Packet type MULTICAST
 Size of frame is 3
 Payload is  0b df 40
 no_repeat is 0
 -------------------------------
 Packet type BROADCAST
 Size of frame is 3
 Payload is  fa 4e 15
 no_repeat is 1
 -------------------------------
 Packet type BROADCAST
 Size of frame is 5
 Payload is  c4 aa c4 cf 4f
 no_repeat is 2
 -------------------------------
 Packet type UNICAST
 Size of frame is 3
 Payload is  2c ce 05
 no_repeat is 3
 -------------------------------
 Packet type BROADCAST
 Size of frame is 2
 Payload is  60 5f
 no_repeat is 2
 -------------------------------
 Packet type BROADCAST
 Size of frame is 4
 Payload is  41 3f 12 f4
 no_repeat is 3
 -------------------------------
 Packet type BROADCAST
 Size of frame is 3
 Payload is  88 01 31
 no_repeat is 1
 -------------------------------
 Packet type UNICAST
 Size of frame is 3
 Payload is  4f 00 dd
 no_repeat is 0
 -------------------------------
 Packet type UNICAST
 Size of frame is 5
 Payload is  45 f2 a2 a1 fd
 no_repeat is 0
 -------------------------------
 Packet type UNICAST
 Size of frame is 4
 Payload is  20 e1 97 c6
 no_repeat is 3
 -------------------------------

参卡文献:
【1】http://www.asic-world.com/systemverilog/random_constraint1.html#Introduction

发布了124 篇原创文章 · 获赞 8 · 访问量 6686

猜你喜欢

转载自blog.csdn.net/qq_43042339/article/details/104551277