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

标签 arrays if-statement comparison vhdl

如何以更好的方式编写此 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;

最佳答案

case 语句可用于与多个值进行比较,然后 caseothers 部分可用作 “其他”,例如:

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;

另一种方法是使用带有值的 std_logic_vector数组,然后创建一个函数来确定 data_in 值是否等于数组中的任一值。然后,typefunction 声明可以位于 architectureprocess 声明部分。 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;

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

关于arrays - 如何构建 if 语句并与各种值进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34500700/

相关文章:

c++ - 将 Byte[]{LSB-0,MSB-0,LSB-1,MSB-1...LSB-N,MSB-N} 转换为 Int[](int-0,int-1,int-N)

python - 如果用户输入他们想再次玩,我如何才能在 python 中选择你自己的冒险故事来重新开始?

c++ - if 中的 OR 语句是否只检查一个条件?平衡支架

php - 3个不同的等于

python - 如何在 Python 中正确检查对象类型?

c# - 比较两个日期值时如何忽略时间部分?

c - 带有 for 循环的数组会出现段错误

javascript - 在javascript中的数组中添加项目

java - 更改 boolean 数组中的值。选择地方而不是值(value)

C++ 帮助 : loops and switches