-
Notifications
You must be signed in to change notification settings - Fork 0
/
ALU.vhd
231 lines (206 loc) · 4.99 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
use ieee.std_logic_unsigned.all;
entity ALU is
Port ( SEL : in STD_LOGIC_VECTOR (3 downto 0);
A : in STD_LOGIC_VECTOR (8 downto 0);
B : in STD_LOGIC_VECTOR (8 downto 0);
Cin : in STD_LOGIC;
RESULT : out STD_LOGIC_VECTOR (7 downto 0);
C : out STD_LOGIC;
Z : out STD_LOGIC);
end ALU;
architecture Behavioral of ALU is
signal overflow_res : std_logic_vector(8 downto 0) := "000000000";
begin
sel_proc : process(SEL, A, B, Cin, overflow_res) is begin
case SEL is
when "0000" => --ADD operation
overflow_res <= std_logic_vector(unsigned(A) + unsigned(B));
C <= overflow_res(8);
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "0001" => --AddC
overflow_res <= (Cin + std_logic_vector(unsigned(A) + unsigned(B)));
C <= overflow_res(8);
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "0010" => --SUB
overflow_res <= std_logic_vector(unsigned(A) - unsigned(B));
C <= overflow_res(8);
if overflow_res = "00000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "0011" => --SUBC
overflow_res <= (std_logic_vector(unsigned(A) - unsigned(B)) - Cin);
C <= overflow_res(8);
if overflow_res = "00000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "0100" => --CMP
overflow_res <= std_logic_vector(unsigned(A) - unsigned(B));
C <= overflow_res(8);
if overflow_res = "00000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= "00000000";
when "0101" => --AND
overflow_res(8) <= '0';
overflow_res <= (A and B);
C <= '0';
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "0110" => --OR
C <= '0';
overflow_res(8) <= '0';
overflow_res <= (A or B);
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "0111" => --EXOR
overflow_res(8) <= '0';
overflow_res <= (A xor B);
C <= '0';
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "1000" => --TEST
overflow_res(8) <= '0';
overflow_res <= (A and B);
C <= '0';
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= "00000000";
when "1001" => --LSL
C <= A(7);
overflow_res(8) <= '0';
overflow_res(7) <= A(6);
overflow_res(6) <= A(5);
overflow_res(5) <= A(4);
overflow_res(4) <= A(3);
overflow_res(3) <= A(2);
overflow_res(2) <= A(1);
overflow_res(1) <= A(0);
overflow_res(0) <= Cin;
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "1010" => --LSR
C <= A(0);
overflow_res(8) <= '0';
overflow_res(7) <= Cin;
overflow_res(6) <= A(7);
overflow_res(5) <= A(6);
overflow_res(4) <= A(5);
overflow_res(3) <= A(4);
overflow_res(2) <= A(3);
overflow_res(1) <= A(2);
overflow_res(0) <= A(1);
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "1011" => --ROL
C <= A(7);
overflow_res(8) <= '0';
overflow_res(7) <= A(6);
overflow_res(6) <= A(5);
overflow_res(5) <= A(4);
overflow_res(4) <= A(3);
overflow_res(3) <= A(2);
overflow_res(2) <= A(1);
overflow_res(1) <= A(0);
overflow_res(0) <= A(7);
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "1100" => --ROR
C <= A(0);
overflow_res(8) <= '0';
overflow_res(7) <= A(0);
overflow_res(6) <= A(7);
overflow_res(5) <= A(6);
overflow_res(4) <= A(5);
overflow_res(3) <= A(4);
overflow_res(2) <= A(3);
overflow_res(1) <= A(2);
overflow_res(0) <= A(1);
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "1101" => --ASR
C <= A(0);
overflow_res(8) <= '0';
overflow_res(7) <= A(7);
overflow_res(6) <= A(7);
overflow_res(5) <= A(6);
overflow_res(4) <= A(5);
overflow_res(3) <= A(4);
overflow_res(2) <= A(3);
overflow_res(1) <= A(2);
overflow_res(0) <= A(1);
if overflow_res = "000000000" then
Z <= '1';
else
Z <= '0';
end if;
RESULT <= overflow_res(7 downto 0);
when "1110" => --MOV
RESULT <= B(7 downto 0);
C <= '0'; --Don't care! C_LD should be low
Z <= '0'; --Don't care! Z_LD should be low
overflow_res <= "000000000";
when "1111" => --unused
C <= '0';
Z <= '0';
overflow_res <= "000000000";
RESULT <= "00000000";
when others =>
C <= '0';
Z <= '0';
overflow_res <= "000000000";
RESULT <= "00000000";
end case;
end process sel_proc;
end Behavioral;