Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that Verilog enum is valid?

If I receive an enum:

my_cmd cmd = my_cmd'(value_from_bus);

How do I easily check that cmd is a valid cmd?

typedef enum int {
  ADD = 1,
  SUBTRACT = 3,
  MULTIPLY = 7
} my_cmd;
like image 634
Victor Lyuboslavsky Avatar asked Jan 07 '15 23:01

Victor Lyuboslavsky


2 Answers

You can also use $cast to check if it is valid, and copy at the same time. So instead of doing: cmd = my_cmd'(value_from_bus);, you can do this:

if ($cast(cmd, value_from_bus))
    $display("Valid: %s", cmd.name());
else
    $display("Invalid");

Example on EDA Playground

like image 64
AldoT Avatar answered Oct 15 '22 03:10

AldoT


You can use enum's name() function:

  if (cmd.name() == "")
    $display("%0d is bad", cmd);
  else
    $display("%s:%0d is good", cmd.name(), cmd);

Example on EDA Playground

like image 31
Victor Lyuboslavsky Avatar answered Oct 15 '22 05:10

Victor Lyuboslavsky