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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…