当前位置:网站首页>System Verilog - randomize
System Verilog - randomize
2022-06-24 17:28:00 【Long water and sky】
Catalog
One 、 Random constraints and distributions
- rand Indicates that every time this class is randomized , These variables are assigned a value .
- randc Indicates periodic randomness , That is, random values can be repeated only after all possible values are assigned .
- Random attributes need to be matched SV Predefined class random functions randomize() Use . That is, only by declaration rand Variable , And call... Later through an object randomize() Function can randomize variables .
Class Packet;
//The random variables
rand bit[31:0]src, dst, data[8];
randc bit[7:0]kind;
//Limit the values for src
constraint c(src > 10;
src <15;);
endclass
Packet p;
initial begin
p = new();//Create a packet
assert(p.randomize()) else
$fatal(0, "Packet::randomize failed");
transmit(p);
end
1. Weight distribution
- key word dist A weight distribution that can be used in constraints to generate random values , In this way, some values are more likely to be selected than others .
- “:=” The operator means that the weight of each value in the range of values is the same ,“:/" The operator means that the weight is equally divided into each value in the value range .
- Weights are not expressed as percentages , The sum of weights does not have to be 100.
- Values and weights can be constants or variables .
constraint c_dist{
src dist {
0:=40, [1:3]:=60};
//src = 0, weight = 40/220
//src = 1, weight = 60/220
//src = 2, weight = 60/220
//src = 3, weight = 60/220
dst dist {
0:/40, [1:3]:/60};
//dst = 0, weight = 40/100
//dst = 1, weight = 20/100
//dst = 2, weight = 20/100
//dst = 3, weight = 20/100
}
2. Set members and inside Operator
Use inside Operator produces a set of values .
// A set of random values
rand int c;// A random variable
int lo, hi;// Non random variables as upper and lower bounds
constraint c_range{
c_inside{
[lo:hi]};// lo<=c And c<=hi
}
have access to $ To represent the minimum and maximum values in the value range .
rand bit[6:0]b;// 0<=b<=127
rand bit[5:0]e;// 0<=e<=63
constraint c_range{
b_inside{
[$:4], [20:$]};//0<=b<=4||20<=b<=127
e_inside{
[$:4], [20:$]};//0<=b<=4||20<=b<=63
}
3. Conditionality
adopt -> perhaps if-else To make a constraint expression valid at a particular time .
constraint c_io{
(io_space_mode) ->
addr[31] == 1'b1;
}
constraint c_len_rw{
if(op == READ)
len inside{
[BYTE:LWRD]};
else
len == LWRD;
}
4. Bidirectional constraint
SV The constraints of are bidirectional , This means that it computes the constraints of all random variables at the same time . Adding or deleting constraints on any variable will directly or indirectly affect the selection of values of all related variables .
Two 、 Constraint block control
- A class can include multiple constraint blocks .
- You can use built-in constraint_mode() Function to turn constraints on or off .(constraint_mode(0) prohibit ,constraint_mode(1) Can make .)
- As declarative code , Constraint blocks use {}.
Use constraint_mode() function .
class Packet;
rand int length;
constraint c_short {
length inside {
[1:32]};}
constraint c_long {
length inside {
[1000:1023]};}
endclass
Packet p;
initial begin
p = new();
// By banning c_short Constraints generate long packets
p.c_short.constraint_mode(0);
assert(p.randomize());
transmit(p);
// By banning all constraints , Short packets are then enabled to generate short packets
p.constraint_mode(0);
p.c_short.constraint_mode(1);
asser(p.randomize());
transmit(p);
end
Embedded constraint :SV Allow to use randomize() with To add additional constraints , This is equivalent to adding constraints to a class .
class Transaction;
rand bit[31:0]addr, data;
constraint c1{
addr inside{
[0:100],[1000:2000]};}
endclass
Transaction t;
initial begin
t =new();
// addr Range :50-100,1000-1500 data < 10
assert(t.randomize() with {
addr >= 50; addr <= 1500;
data < 10;});
driveBus(t);
// mandatory addr Take a fixed value ,data > 10
assert(t.randomize() with {
addr == 2000; data > 10;});
driveBus(t);
end
3、 ... and 、 Random function
SV Two special void Type of pre_randomize() and post_randomize() Function is calling randomize() Do something before or after .
- $random(): Average distribution , return 32 Bit signed random number .
- $urandom(): Average distribution , return 32 Bit unsigned random number .
- $urandom_range(): The average distribution within a specified range .
a = $urandom_range(3, 10);//3~10
a = $urandom_range(10, 3);//3~10
b = $urandom_range(5);//0~5
Techniques of constraint
- Constraints using variables
rand int size;
int max_size = 100;
constraint c_size{
size inside{
[1:max_size]};// By changing max_size To change the value of a random variable size Upper limit
}
// With weighted variables dist constraint
rand rand_e;
rand rand_cmd;
int read8_wt = 1, read16_wt = 1, read32_wt = 1;
constraint c_read{
read_cmd dist{
READ8:=read8_wt,
READ16:=read16_wt,
READ32:=rea32_wt};
}
- Use non random values
If almost all the desired excitation vectors have been generated in a random process with a set of constraints , But there are still a few excitation vectors missing , You can call first randomize() function , Then set the value of the random variable to a fixed expected value .
p.length.rand_mode(0);// Set the packet length to a non random value
p.length = 42;// Set the packet length to a constant
- Randomize individual variables
class Rising;
byte low;//not random
rand byte med, hi;//random variable
constraint up{
low < med; med < hi;}
endclass
initial begin
Rising r;
r = new();
r.randomize();// randomization hi, But it doesn't change. low
r.randomize(med);// Just randomize med
r.randomize(low);// Just randomize low
end
- Turn constraints on or off
When there are many constraint expressions , An independent set of constraints can be established for each instruction , Turn off all other constraints when using . - Define external constraints
External constraints work on all instances of the class , The inline constraint affects only once randomize() call .
// Classes with external constraints
class Packet;
rand bit[7:0]length;
rand bit[7:0]payload[];
constraint c_valid{
length > 0;
payload.size() == length;}
constraint c_external'
endclass
//test.sv
program automatic test;
include"packet.sv" constraint Packet::c_external{
length == 1;}
...
endprogram
Four 、 Array constraint
- Constrains the size of dynamic arrays
class dyn_size;
rand logic[31:0] d[];// Randomize an empty array
constraint d_size {
d.size() inside {
[1:10]};}// Constraint
endclass
Most of the time , The size of the array should be given a range , Prevent the generation of arrays with too large volume or empty arrays .
SV You can use foreach Constrain each element of the array .
class sum;
rand unit len[];
constraint c_len{
foreach(len[i])
len[i] inside{
[1:255]};
len.sum < 1024;
len.size() inside{
[1:8]};}
endclass
- Generate an array with unique element values .
Use foreach Generate unique element values .
class UniqueSlow;
rand bit[7:0] ua[64];
constraint c{
foreach(ua[i])// Operate on each element of the array
foreach(ua[j])
if(i != j)// Except for the element itself
ua[i] != ua[j];// Compare with other elements ( That is, different from other elements )
}
endclass
Use randc Auxiliary classes produce unique element values .
class randc8;
randc bit[7:0]val;
endclass
class LittleUniqueArray;
bit[7:0]ua[64];
function void pre_randomize;
randc8 rc8;
rc8 = new();
foreach(ua[i])begin
assert(rc8.randmize());
ua[i] = rc8.val;
end
endfunction
endclass
- Randomize handle array
If you want to generate multiple objects , You need to create a random handle array . Unlike integer arrays , All elements need to be allocated before randomization . Using dynamic arrays, the maximum number of elements can be allocated as needed , Then use constraints to reduce the size of the array . At randomization , The size of the dynamic handle array can be kept constant or reduced , But you can't add .
5、 ... and 、 stochastic control
1.randsequence
- Use SV Of randsequece To generate a sequence of transactions .
initial begin
for(int i=0; i < 15; i++)begin
randsequence(stream)
stream:cfg_read :=1|
io_read :=2|
mem_read:= 5;
cfg_read:{
cfg_read_task;}|
{
cfg_read_task;} cfg_read;
io_read:{
io_read_task;}|
{
io_read_task;} io_read;
mem_read:{
mem_read_task;}|
{
mem_read_task;} mem_read;
endsequence
end
end
The above code produces stream Sequence , It can be cfg_read,io_read or mem_read, A random sequence randomly selects one of three operations , The weights are 1,2,5.
2.randcase
Use randcase Random control of
initial begin
int len;
randcase
1:len = $urandom_range(0, 2);//10%:0,1,2
8:len = $urandom_range(3, 5);//80%:3,4,5
1:len = $urandom_range(6, 7);//10%:6,7
endcase
end
边栏推荐
- Building a cross public chain platform to solve DAPP development problems
- Using easyjson to improve the efficiency of serialization transmission
- See through the new financial report of Tencent music, online music needs b+c
- Users of the Tiktok open platform are authorized to obtain the user's fan statistics and short video data
- 03. Tencent cloud IOT device side learning -- overview of mqtt control package
- The RTSP video image intelligent analysis platform easynvr cascades to the superior platform through the national standard for playback optimization
- As for IOT safety, 20 CSOs from major manufacturers say
- Open up the construction of enterprise digital procurement, and establish a new and efficient service mode for raw material enterprises
- Classic examples of C language 100
- March 27, 2021: give you a head node of the linked list, and rotate the linked list
猜你喜欢
随机推荐
Jmeter+grafana+influxdb build a visual performance test monitoring platform
This time, talk about the dry goods of industrial Internet | TVP technology closed door meeting
C4D learning notes
Tencent security officially released the IOT security capability map
[version upgrade] Tencent cloud firewall version 2.1.0 was officially released!
Solutions for RTSP video streaming played by several browsers
主链系统发展解析
03. Tencent cloud IOT device side learning -- overview of mqtt control package
[DB Bao 45] MySQL highly available mgr+consult architecture deployment
区块哈希游戏竞猜系统开发(成熟代码)
Advanced anti DDoS IP solutions and which applications are suitable for use
Explanation of MySQL indexing principle
Low education without food? As an old Android rookie in the past six years, I was the most difficult one
AFG EDI requirements details
To redefine the storage architecture, Huawei has used more than five "cores"
Memory alignment in golang
Welcome to the network security threat information sharing program
How to build RTSP test URL in Intranet Environment
Classic examples of C language 100
Several schemes of traffic exposure in kubernetes cluster


