Do enums wrap round?


A customer asked me a couple of questions about how SystemVerilog enum base type values are assigned:

Here is a line from the Doulos Comprehensive SystemVerilog course:

enum { aa, bb, cc, dd = 7, ee, ff, gg = 6, hh = 5 } variable;

The base type of this enum is int (by default). The values assigned to the values are these

aa  0  default for base type (int)
bb  1  next value
cc  2  next value
dd  7  explicitly set
ee  8  next value
ff  9  next value
gg  6  explicitly set
hh  5  explicitly set

So, if we said
enum { aa, bb, cc, dd = 7, ee, ff, gg = 6, hh = 5, ii } variable;

What would be the value of ii ? The answer is that it wouldn't compile, because a value of 6 would be assigned to ii (because that's the next value after 6) and that is no good because it's already taken (by hh).

So, how about this case?

typedef enum logic {zero, one, ex, zed} logic_enum;

This won't compile because SystemVerilog tries to assign the value 2'b10 to ex, which is out of range for the base type. However, this is OK:

typedef enum logic {zero, one, ex=1'bx, zed=1'bz} logic_enum;

where we explicitly assign values to ex and zed. Similarly, this won't compile either:

typedef enum bit[1:0] {one=2'b01, two, three, zero} twobit_enum;

because SystemVerilog tries to assign the value 3'b100 to zero, which is also out of range of the base type (not - 2'b00 - enums do not "wrap round").

https://www.edaplayground.com/x/4by4

A customer asked me about this. I didn't know the answer, so I wrote a few lines of code on EDA Playground. EDA Playground is great for that, because it's always on. You don't have to queue for licences, wait for EDA tools to start, create new files, fire up editors...

Comments

Popular posts from this blog

Bit Width Casting in SystemVerilog

How to convert a std_logic_vector to a hex string?

Can you add/remove more than 1 item from a queue?