sv_enum_datatypes_sample.pdf

Upload: divya-mangeshkar

Post on 04-Jun-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 sv_enum_datatypes_sample.pdf

    1/4

    The SystemVerilogReference Guide

  • 8/14/2019 sv_enum_datatypes_sample.pdf

    2/4

    148 Esperan 20 10

    enum

    Declares a user-defined enumerated type having a setof explicitly named values.

    Syntax

    Where dat a_t ype is optional and defaults to i nt . var is a variable name.

    Rules and Examples

    Declares a variable mst at e which can only take valuesi dl e , s t a r t , pause or done . By default, mst at e is oftype i nt , and the first item in the enumerated list,i dl e , is represented as 0, s ta r t as 1, pause as 2 anddone as 3.

    An enum type declaration is only visible in the scope in

    which it is declared, and the enumeration values mustbe unique in that scope.

    A range notation, using integer constants, can be usedfor name sequences in the enumerated list.

    Control of EncodingExplicit values can be defined in the enumerated list toallow one-hot, gray encoding etc.:-

    Explicit and default encoding can be mixed. Nameswithout an explicit encoding increment from theprevious name:-

    Here S1 has the encoding 3, S2 = 4, S4 = 9. It is anerror if explicit and default encodings overlap, forexample if S3 = 3 , S2 and S3 would have the sameencoding, and this would be a compilation error.

    enum data_type { item1 , item2 . . . } var

    enum {i dl e, st ar t , pause, done} mst at e;

    enum {S[ 2] } seq; / / = S0, S1, S2

    enum {RST, P[ 4: 5] } r ng; / / = RST, P4, P5

    enum {i dl e = 1, st ar t = 2,pause = 4, done = 8} mst at e;

    enum {S0 = 2, S1, S2, S3 = 8, S4} st at es;

  • 8/14/2019 sv_enum_datatypes_sample.pdf

    3/4

    Esperan 20 10 149

    enum

    Explicit Data Types

    An explicit data type can also be defined. Here enumvariable st bi t is a bi t vector of length 3:-

    With explicit data types, any explicit encodings must match the length of the data type:-

    Typing in AssignmentSystemVerilog enumerated types are strongly typed.

    An enum variable can only be directly assigned:-

    A value from the its enumerated list

    A variable of the same enumerated type

    Type casting must be used to assign other types to theenumerated variable.

    A t ypedef declaration can be used to associate a name

    with the enumerated type. The type name can then beused for type casting:-

    Typing in ExpressionWhen used in an expression, an enumerated variablebecomes an object of its data type.

    enum bi t [ 2: 0] {S[ 2] } s t bi t ;

    enum bi t [ 2: 0] {S0 = 3 b001,S1 = 3 b010, S2 = 3 b100} st bi t ;

    mst at e = i dl e;

    mst at e = next _mst at e;mst at e = 2; / / er r or

    t ypedef enum {i dl e, s t ar t ,pause, done} st at e_t ;

    st at e_t mst at e, next _mst at e;mst at e = st at e_t ( 2) ; / / t ype cast

    i nt ai nt ;/ / mst at e i s i nt i n expr essi onai nt = mst at e + 1;

  • 8/14/2019 sv_enum_datatypes_sample.pdf

    4/4

    150 Esperan 20 10

    enum

    Enum Methods

    Enumerte values can be accessed via methods:-

    Methods can be cascaded.

    This example produces the following output:-

    done = 2st ar t = 1i dl e = 0

    next and pr ev methods wrap around the enumeratetype declaration order.

    Warning : use of f i r s t , l ast , pr ev and next methods creates code which is dependent on thedeclaration order of the enumerated type values.

    See Also

    i nt , t ypedef

    Method Description

    f i r s t ( ) Returns first value of enumeration

    l ast ( ) Returns last value of enumeration

    next ( N) Returns Nth next value, wrapping to

    begining if need be. N defaults to 1.pr ev( N) Returns Nth previous value, wrapping

    to end if need be. N defaults to 1.

    num( ) Returns number of values in theenumeration

    name( ) Returns string representation of givenenumeration value

    t ypedef enum {i dl e, st ar t , done} st at e_t ;st at e_ t st = st . l ast ( ) ;

    i ni t i al f or ever begi n $di spl ay ( " %s = %d" , st . name( ) , st ) ; i f ( st == st . f i r s t ( ) )

    br eak st = s t . pr ev( ) ; end

    t ypedef enum {i dl e, st ar t , done} st at e_t ;s t at e_t s t = s t ar t ;i ni t i al s t = s t . next ( 2) ; / / s t ar t + 2 = i dl e