首页 > 其他分享 >SystemVerilog Dynamic Array Randomization

SystemVerilog Dynamic Array Randomization

时间:2023-07-12 10:34:14浏览次数:51  
标签:pkt dynamic Dynamic display Randomization array data Array size

https://verificationguide.com/systemverilog/systemverilog-dynamic-array-randomization/

Dynamic Array Randomize

For a dynamic array, it is possible to randomize both array size and array elements.

randomize dynamic array size

In below example, dynamic array size will get randomized based on size constraint, and array elements will get random values.

  1. Declare array as rand
  2. Write constraint for array size, On randomization array size will get the random size
class dynamic_array;
  rand bit [3:0] array1[ ];
  rand bit [7:0] array2[ ];

  constraint a1_size_c { array1.size() inside {[4:10]}; }
  constraint a2_size_c { array2.size() inside {[4:10]}; }
  
  function void display();
    $display("array1 size is = %0d",array1.size());
    $display("array1 = %p",array1);
    $display("array2 size is = %0d",array2.size());
    $display("array2 = %p",array2);
  endfunction
endclass

program dynamic_array_randomization;
  dynamic_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
  
endprogram 

Simulator Output

array1 size is = 7
array1 = '{'h0, 'hf, 'he, 'h4, 'hf, 'h9, 'h8}
array2 size is = 10
array2 = '{'he, 'hab, 'hb0, 'h29, 'h57, 'h52, 'h26, 'h49, 'h34, 'h49}

array size based on another random variable

In the below example, the array size is constrained based on the value of another element.

  1. Declare array as rand
  2. On randomization, the array will get size based on the value of burst type
typedef enum {SINGLE,INCR,INCR4,WRAP4,INCR8,WRAP8} burst_t;

class dy_array;
  rand burst_t   burst_type;
  rand bit [7:0] data[ ];

  constraint data_s { 
    (burst_type == SINGLE)-> data.size() == 1;
    (burst_type == INCR)  -> data.size() == 1;
    (burst_type == INCR4) -> data.size() == 4;
    (burst_type == WRAP4) -> data.size() == 4;
    (burst_type == INCR8) -> data.size() == 8;
    (burst_type == WRAP8) -> data.size() == 8; }
  
  function void display();
    $display("burst_type = %s",burst_type.name());
    $display("data array size is = %0d",data.size());
    $display("data array = %p",data);
  endfunction
endclass

program dynamic_array_randomization;
  dy_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
endprogram 

Simulator Output

burst_type = WRAP4
data array size is = 4
data array = '{'h54, 'h88, 'h9b, 'h9a}

 

Generate unique elements in an array

In the below example.

  1. Constrain array with element value same as an index value
  2. In post randomization shuffle the array, so that array will not have an incremental values
class dynamic_array;
  rand bit [7:0] array[ ];
  
  constraint size_c  { array.size() inside {[4:10]}; }
  constraint array_c { foreach(array[i]) array[i] == i;}
  
  function void post_randomize();
    array.shuffle();  
  endfunction
  
  function void display();
    $display("array size is = %0d",array.size());
    $display("array = %p",array);
  endfunction
  
endclass

program dynamic_array_randomization;
  dynamic_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
endprogram 

Simulator Output

array size is = 7
array = '{'h3, 'h6, 'h2, 'h1, 'h4, 'h5, 'h0}

Sum method used in a dynamic array

In the below example, an array is randomized in such a way that the sum of all the elements equals to 45.

  1. Declare array with rand
  2. Constraint sum of an array using array method sum()
class dynamic_array;
  rand bit [7:0] array[ ];
  
  constraint size_c   { array.size() inside {[4:10]}; }
  constraint array_c  { array.sum() == 45;}
  constraint array_ec { foreach(array[i]) array[i] > 1;}
  
  function void display();
    $display("array size is = %0d",array.size());
    $display("array = %p",array);
  endfunction
endclass

program dynamic_array_randomization;
  dynamic_array pkt;

  initial begin
    pkt = new();
    pkt.randomize();
    pkt.display();   
  end
endprogram 

Simulator Output

array size is = 7
array = '{'h2, 'h15, 'h2, 'h2, 'h2, 'h2, 'he}

