Cheatsheets - AMIQ Consulting - SV
Cheatsheets - AMIQ Consulting - SV
SVA Syntax
type1 field_name1;
Sequences
....
Properties Declares a user-defined enumerated type having a set of explicitly named values. Syntax:
} <struct_name>;
Data Types data_type If not specified data type defaults to int. struct packed
{
type1 field_name1;
Example:
Integer data types
....
Type Description
Members of a packed structure can be accessed either by their name, or by
bit 2-state data type, user-defined vector size representing unsigned data item[0] value If not specified the first item in an enumerated list will be represented as 0 indexing a vector. By default the first field will be the leftmost item in the vector:
byte 8 bit 2-state integer representing signed data or ASCII character item[n] value If not specified the n-th item in an enumerated list will be represented as item[n-1] Example:
+1
shortint 16 bit 2-state integer representing signed data struct packed
int addr;
longint 64 bit 2-state integer representing signed data Method Description bit[7:0] data;
logic 4-state data type, user-defined vector size, unsigned data first() Returns the first value of an enumeration }my_struct;
reg 4-state data type, user-defined vector size, unsigned data last() Returns the last value of an enumeration In this case my_struct[0] is my_struct.data[0]
wire Used to connect different elements in a design. They can be read or assigned, but next(N) Return the N-th next value, wrapping to the beginning if needed. Default N = 1.
no values can be stored in them.
prev(N) Return the N-th previous value, wrapping to the end if needed. Default N = 1.
integer 32 bit 4-state integer representing signed data
num() Returns the number of values in the enum
time 64 bit 4-state integer representing signed data
Dynamic array
Struct data types
Real data types
Type Description A dynamic array is an unpacked array whose size can be set or changed at runtime. Syntax:
Type Description
type array_name[];
array_name = new[expression1](expression2);
expression2 is an optional initialization of the array Method Description If the assignment is invalid, a task will generate a runtime error, while the function will return 0.
state current_state;
new[expression] Constructor that sets the size of the dynamic array and initializes its elements delete(i) Deletes item at index i, if index is omitted, deletes queue
if(!$cast(current_state,6)
pop_front() Reads and removes the item from the front of the queue $display(“Error in cast”);
Example:
pop_back() Reads and removes the item from the back of the queue
int my_array[];
class my_class;
evious values
Casting local/static/protected items
Methods/method prototypes
endclass
size() Returns the current size or 0 if the array is not created or if it was deleted.
Example:
Static Cast Class Instance Syntax:
In a static cast, if the expression is assignment compatible with the casting type, then the cast shall return my_class obj; // declare an object of class my_class
int array_size;
the value of a variable of the casting type. Syntax:
Obj = new(); // initialize variable to a new allocated object of class my_class
array_size = my_array.size();
casting_type‘(expression)
Example 1:
delete() Empties the array, resulting in a zero-sized array Relevant keywords
shortint a;
Example: int b;
Keyword Description
b = int’(a);
my_array.delete();
static A static class field shares its value with all instances of the class
Example 2: A static method can only access static properties, but it can be called even when
no class instances exist by using class name and the scope resolution operator.
logic[7:0] register;
Example:
Queue data type register = signed’(4’b1100) // register = -4
Example 3:
static int number_of_instances = 0;
shortint a;
function new();
++number_of_instances;
b = a; // implicit cast
endfunction
Example:
return number_of_instances;
Dynamic Cast
int q_int[$];
endfunction
Used to assign values to variables that might not ordinarily be valid because of differing data types. Syntax: endclass
instances_count = my_class::get_count();
Keyword Description
Keyword Description
this The this keyword gives unambiguous access to members of the current class Encapsulation extends It is used to create a new class that inherits the members of a base class.
instance
Keyword Description
class base_calculator;
Example: local Used to specify that class members are only visible within the class where they int result;
class my_class;
result = a + b;
int x;
endfunction
endclass
class my_class;
//constructor;
endfunction
function void mul(int a, int b)
result = a * b;
private = x;
endclass
endfunction
endfunction
endclass
endclass
super The super keyword gives unambiguous access to members of the parent class of my_class instance = new();
instance.private = 1; // ERROR
instance.set_private(1); // OK
Example:
...
Polymorphism
end
class parent;
Keyword Description
//constructor
protected Used to specify that class members are only visible within the class where they virtual Virtual (class method) is used to define basic polymorphic constructs. A virtual
endclass
were declared and any subclasses. method is used to override a method in all of its base classes.
class child extends parent;
//constructor
endfunction
endclass
endclass
class my_extended_class extends my_class
child c = new();
//constructor;
function void set_private(int x);
initial begin
private = x;
$display(c.get_parent_name); // parent
endclass
...
end
my_extended_class instance = new();
initial begin
virtual A virtual class is an abstract class which can only be inherited, but not instantiated. instance.set_private(1); // OK
...
const Class properties can be made read-only by using a const declaration. end
Example:
class my_class;
Keyword Description class my_class;
int x,y;
class base_class;
Interface class function new();
int A = 1;
// simple constructor
int B = 2;
The interface class is a class that can be viewed as having a common set of behaviours. An interface class
endfunction
endfunction
this.y = obj.y;
endclass
// pure virtual methods
// parameters
Operators
class extended_class extends base_class;
// type declarations
int A = 3;
endclass
int B = 4;
Example:
endfunction
= Binary assignment operator
virtual function void printB();
endfunction
pure virtual function void set(SET_TYPE a);
endclass
%= Binary arithmetic modulus assignment operator
endclass
endclass
>>= <<= Binary logical shift assignment operator
initial begin
base = child;
& | ^ Binary bitwise operators
end
class fifo (type T = logic, DEPTH = 1)
~ & ~&
Unary logical reduction operator
| ~|
Class implementation can be customized by using class parameters. Example: q.pop_front();
endfunction
Example:
endclass ^ ~^
class vector#(int size = 1)
&y will return the result of an AND operation between all the bits of y
bit [size -1 : 0] a;
endclass
&& || Binary logical operations
class stack#(type T = int)
Copy-Constructor < > <= >= Binary relational operation
local T items[];
...
The copy constructor is a constructor which creates an object by initializing it with an object of the same
endclass == != Binary logical equality operators
class, which has been created previously. Example:
===
Binary case equality operators.
Instantiation:
!==
vector#(10) a = new();
Unlike logical equality operator, this operators can identify x and z.
stack#(real) b = new();
Symbol Description Verification Features Function Description
++ -- Unary increment/decrement operation randomize(class Built-in method used to randomize all properties declared with rand and randc.
method) The properties that are not declared rand or randc can still be randomized if they
Randomizations are passed as arguments.
rand Used in class declaration to specify that a class property is a random variable with
package my_pacakge;
class my_class;
an uniform distribution
//classes
rand int random_value1, random_value2;
int value;
//variables
//functions
enclass
//structs
//etc..
randc Used in class declaration to specify that a class property is a random variable with obj.randomize(value) // will randomize value as declared as a rand pr
an cyclic distribution operty
Wildcard import - Imports all symbols within the package
import my_package::*;
randc bit [1:0] x; pre_randomize() Built-in method that is called by the randomize() method before the objects are
randomized.
Explicit import - Imports only the mentioned symbol from the package
X can take on values in rage of 0 to 3. Randomize will compute an initial random
permutation of the range values and return them in order on successive calls.
import my_package::my_symbol; class my_class
Processes The values will be generated in the following order 0,2,1,3,2,3,0,1. function void pre_randomization()
endfunction
endclass
Function Description
fork
begin
std::randomize() It is a built-in function used to randomize local variables or class properties post_randomize() Built-in method that is called by the randomize() method after the objects are
//Task 1
randomized.
end
begin
rand int random_value;
//Task 2
if(randomize(random_value))
class my_class;
end
$display(“Successful randomization”); rand bit[27:0] data;
begin
bit[3:0] crc;
//Task n
function void post_randomization()
otherwise.
join_keyword endfunction
endclass
join_keyword Description
Constraint
join_any The fork completes when the first task is completed
rand int random_value;
constraint random_value_c{
Syntax:
class my_class;
random_value >= 0;
}
constraint random_value_c{
class my_class;
0:=20,
constraint my_constraint
}
class latch;
{
endclass
rand bit set,reset;
soft my_int < 100;
constraint random_value_c{
}
// ”->” will be explained in the next section rand_int.randomize() with { my_int >1000; } Syntax:
Keyword Description
Example with solve...before...: if(randomize(random_value) with {random_value < 50;})
$display(“Successful randomization”);
inside Comparison operator which is true if an expression is contained within a specific
class latch;
list
rand bit set,reset;
constraint random_value_c{
}
constraint random_value_c{
endclass -> Implication operator used for conditional constraints. It is similar to the if construct.
random_value inside {[0:15]};
}
In this case set is solved first, so (set,reset) = (1,0) has a 50% chance of occurring, endclass Syntax:
instead of 33%.
Similar to:
if(boolean_statement)
constraint_item
Identifies a variable that will be tracked for coverage
Operators and Description Type Description
methods
Set Syntax:
constraint_mode() Used to enable and disable class constraints. Syntax:
task
coverpoint variable [iff (boolead_expresion)]
range_list1 => range_list2
{ /* bins */ }
// This is equivalent to : (1 => 3), (1 => 4), (2 => 3), (2 => 4)
//[enables|disable] all class constraints
If no bins are defined , the coverpoint creates an automatic bin for every value of the variable.
object.constraint_mode([1|0])
Consecutive Syntax:
objects.constraint_name.constraint_mode([1|0]) covergroup my_cg with function sample(bit[31:0] addr)
Syntax: endgroup
Range of Syntax:
objects.constraint_name.constraint_mode() repetitions [*n:m]
transition_item[* low_limit:high_limit]
Cross Example:
Functional Coverage Correlates bins from two or more coverpoints so that an inter-variable value relationship can be explored.
1[*2:3] is equivalent to (1 => 1), (1 => 1 => 1)
Syntax: Goto repetition[- Syntax:
>n]
Covergroups cross expression1 , expression2, … expression n [iff (boolean expression)];
transfer_item[->repeat_range]
Defines probes that sample relevant information from functional coverage and defines how coverage // or
Examples:
cross expression1 , expression2, … expression n [iff (boolean expression)]
coverpoint signal1;
Where … is any transition that does not contain the value 1
//coverpoints
//cross
coverpoint signal2;
name_cg = new();
Type Description
name_cg.set_inst_name(“my_name”);
Sequence A transition from valueA to valueB is defined as such:
Coverpoints
transfer_item[=repeat_range] assertions
delay = 0
//fail block
Notice that the transition following the nonconsecutive repetition may occur after Concurrent Are SVA directives used to verify that a property holds.
any number of transition as long as the value 1 doesn’t occur again. assertions Syntax:
delay = 2
property my_property;
endproperty
Cover Is SVA directive used to verify that a property occurs during simulation.
range [*n:m]
tracked for each bins.
Syntax:
signal_1[*1:2] ##1 signal_2
bins name = {range};
[label:] cover property (property_name);
property my_property;
cover property (my_property);
illegal_bins Identifies that a certain range of values are illegal. The length of signal1 is 2 clock cycle
SVA Syntax
ignore_bins Identifies that a certain range of values are excluded from coverage.
default Default bins are used for all values not covered in other bin ranges.
{
Temporal delay
signal_1 ##1 signal_2
bins null_addr = {0};
## with integer
bins valid_addr = {[10:20]};
}
Sequence Type Description Operator Description
Non-consecutive Example 1: Overlapping Syntax:
repetition [=n], sequence
Function Description
[=n:m] implication
signal_1[=2] operator |-> sequence_1 |-> sequence_2 $onehot(signal) Returns true if only one bit of the signal HIGH
This is a shortcut for the following sequence sequence_2 will start in the same clock cycle in which sequence_1 will end $onehot0(signal) Returns true if only one bit of the signal is LOW
Example:
!start[*0:$] ##1 start ##1 !start[*0:$] ##1 start ##1 !start[*0:$] $isunknown(signal) Returns true if at most one bit of the signal is X or Z
signal_1[=2] ##1 signal_2 $fell(signal) Returns true if the signal has changed value to 0 in the current evaluation cycle
$stable(signal) Returns true if the signal has the same value as it had in the previous evaluation
cycle
#past(signal, Returns the value of the signal at a previous evaluation cycle specified through
number_of_cc) the number_of_cc argument
Non-overlapping Syntax:
Goto non- Example 1: sequence
consecutive implication
repetition [->n], [- operator |=> sequence_1 |=> sequence_2 Design and Verification Blocks
>n:m] signal_1[->2]
sequence_2 will start one clock cycle after sequence_1 has ended
The difference between the two non-consecutive repetition is that the pattern
matching is finished after the last active pulse. This is a shortcut for the following
sequence:
!start[*0:$] ##1 start ##1 !start[*0:$] ##1 start ##1 Block Description
Observe that signal_1 is matched before returning to the LOW state. Module Syntax:
Example 2:
Example:
module my_module (input input_signal1,
//module logic
Observation!
endmodule
wire output;
.input_signal2(input2)
Properties
Operator Description
Block Description
Interface Syntax:
//<parameters>
//<modports>
endinterface
input rst);
modport transmit (
output output_signal1);
modport receive (
input output_signal1);
endinterface
Module declaration:
Virtual A virtual interface is a variable that represents an interface instance, providing a mechanism
Interface of separating abstract models from the actual RTL implementation.
Syntax:
endinterface
class my_class;
//...
endclass