Reputation: 11
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?
Upvotes: 0
Views: 6637
Reputation: 42648
A do-while
loop works well for iterating over enums
module top;
typedef enum { enum_A = 1, enum_B = -5, enum_C = 10 } enum_t;
typedef enum_t enum_list_t[$];
enum_list_t enum_array = enum_array_values();
function automatic enum_list_t enum_array_values;
enum_t tmp = tmp.first;
do begin
enum_array_values.push_back(tmp);
tmp = tmp.next;
end
while (tmp != tmp.first);
endfunction
initial $display("%p", enum_array);
endmodule
Upvotes: 0
Reputation: 12344
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
Upvotes: 1