VHDL 中的类型枚举
Type enumeration in VHDL
VHDL 中的类型枚举是什么?
我在哪里可以使用它来使代码更短更易于理解?
例如,考虑下面的语句:
TYPE st_State IS (st_Idle, st_CheckHeader1, st_CheckHeader2, st_ReceiveData)
什么时候必须使用它。
您的示例只是名称为 st_State
的类型的声明,该类型包含四个元素。每个元素都有一个从 0
到 Elements - 1
的数字。这类似于 C typedef
和 C enum
.
请查看 this 说明以获取更详细的信息。
一个典型的应用是状态机来命名不同的状态:
architecture Top_Arch of Top is
type State_Type is (S0, S1, S2, S3);
signal CurrentState : State_Type := S0;
begin
process(Clock)
begin
if(rising_edge(Clock)) then
case CurrentState is
when S0 => ...
when S1 => ...
when S2 => ...
when S3 => ...
end case;
end if;
end Top_Arch;
使用此方法可产生更易读和更清晰的代码,但它等同于此方法(未经测试):
architecture Top_Arch of Top is
signal CurrentState : INTEGER RANGE 0 to 3 := 0;
begin
process(Clock)
begin
if(rising_edge(Clock)) then
case CurrentState is
when 0 => ...
when 1 => ...
when 2 => ...
when 3 => ...
end case;
end if;
end Top_Arch;
NOTE: Check the range
statement. You have to use it, because you have to declare each value for your state machine. So you have to use when others
or reduce the integer to 2 bits only. Otherwise you have to declare 2^32 - 1
states.
所以你至少需要一个带有 type <YourType> is ...
的类型声明来声明你的自定义类型和一个使用你的类型的信号(上面例子中的 CurrentState
)。
除了状态机中的状态,枚举类型还有许多其他用途。
您可以将它们用作数组、循环变量等中的索引类型。例如,
type channel is (R,G,B);
Colour : array(channel) of byte;
constant Black : Colour := (R => 0, G => 0, B => 0);
signal VGA_Out : Colour;
-- in a process
for c in channel loop
VGA_Out(c) <= A(c) + B(c); -- mix video signals A and B
end loop;
等等
VHDL 中的类型枚举是什么? 我在哪里可以使用它来使代码更短更易于理解? 例如,考虑下面的语句:
TYPE st_State IS (st_Idle, st_CheckHeader1, st_CheckHeader2, st_ReceiveData)
什么时候必须使用它。
您的示例只是名称为 st_State
的类型的声明,该类型包含四个元素。每个元素都有一个从 0
到 Elements - 1
的数字。这类似于 C typedef
和 C enum
.
请查看 this 说明以获取更详细的信息。
一个典型的应用是状态机来命名不同的状态:
architecture Top_Arch of Top is
type State_Type is (S0, S1, S2, S3);
signal CurrentState : State_Type := S0;
begin
process(Clock)
begin
if(rising_edge(Clock)) then
case CurrentState is
when S0 => ...
when S1 => ...
when S2 => ...
when S3 => ...
end case;
end if;
end Top_Arch;
使用此方法可产生更易读和更清晰的代码,但它等同于此方法(未经测试):
architecture Top_Arch of Top is
signal CurrentState : INTEGER RANGE 0 to 3 := 0;
begin
process(Clock)
begin
if(rising_edge(Clock)) then
case CurrentState is
when 0 => ...
when 1 => ...
when 2 => ...
when 3 => ...
end case;
end if;
end Top_Arch;
NOTE: Check the
range
statement. You have to use it, because you have to declare each value for your state machine. So you have to usewhen others
or reduce the integer to 2 bits only. Otherwise you have to declare2^32 - 1
states.
所以你至少需要一个带有 type <YourType> is ...
的类型声明来声明你的自定义类型和一个使用你的类型的信号(上面例子中的 CurrentState
)。
除了状态机中的状态,枚举类型还有许多其他用途。
您可以将它们用作数组、循环变量等中的索引类型。例如,
type channel is (R,G,B);
Colour : array(channel) of byte;
constant Black : Colour := (R => 0, G => 0, B => 0);
signal VGA_Out : Colour;
-- in a process
for c in channel loop
VGA_Out(c) <= A(c) + B(c); -- mix video signals A and B
end loop;
等等