3

I have command-line plusargs that I want to map to enumerated values.

 vsim foo +MY_PLUSARG=BAR

How do I get the string "BAR" to become the enum BAR?

Ray Salemi
  • 5,247
  • 4
  • 30
  • 63

2 Answers2

4

If you are using UVM 1.2 or have access to the library, you may want to use uvm_enum_wrapper class to do the conversion. It is a template class wrapper defined in uvm_globals.svh and you can use it as follows:

typedef enum {BISTRO, COFFEE_SHOP, BAR} places_e;

typedef uvm_enum_wrapper#(places_e) places_wrapper;
places_e place;

places_wrapper::from_name("BAR", place);

Quite like the code you provided in this solution, the wrapper class works by traversing the enum entries and creating an assoc array for a enum[string] map for the given enum (supplied as template parameter). So if you are using UVM 1.2, don't repeat.

Community
  • 1
  • 1
Puneet Goel
  • 858
  • 5
  • 8
1

The idea behind this solution is to avoid a case statement that hardcodes the members of your enumerated type. You want to be able to change the type in one play.

Let's say you have the following enum:

typedef enum {BISTRO, COFFEE_SHOP, BAR} places_e;

You want your user to be able to type:

 vsim top +MEET_PLACE=BAR

Now you want to translate the string "BAR" to the enum 'Bar'.

You can do this:

   typedef enum {BISTRO, COFFEE_SHOP, BAR} places_e;

module top;

   places_e place_map[string];

   function void make_map;
      places_e pl;
      pl = pl.first();
      do begin
         place_map[pl.name()]=pl;
         pl = pl.next();
      end while (pl != pl.first());
   endfunction // make_map

   function string get_list;
      string ss;
      places_e pl;
      pl = pl.first();
      ss = "";
      do begin
         ss = {ss, " ",pl.name()};
         pl = pl.next();
      end while (pl != pl.first());
      return ss;
   endfunction // get_list

   initial begin
      string place_str;
      places_e place;
      make_map;

      if (!$value$plusargs("MEET_PLACE=%s", place_str)) begin
         $display("You must choose a MEET_PLACE");
         $finish;
         end

      if (! place_map.exists(place_str)) begin
         $display("You must choose from this list: %s", get_list());
         $finish;
      end

      place = place_map[place_str];

      $display("Let's meet at a %s!", place.name());
   end // initial begin
endmodule // top
Ray Salemi
  • 5,247
  • 4
  • 30
  • 63
  • You don't need the `get_list()` function. You can combine with make_list, or just use `$display("You must choose from this list: %0p", place_map.find_index() with(1) );` – dave_59 Feb 05 '16 at 00:15