I’m trying to implement a hex valurs into enum as below.
module enum_datatype;
//declaration
//enum reg[7:0] { red=0x1, green, blue=4, yellow, white=10, black } Colors;
typedef enum reg[7:0] { red=8'h2, green, blue=4, yellow, white=10, black } Colors;
//display members of Colors
initial begin
Colors = Colors.first;
for(int i=0;i<$size(Colors);i++) begin
$display("Colors :: Value of %0s \t is = %0d and Size is %d",Colors.name,Colors, $size(Colors));
Colors = Colors.next;
end
end
endmodule
but I’ve got a hierarchical reference error.
How can I use hex value into enum?
This is C code, how do I get a equivalent systemverilog code?
#include <stdio.h>
enum index { A = 0xa3, B = 0x07, C = 0x18 };
int main()
{
index i[] = { A, B, C };
for (int j = 0; j < 3; j++)
printf("%X ", i[j]); }
module enum_datatype;
//declaration
//enum reg[7:0] { red=0x1, green, blue=4, yellow, white=10, black } Colors;
typedef enum reg[7:0] { red=8'h2, green, blue=8'h4, yellow, white=8'h10, black } Colors;
Colors Color;
//display members of Colors
initial begin
Color = Color.first;
for(int i=0;i<$size(Colors);i++) begin
$display("Colors :: Value of %0s \t is = %0d and Size is %d",Color.name,Color, $size(Color));
Color = Color.next;
end
end
endmodule
Colors is a datatype, declared a variable name Color. This works now!
$size(colors) gives you the number of array elements, 8, which is the same as $bits(Colors) in this case. Colors.num gives the number of enumerations.
You could also use a do-while loop
module enum_datatype;
typedef enum bit[7:0] { red=8'h2, green, blue=8'h4, yellow, white=8'h10, black } Colors;
Colors Color;
//display members of Colors
initial begin
Color = Color.first;
do begin
$display("Colors :: Value of %0s \t is = %0d and Size is %d",Color.name,Color, $bits(Color));
Color = Color.next;
end while(Color != Color.first);
end
endmodule
In reply to yourcheers:
$size(colors) gives you the number of array elements, 8, which is the same as $bits(Colors) in this case. Colors.num gives the number of enumerations.
You could also use a do-while loop
module enum_datatype;
typedef enum bit[7:0] { red=8'h2, green, blue=8'h4, yellow, white=8'h10, black } Colors;
Colors Color;
//display members of Colors
initial begin
Color = Color.first;
do begin
$display("Colors :: Value of %0s \t is = %0d and Size is %d",Color.name,Color, $bits(Color));
Color = Color.next;
end while(Color != Color.first);
end
endmodule
Sir,
Is there a way to use same enumeration value?
because I’d like to add to enum as
typedef enum reg[7:0] { a=10, b=10, c=10} like this one.
I encounters an error message as each enumeration constance must have a unique value.
What am I supposed to do?
In reply to UVM_LOVE:
I think you have hit the XY Problem.
Please explain what you are looking to accomplish without involving an enum.
Sir, I’m trying to find what I want it while resolving.
Actually, I didn’t realize that enum need to be as a unique value
in C language, we can use the same enum value used in C code as the below.
include <stdio.h>
enum index { A = 0xa3, B = 0x18, C = 0x18 };
int main() {
index i = { A, B, C };
for (int j = 0; j < 3; j++)
printf("%X ", i[j]);
}
but can’t in systemverilog. so I’m trying to find a way.
From LRM:
Section 6.19 Enumerations
Both the enumeration names and their integer values shall be unique. It shall be an error to set two values to the same name or to set the same value to two names, regardless of whether the values are set explicitly or by automatic incrementing.