ch2-Enume
2019-10-24
The user can assign any integer value for any of the enumerated names. If any name does not have an assigned value, then it automatically takes the incremented value of the previous name.
1 |
|
Why do we need enumeration ?
To make the code more simple and readable.
Consider the following example without enumeration.
1 |
|
The following code is more readable because of the enumerated names RED, YELLOW and GREEN.
1 |
|
Enumerated-type ranges
name | The next number will be associated with name |
---|---|
name = C | Associates the constant C to name |
name[N] | Generates N named constants : name0, name1, …, nameN-1 |
name[N] = C | First named constant gets value C and subsequent ones are associated to consecutive values |
name[N:M] | First named constant will be nameN and last named constant nameM, where N and M are integers |
name[N:M] = C | First named constant, nameN will get C and subsequent ones are associated to consecutive values until nameM |
Example
In the following example, we’ll try out each of the different styles shown in the table above.
1 |
|
Simulation Log
1 |
|
numerated-Type Methods
SystemVerilog includes a set of specialized methods to enable iterating over the values of enumerated types.
first() | function enum first(); | Returns the value of the first member of the enumeration |
---|---|---|
last() | function enum last(); | Returns the value of the last member of the enumeration |
next() | function enum next (int unsigned N = 1); | Returns the Nth next enumeration value starting from the current value of the given variable |
prev() | function enum prev (int unsigned N = 1); | Returns the Nth previous enumeration value starting from the current value of the given variable |
num() | function int num(); | Returns the number of elements in the given enumeration |
name() | function string name(); | Returns the string representation of the given enumeration value |
Type Checking
Enumerated types are strongly typed and hence a variable of type enum
cannot be assigned an integer value that lie outside the enumeration set unless an explicit cast is used.
1 |
|
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!