Professional Documents
Culture Documents
DT SV
DT SV
DT SV
VERILOG
.
Content
Definition
Integer data types
Real and shortreal data types
Void data type
String data type
Event data type
User-defined types
Enumerations
Structures and unions
Class
Singular and aggregate types
chandle data type
Casting
Definition
The data types byte, shortint, int, longint and integer default to signed.
The data types bit, reg ,time and logic default to unsigned as do arrays of
these types.
But used anywhere that the reg and net data types are
traditionally used (same as verilog) except inout and
multiple drives.
Ex-real data_real;
data_real = {32{4'b1111}};
$display(" data_real = %0f", data_real);
data_real = {32{4'bzx01}};
$display(" data_real = %f", data_real);
o atoi()
o atohex()
o atooct()
o atobin()
o atoreal()
o itoa()
o hextoa()
o octtoa()
o bintoa()
o realtoa()
module string_ex ();
string str1 = “tessolve";
string str2,str; Output-
string str3 = "102.11tess454546olve";
integer value of str1= 0
Initial
integer value of str3= 102
begin
$display("integer value of str1=%d", hexadecimal value of str3=
str1.atoi()); 258
$display("integer value of str3=%d", octal value of str3= 66
str3.atoi()); binary value of str3=
$display(“hexadecimal value of str3= 000000000000000000000000000
%d", str3.atohex()); 00010
$display(“octal value of str3=%d",
str3.atooct());
$display(“binary value of str3=%b",
str3.atobin());
$display(“real value of str3=%g",
str3.atoreal());
str1.itoa(653266);
$display("str1=%s",str1);
str1.hextoa(653266);
$display("str1=%s",str1); real value of str3=102.11
str1.octtoa(653266);
str1=653266
$display("str1=%s",str1);
str1=9f7d2
str1.bintoa(653266);
str1=2373722
$display("str1=%s",str1);
str1=1001111101111101001
str1.realtoa(653266.12);
0
$display("str1=%s",str1);
str1=653266
end
VCS Simulation
endmodule
Report
Event data type
Events provide a handle to a synchronization object.
Syntax:
event variable_name [= initial_value];
Example:
o typedef integer myinteger;
o typedef bit[3:0] nibble ;
o typedef struct { byte a; reg b; shortint unsigned c; } myStruct;
o typedef enum { red, green, blue, yellow, white, black } Colors ;
A type can be used before it is
module main; defined, provided it is first
identified as a type by an empty
typedef int my; typedef:
initial module main;
begin typedef my;
my a=5; my f = 5;
$display ("a=%d", a); typedef int my;
end initial
endmodule begin
$display ("f=%d", f);
Runtime version J-2014.12-SP1-1;
end
Mar 14 13:22 2017
endmodule
a= 5
Runtime version J-2014.12-SP1-1;
VCS Simulation Repo Mar 14 13:25 2017
rt f= 5
VCS Simulation Report
Enumeration datatype
Syntax
enum [ enum_base_type ] { enum_name_declaration { , enum_name_declaration } }
Example
o enum integer {IDLE=0, GNT0=1, GNT1=2} state;
o enum {RED,GREEN,ORANGE} color;
o enum {BRONZE=4, SILVER, GOLD} medal;
o enum {a, b=7, c} alphabet;
Error
cases
oAny enumeration encoding value that is outside the representable range of the enum.
enum bit [3:0] {bronze=5'h13, silver, gold=3'h5} medal4;
// Error in the bronze and gold member declarations
SystemVerilog includes a set of specialized methods to enable iterating over
the values of enumerated types.
o first()
o last()
o next()
o prev()
o num()
o name()
module xx;
typedef enum {Red,Green,
Blue,yellow,white,crown} colors;
colors c,c1;
initial
begin
Compiler version J-2014.12-SP1-
$display ("first element is %d",c.first);
1; Runtime version J-2014.12-
c1=c.first;
SP1-1; Mar 22 06:07 2017
$display ("first element is %s",c1.name);
first element is 0
c1=c1.next;
first element is Red
$display ("next element is %s",c1.name);
c1=c1.prev;
next element is Green
$display ("prev element is %s",c1.name);prev element is Red
$display ("last element is %d",c.last); last element is 5
$display ("no of element is %d",c.num);no of element is 6
end VCS Simulation Repor
endmodule t
module enum_ex;
Compiler version J-2014.12-SP1-1; Runtime
typedef enum { red, green=3, blue[8], version J-2014.12-SP1-1; Mar 15 05:45 2017
yellow[1:5]=14 } Colors; name=red :value= 0
name=green :value= 3
initial name=blue0 :value= 4
begin name=blue1 :value= 5
name=blue2 :value= 6
Colors c = c.first;
name=blue3 :value= 7
forever name=blue4 :value= 8
begin name=blue5 :value= 9
name=blue6 :value= 10
$display( "name=%s :value= %d", c.name, name=blue7 :value= 11
c ); name=yellow1 :value= 14
if( c == c.last ) break; name=yellow2 :value= 15
name=yellow3 :value= 16
c = c.next; name=yellow4 :value= 17
end name=yellow5 :value= 18
the total no of element in enum is 15
$display ("the total no of element in enum
VCS Simulation Report
is %d",c.num );
end
endmodule
Structure
structure is an user defined data type that allows to
combine data items of different kinds.
structure data types to declare complex data type, which is
nothing but a collection of other data types.
Syntax
struct [structure tag] { member definition;
member definition;
...
member definition; }[one or more structure variables];
To access any member of a structure, we use the
member access operator (.).
Structure
module struct_data ();
struct data{ byte a;
reg b;
shortint unsigned c; } myStruct;
myStruct = '{11,1,101};
initial begin
$display ("a = %b b = %b c = %h", myStruct.a, mystruct.b, mystruct.c); // 1011 1 65
#1 $finish; end
endmodule
module mod1;
typedef struct packed {
int x;
int y; } st;
st s1;
int k = 1;
initial begin
#1 s1 = {1, 2+k}; Compiler version J-2014.12-SP1-1;
#1 $display( s1); Runtime version J-2014.12-SP1-1; Mar
20 10:00 2017
#1 $display( s1.x, s1.y);
4294967299
#1 s1 = {x:2, y:3+k}; 13
#1 $display( s1.x, s1.y); 24
#1 $display( s1); 8589934596
#1 $finish; $finish called from file "testbench.sv",
line 14.
end
$finish at simulation time 5
endmodule
VCS Simulation Report
Class
A class is a collection of data (class properties)and a
set of subroutines(methods) that operate on that data.
chandleis a special SystemVerilog type that is used for passing C pointers as arguments to imported DPI
functions.
The size of this type is platform dependent, but shall be at least large enough to hold a pointer on the
machine in which the tool is running
syntax :
chandle variable_name ;
restriction:
— Ports shall not have the chandle data type
— Chandles shall not be assigned to variables of any other type
— Chandles shall not be used:
As ports
In sensitivity lists or event expressions.
Casting
type casting is used for converting one type of data type to
another type of data type , by using a cast (') operation.
Example
int a ;
a = int‘ (2.3 * 3.1); // 7(below X.5 =X)
a = int‘ (2.3 * 3.3); // 8(above and equal X.5 =X+1)
module enum_ex;
typedef enum {Red, Green, Blue} Colors;
typedef enum {Mo,Tu,We,Th,Fr,Sa,Su} Week;
Colors C;
initial
begin
C = Colors'(C+1);
$display ("element=%s value=%d", C.name,C);
output-
C = Colors'(Su);
Compiler version J-2014.12-SP1-1;
$display ("element=%s value=%d", C.name,C);
Runtime version J-2014.12-SP1-1;
// out of range value Mar 15 07:02 2017
C = Colors'(Mo); element=Green value= 1
$display ("element=%s value=%d", element= value= 6
C.name,C);
element=Red value= 0
end
VCS Simulation Repo
endmodule
rt
Dynamic casting
It is used to assign values to variables that might not ordinarily be
valid.
Done by system task-$cast
Syntax:-task cast( singular dest_var, singular source_exp );
function int $cast( singular dest_var, singular source_exp );
Both cases $cast attempts to assign the source expression to the
destination variable.
In case of task If the assignment is invalid,
a runtime error occurs
the destination variable is left unchanged.
In case of function
returns 1 if the cast is legal.and
If the cast fails, the function does not make the assignment and returns 0.
$cast performs a run-time check.
No type checking is done by the compiler, except to check
that the destination variable and source expression are
singulars.
Ex:-
typedef enum { red, green, blue, yellow, white, black }
Colors;
Colors col;
$cast( col, 2 + 3 ); //col=5(black)