Sie sind auf Seite 1von 24

System Verilog Basics

March 02, 2010


SV : The Language

 Unified HDVL
Superset of verilog (IEEE 1394 - 2001).
Many of the desirable features of HDL and
modern S/W languages.
1) OOPS semantics including classes
2) constraint randomization, sequence generation
3) Functional Coverage
4) Assertions

Direct Interface to foreign programming


languages. (using DPI)
What’s New….

 Verification Features
1) New Data types.
2) OOPS (as against AOP in ‘e’).
3) Arrays and Queues.
4) Interfaces and Modports.
5) Clocking and Program Blocks.
6) Constraint Random Stimulus Generation.
7) Intelligent Automated Self Checking.
 Fully compatible with verilog 1364 – 2001.
Adds new identifiers.
SV Over Verilog (Major)

 Extension to data types.


 Extended Operators.
 Extended Procedural statements.
 Enhanced Process Control.
 Enhanced Tasks and Functions.
 Classes
 Random Constraints
 Inter process Communication.
 Cycle based functionality.
 Assertion Mechanism.
 Functional Coverage.
Data Types (Integer)

Shortint 2 state SV data type, 16 bit signed integer.


Int 2 state SV data type, 32 bit signed integer.
Longint 2 state SV data type, 64 bit signed integer.
Byte 2 state SV data type, 8 bit signed integer or ASCII
char.
Bit 2 state, user defined vector type.
Logic 4 state, user defined vector type.
Reg 4 state verilog 2001 data type, user defined type.
Integer 4 state verilog 2001 data type, 32 bit signed integer.
Time 4 state verilog 2001 data type, 64 bit unsigned
integer.
Data Types : Others

 Void data type :- Represents non-existent data. Specified


as return type of functions.
 Real and Shortreal :- Similar to C double and C float.
 Chandle :- storage for pointer used during DPI.
 String :- dynamically allocated array of bytes.
 Event :- handle to synchronization object.
 User Defined types :- using typedef.
 Enumerations :- declares a set of integral named
constants.
 Structures & Unions
 Classes :- collection of data and subroutines.
String : Data type explained

 Syntax string variable_name [= initial_value];


 Examples:
string team = “ipdev”;
byte b = “B”;
 Implementation Scenarios: (from LRM)
string b = “”;
string a = {“Hi”, b} ; //concatenation
b = {5{“Hi”}}; //replication
b = {I {“Hi”}}; // non constant replication; I is
integer.
a = {a, b};
 Operations:
== , !=, <, >, <=, >= (lexicographical), str[index],
str.method()
String : Methods
len() Returns number of characters in string - zero for empty string
putc() str.putc(i, c) replaces the ith character in str with the given integral
value.
getc() str.getc(i) returns the ASCII code of the ith character in str
toupper() str.toupper() returns a string with characters in str converted to
uppercase
tolower() str.lower() returns a string with characters in str converted to lowercase
compare() str.compare(s) compares str and s, as in the ANSI C strcmp function
Icompare() Case insensitive comparison
Substr() str.substr(i, j) returns a new string that is a substring formed by
characters in position I through j of str.
Atoi str.atoi() returns the integer corresponding to the ASCII decimal
Atohex representation in str. For example:
Atooct str = "123";
Atobin int i = str.atoi(); // assigns 123 to i.
Atoreal str.atoreal() returns the real number corresponding to the ASCII decimal
representation in str.
User Defined Type : Explained

 Syntax typedef <data_type> <type_identifier>


 Examples:
typedef int intP;
intP var1, var2;

typedef can be used before it is defined.


typedef my_var;
my_var = 1;
typedef int my_var;
 Typedef can also be used with enum, class, struct,
union.
Enumeration : Explained

 An enumerated type declares a set of integral


named constants.
 Enumerated data types also can be easily
referenced or displayed using the enumerated
names as opposed to the enumerated values.
 In the absence of a data type declaration, the
default data type shall be int.
 Any other data type used with enumerated types
shall require an explicit data type declaration.
 Examples:
Enumeration : Explained

enum {red, yellow, green} light1,


light2; // anonymous default int type
enum {IDLE, XX=’x, S1=2’b01, S2=2’b10} state,
next; // ERROR int can not hold X
enum integer {IDLE, XX=’x, S1=’b01, S2=’b10} state,
next; // OK integer can hold X
enum integer {IDLE, XX=’x, S1, S2} state,
next; // ERROR S1 and S2 are unassigned
enum {bronze=3, silver, gold}
medal; // Silver=4 and Gold = 5
enum {a=3, b=7, c}
alphabet; // c = 8 – increments
after b
enum {a=0, b=7, c, d=8}
alphabet; // ERROR C and D are 8
Enumeration : Methods
first() The first() method returns the value of the first member of the
enumeration
last() The last() method returns the value of the last member of the
enumeration
next (int n=1) The next() method returns the Nth next enumeration value
(default is the next one) starting from the current value of the
given variable. A wrap to the start of the enumeration occurs when
the end of the enumeration is reached. If the given value is not a
member of the enumeration, the next() method returns the default
initial value for the enumeration.
Prev (int n=1) The prev() method returns the Nth previous enumeration value
(default is the previous one) starting from the current value of the
given variable. A wrap to the end of the enumeration occurs when
the start of the enumeration is reached. If the given value is not a
member of the enumeration, the prev() method returns the default
initial value for the enumeration.
Num() The num() method returns the number of elements in the given
enumeration
Name() The name() method returns the string representation of the given
enumeration value. If the given value is not a member of the
enumeration, the name() method returns the empty string
Structures & Unions : Explained

 Syntax struct/union {struct_members} struct_name;


 Examples:
