System Verilog Introduction, Data Types
System Verilog Introduction, Data Types
System Verilog Introduction, Data Types
INTRODUCTION, DATA
TYPES
What is SV?
Constrained
Randomization
OOPS Improved
Data Types
System Verilog
Functional
Synchronization
Coverage
Assertions
Futurewiz
www.futurewiz.co.in
Regions in SV
Futurewiz
www.futurewiz.co.in
Data Type
Futurewiz
www.futurewiz.co.in
4-State Type
Futurewiz
www.futurewiz.co.in
4-State Type
Example4:
module example1;
int a;
int unsigned b; //unsigned integer
bit signed [7:0] c; //same as byte
initial
begin
a=-32’d127;
b=‘1; //SV offers un-sized literal to fill all
c=‘0; // locations with given number
end
endmodule
Futurewiz
www.futurewiz.co.in
Example2
module example2;
int a;
logic [31:0] b=‘Z; //b=32’hzzzz_zzzz
initial
begin
a=b; //a=32’h0000_0000
b=32’h123x_5678;
if($isunknown(b)) $display(“b is unknown”);
else $display(“b is known”);
end
endmodule
Futurewiz
www.futurewiz.co.in
Real Type
Example: Usage:
Futurewiz
www.futurewiz.co.in
Arrays
Futurewiz
www.futurewiz.co.in
Fixed Array
Example:
int array1 [16] [8]; //16 rows , 8 columns
bit array2 [3:0] [7:0]; //4 rows , 8 columns
bit [7:0] array3 [4]; //4 rows each containing 8 bits
Futurewiz
www.futurewiz.co.in
Unpacked Array
Unused
array1 [0] 7 6 5 4 3 2 1 0
Memory
array1 [1] Unused 7 6 5 4 3 2 1 0
Memory
array1 [2] Unused 7 6 5 4 3 2 1 0
Memory
array1 [3] Unused 7 6 5 4 3 2 1 0
Memory
Futurewiz
www.futurewiz.co.in
Unpacked Array
Initializing Array:
int array1 [2] [4] = ‘{ ‘{ 1, 2, 3, 4 } , ‘{ 5, 6, 7, 8 } };
int array5 [2] [2] [2] = ‘{ ‘{ ‘{4, 5}, ‘{3, 1} }, ‘{ ‘{1, 7}, ‘{2, 5} }
};
Futurewiz
www.futurewiz.co.in
Unpacked Array
Accessing Array
int array1 [2] [4];
int array2 [0:5];
byte array3 [0:2] [1:4];
int a, b;
byte c;
a= array1[1] [3];
b= array2[4];
c= array3[1] [2];
Futurewiz
www.futurewiz.co.in
Basic Array Operation
initial
begin
for ( int i=0; i <$size(array1); i++) //$size returns size of
array
array1[ i ]= 0;
foreach(array2[ k ]) //k is defined implicitly
array2[ k ]=$random;
end
Futurewiz
www.futurewiz.co.in
Basic Array Operation
Example:
bit [7:0] array1[10] [20];
initial
begin
array1=‘{10 { ‘{0:2, 1 : 0 , default:$random} } };
foreach(array1[i ,k])
$display(“array1[%0d] [%0d]=%0d”, i, k, array1[i] [k]);
end
Futurewiz
www.futurewiz.co.in
Packed Array
7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
b[0] 76543210765432107654321076543210
b[0] [2] b[0] [1] [4:0]
b[1] 76543210765432107654321076543210
b[1]
b[2] 76543210765432107654321076543210
b[2] [2] [2]
b[3] 76543210765432107654321076543210
b[3] [0]
Futurewiz
www.futurewiz.co.in
Packed vs. Unpacked Array
Futurewiz
www.futurewiz.co.in
Dynamic Array
Futurewiz
www.futurewiz.co.in
Dynamic Array
module top;
int dyn1 [ ]; //Defining Dynamic Array (empty subscript)
int dyn2 [4] [ ];
initial
begin
dyn1=new[10]; //Allocate 10 elements
foreach (dyn1[ i ]) dyn1[ i ]=$random; // Initializing Array
dyn1=new[20] (dyn1); // Resizing array and
// Copying older values
dyn1=new[50]; // Resizing to 50 elements Old Values are lost
dyn1.delete; // Delete all elements
end
endmodule
Futurewiz
www.futurewiz.co.in
Dynamic Array
initial
begin
repeat (2)
if (dyn1.size != 0)
begin
foreach(dyn1 [ i ] ) $display(“dyn1[%0d]=%0d”, i, dyn[ i ] );
dyn1.delete;
end
else
$display(“Array is empty”);
end
Futurewiz
www.futurewiz.co.in
Queue
int A [$] = ‘{ 0, 1, 2, 3, 4, 5, 6 }; A 0 1 2 3 4 5 6
int x, y, z;
int array1 [ * ];
int array2 [ int ];
//Array can be indexed by any integral expression.
class xyz; …
int array4 [ xyz ];
//Indices can be objects of xyz.
Futurewiz
www.futurewiz.co.in
Associative Array
int xyz [ * ];
5 7 2 1 3 9
0 1 2 3 7 10
xyz[0]=5; //Memory allocated during assignment
xyz[1]=7;
xyz[2]=2;
xyz[3]=1;
xyz[7]=3;
xyz[10]=9;
Futurewiz
www.futurewiz.co.in
Associative Array Methods
Futurewiz
www.futurewiz.co.in
Associative Array Methods
Futurewiz
www.futurewiz.co.in
Associative Array Methods
initial
begin
a.first(index); //index=Jan //\\
$display(a[index]);
while(a.next(index)) //Go through all index
$display(a[index]);
end
Futurewiz
www.futurewiz.co.in
User Defined
uint8 a, b;
word c, d;
a=8’d10;
c=16’d25;
Futurewiz
www.futurewiz.co.in
Structures
Declaration :
Futurewiz
www.futurewiz.co.in
Structures
Initializing : Accessing :
Futurewiz
www.futurewiz.co.in
Unions
Example :
union
{ real a;
int b;
bit [7:0] c; } exam1;
Futurewiz
www.futurewiz.co.in
Unions
Example :
typedef union
{ shortint a;
00 00 00 00
int b;
bit [7:0] c; } my_un;
my_un un1; 00 00 F0 F0
un1.a=16’hf0f0;
$displayh(un1.b);
00 00 F0 AA
un1.c=8’b1010_1010;
$displayh(un1.b);
Futurewiz
www.futurewiz.co.in
Structures vs Unions
Structure Union
Futurewiz
www.futurewiz.co.in
String
Futurewiz
www.futurewiz.co.in
String Operators
Futurewiz
www.futurewiz.co.in
Enumerated Type
Futurewiz
www.futurewiz.co.in
Enumerated Type
Example :
enum { RED, GREEN, BLUE } color;
//RED=0, GREEN=1, BLUE=2
Futurewiz
www.futurewiz.co.in
const
Futurewiz
www.futurewiz.co.in
Casting
Example :
int a;
initial a=int’(3.0 * 2.0);
Futurewiz
www.futurewiz.co.in
Casting
int a;
real b=3.0;
Futurewiz
www.futurewiz.co.in
Casting
Example :
int a=-10;
initial
begin
$display(a>>>1); // -5
$display(unsigned’(a)>>>1); // positive value
const’(a); // changing to constant
a=3; //Runtime error
end
Futurewiz
www.futurewiz.co.in