如何构建 if 语句并与各种值进行比较?

2024-06-19

我该怎么写这个if以更好的方式声明条件?

if ((data_in(8 downto 1)=x"70") or (data_in(8 downto 1)=x"69") or 
    (data_in(8 downto 1)=x"72") or (data_in(8 downto 1)=x"7A") or
    (data_in(8 downto 1)=x"6B") or (data_in(8 downto 1)=x"73") or
    (data_in(8 downto 1)=x"74") or (data_in(8 downto 1)=x"6C") or
    (data_in(8 downto 1)=x"75") or (data_in(8 downto 1)=x"7D")) then
      data_make_code <= data_in (8 downto 1); -- enter key to buffer
      wrong_data <='0';
      cnt_bit :=0;
      -- if valid key then
      current_state <= break_code_receive; 
elsif
 ...
end if;

A case语句可用于比较多个值,并且others的一部分case然后可以用作"else", like:

case data_in(8 downto 1) is
  when x"70" | x"69" | x"72" | x"7A" | x"6B" |
       x"73" | x"74" | x"6C" | x"75" | x"7D" =>
    ...  -- if part of code
  when others =>
    ...  -- else part of code
end case;

另一种方法是使用array of std_logic_vector与值,然后创建一个可以确定是否data_invalue 等于数组中的任意一个值。这type and function声明可以在architecture or process声明部分。 VHDL-2008 中的代码如下所示:

type slv_array is array (natural range <>) of std_logic_vector;

function in_array(val : std_logic_vector; set : slv_array) return boolean is
begin
  for idx in set'range loop
    if val = set(idx) then
      return TRUE;
    end if;
  end loop;
  return FALSE;
end function;

...

if in_array(data_in, (x"70", x"69", x"72", x"7A", x"6B", 
                      x"73", x"74", x"6C", x"75", x"7D")) then
  ...  -- if part of code
else
  ...  -- else part of code
end if;

替代方法需要一些声明,但更普遍适用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何构建 if 语句并与各种值进行比较? 的相关文章

随机推荐