System Verilog (3) 枚举
(2) Enumeration 枚举类型,类似状态机
写了三个例子,包含枚举的声明、变量类型以及一些操作。
- 自定义一个枚举类型(traffic_lights),然后例化一个状态变量,monitor函数监控变量变化的时间($time)、变量名(.name),变量值
module enumeration_basic;
//enum {red, green, yellow} traffic_lights;
typedef enum {red, green, yellow} traffic_lights;
traffic_lights light;
initial begin
$display("default light_name=%s, light=%0d",light.name,light);
light = red ;
#1;
light = yellow;
#1;
light = green;
#1;
end
initial begin
$monitor ("time %0t ns, ligh_name=%s, light=%0d",$time,light.name,light);
end
endmodule
编译结果
# Loading sv_std.std
# Loading work.enumeration_basic(fast)
#
# vsim -voptargs=+acc=npr
# run -all
# default light_name=red, light=0
# time 0 ns, ligh_name=red, light=0
# time 1 ns, ligh_name=yellow, light=2
# time 2 ns, ligh_name=green, light=1
# exit
# End time: 08:48:16 on Mar 29,2022, Elapsed time: 0:00:02
# Errors: 0, Warnings: 0
Done
- 枚举变量范围的六种情况

查看代码
查看代码
module enumeration_var;
typedef enum {black[3:5]} colorset_1;
typedef enum {blue[3:5]=4} colorset_2;
typedef enum {purple[4]} colorset_3;
typedef enum {pink[4]=4} colorset_4;
typedef enum {red=1,yellow=2,green=3} colorset_5;
typedef enum {red1, green1, blue} colorset_6;
colorset_6 c6;
colorset_5 c5;
colorset_4 c4;
colorset_3 c3;
colorset_2 c2;
colorset_1 c1;
initial begin
$display("default color is %s with value of %0d",c6.name,c6);
$display("default color is %s with value of %0d",c5.name,c5);
$display("default color is %s with value of %0d",c4.name,c4);
$display("default color is %s with value of %0d",c3.name,c3);
$display("default color is %s with value of %0d",c2.name,c2);
$display("default color is %s with value of %0d",c1.name,c1);
c1= black3;
c2= blue3;
c3= purple0;
c4= pink0;
c5= red;
c6= red1;
#1;
$display("color is %s with value of %0d",c6.name,c6);
$display("color is %s with value of %0d",c5.name,c5);
$display("color is %s with value of %0d",c4.name,c4);
$display("color is %s with value of %0d",c3.name,c3);
$display("color is %s with value of %0d",c2.name,c2);
$display("color is %s with value of %0d",c1.name,c1);
c1= black4;
c2= blue4;
c3= purple1;
c4= pink1;
c5= yellow;
c6= green1;
#1;
$display("color is %s with value of %0d",c6.name,c6);
$display("color is %s with value of %0d",c5.name,c5);
$display("color is %s with value of %0d",c4.name,c4);
$display("color is %s with value of %0d",c3.name,c3);
$display("color is %s with value of %0d",c2.name,c2);
$display("color is %s with value of %0d",c1.name,c1);
c1= black3;
c2= blue3;
c3= purple0;
c4= pink0;
c5= red;
c6= red1;
#1;
$display("color is %s with value of %0d",c6.name,c6);
$display("color is %s with value of %0d",c5.name,c5);
$display("color is %s with value of %0d",c4.name,c4);
$display("color is %s with value of %0d",c3.name,c3);
$display("color is %s with value of %0d",c2.name,c2);
$display("color is %s with value of %0d",c1.name,c1);
end
endmodule
编译结果
查看代码
# Loading sv_std.std
# Loading work.enumeration_var(fast)
#
# vsim -voptargs=+acc=npr
# run -all
# default color is red1 with value of 0
# default color is with value of 0
# default color is with value of 0
# default color is purple0 with value of 0
# default color is with value of 0
# default color is black3 with value of 0
# color is red1 with value of 0
# color is red with value of 1
# color is pink0 with value of 4
# color is purple0 with value of 0
# color is blue3 with value of 4
# color is black3 with value of 0
# color is green1 with value of 1
# color is yellow with value of 2
# color is pink1 with value of 5
# color is purple1 with value of 1
# color is blue4 with value of 5
# color is black4 with value of 1
# color is red1 with value of 0
# color is red with value of 1
# color is pink0 with value of 4
# color is purple0 with value of 0
# color is blue3 with value of 4
# color is black3 with value of 0
# exit
# End time: 10:07:16 on Mar 29,2022, Elapsed time: 0:00:01
# Errors: 0, Warnings: 0
Done
对枚举变量的操作SV内置了六个函数,分别是.name, .first, .last, .prev, .next, .num
查看代码
module enum_methods;
typedef enum {pink, green, blue, yellow} colorlist;
// pink=0, green=1, blue=2, yellow=3
colorlist color;
initial begin
color = yellow;
#1;
$display("current color is %s", color.name);
$display("Next color is %0d",color.next);
$display("previous color is %0d", color.prev);
$display("First color is %0d", color.first);
$display("last color is %0d", color.last);
$display("The number of color is %0d", color.num);
end
endmodule
编译结果
查看代码
# Loading sv_std.std
# Loading work.enum_methods(fast)
#
# vsim -voptargs=+acc=npr
# run -all
# current color is yellow
# Next color is 0
# previous color is 2
# First color is 0
# last color is 3
# The number of color is 4
# exit
# End time: 10:33:19 on Mar 29,2022, Elapsed time: 0:00:01
# Errors: 0, Warnings: 0
Done

浙公网安备 33010602011771号