Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
156 views
in Technique[技术] by (71.8m points)

How to make an array with all the enum values in system verilog?

I have an enum :

typedef enum { enum_A = 1, enum_B = -5, enum_C = 10 } enum_t;

I want to make an array that has all the values of the enum:

enum_t enum_array[ enum_t.num() ] = { enum_A, enum_B, enum_C };

The problem is how to initialize the array without litrally writing the enum values?

I tried foreach & simple for - nothing works...

enum_t enum_array[ enum_t.num() ] =   enum_array_alues();
function   enum_t [enum_t.num() -1:0] enum_array_alues();
    automatic enum_t enum_val = enum_t.first();
    foreach ( enum_array_alues[val] ) begin
        enum_array_alues[val] = enum_val;
        enum_val              = enum_val.next(); << .next() on a non-contiguous enum type not supported
    end
endfunction;

Is there a solution to this problem?

question from:https://stackoverflow.com/questions/65868547/how-to-make-an-array-with-all-the-enum-values-in-system-verilog

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

System Verilog could be ugly :-). This is one of these cases. You cannot use enum methods on typedefs, you need an enum variable to do so. Also, function return type cannot be directly defined as an array, you need a typedef for it.

Also, different compilers have their own minds as well. The following works with vcs, mentor and aldera. However, it failed with cadence.

  typedef enum { enum_A = 1, enum_B = -5, enum_C = 10, hello } enum_t;
  enum_t enum_var;
  typedef enum_t enum_a[enum_var.num()];
  
  enum_a enum_array;

  initial begin
    enum_array = enum_array_values();
  end
  
  function  automatic enum_a enum_array_values();
    enum_t enum_val = enum_val.first;  
    
    foreach(enum_array_values[i]) begin
      enum_array_values[i]  = enum_val;
      enum_val = enum_val.next();
    end
  endfunction

So, my suggestion is to use dynamic arrays. It seems that this method below works with all compilers in eda playground. It is less ugly but requires a bit more code.

module top;
// Code your design here
 typedef enum { enum_A = 1, enum_B = -5, enum_C = 10 } enum_t;
 typedef enum_t enum_array_t[];
  
  enum_t enum_array[];

  initial begin
    enum_array =   enum_array_values();
    foreach (enum_array[i])
      $display(enum_array[i].name);
  end
  
  function  automatic enum_array_t enum_array_values();
    int i = 0;
    enum_t enum_val;
    enum_val = enum_val.first();
    
    forever begin
      enum_array_values     = new [i+1] (enum_array_values);
      enum_array_values[i]  = enum_val;
      if (enum_val == enum_val.last)
        break;
      i++;
      enum_val = enum_val.next();
    end
  endfunction
endmodule

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...