有什么方法可以按名称将枚举值作为命令行参数传递?
Any way to pass enum values by name as commandline args?
有什么方法可以从命令行按名称传递枚举的值吗?相反,最干净的解决方案是什么?
我尝试了下面的方法,
typedef enum int unsigned { white, black, red } colour_e;
class house;
rand colour_e colour;
string n_colour = "white";
constraint c_colour {
colour.name() == n_colour;
}
endclass: house
program top;
house paradise;
initial begin
paradise = new();
void'($value$plusargs("colour=%0s", paradise.n_colour));
if (!paradise.randomize())
$display("not randomized");
else
$display("paradise.colour = %s",paradise.colour);
end
endprogram: top
我想通过这样的东西+colour=black
。因此 paradise.colour 被分配为黑色。
vcs 因在约束中使用 enum.name()
而被抄袭。
下面是错误。
Error-[NYI-CSTR-SYS-FTC] NYI constraint: sys function calls
T-enum_1.sv, 9 $unit, "this.colour.name" System function calls are
not yet implemented in constraints. Remove the function call or if
possible replace it with an integral state variable assigned in
pre_randomize().
里维埃拉哭的如下
ERROR VCP7734 "Type of 'this.colour.name()' is not allowed in a
constraint block. Only integral types are allowed in a constraint
block." "design.sv" 9 1 ERROR VCP7734 "Type of 'n_colour' is not
allowed in a constraint block. Only integral types are allowed in a
constraint block." "design.sv" 9 1 WARNING VCP7114 "STRING value
expected for format specifier %s as parameter
paradise.colour." "testbench.sv" 13 54
这给我带来了一个问题,约束块中的所有内容都必须是整数类型(就像我们不能将 string
声明为 rand
变量一样)?
任何想玩代码的人请查看 EDA 游乐场的代码 here
使用uvm_enum_wrapper class进行从字符串到相应枚举值的转换。它是 uvm_globals.svh(UVM 1.2 的一部分)中定义的模板 class 包装器,您可以按如下方式使用它:
typedef enum {white, black, red} colour_e;
typedef uvm_enum_wrapper#(colour_e) colour_wrapper;
string colour_str;
void'($value$plusargs("colour=%0s", colour_str));
colour_wrapper::from_name(colour_str, paradize.n_colour);
包装器 class uvm_enum_wrapper 通过遍历枚举条目并为给定枚举类型的枚举 [string] 映射创建关联数组(作为模板参数提供)。有关详细信息,请查看 the documentation.
同样有另一种解决方案。如果您不使用 UVM 1.2 版本,那么您可以将字符串作为输入参数并将该字符串参数转换为 int 类型。之后就可以直接通过 $cast 将 int 类型转换为 enum 类型了。
因为无法将字符串转换为枚举类型(UVM 1.2 除外),所以您必须为此添加一个额外的步骤。
module Test;
typedef enum {black,red,blue} color_e; //enum declaration
color_e color; //type of enum
string cmd_str; //Commandline string input
int cmd_int; //Input string is first converted into int
initial
begin
$value$plusargs("enum_c=%0s",cmd_str); //Take input argument as string it may be 0,1 or 2
cmd_int=cmd_str.atoi(); //Now convert that string argument into int type
$cast(color,cmd_int); //Casting int to enum type
$display("Value of color is --> %p",color); //Display value of enum
end
endmodule
有什么方法可以从命令行按名称传递枚举的值吗?相反,最干净的解决方案是什么?
我尝试了下面的方法,
typedef enum int unsigned { white, black, red } colour_e;
class house;
rand colour_e colour;
string n_colour = "white";
constraint c_colour {
colour.name() == n_colour;
}
endclass: house
program top;
house paradise;
initial begin
paradise = new();
void'($value$plusargs("colour=%0s", paradise.n_colour));
if (!paradise.randomize())
$display("not randomized");
else
$display("paradise.colour = %s",paradise.colour);
end
endprogram: top
我想通过这样的东西+colour=black
。因此 paradise.colour 被分配为黑色。
vcs 因在约束中使用 enum.name()
而被抄袭。
下面是错误。
Error-[NYI-CSTR-SYS-FTC] NYI constraint: sys function calls T-enum_1.sv, 9 $unit, "this.colour.name" System function calls are not yet implemented in constraints. Remove the function call or if possible replace it with an integral state variable assigned in pre_randomize().
里维埃拉哭的如下
ERROR VCP7734 "Type of 'this.colour.name()' is not allowed in a constraint block. Only integral types are allowed in a constraint block." "design.sv" 9 1 ERROR VCP7734 "Type of 'n_colour' is not allowed in a constraint block. Only integral types are allowed in a constraint block." "design.sv" 9 1 WARNING VCP7114 "STRING value expected for format specifier %s as parameter paradise.colour." "testbench.sv" 13 54
这给我带来了一个问题,约束块中的所有内容都必须是整数类型(就像我们不能将 string
声明为 rand
变量一样)?
任何想玩代码的人请查看 EDA 游乐场的代码 here
使用uvm_enum_wrapper class进行从字符串到相应枚举值的转换。它是 uvm_globals.svh(UVM 1.2 的一部分)中定义的模板 class 包装器,您可以按如下方式使用它:
typedef enum {white, black, red} colour_e;
typedef uvm_enum_wrapper#(colour_e) colour_wrapper;
string colour_str;
void'($value$plusargs("colour=%0s", colour_str));
colour_wrapper::from_name(colour_str, paradize.n_colour);
包装器 class uvm_enum_wrapper 通过遍历枚举条目并为给定枚举类型的枚举 [string] 映射创建关联数组(作为模板参数提供)。有关详细信息,请查看 the documentation.
同样有另一种解决方案。如果您不使用 UVM 1.2 版本,那么您可以将字符串作为输入参数并将该字符串参数转换为 int 类型。之后就可以直接通过 $cast 将 int 类型转换为 enum 类型了。 因为无法将字符串转换为枚举类型(UVM 1.2 除外),所以您必须为此添加一个额外的步骤。
module Test;
typedef enum {black,red,blue} color_e; //enum declaration
color_e color; //type of enum
string cmd_str; //Commandline string input
int cmd_int; //Input string is first converted into int
initial
begin
$value$plusargs("enum_c=%0s",cmd_str); //Take input argument as string it may be 0,1 or 2
cmd_int=cmd_str.atoi(); //Now convert that string argument into int type
$cast(color,cmd_int); //Casting int to enum type
$display("Value of color is --> %p",color); //Display value of enum
end
endmodule