Search code examples
enumstypedefsystem-verilog

System Verilog typedef of typedef


typedef enums allow a convenient way to describe a set of name-value pairs. Is there a way to chain them to create deeper structures using enum at all levels?

For instance, I have the following:

typedef enum logic {ALPHA=0, BETA=1} a_t;
typedef enum logic {GAMMA=0, DELTA=1} b_t;
typedef enum logic {ZETA=0, ETA=1} c_t;
...

I want to create a variable c which is formed of a_t and b_t. Is this possible?

Something like:

a_t b_t c;

so at every dimension of c, I can have enums.

EDIT: Some clarification - assume a_t, b_t and c_t are immutable as they are generated automatically. And there are hundreds of such different enums. I want to create bigger structures as I need because automatically generating all combinations of them would make the code too big and messy.

For instance, say my a_t describes number of masters and b_t describes number of slaves. I want to create a structure where I have this hierarchy in my signal, and at the same time allow enums for them to allow easy of readability and use.

So, something like this:

c[MASTER_0][SLAVE_0]
c[MASTER_0][SLAVE_1]
c[MASTER_1][SLAVE_0]
c[MASTER_1][SLAVE_1]

Solution

  • Are you perhaps referring to an associative array, such as:

    c[ALPHA] = BETA;
    

    If so, you could simply refer to it as:

    b_t c[a_t];
    

    Which means create an associative array c who's key is of enums a_t and value is of enums b_t. You could keep going if you'd like :)

    typedef enum logic {ALPHA=0, BETA=1} a_t;
    typedef enum logic {GAMMA=0, DELTA=1} b_t;
    typedef enum logic {BAD_AT=0, GREEK_LETTERS=1} c_t;
    
    c_t my_data_structure[a_t][b_t];
    
    // Assigning a value
    my_data_structure[ALPHA][GAMMA] = GREEK_LETTERS;
    

    See an example on EDA Playground here.

    Also, I think you're slightly misunderstanding the use of typedef. It does not exactly describe a set of name-value pairs, rather it gives a new name to a data type. It is the enum that is actually creating a 'set of name-value pairs', but I'd clarify that it is essentially assigning identifiers to values. It would help if you could explain the application for a clearer answer.