Beruflich Dokumente
Kultur Dokumente
Topics in Randomization
Advantages of Randomization Constrained Random Simulation Constraint Randomization Object Randomization In-line constraints Solving constraints Randomization techniques Random Case Random Number Functions
Page 1 of 2
Advantages of Randomization
Directed testing detects the bugs you expect but Random testing detects the bugs you did not expect. Automatic stimulus generation
Change the characteristics of the data driving the DUT
Page 1 of 2
Constraint Solver
Valid Inputs
Page 1 of 2
Randomization Example
Program test; class Transaction; rand bit [31:0] in_1,in_2,data[]; // Dynamic array randc bit [2:0] in_c; // Cyclic Randomization constraint c_length { data.size inside {[50:500]}; } // Limit array size endclass Transaction tx; initial begin tx = new(); assert(tx.randomize()); send(tx); end endprogram
Page 1 of 2
Constraint Randomization
Purely random stimulus takes too long to do something interesting So we need to limit the possible stimulus with constraint blocks Constraint distribution weights forms the basis for Random testbench Distribution weights can be variables or constants
:= assigns weight to each element :/ divides weight evenly in range
Constraint c_data { data.size <= 500; data.size > 50; In_c == 0; cntrl inside {[2:10], 20, 40, [100:107]}; if (test_mode == CONGEST) dest inside {[in_1-100:in_1+100]};} constraint c_long {data.size > 5000;}
constraint c_0 { src dist {0:=30, [1:3]:=60}; dst dist {0:/30, [1:3]:/60}; }
Page 1 of 2
Array Constraints
Constrain its size, individual elements, or all elements
class C; rand bit [5:0] ary[]; constraint cc {ary.size inside {[1:5]}; ary[0] > 0; foreach (ary[i]) if (i > 0) ary[i] > ary[i-1]);} function void pre_randomize; ary.delete; endfunction endclass
Page 1 of 2
Object Randomization
Random variables
rand returns values over the entire range randc random cyclic value up to 16 bits
Object variables are randomized by randomize() The method is automatically available to classes with random variables. Returns a 1 upon success, 0 on failure
Optional: pre_randomize() & post_randomize() void functions which will be called automatically
pre_randomize() set up random weights post_randomize() cleanup calculations like CRC
Page 1 of 2
In-line Constraints
Constraints may be defined at the time of randomization
Allows test-specific constraints Dont modify the original class for just a single test In-line constraints are additive with existing class constraints Supports all SystemVerilog constraints and distributions
initial begin Transaction tx = new(); s = tx.randomize() with {in_1 >= 50; in_1 <= 1500; in_2 < 10;}; driveBus(tx); // force in_1 to a specific value s = tx.randomize() with { in_1 == 2000; in_2 > 10;}; driveBus(tx); end
Page 1 of 2
Solving Constraints
The solver has to handle algebraic factoring, complex Boolean expressions, mixed integer and bit expressions and more. All constraints interact bidirectional and are solved concurrently Solving rules are related to precedence, sign extension, truncation and wrap-around when creating constraints
Conditional operator: if else if else Behaves like a procedural if, except the conditionals are evaluated bi -directionally. Equivalent to implication. Implication Operator: -> Short version of if Ex: (mode == SMALL) -> (data.size < 10); Global Constraints: x < other object.y; References to rand object data members in the constraints get solved simultaneously Variable ordering: solve x before y; Otherwise all constraints solved simultaneously
Page 1 of 2
Randomization Techniques
Watch out for signed variables What are legal values for first and second? class Environment; rand byte in_1,in_2; constraint c { in_1 + in_2 < 8h40;} endclass Make instances rand Or they wont be randomized Dont call randomize() in new() constructor Test may want to change constraints first Use rand_mode to make a variable random / non-random env.in_1.rand_mode(0); Just replace result of randomization for a directed test
Page 1 of 2
Page 1 of 2
Random Case
Randomization can be done without a class? Use randcase or $urandom_range
randcase
2: len = $urandom_range(0, 2); // 20%: 0, 1, or 2 5: len = $urandom_range(3, 5); // 50%: 3, 4, or 5 3: len = $urandom_range(6, 7); // 30%: 6 or 7 endcase
Useful for creating single variable, stateless code, or nested set of actions Constrained randomization is easier to modify, and can make state variables for scoreboard The bad_crc variable can be used for both generating stimulus and checking the response
Page 1 of 2
Page 1 of 2
Summary
Constrained-random stimulus generation feature in SystemVerilog helps in creating a robust verification environment SystemVerilog offers alternatives in generating random stimulus
Page 2 of 2