当前位置:   article > 正文

systemverilog std::randomize()

std::randomize
  1. 要随机产生一个4bit的 -1 ~ -7的数:
  2. val = $urandom_range(-1,-7);
  3. 要随机产生一个4bit0 ~ -7的数:
  4. val = $urandom_range(-0,-7); // 这样写错误
  5. 需要写成
  6. std::randomize(val) with { val inside {0,[9:15]};};
  1. #val变量中4个bit为1,其余为0.
  2. bit [100:0] val
  3. void'(std::randomize(val) with { $countones(val) inside {4} ; });
  4. #val变量中4个1,其余为0. (绿皮书6.13.2节 书本错误)
  5. bit val[10];
  6. void'(std::randomize(val) with { val.sum() with (int'(item)) == 4'h4; }); ## item含义参考绿皮书p35
  7. 如下示例和仿真器有关:
  8. #十个数相加和为50,每个数都小于7
  9. 第一种写法:
  10. bit [5:0] d1[10];
  11. void'(std::randomize(d1) with {d1.sum() with ((item<7) * item) == 50;});
  12. 第二种写法:
  13. bit [5:0] d1[10];
  14. void'(std::randomize(d1) with {d1.sum() with ((item<7) ? item : 0) == 50;});
  15. #随机出任意5个小于10的数
  16. bit [5:0] d1[10];
  17. void'(std::randomize(d1) with {d1.sum() with (int'(item<10 )) == 5;}); #需要加上int

参考:

https://www.cnblogs.com/-9-8/p/4414449.html

http://www.itkeyword.com/doc/1250864225704845x672/difference-between-stdrandomize-and-class-based-randomize

Systemverilog randsequence 中的 rand join 使用方法

Example:

  1. constraint cons_value{
  2. num1 < 1024;
  3. num2 > 0;
  4. (rd_flag == 1'b1) -> data_queue.size == num1 - num1;
  5. data_queue.sum == 1024;
  6. if(mode == 1'b0) { num dist {[1:10]:/20,[11:12]:/60};}
  7. if(gen_mode == 1){
  8. sim inside {0,1,2};
  9. (sim == 2) -> a == 4;
  10. } else if (gen_mode == 4) {
  11. b ==5;
  12. }
  13. foreach(data_queue[i]){
  14. data_queue[i] inside {[0:data]};
  15. if(wr_flag == 1) {
  16. data_queue[i] % 9 == 0;
  17. }
  18. }
  19. }

assert(std::randomize(val_a) with {val_a inside {[0:10],[100:1000]};};
  1. sucess = std::randomize(val_1,val_2,val_3) with {
  2. val_1 inside { A ,B ,C};
  3. val_2 dist { A := 2 ,B := 5 ,C := 4 };
  4. val_3 inside {[0:{32{1'b}}]};
  5. };
  6. if( sucess == 0 ) begin
  7. `uvm_fatal("TEST", " randomization failed")
  8. end

  1. class rand_value extends uvm_sequence_item;
  2. randc int value;
  3. constraint c_value {value inside {[1:16]};}
  4. endclass
  5. rand_value r_value;
  6. r_value = new();
  7. repeat(16) begin
  8. //r_value = new(); #放在内部错误,无法遍历到
  9. r_value.randomize();
  10. $display("value:%0d",r_value.vaule);
  11. end

post_randomize()

  1. post_randomize 用于有先后关系的随机化
  2. class Package_Gen;
  3. rand bit [7:0] value[];
  4. rand bit [7:0] index[];
  5. package_type cmd_s;
  6. constraint cons_package {
  7. value.size inside {[0:8]};
  8. index.size inside {[0:1024]};
  9. index.size % 16 == 0;
  10. }
  11. function void post_randomize();
  12. cmd_s.block_value = value.size;
  13. cmd_s.index_value = index.size;
  14. endfunciton
  15. endclass
  1. class c1;
  2. rand int randnum;
  3. int hist[$];
  4. constraint cstr1 {randnum inside {[0:10]};};
  5. constraint cstr2 {!(randnum inside {hist});};
  6. function void post_randomize();
  7. hist.push_back(randnum);
  8. endfunction
  9. endclass
  10. module m1;
  11. initial begin
  12. c1 i1;
  13. i1 = new();
  14. repeat(10) begin
  15. assert(i1.randomize());
  16. $display("m1::proc2.i1 randnum %0d", i1.randnum);
  17. end
  18. end
  19. endmodule

动态数组最后一个元素,约束为固定值。数组大小未知。

  1. rand int some_dynamic_array[];
  2. constraint last_elem_c {
  3. foreach(some_dynamic_array[i])
  4. if (i == some_dynamic_array.size() - 1)
  5. some_dynamic_array[i] == 5;
  6. }

固定值包含在数组中(inside是双向作用的)

  1. constraint contains_c {
  2. 2 inside { some_dynamic_array };
  3. }

  数组中不包含某个值

  1. constraint not_contains_c {
  2. !( 5 inside { some_dynamic_array });
  3. }
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/107107
推荐阅读
相关标签
  

闽ICP备14008679号