如何以更好的方式编写此 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
语句可用于与多个值进行比较,然后 case
的 others
部分可用作 “其他”
,例如:
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
值是否等于数组中的任一值。然后,type
和 function
声明可以位于 architecture
或 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;
替代方法需要一些声明,但更普遍适用。
关于arrays - 如何构建 if 语句并与各种值进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34500700/