Display localparam name instead of value

Hello forum grus,
I am a new minion of system verilog.

I had a bunch of localparam as the following:
localparam CMD_A = 'd0;
localparam CMD_B = 'd1;

Is there a way to use $display to display CMD_A instead of the “%d” option which prints int?

thanks

In reply to pepes:

You need to explain why $display(“CMD_A”); does not do what you want.

In reply to dave_59:

I have a few outputs map to different commands (CMD_A CMD_B…) using always@(*) and would like to display the state names (instead of the number) as the testbench runs.

In reply to pepes:

That’s a slightly better explanation. You have a state variable and you want to map certain integer values back to a string name to print. There is a couple of different things you could do.

Instead of using parameters, you can use an enumerated type:

typedef enum {CMD_A, CMD_B, ...} command_t;
command_t state;
...
state = CMD_A;
$display("state is %s", state.name); // prints: state is CMD_A

By default, CMD_A has the value 0, CMD_B has the value 1. You can assign other encodings.

You can also define an associative array whose sole purpose is mapping values to strings.

string MAP[int] = '{default:"UNKNOWN", 0:"CMD_A", 1:"CMD_B"};
int state;
...
state = CMD_A;
$display("state is %s", MAP[state]); // prints: state is CMD_A