SystemVerilog 3.1 adds string, chandle
and class data types, and enhances the Verilog
event and
SystemVerilog 3.0 enum data types.
SystemVerilog 3.1 also extends the user defined types by providing support for
object-oriented class.
data_type_common_item data_type ::=
integer_vector_type
[ signing ] { packed_dimension
}
| integer_atom_type [ signing ]
| type_identifier { packed_dimension }
| non_integer_type
| struct_union [ packed [ signing ] ]
{ struct_union_member { struct_union_member
} }
{ packed_dimension }14
| enum [ enum_base_type ] { enum_name_declaration { , enum_name_declaration } }
| string
| chandle
| virtual [ interface ] interface_identifier
| [ class_scope
| package_scope ] type_identifier { packed_dimension }
| class_type
| event
| ps_covergroup_identifier
data_type ::=
data_type_common_item
| event
| class_scope_type_identifier
| covergroup_identifier
enum_base_type ::=
integer_atom_type [ signing ]
| integer_vector_type [ signing ] [ packed_dimension ]
| type_identifier [ packed_dimension ]26
class_scope_type_identifier::=
class_scope [ :: type_identifier
]
class_scope ::=
class_identifier [ parameter_value_assignment ]
{ :: class_identifier [ parameter_value_assignment ] }
class_scope ::=
class_type ::
class_type ::=
ps_class_identifier [ parameter_value_assignment ]
{ :: class_identifier [ parameter_value_assignment ] }
integer_atom_type ::= byte | shortint | int | longint | integer | time
integer_vector_type ::= bit | logic | reg
non_integer_type ::= time | shortreal | real | realtime
simple_type ::= integer_type | non_integer_type | ps_type_identifier
struct_union_member29 ::=
{ attribute_instance } data_type { packed_dimension
}
data_type_or_void list_of_variable_identifiers ;
| { attribute_instance
} void list_of_variable_identifiers ;
data_type_or_void ::=
data_type | void
variable_decl_assignment ::=
variable_identifier
variable_dimension [ = expression ]
| variable_identifier dynamic_array_variable_identifier [ ] = new [ expression ] [ ( variable_identifier
) ] [ = dynamic_array_new ]
| class_variable_identifier = new [ ( list_of_arguments ) ] [ = class_new ]
| [ covergroup_variable_identifier
] = new [ ( list_of_arguments ) ]17
4-state Verilog-2001 data
type, at least 32 bit signed integer
The void
data type represents non-existent
data. This type can be specified as the return type of functions, indicating no
return value. This type can also be used for members
of tagged unions (see Section 3.11)
SystemVerilog includes a string
data type, which is a variable size,
dynamically allocated array of characters
bytes. SystemVerilog also includes a number of
special methods to work with strings.
Verilog supports string literals, but only at the lexical
level. In Verilog, string literals behave like packed
arrays of a width that is a multiple of 8 bits. A string literal assigned to a
packed array is an
integral variable of a different size is either
truncated to the size of the array variable or padded with zeroes to the left as necessary.
In SystemVerilog string
literals behave exactly the same as in Verilog. However, SystemVerilog also supports the string
data type to which a string literal
can be assigned. When using the string data type instead of a packed
array an integral variable, strings
can be of arbitrary length and no truncation occurs. Literal strings are
implicitly converted to the string type when assigned to a string
type or used in an expression
involving string type operands.
Variables of type string
can be indexed from 0 to N-1 (the
last element of the array), and they can take on the special value “”, which is
the empty string. Reading an element of a string yields
a byte.
SystemVerilog provides a
set of operators that can be used to manipulate combinations of string
variables and string literals. The basic operators defined on the string string data
type are listed in Table 3-2, which follows.
A string literal can be
assigned to a string, a character, or a packed array or an integral type.
If their size differs the literal is right justified and either truncated on the left or zero filled on the
left, as necessary. For example:
A string, string literal, or packed array integral
value can be assigned to a string variable. The string variable shall grow or shrink
to accommodate the packed array integral value. If the size (in bits) of the packed array integral
value is not a multiple of 8, then the packed array is zero filled on
the left.
a = {"Hi",b}; // OK
r = {"H",""}; // yields "H\0" ""
is converted to 8’b0
b = {"H",""}; // yields "H" "" is
the empty string
a[0] = "h"; // OK same as a[0] = "hi" )
Comparison. Relational operators return 1 if the corresponding
condition is true using the lexicographical ordering of the two strings Str1 and Str2.
The comparison behaves like the ANSI C strcmp function (or the compare compare string method) (with regard
to the lexical ordering) and embedded null bytes are included. Both
operands can be of type string, or one of them can be a string literal.
Str[index] |
Indexing. Returns a byte, the ASCII code at the given
index. Indexes range from 0 to N-1,
where N is the number of characters in the string. If given an index out of range, returns
0. Semantically equivalent to Str.getc(index), in section 3.7.3. |
Str.method(...) |
The dot (.) operator is used to invoke a specified method on strings. |
Note: str.putc(j, x) is identical semantically
equivalent to str[j] =
x.
Note: x = str.getc(j)
is identical
semantically equivalent to x = str[j].
—
str.compare(s) compares
str and s, as in the ANSI C strcmp function (with
regard to lexical ordering and return value),
with a compatible return value and embedded null bytes are included
—
str.icompare(s) compares str and
s,
like the ANSI C strcmp function
(with regard to lexical ordering and return value),
with a compatible return value, but the
comparison is case insensitive and
embedded null bytes are included.
The string is
converted until the first non-digit is encountered. The conversion
scans all leading digits and underscore characters (_) and stops as soon as it encounters any
other character, or the end of the string.
Returns zero if no digits were encountered. It does not parse the full syntax for integer
literals (sign, size, tick, base).
— str.atoreal() returns
the real number corresponding to the ASCII decimal representation in str.
The
conversion parses Verilog syntax for real
constants. The scan stops as soon as it encounters
any character that does not conform to this syntax, or the end of the
string. Returns zero if no digits were
encountered.
User defined type
identifiers have the same scoping rules as data identifiers, except that
hierarchical reference to type identifiers shall not be allowed. References to
type identifiers defined within an interfaces
interface through ports are allowed provided
they are locally re-defined before being used.
data_type_common_item data_type ::= // from Annex A.2.2.1
...
| enum [ enum_base_type ] { enum_name_declaration { , enum_name_declaration } }
enum_base_type ::=
integer_atom_type [ signing ]
| integer_vector_type
[ signing ] [ packed_dimension
]
| type_identifier [ packed_dimension ]26
// Syntax error: IDLE=2’b00, XX=2’bx
<ERROR>, S1=2’b01??,
S2=2’b10??
Generates N labels named constants in the sequence: name0, name1, ..., nameN-1. N must be an
integral constant
enum
{ register[1 2] = 1, register[2:4] = 10 } vr;
3.10.4 Enumerated types in comparison expressions
From the previous
declaration, blue has
the numerical value 2. This example assigns a the value
of 6 (2*3). Next, and it assigns b a value of 4 (3+1).
An enum
variable or identifier used as part of an expression is automatically cast to
the base type of the enum
declaration (either explicitly or
using int as the default). An assignment to an enum variable from an expression other than an enum variable or identifier of the
same type shall require a cast. Casting to an enum
type shall cause a conversion of the
expression to its base type without checking the validity of the value (unless
a dynamic cast is used as described in Section 3.15).
The first() method
returns the value of the first member of the enumeration enum.
The last() method
returns the value of the last member of the enumeration enum.
struct_union_member29 ::=
{ attribute_instance } data_type { packed_dimension
}
data_type_or_void list_of_variable_identifiers ;
| { attribute_instance
} void list_of_variable_identifiers ;
data_type_or_void ::=
data_type | void
typedef
union { int
i; shortreal
f; } num; // named union type
num n;
n.f =
0.0; // set n in floating point format
A packed union
shall contain members that must be packed structures, or packed arrays or
integer data types all of the same size (in contrast to an unpacked union,
where the members can be different sizes). This ensures that you can read back
a union member that was written as another member. A packed union can also be
used as a whole with arithmetic and logical operators, and its behavior is
determined by the signed or unsigned keyword, the latter being the default. If
a packed union contains a 2-state member and a 4-state member, the entire union
is 4 state. There is an implicit conversion from
4-state to 2-state when reading and from 2-state to 4-state when writing the
2-state bit member.
tag is 0 for Add, 1 for Jmp
Outer tag is 0
for Add, 1 for Jmp
Inner tag is 0
for JmpU, 1 for JmpC
A class is a
collection of data and a set of subroutines that operate on that data. The data
in a class is are referred
to as class properties, and its subroutines are called methods. The class
properties and methods, taken together, define the contents and capabilities of
a class instance or object.
class_declaration ::=
[ virtual ] class [ lifetime ] class_identifier
[ parameter_port_list ]
[ extends class_identifier [ parameter_value_assignment ] ] [ ( list_of_arguments
) ] ];
[ extends class_type [ ( list_of_arguments ) ] ];
{ class_item }
endclass [ : class_identifier]
class
Packet;
simple_type ::= integer_type
| non_integer_type | ps_type_identifier
A data type can be changed
by using a cast ( ’
) operation. The expression to be
cast must be enclosed in parenthesis parentheses or within concatenation or replication
braces and is self-determined.
typedef struct {
bit isfloat;
union { int i; shortreal f; } n; // anonymous type
} tagged_st; // named structure
typedef
bit [$bits(tagged_st) - 1 : 0] tagbits; // tagged_st
defined above
tagged_st a
[7:0]; // unpacked
array of structures
tagbits t = tagbits’(a[3]); //
convert structure to array of bits
a[4] = tagged_st’(t); //
convert array of bits back to structure