forked from buccolo/MIPS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalu.vhd
154 lines (126 loc) · 4.07 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
library ieee;
use ieee.numeric_std.all;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity ALU is
generic(
W : natural := 32;
Cw : natural := 3
);
port(
SrcA : in std_logic_vector(W-1 downto 0);
SrcB : in std_logic_vector(W-1 downto 0);
AluControl : in std_logic_vector(3 downto 0);
AluResult : out std_logic_vector(W-1 downto 0);
Zero : out std_logic;
Overflow : out std_logic;
CarryOut : out std_logic
);
end ALU;
-- +------------------------+
-- | Lista de Operacoes |
-- +------------------------+
-- | 0000 | A & B |
-- | 0001 | A | B |
-- | 0010 | A + B |
-- | 0011 | Not used |
-- | 0100 | A & not B |
-- | 0101 | A | not B |
-- | 0110 | A - B |
-- | 0111 | SLT |
-- | 1000 | A XOR B |
-- | 1001 | A SLTU B |
-- +------------------------+
architecture arc_alu of ALU is begin
process (AluControl, SrcA, SrcB)
variable Big: std_logic_vector(32 downto 0);
variable TempLogic: std_logic_vector(31 downto 0);
variable SrcBNeg: std_logic_vector(31 downto 0);
begin
-- A AND B --------------------------------------------------------------------------------------
if(AluControl = "0000") then
TempLogic := SrcA AND SrcB;
AluResult <= TempLogic;
if (TempLogic = "00000000000000000000000000000000") then
Zero <= '1';
else
Zero <= '0';
end if;
-- A OR B --------------------------------------------------------------------------------------
elsif (AluControl = "0001") then
TempLogic := SrcA OR SrcB;
AluResult <= TempLogic;
if (TempLogic = "00000000000000000000000000000000") then
Zero <= '1';
else
Zero <= '0';
end if;
-- A + B --------------------------------------------------------------------------------------
elsif (AluControl = "0010") then
Big := ( '0' & SrcA ) + ( '0' & SrcB );
CarryOut <= Big(32);
Overflow <= Big(32) xor SrcA(31) xor SrcB(31) xor Big(31);
--Overflow <= ( SrcA(31) xnor SrcB(31) ) and ( SrcA(31) xor Big(31) );
AluResult <= Big(31 downto 0);
if (Big(31 downto 0) = "00000000000000000000000000000000") then
Zero <= '1';
else
Zero <= '0';
end if;
-- A AND NOT B --------------------------------------------------------------------------------------
elsif (AluControl = "0100") then
TempLogic := SrcA AND NOT SrcB;
AluResult <= TempLogic;
if (TempLogic = "00000000000000000000000000000000") then
Zero <= '1';
else
Zero <= '0';
end if;
-- A OR NOT B --------------------------------------------------------------------------------------
elsif (AluControl = "0101") then
TempLogic := SrcA OR NOT SrcB;
AluResult <= TempLogic;
if (TempLogic = "00000000000000000000000000000000") then
Zero <= '1';
else
Zero <= '0';
end if;
-- A - B --------------------------------------------------------------------------------------
elsif (AluControl = "0110") then
-- Transforma o B em C2
SrcBNeg := ("11111111111111111111111111111111" - SrcB) + "00000000000000000000000000000001";
-- Faz a soma normalmente :)
Big := ( '0' & SrcA ) + ( '0' & SrcBNeg );
CarryOut <= Big(32);
Overflow <= Big(32) xor SrcA(31) xor SrcBNeg(31) xor Big(31);
--Overflow <= ( SrcA(31) xnor SrcBNeg(31) ) and ( SrcA(31) xor Big(31) );
AluResult <= Big(31 downto 0);
if (Big(31 downto 0) = "00000000000000000000000000000000") then
Zero <= '1';
else
Zero <= '0';
end if;
-- A SLT B --------------------------------------------------------------------------------------
elsif (AluControl = "0111") then
if (SrcA < SrcB) then
AluResult <= SrcA;
if (SrcA = "00000000000000000000000000000000") then
Zero <= '1';
else
Zero <= '0';
end if;
else
AluResult <= SrcB;
if (SrcB = "00000000000000000000000000000000") then
Zero <= '1';
else
Zero <= '0';
end if;
end if;
-- A XOR B --------------------------------------------------------------------------------------
elsif (AluControl = "1000") then
AluResult <= SrcA XOR SrcB;
end if;
end process;
end;