-
Notifications
You must be signed in to change notification settings - Fork 5
/
alu.vhd
82 lines (71 loc) · 2.5 KB
/
alu.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
Entity alu is
Generic(W : natural := 8; Cw : natural := 3);
port(SrcA : in std_logic_vector(W-1 downto 0);
SrcB : in std_logic_vector(W-1 downto 0);
Cin : in std_logic;
AluControl : in std_logic_vector(Cw-1 downto 0);
AluResult : out std_logic_vector(W-1 downto 0);
Zero : out std_logic;
Overflow : out std_logic;
CarryOut : out std_logic;
Neg : out std_logic --negative signal
);
End alu;
Architecture RTL of alu is
begin
process (AluControl, srca, srcb, cin)
Variable Y : std_logic_vector(W downto 0);
Variable Aux : std_logic_vector(2 downto 0);
Variable my_zero : std_logic_vector(W-1 downto 0);
begin
Y := "000000000";
my_zero := "00000000";
Aux := AluControl;
case Aux is
when "000" => Y := '0' & SrcB; -- returns the second source
when "001" => Y := ('0' & SrcA) + ('0' & SrcB) + ("00000000" & Cin); -- add sources with cin
when "010" => Y := ('0' & SrcA) + ('0' & SrcB); -- add sources lonely
when "011" => Y := '0' & (SrcA - SrcB); -- sub
when "100" => Y := '0' & (SrcA and SrcB); -- and
when "101" => Y := '0' & (SrcA or SrcB); -- or
when "111" =>
if (SrcA < SrcB) then
Y := "000000001";
else
Y := Y;
end if;
when others => NULL;
end case;
--Check if it was Negative
if Y < 0 then
Neg <= '1';
else
Neg <= '0';
end if;
-- Check if it was Zero
if Y = 0 then
Zero <= '1';
else
Zero <= '0';
end if;
-- Check if there was overflow
if ((SrcA > my_zero AND SrcB > my_zero)
AND (Y < ('0' & my_zero))) OR ((SrcA < my_zero AND SrcB < my_zero) AND (Y > ('0' & my_zero))) then
Overflow <= '1';
else
Overflow <= '0';
end if;
-- Check if there was CarryOut
if Y(8) = '1' then
CarryOut <= '1';
else
CarryOut <= '0';
end if;
-- Final Result
AluResult <= Y(7 downto 0);
end process;
end RTL;