Printing an enum by name


#1

While displaying messages, how do I print an enum’s name?


#2

You can call the .name() function on a variable of enum type to return the enum type as a string.

You can also just use .name without the braces.

module test_enum;
    typedef enum {
        REG0, REG1, REG2
    } regname_e;

    initial begin
        regname_e regname;

        regname = REG1;
        $display("regname is %s", regname.name);

        regname = REG2;
        $display("regname is %s", regname.name());
    end
endmodule

Output

regname is REG1
regname is REG2