SlideShare a Scribd company logo
1 of 47
Download to read offline
INF5430
SystemVerilog for Verification
Chapter 6.1-12
Randomization
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Chapter 6: Randomization
2
•Directed testing:
•Checks only anticipated bugs
•Scales poorly as requirements change
•Little upfront work
•Linear progress
•Random testing:
•Checks unanticipated bugs
•Scales well as requirements change
•More upfront work
•Better than linear progress
Test Plan
Completion
100%
Time
Directed
Testing
Random
Testing
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.1 Introduction
3
•A testbench based on randomization is a shotgun
•The features you are trying to test is the target
•How to cover untested areas?
•More random testing with tighter constraints
•Directed testing
•When is testing done?
•Functional coverage
•Code coverage
Features
Features
Features
Features
Features
Shotgun Verification or The Homer Simpson Guide to Verification, Peet James
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.2 What to randomize?
4
Much more than data
1. Device configuration
2. Environment configuration
3. Primary input data
4. Encapsulated input data
5. Protocol exceptions
6. Errors and violations
7. Delays
8. Test order
9. Seed for the random test
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.3 Randomization in SystemVerilog
5
•Specified within a class along with constraints
•Variable declared with rand keyword distributes values uniformly
•Variable declared with randc keyword distributes values cyclically
•No repeats within an iteration
•Constraints specified with constraint keyword
•New values selected when randomize() function called
•Returns 1 if constraints can be solved, 0 otherwise
3, 2, 0, 0, 3, 1, .....
initial permutation 0,3,2,1
next permutation 0,3,2,1
next permutation 2,0,1,3
rand bit [1:0] y;
randc bit [1:0] y;
constraint y_c {y >=1;y<3;}
<handle>.randomize();
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.3.1 Simple class with Random Variables
6
class Packet;
rand bit [31:0] src, dst, data[8];
randc bit [7:0] kind;
constraint c {src > 10; src < 15;}
endclass
Packet p;
initial begin
p=new();
if (!p.randomize())
$finish;
transmit(p);
end
Constraint expressions
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.3.2 Checking the result from randomization
7
• Always check the result of a call to randomize()
• Text uses a macro to check the results from randomization
`define SV_RAND_CHECK(r) 
do begin 
if (!(r)) begin 
$display("%s:%0d: Randomization failed "%s"", 
`__FILE__, `__LINE__, `"r`"); 
$finish; 
end 
end while (0)
# test.sv:13: Randomization failed “p.randomize()"
`SV_RAND_CHECK(p.randomize());
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.3.3 The constraint solver
8
•Solves constraint expressions
•Same seed results in the same random values
•Use different seeds in each nightly regression run.
•Constraints may take a long time to solve
•Solver is specific to each simulator vendor/release.
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.3.4 What can be randomized?
9
•2-state variables
•4-state variables except no X’s or Z’s will be created.
•Integers
•Bit vectors
•Arrays
•Time
•Real, string
•Handle in constraint
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.2 Simple Expressions
10
•Each constraint expression should contain only 1 relational
operator
• <, <=, ==, >,=>
• Constraint bad is broken down into multiple binary relational
expressions from left to right. lo and med are randomized.
• lo < med is evaluated, but not constrained. Results in 0 or 1.
• hi > 0 or 1 constraint is then evaluated.
• Not what you want!
• Correct constraint:
class Order_bad;
rand bit [7:0] lo, med, hi;
constraint bad {lo < med < hi;}
endclass
constraint good{lo < med;
med < hi;}
lo=20, med=224, hi=164
lo=114, med=39, hi=189
lo=186, med=148, hi=161
lo=214, med=223, hi=201
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.3 Equivalence Expressions
11
•Suppose you want to constrain a value to be equal to an expression
• len must be declared as random
•Using = is a syntax error
class order;
rand bit [7:0] addr_mode, size, len;
constraint order_c {len == addr_mode*4 + size;}
endclass
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.4 Weighted Distributions
12
•Weighted distributions cause a non-uniform distribution
•Weights do not have to add up to 100% and can be variables
•Cannot be used with randc
•What would this be used for?
• For a CPU want less or more of a particular opcode
• For a datapath want max neg, 0, and max pos more often
constraint <constraint name> {<variable name> dist {<distribution>}};
:= operator indicates the weight is the same for all values
:/ operator indicates the weight is distributed across all values
constraint c_dist {
src dist {0:=40, [1:3]:=60};
dst dist {0:/40, [1:3]:/60};
}
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.4 Weighted Distributions := operator
13
:= operator indicates the weight is the same for all values
src = 0, weight = 40
src = 1, weight = 60
src = 2, weight = 60
src = 3, weight = 60
/220
/220
/220
/220
=18%
=27%
=27%
=27%
constraint src_dist { src dist {0:=40, [1:3]:=60} ;}
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.4 Weighted Distributions :/ operator
14
:/ operator indicates the weight is distributed across all values
dst = 0, weight = 40
dst = 1, weight = 20
dst = 2, weight = 20
dst = 3, weight = 20
/100
/100
/100
/100
=40%
=20%
=20%
=20%
constraint dst_dist {dst dist {0:/40, [1:3]:/60} ;}
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.4 Weighted Distributions (cont.)
15
•Weights can be constants, ranges, or variables.
•Using variables allows the weights to be adjusted on the fly
class BusOp;
typedef enum {BYTE, WORD, LWRD} length_e;
rand length_e len;
bit [31:0] w_byte=1, w_word=3, w_lwrd=5;
constraint c_len {
len dist {BYTE := w_byte,
WORD := w_word,
LWRD := w_lwrd};
}
endclass
constraint address_c {address > 2; address < 5;}
constraint address_range{address inside{[3:4]};}
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.5 Set membership and the inside operator
16
•Alternative to {var>value1 ; var<value2} is the inside keyword
•Using the ! operator can exclude ranges
equivalent
constraint c_range {
!(c inside{[lo:hi]});
}
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.6 Using an array in a set
17
•Suppose you want to create multiple equivalence constraints
•For example: f can only equal 1, 2, 3, 5, 8
•Alternate solution is to store the values in an array
•Can specify that values in the array are NOT to be chosen
rand bit [7:0] f;
constraint c_fibonacci {
(f==1) || (f==2) || (f==3) || (f==5) || (f==8));}
rand bit [7:0] f;
bit [31:0] vals[]= ‘{1,2,3,5,8};
constraint c_fibonacci {f inside vals;}
rand bit [7:0] notf;
bit [31:0] vals[]= ‘{1,2,3,5,8};
constraint c_fibonacci {!(notf inside vals);}
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.7 Bidirectional Constraints
18
•Constraint blocks are not procedural but declarative.
•All constraints are active at the same time.
Solution r s t
A 6 6 7
B 6 6 8
C 6 6 9
D 7 7 8
E 7 7 9
F 8 8 9
rand bit [15:0] r,s,t;
constraint c_bidir {
r < t;
s == r;
t < 10;
s > 5;}
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.8 Implication Constraints
19
Suppose you want to impose different constraints depending on a var
Solution 1: Solution 2:
constraint mode_c {
if (mode == small)
len < 10;
else if (mode == large)
len > 100;
}
constraint mode_c {
(mode == small) -> len < 10;
(mode == large) -> len > 100;
}
equivalent
equivalent
equivalent
{(a==1)->(b==0)};
{if (a==1) b==0;}
{!(a==1) || (b==0);}
{(a==0) || (b==0);}
Solution a b
A 0 0
B 0 1
C 1 0
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.4.9 Equivalence operator
20
•The equivalence operator <-> is bidirectional.
•A<->B is defined as ((A->B) && (B->A))
rand bit d, e;
constraint c { d==1 <-> e==1; }
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.5 Solution Probabilities
21
It’s important to understand how constraints affect the probability of
the solution.
Unconstrained:
class Unconstrained;
rand bit x;
rand bit [1:0] y;
endclass
Solution x y Probability
A 0 0 1/8
B 0 1 1/8
C 0 2 1/8
D 0 3 1/8
E 1 0 1/8
F 1 1 1/8
G 1 2 1/8
H 1 3 1/8
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.5.2 Implication
22
class Imp1;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
}
endclass
Solution x y Probability
A 0 0 1/2
B 0 1 0
C 0 2 0
D 0 3 0
E 1 0 1/8
F 1 1 1/8
G 1 2 1/8
H 1 3 1/8
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.5.3 Implication and bidirectional
constraints
23
class Imp2;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
y>0;
(x==0)->y==0;
}
endclass
Solution x y Probability
A 0 0 0
B 0 1 0
C 0 2 0
D 0 3 0
E 1 0 0
F 1 1 1/3
G 1 2 1/3
H 1 3 1/3
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.5.4 Guiding Distribution with solve/before
24
• Solve before tells the solver to solve for 1 variable before
another.
•The possible solutions does not change, just the probability.
class SolveBefore;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
solve x before y;
}
endclass
Solution x y Probability
A 0 0 1/2
B 0 1 0
C 0 2 0
D 0 3 0
E 1 0 1/8
F 1 1 1/8
G 1 2 1/8
H 1 3 1/8
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
solve y before x;
25
class Imp1;
rand bit x;
rand bit [1:0] y;
constraint c_xy {
(x==0)->y==0;
solve y before x;
}
endclass
Solution x y Probability
A 0 0 1/8
B 0 1 0
C 0 2 0
D 0 3 0
E 1 0 1/8
F 1 1 1/4
G 1 2 1/4
H 1 3 1/4
class Packet
rand bit [31:0] length;
constraint c_short {length inside {[1:32]};}
constraint c_long {length inside {[1000:1023]};}
endclass
Packet p:
initial begin
p=new();
p.c_short.constraint_mode(0);
`SV_RAND_CHECK(p.randomize());
transmit(p);
....
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.6 Controlling Multiple Constraint Blocks
26
...
p.constraint_mode(0);
p.c_short.constraint_mode(1);
`SV_RAND_CHECK(p.randomize());
transmit(p);
end // initial
Use the constraint_mode() function to turn constraints on/off
<handle>.constraint_mode(<0/1>);
<handle>.<constraint>.constraint_mode(<0/1>);
6.7 Valid Constraints
27
•A suggested technique to creating valid stimulus is to create valid
constraints
•Turn the constraint off to test the system’s response to invalid
stimulus.
•For example, suppose a read-modify-write command is only valid if
the length is a long word.
class Transaction;
typedef enum {BYTE, WORD, LWRD, QWRD} length_e;
typedef enum {READ, WRITE, RMW, INTR} access_e;
rand length_e length;
rand access_e access;
constraint valid_RMW_LWRD {
(access == RMW) -> (length == LWRD);
}
endclass
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
class Transaction;
rand bit [31:0] addr, data;
constraint c1 {addr inside{[0:100], [1000:2000]};}
endclass
intitial begin
Transaction t;
t=new();
`SV_RAND_CHECK(t.randomize() with {addr >=50; addr <=1500;
data <10;});
driveBus(t);
`SV_RAND_CHECK(t.randomize() with {addr ==2000; data >10;});
driveBus(t);
end
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.8 In-line Constraints
28
•In-line constraints create constraints outside of the class.
•Add to existing constraints if they are enabled.
•For example, a single test needs to be written with tighter than
usual address constraints
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.9 pre_randomize/post_randomize
29
•Implicitly called before/after every call to randomize()
•void function
•Cannot consume time.
•Can only call other functions.
• Does not return a value
•Overload to add your functionality
•post_randomize() is good for cleaning up
class Packet;
rand bit [31:0] length;
constraint c_length {
length inside {[1:100]};
}
endclass
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.11 Constraint Tips and Techniques
30
•Instead of hardcoding constraints use variables with defaults
•Allows the constraint to be modified without modifying the class
•Allows invalid stimulus to be generated
class Packet;
rand bit [31:0] length;
int max_length= 100;
constraint c_length {
length inside {[1:max_length]};
}
endclass
initial begin
Packet p1 = new();
p1.max_length = 200;
p1.randomize();
end
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.11.2 Using Nonrandom Values
31
class Packet;
rand bit [7:0] length;
constraint c_length{length > 0;}
..... // Other constraints depending on length
endclass
initial begin
Packet p = new();
`SV_RAND_CHECK(p.randomize());
p.length.rand_mode(0);
p.length = 42; (or: 0;)
`SV_RAND_CHECK(p.randomize());
p.rand_mode(0);
end
Create an invalid length zero
Value for length will be
included in constraint solution
•constraint_mode() turns on/off constraints
•rand_mode() makes a variable or every variable in an object non-
random
Make length nonrandom
class Transaction;
rand bit [31:0] addr, data;
constraint c1 {addr inside{[0:100], [1000:2000]};}
endclass
Transaction t;
initial begin
t=new();
`SV_RAND_CHECK(t.randomize());
t.addr = 200;
`SV_RAND_CHECK(t.randomize(null));
end
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.11.3 Checking Values using Constraints
32
•If you change the value of random variables how do you know all
your random variables are still valid?
•Use a call to <handle>.randomize(null) to check.
...Randomization failed
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.11.4 Randomizing Individual Variables
33
Can pass variables to randomize() to randomize only a subset of
variables
class Rising;
bit [7:0] low;
rand bit [7:0] med, hi;
constraint up { low < med; med < hi; }
endclass
initial begin
Rising r;
r = new();
`SV_RAND_CHECK(r.randomize());
`SV_RAND_CHECK(r.randomize(med));
`SV_RAND_CHECK(r.randomize(low)); // Surprisingly!!
`SV_RAND_CHECK(r.randomize(low, med));
end
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.11.5 Turn Constraints Off and On
34
•Use many simple constraints instead of 1 complex constraint
•Turn on the constraints needed
class Instruction;
typedef enum {NOP, HALT, CLR, NOT} opcode_e;
rand opcode_e opcode;
bit [1:0] n_operands;
constraint c_operands{
if (n_operands == 0)
(opcode == NOP) || (opcode == HALT);
else if (n_operands == 1)
(opcode == CLR) || (opcode == NOT);
.....
}
endclass
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.11.5 Turn Constraints Off and On (cont.)
35
class Instruction;
typedef enum {NOP, HALT, CLR, NOT} opcode_e;
rand opcode_e opcode;
constraint c_no_operands{
(opcode == NOP) || (opcode == HALT);}
constraint c_one_operand{
(opcode == CLR) || (opcode == NOT);}
.....
}
endclass
initial begin
Instruction instr = new();
instr.constraint_mode(0);
instr.c_no_operands.constraint_mode(1);
`SV_RAND_CHECK(instr.randomize());
end
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.12 Common Randomization Problems
36
•Using a signed variable isn’t an issue if you control the values
•However, a randomized signed variable will produce negative values
•Some valid solutions of {pkt1_len, pkt2_len} are:
for (int i=0;i<=5;i++)
class SignedVars;
rand byte pkt1_len, pk2_len;
constraint total_len {pkt1_len + pk2_len == 64;}
endclass
(32,32)
(2,62)
(-63, 127)
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
6.12.1 Use Signed Values with care
37
•Might be temped to declare pkt1_len, pk2_len as large unsigned
•A valid solution of {pkt1_len, pkt2_len} is
•One solution is to constrain the max values of pkt1_len and pk2_len
•Best solution is to only use values as wide as required
class Vars32;
rand bit [31:0] pkt1_len, pk2_len;
constraint total_len {pkt1_len + pk2_len == 64;}
endclass
class Vars8;
rand bit [7:0] pkt1_len, pkt2_len;
constraint total_len {pkt1_len + pkt2_len == 9’d64;}
endclass
(32’h80000040, 32’h80000000) = 32’h40=32’d64
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 1
38
Write the SystemVerilog code for the following items:
1) Create a class Exercise1containing two variables, 8-bit data
and 4-bit address. Create a constraint block that keeps addressto
3 or 4.
2) In an initialblock, construct an Exercise1object and
randomize it. Check the status from randomization.
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 1 solution
39
Write the SystemVerilog code for the following items:
1) Create a class Exercise1containing two variables, 8-bit dataand 4-bit
address. Create a constraint block that keeps addressto 3 or 4.
2) In an initialblock, construct an Exercise1object and randomize it. Check
the status from randomization.
class Exercise1;
rand bit [7:0] data;
rand bit [3:0] address;
constraint address_c {
address > 2;
address < 5;
}
endclass
initial begin
Exercise1 MyExercise1;
MyExercise1 = new;
`SV_RAND_CHECK(MyExercise1.randomize());
end
// or
// ((address==3) || (address==4));
// or
// address inside {[3:4]};
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 2
40
Modify the solution for Exercise1 to create a new class
Exercise2 so that:
1. data is always equal to 5
2. Probability of address = 4’d0 is 10%
3. Probability of address being between [1:14] is 80%
4. Probability of address = 4’d15 is 10%
Demonstrate its usage by generating 20 new data and address
values and check for error.
Constraint Exercise 2 solution
41
Modify the solution for Exercise1 to create a new class Exercise2 so that:
1. datais always equal to 5
2. Probability of address = 4’d0 is 10%
3. Probability of addressbeing between [1:14] is 80%
4. Probability of address = 4’d15 is 10%
package my_package;
class Exercise2;
rand bit [7:0] data;
rand bit [3:0] address;
constraint data_c {data == 5;}
constraint address_dist {
address dist {0:=10,
[1:14]:/80,
15:=10};
}
function void print_all;
$display("data = %d, address = %d", data, address);
endfunction
endclass
endpackage
The := operator when the weight
is the same for every specified
value in the range.
The :/ operator when the weight
is to be equaly divided between
all values.
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 2 solution cont.
42
Demonstrate its usage by generating 20 new dataand addressvalues and check for error.
program automatic test;
import my_package::*;
initial begin
Exercise2 MyExercise2;
repeat (20) begin
MyExercise2 = new;
`SV_RAND_CHECK(MyExercise2.randomize());
MyExercise2.print_all();
end
end
endprogram
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 3
43
class Stim;
const bit [31:0] CONGEST_ADDR = 42;
typedef enum {READ, WRITE, CONTROL} stim_e;
randc stim_e kind;
rand bit [31:0] len, src, dst;
bit congestion_test;
constraint c_stim {
len < 1000;
len > 0;
if (congestion_test) {
dst inside {[CONGEST_ADDR-10:CONGEST_ADDR+10]};
src == CONGEST_ADDR;
} else
src inside {0, [2:10], [100:107]};
}
endclass
What are the constraints on len,
dst, and src for this code?
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 3 solution
44
class Stim;
const bit [31:0] CONGEST_ADDR = 42;
typedef enum {READ, WRITE, CONTROL} stim_e;
randc stim_e kind;
rand bit [31:0] len, src, dst;
bit congestion_test;
constraint c_stim {
len < 1000;
len > 0;
if (congestion_test) {
dst inside {[CONGEST_ADDR-10:CONGEST_ADDR+10]};
src == CONGEST_ADDR;
} else
src inside {0, [2:10], [100:107]};
}
endclass
What are the constraints on len, dst, and src for
this code?
• len must be between 1 and 999 inclusive
• if bit congestion_test is 1 dst must be inside 42-10 =32 to 42+10 (52) and src = 42
• else src can take on values 0, 2 to 10, and 100 to 107. dst is unconstrained.
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 4
45
For the following class create:
1. A constraint that limits read transactions addresses to the range
0 to 7, inclusive
2. Write behavioral code to turn off the above constraint. Construct
and randomize a MemTrans object with an in-line constraint that
limits read transaction addresses to the range 0 to 8, inclusive.
Test that the in-line constraint is working.
class MemTrans;
rand bit rw; // read if rw = 0, write if rw = 1
rand bit [7:0] data_in;
rand bit [3:0] address;
endclass
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 4 solution
46
A constraint that limits read transactions addresses to the range 0 to 7, inclusive:
or
class MemTrans;
rand bit rw; // read if rw = 0, write if rw = 1
rand bit [7:0] data_in;
rand bit [3:0] address;
constraint valid_rw_addr { (rw == 0)->(address inside {[0:7]}); }
endclass // MemTrans
class MemTrans;
rand bit rw; // read if rw = 0, write if rw = 1
rand bit [7:0] data_in;
rand bit [3:0] address;
constraint valid_rw_addr { if (!rw) address inside {[0:7]}; }
endclass // MemTrans
Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
Constraint Exercise 4 solution cont.
47
Write behavioral code to turn off the above constraint. Construct and randomize a
MemTrans object with an in-line constraint that limits read transaction addresses
to the range 0 to 8, inclusive. Test that the in-line constraint is working.
MemTrans MyMemTrans;
initial begin
MyMemTrans = new();
MyMemTrans.valid_rw_address.constraint_mode(0);
`SV_RAND_CHECK(MyMemTrans.randomize() with {(rw == 0)->(address inside {[0:8]});});
end

More Related Content

Similar to inf5430_sv_randomization.pdf

System Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSystem Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancements
Subash John
 
NIPS2007: structured prediction
NIPS2007: structured predictionNIPS2007: structured prediction
NIPS2007: structured prediction
zukun
 

Similar to inf5430_sv_randomization.pdf (20)

Xgboost
XgboostXgboost
Xgboost
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
C tutorial
C tutorialC tutorial
C tutorial
 
Linear regression
Linear regressionLinear regression
Linear regression
 
The TclQuadcode Compiler
The TclQuadcode CompilerThe TclQuadcode Compiler
The TclQuadcode Compiler
 
How Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzerHow Data Flow analysis works in a static code analyzer
How Data Flow analysis works in a static code analyzer
 
System Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancementsSystem Verilog 2009 & 2012 enhancements
System Verilog 2009 & 2012 enhancements
 
Towards Evaluating Size Reduction Techniques for Software Model Checking
Towards Evaluating Size Reduction Techniques for Software Model CheckingTowards Evaluating Size Reduction Techniques for Software Model Checking
Towards Evaluating Size Reduction Techniques for Software Model Checking
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Chapter 00 revision
Chapter 00 revisionChapter 00 revision
Chapter 00 revision
 
System Verilog Tutorial - VHDL
System Verilog Tutorial - VHDLSystem Verilog Tutorial - VHDL
System Verilog Tutorial - VHDL
 
Verilog Tutorial - Verilog HDL Tutorial with Examples
Verilog Tutorial - Verilog HDL Tutorial with ExamplesVerilog Tutorial - Verilog HDL Tutorial with Examples
Verilog Tutorial - Verilog HDL Tutorial with Examples
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
05-Debug.pdf
05-Debug.pdf05-Debug.pdf
05-Debug.pdf
 
NIPS2007: structured prediction
NIPS2007: structured predictionNIPS2007: structured prediction
NIPS2007: structured prediction
 
Pepe Vila - Cache and Syphilis [rooted2019]
Pepe Vila - Cache and Syphilis [rooted2019]Pepe Vila - Cache and Syphilis [rooted2019]
Pepe Vila - Cache and Syphilis [rooted2019]
 
NSC #2 - D2 06 - Richard Johnson - SAGEly Advice
NSC #2 - D2 06 - Richard Johnson - SAGEly AdviceNSC #2 - D2 06 - Richard Johnson - SAGEly Advice
NSC #2 - D2 06 - Richard Johnson - SAGEly Advice
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)
 

More from SamHoney6

eetop.cn_UVM_REG_workshop.--------------pdf
eetop.cn_UVM_REG_workshop.--------------pdfeetop.cn_UVM_REG_workshop.--------------pdf
eetop.cn_UVM_REG_workshop.--------------pdf
SamHoney6
 
Cadence GenusTutorial------------ .pdf.pdf
Cadence GenusTutorial------------ .pdf.pdfCadence GenusTutorial------------ .pdf.pdf
Cadence GenusTutorial------------ .pdf.pdf
SamHoney6
 
snug07_Verilog Gotchas for Verification.pdf
snug07_Verilog Gotchas for Verification.pdfsnug07_Verilog Gotchas for Verification.pdf
snug07_Verilog Gotchas for Verification.pdf
SamHoney6
 
04+ECETEMT092-+WDT+APB+UVM.pdf
04+ECETEMT092-+WDT+APB+UVM.pdf04+ECETEMT092-+WDT+APB+UVM.pdf
04+ECETEMT092-+WDT+APB+UVM.pdf
SamHoney6
 
14-Bill-Tiffany-SigmaSense-VF2023.pdf
14-Bill-Tiffany-SigmaSense-VF2023.pdf14-Bill-Tiffany-SigmaSense-VF2023.pdf
14-Bill-Tiffany-SigmaSense-VF2023.pdf
SamHoney6
 
RF-Transceiver.pdf
RF-Transceiver.pdfRF-Transceiver.pdf
RF-Transceiver.pdf
SamHoney6
 
UVM_TB_20220621_slides-1.pdf
UVM_TB_20220621_slides-1.pdfUVM_TB_20220621_slides-1.pdf
UVM_TB_20220621_slides-1.pdf
SamHoney6
 
Web Template Mechanisms in SOC Verification - DVCon.pdf
Web Template Mechanisms in SOC Verification - DVCon.pdfWeb Template Mechanisms in SOC Verification - DVCon.pdf
Web Template Mechanisms in SOC Verification - DVCon.pdf
SamHoney6
 
cupdf.com_chapter-11-system-level-verification-issues-the-importance-of-verif...
cupdf.com_chapter-11-system-level-verification-issues-the-importance-of-verif...cupdf.com_chapter-11-system-level-verification-issues-the-importance-of-verif...
cupdf.com_chapter-11-system-level-verification-issues-the-importance-of-verif...
SamHoney6
 
UVM-based RISC-V processor Verification Paltform ---.pdf
UVM-based RISC-V processor Verification Paltform ---.pdfUVM-based RISC-V processor Verification Paltform ---.pdf
UVM-based RISC-V processor Verification Paltform ---.pdf
SamHoney6
 
SVA Advanced Topics- SVAUnit and Assertions for Introduction to SystemVerilog...
SVA Advanced Topics- SVAUnit and Assertions for Introduction to SystemVerilog...SVA Advanced Topics- SVAUnit and Assertions for Introduction to SystemVerilog...
SVA Advanced Topics- SVAUnit and Assertions for Introduction to SystemVerilog...
SamHoney6
 

More from SamHoney6 (14)

eetop.cn_UVM_REG_workshop.--------------pdf
eetop.cn_UVM_REG_workshop.--------------pdfeetop.cn_UVM_REG_workshop.--------------pdf
eetop.cn_UVM_REG_workshop.--------------pdf
 
Cadence GenusTutorial------------ .pdf.pdf
Cadence GenusTutorial------------ .pdf.pdfCadence GenusTutorial------------ .pdf.pdf
Cadence GenusTutorial------------ .pdf.pdf
 
snug07_Verilog Gotchas for Verification.pdf
snug07_Verilog Gotchas for Verification.pdfsnug07_Verilog Gotchas for Verification.pdf
snug07_Verilog Gotchas for Verification.pdf
 
04+ECETEMT092-+WDT+APB+UVM.pdf
04+ECETEMT092-+WDT+APB+UVM.pdf04+ECETEMT092-+WDT+APB+UVM.pdf
04+ECETEMT092-+WDT+APB+UVM.pdf
 
14-Bill-Tiffany-SigmaSense-VF2023.pdf
14-Bill-Tiffany-SigmaSense-VF2023.pdf14-Bill-Tiffany-SigmaSense-VF2023.pdf
14-Bill-Tiffany-SigmaSense-VF2023.pdf
 
RF-Transceiver.pdf
RF-Transceiver.pdfRF-Transceiver.pdf
RF-Transceiver.pdf
 
FAC14_Vlach.pdf
FAC14_Vlach.pdfFAC14_Vlach.pdf
FAC14_Vlach.pdf
 
vip-shielding.pdf
vip-shielding.pdfvip-shielding.pdf
vip-shielding.pdf
 
UVM_TB_20220621_slides-1.pdf
UVM_TB_20220621_slides-1.pdfUVM_TB_20220621_slides-1.pdf
UVM_TB_20220621_slides-1.pdf
 
Sva.pdf
Sva.pdfSva.pdf
Sva.pdf
 
Web Template Mechanisms in SOC Verification - DVCon.pdf
Web Template Mechanisms in SOC Verification - DVCon.pdfWeb Template Mechanisms in SOC Verification - DVCon.pdf
Web Template Mechanisms in SOC Verification - DVCon.pdf
 
cupdf.com_chapter-11-system-level-verification-issues-the-importance-of-verif...
cupdf.com_chapter-11-system-level-verification-issues-the-importance-of-verif...cupdf.com_chapter-11-system-level-verification-issues-the-importance-of-verif...
cupdf.com_chapter-11-system-level-verification-issues-the-importance-of-verif...
 
UVM-based RISC-V processor Verification Paltform ---.pdf
UVM-based RISC-V processor Verification Paltform ---.pdfUVM-based RISC-V processor Verification Paltform ---.pdf
UVM-based RISC-V processor Verification Paltform ---.pdf
 
SVA Advanced Topics- SVAUnit and Assertions for Introduction to SystemVerilog...
SVA Advanced Topics- SVAUnit and Assertions for Introduction to SystemVerilog...SVA Advanced Topics- SVAUnit and Assertions for Introduction to SystemVerilog...
SVA Advanced Topics- SVAUnit and Assertions for Introduction to SystemVerilog...
 

Recently uploaded

call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
amitlee9823
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
instagramfab782445
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
amitlee9823
 
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
nirzagarg
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
amitlee9823
 
➥🔝 7737669865 🔝▻ dehradun Call-girls in Women Seeking Men 🔝dehradun🔝 Escor...
➥🔝 7737669865 🔝▻ dehradun Call-girls in Women Seeking Men  🔝dehradun🔝   Escor...➥🔝 7737669865 🔝▻ dehradun Call-girls in Women Seeking Men  🔝dehradun🔝   Escor...
➥🔝 7737669865 🔝▻ dehradun Call-girls in Women Seeking Men 🔝dehradun🔝 Escor...
amitlee9823
 

Recently uploaded (20)

call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️call girls in Dakshinpuri  (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
call girls in Dakshinpuri (DELHI) 🔝 >༒9953056974 🔝 genuine Escort Service 🔝✔️✔️
 
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
Pooja 9892124323, Call girls Services and Mumbai Escort Service Near Hotel Hy...
 
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
Escorts Service Nagavara ☎ 7737669865☎ Book Your One night Stand (Bangalore)
 
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
💫✅jodhpur 24×7 BEST GENUINE PERSON LOW PRICE CALL GIRL SERVICE FULL SATISFACT...
 
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
❤Personal Whatsapp Number 8617697112 Samba Call Girls 💦✅.
 
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
Abortion pill for sale in Muscat (+918761049707)) Get Cytotec Cash on deliver...
 
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
UI:UX Design and Empowerment Strategies for Underprivileged Transgender Indiv...
 
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
call girls in Kaushambi (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝...
 
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
8377087607, Door Step Call Girls In Kalkaji (Locanto) 24/7 Available
 
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verifiedSector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
Sector 104, Noida Call girls :8448380779 Model Escorts | 100% verified
 
Hingoli ❤CALL GIRL 8617370543 ❤CALL GIRLS IN Hingoli ESCORT SERVICE❤CALL GIRL
Hingoli ❤CALL GIRL 8617370543 ❤CALL GIRLS IN Hingoli ESCORT SERVICE❤CALL GIRLHingoli ❤CALL GIRL 8617370543 ❤CALL GIRLS IN Hingoli ESCORT SERVICE❤CALL GIRL
Hingoli ❤CALL GIRL 8617370543 ❤CALL GIRLS IN Hingoli ESCORT SERVICE❤CALL GIRL
 
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
RT Nagar Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bang...
 
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
Nisha Yadav Escorts Service Ernakulam ❣️ 7014168258 ❣️ High Cost Unlimited Ha...
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...Top Rated  Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
Top Rated Pune Call Girls Koregaon Park ⟟ 6297143586 ⟟ Call Me For Genuine S...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
Jigani Call Girls Service: 🍓 7737669865 🍓 High Profile Model Escorts | Bangal...
 
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...Booking open Available Pune Call Girls Nanded City  6297143586 Call Hot India...
Booking open Available Pune Call Girls Nanded City 6297143586 Call Hot India...
 
WhatsApp Chat: 📞 8617697112 Call Girl Baran is experienced
WhatsApp Chat: 📞 8617697112 Call Girl Baran is experiencedWhatsApp Chat: 📞 8617697112 Call Girl Baran is experienced
WhatsApp Chat: 📞 8617697112 Call Girl Baran is experienced
 
➥🔝 7737669865 🔝▻ dehradun Call-girls in Women Seeking Men 🔝dehradun🔝 Escor...
➥🔝 7737669865 🔝▻ dehradun Call-girls in Women Seeking Men  🔝dehradun🔝   Escor...➥🔝 7737669865 🔝▻ dehradun Call-girls in Women Seeking Men  🔝dehradun🔝   Escor...
➥🔝 7737669865 🔝▻ dehradun Call-girls in Women Seeking Men 🔝dehradun🔝 Escor...
 

inf5430_sv_randomization.pdf

  • 2. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Chapter 6: Randomization 2 •Directed testing: •Checks only anticipated bugs •Scales poorly as requirements change •Little upfront work •Linear progress •Random testing: •Checks unanticipated bugs •Scales well as requirements change •More upfront work •Better than linear progress Test Plan Completion 100% Time Directed Testing Random Testing
  • 3. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.1 Introduction 3 •A testbench based on randomization is a shotgun •The features you are trying to test is the target •How to cover untested areas? •More random testing with tighter constraints •Directed testing •When is testing done? •Functional coverage •Code coverage Features Features Features Features Features Shotgun Verification or The Homer Simpson Guide to Verification, Peet James
  • 4. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.2 What to randomize? 4 Much more than data 1. Device configuration 2. Environment configuration 3. Primary input data 4. Encapsulated input data 5. Protocol exceptions 6. Errors and violations 7. Delays 8. Test order 9. Seed for the random test
  • 5. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.3 Randomization in SystemVerilog 5 •Specified within a class along with constraints •Variable declared with rand keyword distributes values uniformly •Variable declared with randc keyword distributes values cyclically •No repeats within an iteration •Constraints specified with constraint keyword •New values selected when randomize() function called •Returns 1 if constraints can be solved, 0 otherwise 3, 2, 0, 0, 3, 1, ..... initial permutation 0,3,2,1 next permutation 0,3,2,1 next permutation 2,0,1,3 rand bit [1:0] y; randc bit [1:0] y; constraint y_c {y >=1;y<3;} <handle>.randomize();
  • 6. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.3.1 Simple class with Random Variables 6 class Packet; rand bit [31:0] src, dst, data[8]; randc bit [7:0] kind; constraint c {src > 10; src < 15;} endclass Packet p; initial begin p=new(); if (!p.randomize()) $finish; transmit(p); end Constraint expressions
  • 7. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.3.2 Checking the result from randomization 7 • Always check the result of a call to randomize() • Text uses a macro to check the results from randomization `define SV_RAND_CHECK(r) do begin if (!(r)) begin $display("%s:%0d: Randomization failed "%s"", `__FILE__, `__LINE__, `"r`"); $finish; end end while (0) # test.sv:13: Randomization failed “p.randomize()" `SV_RAND_CHECK(p.randomize());
  • 8. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.3.3 The constraint solver 8 •Solves constraint expressions •Same seed results in the same random values •Use different seeds in each nightly regression run. •Constraints may take a long time to solve •Solver is specific to each simulator vendor/release.
  • 9. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.3.4 What can be randomized? 9 •2-state variables •4-state variables except no X’s or Z’s will be created. •Integers •Bit vectors •Arrays •Time •Real, string •Handle in constraint
  • 10. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.2 Simple Expressions 10 •Each constraint expression should contain only 1 relational operator • <, <=, ==, >,=> • Constraint bad is broken down into multiple binary relational expressions from left to right. lo and med are randomized. • lo < med is evaluated, but not constrained. Results in 0 or 1. • hi > 0 or 1 constraint is then evaluated. • Not what you want! • Correct constraint: class Order_bad; rand bit [7:0] lo, med, hi; constraint bad {lo < med < hi;} endclass constraint good{lo < med; med < hi;} lo=20, med=224, hi=164 lo=114, med=39, hi=189 lo=186, med=148, hi=161 lo=214, med=223, hi=201
  • 11. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.3 Equivalence Expressions 11 •Suppose you want to constrain a value to be equal to an expression • len must be declared as random •Using = is a syntax error class order; rand bit [7:0] addr_mode, size, len; constraint order_c {len == addr_mode*4 + size;} endclass
  • 12. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.4 Weighted Distributions 12 •Weighted distributions cause a non-uniform distribution •Weights do not have to add up to 100% and can be variables •Cannot be used with randc •What would this be used for? • For a CPU want less or more of a particular opcode • For a datapath want max neg, 0, and max pos more often constraint <constraint name> {<variable name> dist {<distribution>}}; := operator indicates the weight is the same for all values :/ operator indicates the weight is distributed across all values constraint c_dist { src dist {0:=40, [1:3]:=60}; dst dist {0:/40, [1:3]:/60}; }
  • 13. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.4 Weighted Distributions := operator 13 := operator indicates the weight is the same for all values src = 0, weight = 40 src = 1, weight = 60 src = 2, weight = 60 src = 3, weight = 60 /220 /220 /220 /220 =18% =27% =27% =27% constraint src_dist { src dist {0:=40, [1:3]:=60} ;}
  • 14. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.4 Weighted Distributions :/ operator 14 :/ operator indicates the weight is distributed across all values dst = 0, weight = 40 dst = 1, weight = 20 dst = 2, weight = 20 dst = 3, weight = 20 /100 /100 /100 /100 =40% =20% =20% =20% constraint dst_dist {dst dist {0:/40, [1:3]:/60} ;}
  • 15. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.4 Weighted Distributions (cont.) 15 •Weights can be constants, ranges, or variables. •Using variables allows the weights to be adjusted on the fly class BusOp; typedef enum {BYTE, WORD, LWRD} length_e; rand length_e len; bit [31:0] w_byte=1, w_word=3, w_lwrd=5; constraint c_len { len dist {BYTE := w_byte, WORD := w_word, LWRD := w_lwrd}; } endclass
  • 16. constraint address_c {address > 2; address < 5;} constraint address_range{address inside{[3:4]};} Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.5 Set membership and the inside operator 16 •Alternative to {var>value1 ; var<value2} is the inside keyword •Using the ! operator can exclude ranges equivalent constraint c_range { !(c inside{[lo:hi]}); }
  • 17. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.6 Using an array in a set 17 •Suppose you want to create multiple equivalence constraints •For example: f can only equal 1, 2, 3, 5, 8 •Alternate solution is to store the values in an array •Can specify that values in the array are NOT to be chosen rand bit [7:0] f; constraint c_fibonacci { (f==1) || (f==2) || (f==3) || (f==5) || (f==8));} rand bit [7:0] f; bit [31:0] vals[]= ‘{1,2,3,5,8}; constraint c_fibonacci {f inside vals;} rand bit [7:0] notf; bit [31:0] vals[]= ‘{1,2,3,5,8}; constraint c_fibonacci {!(notf inside vals);}
  • 18. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.7 Bidirectional Constraints 18 •Constraint blocks are not procedural but declarative. •All constraints are active at the same time. Solution r s t A 6 6 7 B 6 6 8 C 6 6 9 D 7 7 8 E 7 7 9 F 8 8 9 rand bit [15:0] r,s,t; constraint c_bidir { r < t; s == r; t < 10; s > 5;}
  • 19. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.8 Implication Constraints 19 Suppose you want to impose different constraints depending on a var Solution 1: Solution 2: constraint mode_c { if (mode == small) len < 10; else if (mode == large) len > 100; } constraint mode_c { (mode == small) -> len < 10; (mode == large) -> len > 100; } equivalent equivalent equivalent {(a==1)->(b==0)}; {if (a==1) b==0;} {!(a==1) || (b==0);} {(a==0) || (b==0);} Solution a b A 0 0 B 0 1 C 1 0
  • 20. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.4.9 Equivalence operator 20 •The equivalence operator <-> is bidirectional. •A<->B is defined as ((A->B) && (B->A)) rand bit d, e; constraint c { d==1 <-> e==1; }
  • 21. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.5 Solution Probabilities 21 It’s important to understand how constraints affect the probability of the solution. Unconstrained: class Unconstrained; rand bit x; rand bit [1:0] y; endclass Solution x y Probability A 0 0 1/8 B 0 1 1/8 C 0 2 1/8 D 0 3 1/8 E 1 0 1/8 F 1 1 1/8 G 1 2 1/8 H 1 3 1/8
  • 22. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.5.2 Implication 22 class Imp1; rand bit x; rand bit [1:0] y; constraint c_xy { (x==0)->y==0; } endclass Solution x y Probability A 0 0 1/2 B 0 1 0 C 0 2 0 D 0 3 0 E 1 0 1/8 F 1 1 1/8 G 1 2 1/8 H 1 3 1/8
  • 23. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.5.3 Implication and bidirectional constraints 23 class Imp2; rand bit x; rand bit [1:0] y; constraint c_xy { y>0; (x==0)->y==0; } endclass Solution x y Probability A 0 0 0 B 0 1 0 C 0 2 0 D 0 3 0 E 1 0 0 F 1 1 1/3 G 1 2 1/3 H 1 3 1/3
  • 24. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.5.4 Guiding Distribution with solve/before 24 • Solve before tells the solver to solve for 1 variable before another. •The possible solutions does not change, just the probability. class SolveBefore; rand bit x; rand bit [1:0] y; constraint c_xy { (x==0)->y==0; solve x before y; } endclass Solution x y Probability A 0 0 1/2 B 0 1 0 C 0 2 0 D 0 3 0 E 1 0 1/8 F 1 1 1/8 G 1 2 1/8 H 1 3 1/8
  • 25. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 solve y before x; 25 class Imp1; rand bit x; rand bit [1:0] y; constraint c_xy { (x==0)->y==0; solve y before x; } endclass Solution x y Probability A 0 0 1/8 B 0 1 0 C 0 2 0 D 0 3 0 E 1 0 1/8 F 1 1 1/4 G 1 2 1/4 H 1 3 1/4
  • 26. class Packet rand bit [31:0] length; constraint c_short {length inside {[1:32]};} constraint c_long {length inside {[1000:1023]};} endclass Packet p: initial begin p=new(); p.c_short.constraint_mode(0); `SV_RAND_CHECK(p.randomize()); transmit(p); .... Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.6 Controlling Multiple Constraint Blocks 26 ... p.constraint_mode(0); p.c_short.constraint_mode(1); `SV_RAND_CHECK(p.randomize()); transmit(p); end // initial Use the constraint_mode() function to turn constraints on/off <handle>.constraint_mode(<0/1>); <handle>.<constraint>.constraint_mode(<0/1>);
  • 27. 6.7 Valid Constraints 27 •A suggested technique to creating valid stimulus is to create valid constraints •Turn the constraint off to test the system’s response to invalid stimulus. •For example, suppose a read-modify-write command is only valid if the length is a long word. class Transaction; typedef enum {BYTE, WORD, LWRD, QWRD} length_e; typedef enum {READ, WRITE, RMW, INTR} access_e; rand length_e length; rand access_e access; constraint valid_RMW_LWRD { (access == RMW) -> (length == LWRD); } endclass Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
  • 28. class Transaction; rand bit [31:0] addr, data; constraint c1 {addr inside{[0:100], [1000:2000]};} endclass intitial begin Transaction t; t=new(); `SV_RAND_CHECK(t.randomize() with {addr >=50; addr <=1500; data <10;}); driveBus(t); `SV_RAND_CHECK(t.randomize() with {addr ==2000; data >10;}); driveBus(t); end Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.8 In-line Constraints 28 •In-line constraints create constraints outside of the class. •Add to existing constraints if they are enabled. •For example, a single test needs to be written with tighter than usual address constraints
  • 29. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.9 pre_randomize/post_randomize 29 •Implicitly called before/after every call to randomize() •void function •Cannot consume time. •Can only call other functions. • Does not return a value •Overload to add your functionality •post_randomize() is good for cleaning up
  • 30. class Packet; rand bit [31:0] length; constraint c_length { length inside {[1:100]}; } endclass Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.11 Constraint Tips and Techniques 30 •Instead of hardcoding constraints use variables with defaults •Allows the constraint to be modified without modifying the class •Allows invalid stimulus to be generated class Packet; rand bit [31:0] length; int max_length= 100; constraint c_length { length inside {[1:max_length]}; } endclass initial begin Packet p1 = new(); p1.max_length = 200; p1.randomize(); end
  • 31. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.11.2 Using Nonrandom Values 31 class Packet; rand bit [7:0] length; constraint c_length{length > 0;} ..... // Other constraints depending on length endclass initial begin Packet p = new(); `SV_RAND_CHECK(p.randomize()); p.length.rand_mode(0); p.length = 42; (or: 0;) `SV_RAND_CHECK(p.randomize()); p.rand_mode(0); end Create an invalid length zero Value for length will be included in constraint solution •constraint_mode() turns on/off constraints •rand_mode() makes a variable or every variable in an object non- random Make length nonrandom
  • 32. class Transaction; rand bit [31:0] addr, data; constraint c1 {addr inside{[0:100], [1000:2000]};} endclass Transaction t; initial begin t=new(); `SV_RAND_CHECK(t.randomize()); t.addr = 200; `SV_RAND_CHECK(t.randomize(null)); end Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.11.3 Checking Values using Constraints 32 •If you change the value of random variables how do you know all your random variables are still valid? •Use a call to <handle>.randomize(null) to check. ...Randomization failed
  • 33. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.11.4 Randomizing Individual Variables 33 Can pass variables to randomize() to randomize only a subset of variables class Rising; bit [7:0] low; rand bit [7:0] med, hi; constraint up { low < med; med < hi; } endclass initial begin Rising r; r = new(); `SV_RAND_CHECK(r.randomize()); `SV_RAND_CHECK(r.randomize(med)); `SV_RAND_CHECK(r.randomize(low)); // Surprisingly!! `SV_RAND_CHECK(r.randomize(low, med)); end
  • 34. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.11.5 Turn Constraints Off and On 34 •Use many simple constraints instead of 1 complex constraint •Turn on the constraints needed class Instruction; typedef enum {NOP, HALT, CLR, NOT} opcode_e; rand opcode_e opcode; bit [1:0] n_operands; constraint c_operands{ if (n_operands == 0) (opcode == NOP) || (opcode == HALT); else if (n_operands == 1) (opcode == CLR) || (opcode == NOT); ..... } endclass
  • 35. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.11.5 Turn Constraints Off and On (cont.) 35 class Instruction; typedef enum {NOP, HALT, CLR, NOT} opcode_e; rand opcode_e opcode; constraint c_no_operands{ (opcode == NOP) || (opcode == HALT);} constraint c_one_operand{ (opcode == CLR) || (opcode == NOT);} ..... } endclass initial begin Instruction instr = new(); instr.constraint_mode(0); instr.c_no_operands.constraint_mode(1); `SV_RAND_CHECK(instr.randomize()); end
  • 36. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.12 Common Randomization Problems 36 •Using a signed variable isn’t an issue if you control the values •However, a randomized signed variable will produce negative values •Some valid solutions of {pkt1_len, pkt2_len} are: for (int i=0;i<=5;i++) class SignedVars; rand byte pkt1_len, pk2_len; constraint total_len {pkt1_len + pk2_len == 64;} endclass (32,32) (2,62) (-63, 127)
  • 37. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 6.12.1 Use Signed Values with care 37 •Might be temped to declare pkt1_len, pk2_len as large unsigned •A valid solution of {pkt1_len, pkt2_len} is •One solution is to constrain the max values of pkt1_len and pk2_len •Best solution is to only use values as wide as required class Vars32; rand bit [31:0] pkt1_len, pk2_len; constraint total_len {pkt1_len + pk2_len == 64;} endclass class Vars8; rand bit [7:0] pkt1_len, pkt2_len; constraint total_len {pkt1_len + pkt2_len == 9’d64;} endclass (32’h80000040, 32’h80000000) = 32’h40=32’d64
  • 38. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 1 38 Write the SystemVerilog code for the following items: 1) Create a class Exercise1containing two variables, 8-bit data and 4-bit address. Create a constraint block that keeps addressto 3 or 4. 2) In an initialblock, construct an Exercise1object and randomize it. Check the status from randomization.
  • 39. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 1 solution 39 Write the SystemVerilog code for the following items: 1) Create a class Exercise1containing two variables, 8-bit dataand 4-bit address. Create a constraint block that keeps addressto 3 or 4. 2) In an initialblock, construct an Exercise1object and randomize it. Check the status from randomization. class Exercise1; rand bit [7:0] data; rand bit [3:0] address; constraint address_c { address > 2; address < 5; } endclass initial begin Exercise1 MyExercise1; MyExercise1 = new; `SV_RAND_CHECK(MyExercise1.randomize()); end // or // ((address==3) || (address==4)); // or // address inside {[3:4]};
  • 40. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 2 40 Modify the solution for Exercise1 to create a new class Exercise2 so that: 1. data is always equal to 5 2. Probability of address = 4’d0 is 10% 3. Probability of address being between [1:14] is 80% 4. Probability of address = 4’d15 is 10% Demonstrate its usage by generating 20 new data and address values and check for error.
  • 41. Constraint Exercise 2 solution 41 Modify the solution for Exercise1 to create a new class Exercise2 so that: 1. datais always equal to 5 2. Probability of address = 4’d0 is 10% 3. Probability of addressbeing between [1:14] is 80% 4. Probability of address = 4’d15 is 10% package my_package; class Exercise2; rand bit [7:0] data; rand bit [3:0] address; constraint data_c {data == 5;} constraint address_dist { address dist {0:=10, [1:14]:/80, 15:=10}; } function void print_all; $display("data = %d, address = %d", data, address); endfunction endclass endpackage The := operator when the weight is the same for every specified value in the range. The :/ operator when the weight is to be equaly divided between all values. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4
  • 42. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 2 solution cont. 42 Demonstrate its usage by generating 20 new dataand addressvalues and check for error. program automatic test; import my_package::*; initial begin Exercise2 MyExercise2; repeat (20) begin MyExercise2 = new; `SV_RAND_CHECK(MyExercise2.randomize()); MyExercise2.print_all(); end end endprogram
  • 43. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 3 43 class Stim; const bit [31:0] CONGEST_ADDR = 42; typedef enum {READ, WRITE, CONTROL} stim_e; randc stim_e kind; rand bit [31:0] len, src, dst; bit congestion_test; constraint c_stim { len < 1000; len > 0; if (congestion_test) { dst inside {[CONGEST_ADDR-10:CONGEST_ADDR+10]}; src == CONGEST_ADDR; } else src inside {0, [2:10], [100:107]}; } endclass What are the constraints on len, dst, and src for this code?
  • 44. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 3 solution 44 class Stim; const bit [31:0] CONGEST_ADDR = 42; typedef enum {READ, WRITE, CONTROL} stim_e; randc stim_e kind; rand bit [31:0] len, src, dst; bit congestion_test; constraint c_stim { len < 1000; len > 0; if (congestion_test) { dst inside {[CONGEST_ADDR-10:CONGEST_ADDR+10]}; src == CONGEST_ADDR; } else src inside {0, [2:10], [100:107]}; } endclass What are the constraints on len, dst, and src for this code? • len must be between 1 and 999 inclusive • if bit congestion_test is 1 dst must be inside 42-10 =32 to 42+10 (52) and src = 42 • else src can take on values 0, 2 to 10, and 100 to 107. dst is unconstrained.
  • 45. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 4 45 For the following class create: 1. A constraint that limits read transactions addresses to the range 0 to 7, inclusive 2. Write behavioral code to turn off the above constraint. Construct and randomize a MemTrans object with an in-line constraint that limits read transaction addresses to the range 0 to 8, inclusive. Test that the in-line constraint is working. class MemTrans; rand bit rw; // read if rw = 0, write if rw = 1 rand bit [7:0] data_in; rand bit [3:0] address; endclass
  • 46. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 4 solution 46 A constraint that limits read transactions addresses to the range 0 to 7, inclusive: or class MemTrans; rand bit rw; // read if rw = 0, write if rw = 1 rand bit [7:0] data_in; rand bit [3:0] address; constraint valid_rw_addr { (rw == 0)->(address inside {[0:7]}); } endclass // MemTrans class MemTrans; rand bit rw; // read if rw = 0, write if rw = 1 rand bit [7:0] data_in; rand bit [3:0] address; constraint valid_rw_addr { if (!rw) address inside {[0:7]}; } endclass // MemTrans
  • 47. Chapter 6 Copyright 2011 G. Tumbush, C. Spear v1.4 Constraint Exercise 4 solution cont. 47 Write behavioral code to turn off the above constraint. Construct and randomize a MemTrans object with an in-line constraint that limits read transaction addresses to the range 0 to 8, inclusive. Test that the in-line constraint is working. MemTrans MyMemTrans; initial begin MyMemTrans = new(); MyMemTrans.valid_rw_address.constraint_mode(0); `SV_RAND_CHECK(MyMemTrans.randomize() with {(rw == 0)->(address inside {[0:8]});}); end