赞
踩
Verilog系统自带一些随机数发生函数。最常用的有random。语法如下:
$random(seed);
如果希望生成的随机数具有一定的概率分布,还有一些系统函数可以选用。
简单的语法如下:
$dist_uniform (seed, start, end) ;
$dist_normal (seed, mean, standard_deviation) ;
$dist_exponential (seed, mean) ;
$dist_poisson (seed, mean) ;
$dist_chi_square (seed, degree_of_freedom) ;
$dist_t (seed, degree_of_freedom) ;
$dist_erlang (seed, k_stage, mean) ;
parameter MAX_NUM = 512;
parameter MIN_NUM = 256;
reg [15:0] num_range1, num_range2, num_range3 ;
always@(posedge clk) begin
//产生的随机数范围为 -511 ~ 511, ±(MAX_NUM-1)
num_range1 <= $random() % MAX_NUM;
//产生的随机数范围为 0 ~ 511, (0 ~ MAX_NUM-1)
num_range2 <= {$random()} % MAX_NUM;
//产生的随机数范围为 MIN_NUM ~ MAX_NUM,包含边界
num_range3 <= MIN_NUM + {$random()} % (MAX_NUM-MIN_NUM+1);
end
obj.randomize & std::randomize
例如:
program automatic test; class pkt; logic [15:0] pkt_size; function new(); pkt_size = 100; endfunction: new function logic [7:0] get_num(); logic [7:0] scope_var; // When this function is called, randomize class // member var 'pkt_size' using the class's // in-built randomize method randomize(pkt_size); // Using SV std lib's scope randomize this // function's local'scope_var' std::randomize(scope_var); $display("pkt.get_num: pkt_size %0d scope_var %0d", pkt_size, scope_var); endfunction: get_num endclass: pkt initial begin pkt p; p = new(); p.get_num(); end endprogram
sv通过约束(constraint)来实现概率分布控制。例如:
randomize(pkt_size) with {
pkt_size inside {[10:50]};
};
// Using SV std lib's scope randomize
std::randomize(scope_var) with {
scope_var inside {10, 20, 30};
};
p.srandom(seed)
# Synopsys-VCS
% ./simv +ntb_random_seed=10
# Mentor-Questa
% vsim -c test -sv_seed 10 -do "run -all"
参考文献:
https://www.systemverilog.io/randomization#id-1c
https://www.runoob.com/w3cnote/verilog2-random.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。