📄 算术逻辑单元.txt
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity alu is
port
(a: in unsigned(3 downto 0);
b: in unsigned(3 downto 0);
cin: in std_logic;
s: in std_logic_vector(2 downto 0);
bcdout: out std_logic_vector(3 downto 0);
cout: out std_logic);
end alu;
architecture behavor of alu is
signal c,y: std_logic_vector(3 downto 0);
signal q: unsigned(3 downto 0);
begin
process(a,b,cin,s)
begin
case s is
when "000"=>
y(0)<=a(0) xor b(0) xor cin;
c(0)<=(a(0) and b(0)) or (b(0) and cin) or (a(0) and cin);
gen1:for i in 1 to 3 loop
y(i)<=a(i) xor b(i) xor c(i-1);
c(i)<=(c(i-1) and a(i)) or (c(i-1) and b(i)) or (a(i) and b(i));
end loop;
bcdout<=y(3)&y(2)&y(1)&y(0);
cout<=c(3);
when "001"=>
y(0)<=a(0) xor b(0) xor cin;
c(0)<=(cin and not a(0)) or (cin and b(0)) or (not a(0) and b(0));
gen2:for i in 1 to 3 loop
y(i)<=a(i) xor b(i) xor c(i-1);
c(i)<=(c(i-1) and not a(i)) or (c(i-1) and b(i)) or (not a(i) and b(i));
end loop;
bcdout<=y(3)&y(2)&y(1)&y(0);
cout<=c(3);
when "010"=>
if cin='0' then
bcdout <=a(3)&a(2)&a(1)&a(0);
else
bcdout <=a+1;
end if;
cout<='0';
when "011"=>
if cin='0' then
bcdout <=a(3)&a(2)&a(1)&a(0);
else
bcdout <=a-1;
end if;
cout<='0';
when "100"=>
y(3)<=a(3) and b(3);
y(2)<=a(2) and b(2);
y(1)<=a(1) and b(1);
y(0)<=a(0) and b(0);
bcdout<=y(3)&y(2)&y(1)&y(0);
when "101"=>
y(3)<=a(3) or b(3);
y(2)<=a(2) or b(2);
y(1)<=a(1) or b(1);
y(0)<=a(0) or b(0);
bcdout<=y(3)&y(2)&y(1)&y(0);
when "110"=>
y(3)<=a(3) xor b(3);
y(2)<=a(2) xor b(2);
y(1)<=a(1) xor b(1);
y(0)<=a(0) xor b(0);
q<=y(3)&y(2)&y(1)&y(0);
bcdout<=y(3)&y(2)&y(1)&y(0);
when "111"=>
y(3)<=not a(3);
y(2)<=not a(2);
y(1)<=not a(1);
y(0)<=not a(0);
bcdout<=y(3)&y(2)&y(1)&y(0);
when others=>
bcdout<="0000";
cout<='0';
end case;
end process;
end behavor;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -