OpenCores
URL https://opencores.org/ocsvn/opencpu32/opencpu32/trunk

Subversion Repositories opencpu32

[/] [opencpu32/] [trunk/] [hdl/] [opencpu32/] [Alu.vhd] - Blame information for rev 42

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 leonardoar
--! @file
2
--! @brief Arithmetic logic unit http://en.wikipedia.org/wiki/Arithmetic_logic_unit
3
 
4 8 leonardoar
--! Use standard library and import the packages (std_logic_1164,std_logic_unsigned,std_logic_arith)
5 5 leonardoar
library IEEE;
6 8 leonardoar
use ieee.std_logic_1164.all;
7
use ieee.std_logic_unsigned.all;
8
use ieee.std_logic_arith.all;
9 5 leonardoar
 
10
--! Use CPU Definitions package
11 8 leonardoar
use work.pkgOpenCPU32.all;
12 5 leonardoar
 
13
--! ALU is a digital circuit that performs arithmetic and logical operations.
14
 
15
--! ALU is a digital circuit that performs arithmetic and logical operations. It's the fundamental part of the CPU
16
entity Alu is
17 28 leonardoar
    generic (n : integer := nBits - 1);                                         --! Generic value (Used to easily change the size of the Alu on the package)
18
         Port ( A : in  STD_LOGIC_VECTOR (n downto 0);                   --! Alu Operand 1
19
           B : in  STD_LOGIC_VECTOR (n downto 0);                        --! Alu Operand 2
20
           S : out  STD_LOGIC_VECTOR (n downto 0);                       --! Alu Output
21
                          flagsOut : out STD_LOGIC_VECTOR(2 downto 0);   --! Flags from current operation
22
           sel : in  aluOps);                                                                           --! Select operation
23 5 leonardoar
end Alu;
24
 
25 9 leonardoar
--! @brief Arithmetic logic unit, refer to this page for more information http://en.wikipedia.org/wiki/Arithmetic_logic_unit
26
--! @details This circuit will be excited by the control unit to perfom some arithimetic, or logic operation (Depending on the opcode selected)
27 10 leonardoar
--! \n You can see some samples on the Internet: http://www.vlsibank.com/sessionspage.asp?titl_id=12222
28 5 leonardoar
architecture Behavioral of Alu is
29
 
30
begin
31 9 leonardoar
        --! Behavior description of combinational circuit (Can not infer any FF(Flip flop)) of the Alu
32 8 leonardoar
        process (A,B,sel) is
33 17 leonardoar
        variable mulResult : std_logic_vector(((nBits*2) - 1)downto 0);
34 28 leonardoar
        variable FLAG_CARRY, FLAG_ZERO , FLAG_SIGN : STD_LOGIC;
35
        variable intermediate_S : STD_LOGIC_VECTOR(nBits downto 0);      -- One more bit to detect overflows...
36 8 leonardoar
        begin
37
                case sel is
38 16 leonardoar
                        when alu_pass =>
39
                                --Pass operation
40 28 leonardoar
                                intermediate_S := '0' & A;
41 16 leonardoar
 
42 20 leonardoar
                        when alu_passB =>
43
                                --Pass operation
44 28 leonardoar
                                intermediate_S := '0' & B;
45 20 leonardoar
 
46 8 leonardoar
                        when alu_sum =>
47 9 leonardoar
                                --Sum operation
48 28 leonardoar
                                intermediate_S := ('0' & A) + ('0' & B);
49 8 leonardoar
 
50
                        when alu_sub =>
51 9 leonardoar
                                --Subtraction operation
52 28 leonardoar
                                intermediate_S := ('0' & A) - ('0' & B);
53 8 leonardoar
 
54
                        when alu_inc =>
55 9 leonardoar
                                --Increment operation
56 28 leonardoar
                                intermediate_S := ('0' & A) + conv_std_logic_vector(1, nBits);
57 8 leonardoar
 
58
                        when alu_dec =>
59 9 leonardoar
                                --Decrement operation
60 28 leonardoar
                                intermediate_S := ('0' & A) - conv_std_logic_vector(1, nBits);
61 9 leonardoar
 
62
                        when alu_mul =>
63
                                --Multiplication operation
64 17 leonardoar
                                mulResult := A * B;
65 28 leonardoar
                                intermediate_S := mulResult(nBits downto 0);
66 8 leonardoar
 
67
                        when alu_and =>
68 9 leonardoar
                                --And operation
69 28 leonardoar
                                intermediate_S := '0' & (A and B);
70 8 leonardoar
 
71
                        when alu_or =>
72 9 leonardoar
                                --Or operation
73 28 leonardoar
                                intermediate_S := '0' & (A or B);
74 8 leonardoar
 
75
                        when alu_xor =>
76 9 leonardoar
                                --Xor operation
77 28 leonardoar
                                intermediate_S := '0' & (A xor B);
78 9 leonardoar
 
79
                        when alu_not =>
80
                                --Not operation
81 28 leonardoar
                                intermediate_S := not ('0' & A);
82
 
83
                        when alu_shfLt =>
84
                                -- Shift left operand A (Get current value bring to left and add a zero to the right)
85
                                intermediate_S := '0' & (A((A'HIGH - 1) downto 0) & '0');  -- "&" is the concatenate operator
86
 
87
                        when alu_shfRt =>
88
                                -- Shift right operand A (Add a zero to the left and copy the current value to the right)
89
                                intermediate_S := '0' & ('0' & A(A'HIGH downto 1));       -- "&" is the concatenate operator
90
 
91
                        when alu_roRt =>
92
                                -- Rotate right operand A (Get the lowest bit of A, and concatenate with the others bits, taking out the latest one...)
93
                                intermediate_S := '0' & (A(A'LOW) & A(A'HIGH downto 1)); -- If A is (7 downto 0) A'LOW is 0, and A'HIGH is 7
94
 
95
                        when alu_roLt =>
96
                                -- Rotate left operand A (Get the the bits from the second highest and concatenate in the end with the highest one...)
97
                                intermediate_S := '0' & (A((A'HIGH - 1) downto 0) & A(A'HIGH));
98 8 leonardoar
 
99
                        when others =>
100 28 leonardoar
                                intermediate_S := (others => 'Z');
101
                end case;
102
 
103
                -- Get flags
104
                if (intermediate_S = 0) then
105
                        FLAG_ZERO := '1';
106
                else
107
                        FLAG_ZERO := '0';
108
                end if;
109
                FLAG_SIGN := intermediate_S(intermediate_S'HIGH - 1);
110
                FLAG_CARRY := intermediate_S(intermediate_S'HIGH);
111
 
112
                -- Pass output
113
                S <= intermediate_S(S'RANGE); -- S'RANGE == S(31 downto 0);
114
                flagsOut <= FLAG_SIGN & FLAG_ZERO & FLAG_CARRY;
115 8 leonardoar
        end process;
116 5 leonardoar
 
117
end Behavioral;
118
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.