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

Subversion Repositories nanoblaze

[/] [nanoblaze/] [trunk/] [Circuit/] [alu.vhd] - Blame information for rev 11

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 10 fcorthay
--##############################################################################
2
--
3
--  alu
4
--      The processor ALU
5
--
6
--      Arithmetic and logic unit.
7
--
8
--------------------------------------------------------------------------------
9
--
10
--  Versions / Authors
11
--      1.0 Francois Corthay    first implementation
12
--
13
--  Provided under GNU LGPL licence: <http://www.gnu.org/copyleft/lesser.html>
14
--
15
--  by the electronics group of "HES-SO//Valais Wallis", in Switzerland:
16
--  <http://www.hevs.ch/en/rad-instituts/institut-systemes-industriels/>.
17
--
18
--------------------------------------------------------------------------------
19
--
20
--  Hierarchy
21
--      Used by "nanoblaze/nanoProcessor/aluAndRegisters".
22
--
23
--##############################################################################
24
 
25
LIBRARY ieee;
26
  USE ieee.std_logic_1164.all;
27
  USE ieee.numeric_std.all;
28
 
29
ENTITY alu IS
30
  GENERIC(
31
    aluCodeBitNb : positive := 5;
32
    dataBitNb    : positive := 8
33
  );
34
  PORT(
35
    aluCode : IN  std_ulogic_vector(aluCodeBitNb-1 DOWNTO 0);
36
    opA     : IN  signed(dataBitNb-1 DOWNTO 0);
37
    opB     : IN  signed(dataBitNb-1 DOWNTO 0);
38
    cIn     : IN  std_ulogic;
39
    aluOut  : OUT signed(dataBitNb-1 DOWNTO 0);
40
    cOut    : OUT std_ulogic;
41
    zero    : OUT std_ulogic
42
  );
43
END alu ;
44
 
45
--==============================================================================
46
 
47
ARCHITECTURE RTL OF alu IS
48
 
49
  signal aluCodeInt: unsigned(aluCode'range);
50
  signal aArith: signed(opA'high+1 downto 0);
51
  signal bArith: signed(opA'high+1 downto 0);
52
  signal cInArith: signed(1 downto 0);
53
  signal cInShift: std_ulogic;
54
  signal yArith: signed(aluOut'high+1 downto 0);
55
  signal aluOutInt: signed(aluOut'range);
56
 
57
BEGIN
58
  ------------------------------------------------------------------------------
59
                                      -- clear aluCode don't care LSB for shifts
60
  aluCodeInt(aluCode'high downto 1) <= unsigned(aluCode(aluCode'high downto 1));
61
 
62
  cleanupLsb: process(aluCode)
63
  begin
64
    if aluCode(aluCode'high) = '1' then
65
      aluCodeInt(0) <= '0';
66
    else
67
      aluCodeInt(0) <= aluCode(0);
68
    end if;
69
  end process cleanupLsb;
70
 
71
  ------------------------------------------------------------------------------
72
                                             -- values for arithmetic operations
73
  aArith <= signed(resize(unsigned(opA), aArith'length));
74
  bArith <= signed(resize(unsigned(opB), bArith'length));
75 11 fcorthay
  cInArith <= (0 => cIn, others => '0');
76 10 fcorthay
 
77
  process(aluCode, cIn, opA)
78
  begin
79
    case aluCode(2 downto 1) is
80
      when "00"   => cInShift <= cIn;
81
      when "01"   => cInShift <= opA(opA'high);
82
      when "10"   => cInShift <= opA(opA'low);
83
      when "11"   => cInShift <= aluCode(0);
84
      when others => cInShift <= '-';
85
    end case;
86
  end process;
87
 
88
  ------------------------------------------------------------------------------
89
                                                               -- alu operations
90
  aluOperation: process(
91
    aluCodeInt,
92
    opA, opB,
93
    aArith, bArith, cInArith,
94
    cInShift,
95 11 fcorthay
    yArith, aluOutInt
96 10 fcorthay
  )
97
    variable xorAcc: std_ulogic;
98
  begin
99
    yArith <= (others => '-');
100
    cOut   <= '-';
101
    aluOutInt <= (others => '-');
102
    case to_integer(aluCodeInt) is
103
      when  0 =>                                        -- LOAD sX, kk
104
        aluOutInt <= opB;
105
      when  2 =>                                        -- INPUT sX, pp
106
        aluOutInt <= opB;
107
      when  3 =>                                        -- FETCH sX, ss
108
        aluOutInt <= opB;
109
      when  5 =>                                        -- AND sX, kk
110
        aluOutInt <= opA and opB;
111
        cOut      <= '0';
112
      when  6 =>                                        -- OR sX, kk
113
        aluOutInt <= opA or opB;
114
        cOut      <= '0';
115
      when  7 =>                                        -- XOR sX, kk
116
        aluOutInt <= opA xor opB;
117
        cOut      <= '0';
118
      when  9 =>                                        -- TEST sX, kk
119
        aluOutInt <= opA and opB;
120
        xorAcc := '0';
121
        for index in aluOutInt'range loop
122
          xorAcc := xorAcc xor aluOutInt(index);
123
        end loop;
124
        cOut      <= xorAcc;
125
      when 10 =>                                        -- COMPARE sX, kk
126
        yArith    <= aArith - bArith;
127
        aluOutInt <= yArith(aluOut'range);
128
        cOut      <= yArith(yArith'high);
129
      when 12 =>                                        -- ADD sX, kk
130
        yArith    <= aArith + bArith;
131
        aluOutInt <= yArith(aluOut'range);
132
        cOut      <= yArith(yArith'high);
133
      when 13 =>                                        -- ADDCY sX, kk
134
        yArith    <= (aArith + bArith) + cInArith;
135
        aluOutInt <= yArith(aluOut'range);
136
        cOut      <= yArith(yArith'high);
137
      when 14 =>                                        -- SUB sX, kk
138
        yArith    <= aArith - bArith;
139
        aluOutInt <= yArith(aluOut'range);
140
        cOut      <= yArith(yArith'high);
141
      when 15 =>                                        -- SUBCY sX, kk
142
        yArith    <= (aArith - bArith) - cInArith;
143
        aluOutInt <= yArith(aluOut'range);
144
        cOut      <= yArith(yArith'high);
145
      when 16 to 23 =>                                  -- SL sX
146
        aluOutInt <= opA(opA'high-1 downto 0) & cInShift;
147
        cOut      <= opA(opA'high);
148
      when 24 to 31 =>                                  -- SR sX
149
        aluOutInt <= cInShift & opA(opA'high downto 1);
150
        cOut      <= opA(0);
151
      when others =>
152
        aluOutInt <= (others => '-');
153
    end case;
154
  end process aluOperation;
155
 
156
  aluOut <= aluOutInt;
157
  zero <= '1' when aluOutInt = 0 else '0';
158
 
159
END ARCHITECTURE RTL;

powered by: WebSVN 2.1.0

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