This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alu.vhd
46 lines (39 loc) · 1.79 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
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity alu is
generic (width : integer);
port (
In1 : in std_logic_vector(width-1 downto 0);
In2 : in std_logic_vector(width-1 downto 0);
Sel : in std_logic_vector(2 downto 0);
Result : out std_logic_vector(width-1 downto 0);
NZVC : out std_logic_vector(3 downto 0)
);
end entity;
architecture alu_arch of alu is
signal intermediate : std_logic_vector(width downto 0);
begin
with Sel select
intermediate <=
-- Addition
std_logic_vector(unsigned('0' & In1) + unsigned('0' & In2)) when "000",
-- Subtraction
std_logic_vector(unsigned('0' & In1) - unsigned('0' & In2)) when "001",
-- Logic AND
'0' & (In1 and In2) when "010",
-- Logic OR
'0' & (In1 or In2) when "011",
-- Increment
std_logic_vector(unsigned('0' & In1) + to_unsigned(1, In1'length+1)) when "100",
-- Decrement
std_logic_vector(unsigned('0' & In1) - to_unsigned(1, In1'length+1)) when "101",
-- Other (invalid) operation: return zero
std_logic_vector(to_unsigned(0, intermediate'length)) when others;
-- Result generation from intermediate value
Result <= intermediate(intermediate'high-1 downto 0);
NZVC(3) <= Result(Result'high);
NZVC(2) <= '1' when Result = std_logic_vector(to_unsigned(0, Result'length)) else '0';
NZVC(1) <= '1' when In1(In1'high) = In2(In2'high) and In1(In1'high) /= Result(Result'high) else '0';
NZVC(0) <= intermediate(intermediate'high);
end architecture;