标签:pkt,dynamic,Dynamic,display,Randomization,array,data,Array,size
From: https://www.cnblogs.com/skyaha/p/17546865.html

相关文章

  • MySQL 8.0 Dynamic Redo Log Sizing翻译
    本文是MySQL8.0DynamicRedoLogSizing[1]这篇文章的翻译。如有翻译不当的地方,敬请谅解,请尊重原创和翻译劳动成果,转载的时候请注明出处。谢谢!这篇博文将讨论MySQL8.0.30中引入的最新功能/特性:重做日志动态调整大小(dynamicredologsizing)。除了InnoDB缓冲池(bufferpool)......
  • Qt QJsonDocument以及与QJsonArray、QJsonObject、QJsonValue的关联
    0、说明QJsonDocument类提供了read/writeJSON文档的方法。用QJsonDocument::fromJson()方法,可以从将一个JSON文件(或者QByteArray数据)转换为QJsonDocument,用QJsonDocument::toJson()则能起到相反的用法。在此过程中的语法解析是很高效的,并且可以将JSON转换为Qt使用的二......
  • LWC 54:697. Degree of an Array
    LWC54:697.DegreeofanArray传送门:697.DegreeofanArrayProblem:Givenanon-emptyarrayofnon-negativeintegersnums,thedegreeofthisarrayisdefinedasthemaximumfrequencyofanyoneofitselements.Yourtaskistofindthesmallestpossibleleng......
  • ArrayList 源码阅读
    ArrayList源码阅读常用的有序集合,采用的是线性结构,和ArrayList形成对比的是LinkedList,线性表的优点在于遍历查询,链表优点在于修改。属性privatestaticfinallongserialVersionUID=8683452581122892189L;/***m默认大小*/privatestaticfinalintDEFAULT_CAPA......
  • ChatGPT还是有点东西的-public static <T> List<T> Arrays.asList(T... a) {...}
    背景业务开发需要判断业务状态是否在30、40、50、60的集合内,所以写了以下代码int[]inLiq={30,40,50,60};returnArrays.asList(inLiq).contains(o.getOrderStatus());自我Review代码时,验证了下这行代码,发现状态为30时,仍然返回false。在自我怀疑中调整代码,并验证,代码如下:......
  • Dynamics CRM字段安全配置文件,实现某个人只能看某条记录的某个字段
    共享安全字段https://blog.csdn.net/bzpfly/article/details/115652147 具体代码写法:https://learn.microsoft.com/zh-cn/power-apps/developer/data-platform/webapi/reference/fieldpermission?view=dataverse-latest  ......
  • 2023.0705 学习记录(递归,var,foreach,Array)
    递归1.做一个累乘的递归代码:publicstaticintmultiplications(inta){if(a==1){return1;}returna*multiplications(a-1);}2.做一个1-2+3-4..递归pub......
  • Dynamics CRM 邮箱设置 “允许使用凭据进行电子邮件处理” 被禁用的解决
     使用部署管理员账号登录操作系统OS(一般是域账号),打开powerShell:执行如下命令:add-PSSnapinMicrosoft.Crm.PowerShell$setting=Get-CrmSettingServerSideSyncEmailSettings$setting.AllowCredentialsEntryViaNonSecureChannels=$TrueSet-CrmSetting$settingGet-CrmSet......
  • JavaScript(三)Array的高阶函数
    map、reducemap:map()方法定义在JavaScript的Array中,接收一个函数对象作为参数,函数定义运算规则,对array中的每个元素进行运算,结果是一个新的array。functionpow(x){returnx*x;}vararr=[1,2,3,4,5,6,7,8,9];varresults=arr.map(pow);//[1,4,9......
  • C++面试八股文:std::array如何实现编译器排序?
    C++面试八股文:std::array如何实现编译器排序?某日二师兄参加XXX科技公司的C++工程师开发岗位第25面:面试官:array熟悉吗?二师兄:你说的是原生数组还是std::array?面试官:你觉得两者有什么区别?二师兄:区别不是很大,原生数组(非动态数组)和std::array都在栈上开辟空间,初始化的时候......