修改程序,用条件赋值语句代替实现原由if语句完成的程序功能。 library ieee; Use ieee.std_logic_1164.all; Entity encoder4_2 is Port( I0,I1,I2,I3 : in std_logic; Y1,Y0 : out std_logic); End encoder4_2; Architecture rtl of encoder4_2 is Begin Process (I0,I1,I2,I3) Begin if i3='1' then y1<='1'y0<='1' elsif i2='1' then y1<='1'y0<='0' elsif i1='1' then y1<='0'y0<='1' elsif i0='1' then y1<='0'y0<='0' else y1<=‘x'y0<=‘x' end if; end process; end rtl;
时间: 2024-01-01 17:04:45 浏览: 30
以下是使用条件赋值语句代替if语句完成的程序:
```vhdl
library ieee;
use ieee.std_logic_1164.all;
entity encoder4_2 is
port (
I0, I1, I2, I3: in std_logic;
Y1, Y0: out std_logic
);
end encoder4_2;
architecture rtl of encoder4_2 is
begin
process (I0, I1, I2, I3)
begin
Y1 <= (not I3) and (not I2) when I1 = '0' and I0 = '0' else
(not I3) and I2 when I1 = '0' and I0 = '1' else
I3 and (not I2) when I1 = '1' and I0 = '0' else
I3 and I2 when I1 = '1' and I0 = '1' else
'X';
Y0 <= (not I3) and (not I2) when I1 = '0' and I0 = '0' else
(not I3) and (not I0) when I1 = '0' and I0 = '1' else
(not I1) and (not I0) when I1 = '1' and I0 = '0' else
(not I1) and I2 when I1 = '1' and I0 = '1' else
'X';
end process;
end rtl;
```
该程序使用了条件赋值语句的形式,将if语句转换为逻辑表达式的形式。例如,原来的if语句:
```vhdl
if i3 = '1' then
y1 <= '1';
y0 <= '1';
elsif i2 = '1' then
y1 <= '1';
y0 <= '0';
elsif i1 = '1' then
y1 <= '0';
y0 <= '1';
elsif i0 = '1' then
y1 <= '0';
y0 <= '0';
else
y1 <= 'X';
y0 <= 'X';
end if;
```
转换为条件赋值语句后的形式:
```vhdl
Y1 <= (not I3) and (not I2) when I1 = '0' and I0 = '0' else
(not I3) and I2 when I1 = '0' and I0 = '1' else
I3 and (not I2) when I1 = '1' and I0 = '0' else
I3 and I2 when I1 = '1' and I0 = '1' else
'X';
Y0 <= (not I3) and (not I2) when I1 = '0' and I0 = '0' else
(not I3) and (not I0) when I1 = '0' and I0 = '1' else
(not I1) and (not I0) when I1 = '1' and I0 = '0' else
(not I1) and I2 when I1 = '1' and I0 = '1' else
'X';
```
每个条件赋值语句由三部分组成:条件表达式、then关键字和结果表达式。条件表达式由逻辑运算符(如and、or、not)和信号组成,用于判断条件是否成立;结果表达式由信号、常量或运算表达式组成,表示条件成立时的结果。当条件表达式成立时,结果表达式的值将被赋给信号。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)