要随机产生一个4bit的 -1 ~ -7的数:
val = $urandom_range(-1,-7);
要随机产生一个4bit的 0 ~ -7的数:
val = $urandom_range(-0,-7); // 这样写错误
需要写成
std::randomize(val) with { val inside {0,[9:15]};};
参考:
https://www.cnblogs.com/-9-8/p/4414449.html
https://blog.51cto.com/13824643/2145849 randsequence / randcase
Example:
constraint cons_value{
num1 < 1024;
num2 > 0;
(rd_flag == 1'b1) -> data_queue.size == num1 - num1;
data_queue.sum == 1024;
if(mode == 1'b0) { num dist {[1:10]:/20,[11:12]:/60};}
if(gen_mode == 1){
sim inside {0,1,2};
(sim == 2) -> a == 4;
} else if (gen_mode == 4) {
b ==5;
}
foreach(data_queue[i]){
data_queue[i] inside {[0:data]};
if(wr_flag == 1) {
data_queue[i] % 9 == 0;
}
}
}
#val变量中4个bit为1,其余为0.
void'(std::randomize(val) with { $countones(val) inside {tr.num} ; });
assert(std::randomize(val_a) with {val_a inside {[0,10],[100,1000]};};
sucess = std::randomize(val_1,val_2,val_3) with {
val_1 inside { A ,B ,C};
val_2 dist { A := 2 ,B := 5 ,C := 4 };
val_3 inside {[0:{32{1'b}}]};
};
if( sucess == 0 ) begin
`uvm_fatal("TEST", " randomization failed")
end
class rand_value extends uvm_sequence_item;
randc int value;
constraint c_value {value inside {[1:16]};}
endclass
rand_value r_value;
r_value = new();
repeat(16) begin
//r_value = new(); #放在内部错误,无法遍历到
r_value.randomize();
$display("value:%0d",r_value.vaule);
end
post_randomize()
post_randomize 用于有先后关系的随机化
class Package_Gen;
rand bit [7:0] value[];
rand bit [7:0] index[];
package_type cmd_s;
constraint cons_package {
value.size inside {[0:8]};
index.size inside {[0:1024]};
index.size % 16 == 0;
}
function void post_randomize();
cmd_s.block_value = value.size;
cmd_s.index_value = index.size;
endfunciton
endclass
class c1;
rand int randnum;
int hist[$];
constraint cstr1 {randnum inside {[0:10]};};
constraint cstr2 {!(randnum inside {hist});};
function void post_randomize();
hist.push_back(randnum);
endfunction
endclass
module m1;
initial begin
c1 i1;
i1 = new();
repeat(10) begin
assert(i1.randomize());
$display("m1::proc2.i1 randnum %0d", i1.randnum);
end
end
endmodule