struct { int i; bit [22:0] addr;} instr;
struct packed {bit [11:0] var1; bit [9:0]var2;} decode;
// First member is the most significant
struct packed signed {integer b, logic [31:0]c} data;
//default is unsigned.
 If any data type within a packed structure is 4 state, every
member is converted 4 state (automatic type casting).
 Cannot use real and shortreal types inside packed structure.
 Tagged Unions : union tagged { bit [7:0]b, int i} mine;
Casting

 A data type can be changed using a cast (‘) operation.


 The expression to be cast should be in parentheses or concatenation
braces.
 Examples:
int ‘(2.0*3.0);
shortint ‘{8’h FA, 8’h AB};
 $cast for dynamic casting
 Syntax $cast (dest, src);
dest = variable to which assignment is made.
src = expression that is to be assigned to dest.
Arrays

 Packed Arrays:
1) Dimensions declared left side of object name
2) Contiguous set of bits
 Examples :
bit [7:0] data;
bit [21:0] address;

 Unpacked Arrays:
1) Dimensions declared right side of object name.
2) size can be modified at run time.
3) Fixed, Dynamic and Associative
Fixed Array

 Static
 Supports multiple dimensions
 Out of bound writes are ignored – No compilation error
 Out of bound reads give ‘x’ – even for 2 state
 Examples:
int data [256];
bit [31:0] addr [1024];
bit [31:0][3:0] d [8];
Dynamic Arrays

 An unpacked array whose size can be set or changed at run


time.
 The space for a dynamic array does not exist until the array
is explicitly created at run time.
 The new[] operator is used to set or change the size of the
array.
 The size() built-in method returns the current size of the
array.
 The delete() built-in method clears all the elements yielding
an empty array (zero size) (name.delete())
 Example:
integer addr []; //declaration
addr = new[100];// creation
addr = new [100](addr); // create and preserve
Queues

 Queues – type queue_name [$]


 Variable size, ordered collection of homogeneous element

 Each element is identified with index – it’s represents

position in queue
 Zero represents the first and $ represents last

 A queue is analogous to a one-dimensional unpacked array

that grows and shrinks automatically.


 Queues can be manipulated using the indexing,

concatenation, slicing operator syntax, and equality


operators.
 Examples:

int p[$];
int q[$] = {2,3,4};
q = {q,6}; q = {q[1:$]}; q = {q[0:pos-1], 3, q [pos:$]};
Queue : Methods()
size() The size() method returns the number of items in the
queue. If the queue is empty, it returns 0.
Insert() The insert() method inserts the given item at the specified
index position.
Q.insert(i, e) is equivalent to: Q = {Q[0:i-1], e, Q[i,$]}
delete() function void delete(int index);
The delete() method deletes the item at the specified index
position.
pop_front() The pop_front() method removes and returns the first
element of the queue. e = Q.pop_front() is equivalent to: e
= Q[0]; Q = Q[1,$]
Pop_back() The pop_back() method removes and returns the last
element of the queue. e = Q.pop_back() is equivalent to: e
= Q[$]; Q = Q[0,$-1]
push_back() The push_back() method inserts the given element at the
end of the queue. Q.push_back(e) is equivalent to: Q = {Q,
e}
Events

 Declare Events
event my_event;
 Trigger Events
-> my_event;
 Wait for Events – triggered is built in.
@ my_event or wait (my_event.triggered)
 Events can be passed as argument.
Refreshers/Experiments

 Value of str?
string str = “hello”;
bit [11:0] b = 12’h5a4;
str = b;

 Output of the snippet


string s1 = “Read”;
string s2 = “Write”;
if(s1 > s2) $display (“Read Operation”);
else {$display (“Write Operation”);
Cont’d

 Queue operation
Predict the outputs at successive steps
int j = 1,b[$] = {3,4},q[$] = {0,2,5};
initial begin
q.insert(1,j);
q.insert(3,b);
q.delete(1);
q.push_front(6);
j =q.pop_back;
q.push_back(8);
j = q.pop_front;
end
Cont’d

 Arrays, using word and bit subscripts


initial begin
bit [31:0] src [5] = ‘{5{5}};
$displayb(src[0],
src[0][0],
src[0][2:1]);
end
predict result of $displayb?
Cont’d

 Array and Queue methods


int f[6] = ‘{1,6,8,6,2,6};
int q[$] = ‘{1,3,5,7}, tq[$];

tq = q.min;
tq = q.max;
tq = f.unique;

Determine value of tq at each step.


What is the return type of min, max, unique methods.

Das könnte Ihnen auch gefallen