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

Subversion Repositories artificial_neural_network

[/] [artificial_neural_network/] [trunk/] [ANN_kernel/] [RTL_VHDL_files/] [adder_tree.vhd] - Blame information for rev 8

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

Line No. Rev Author Line
1 3 ojosynariz
----------------------------------------------------------------------------------
2
-- Company:
3
-- Engineer:
4
--
5
-- Create Date:    15:27:42 06/20/2013
6
-- Design Name:    Configurable ANN
7
-- Module Name:    adder_tree - Behavioral
8
-- Project Name:
9
-- Target Devices:
10
-- Tool versions:
11
-- Description: Recursive adder tree
12
--
13
-- Dependencies:
14
--
15
-- Revision:
16
-- Revision 0.01 - File Created
17
-- Additional Comments:
18
--
19
----------------------------------------------------------------------------------
20
 
21
library IEEE;
22
use IEEE.STD_LOGIC_1164.ALL;
23
use ieee.numeric_std.all;
24
 
25
 
26
entity adder_tree is
27
   generic
28
   (
29
      NumIn   : integer := 9;  -- Number of inputs
30
      Nbit    : integer := 12  -- Bit width of the input data
31
   );
32
 
33
   port
34
   (
35
      -- Input ports
36
      reset    : in  std_logic;
37
      clk      : in  std_logic;
38
      en       : in  std_logic; -- Enable
39
      inputs   : in  std_logic_vector((Nbit*NumIn)-1 downto 0); -- Input data
40
 
41
      -- Output ports
42
      en_out   : out std_logic; -- Output enable (output data validation)
43
      output   : out std_logic_vector(Nbit-1 downto 0) -- Output of the tree adder
44
   );
45
end adder_tree;
46
 
47
architecture Behavioral of adder_tree is
48
 
49 8 jstefanowi
 
50
 
51
   component adder_tree is
52
      generic
53
      (
54
         NumIn   : integer := 9;  -- Number of inputs
55
         Nbit    : integer := 12  -- Bit width of the input data
56
      );
57
 
58
      port
59
      (
60
         -- Input ports
61
         reset    : in  std_logic;
62
         clk      : in  std_logic;
63
         en       : in  std_logic; -- Enable
64
         inputs   : in  std_logic_vector((Nbit*NumIn)-1 downto 0); -- Input data
65
 
66
         -- Output ports
67
         en_out   : out std_logic; -- Output enable (output data validation)
68
         output   : out std_logic_vector(Nbit-1 downto 0) -- Output of the tree adder
69
      );
70
   end component;
71
 
72 3 ojosynariz
   constant NumIn2 : integer := NumIn/2; -- Number of imputs of the next adder tree layer
73
 
74
   signal next_en : std_logic := '0'; -- Next adder tree layer enable
75
   signal res : std_logic_vector((Nbit*((NumIn2)+(NumIn mod 2)))-1 downto 0); -- Partial results
76
 
77
   signal resL_reg : std_logic_vector((Nbit*NumIn2)-1 downto 0);
78
   signal resH_reg : std_logic_vector(Nbit-1 downto 0);
79
begin
80
 
81
-- Additions:
82
add_proc:
83
   process (clk) -- Synchronous to allow pipeline
84
   begin
85
      if (clk'event and clk = '1') then
86
         if (reset = '1') then
87
            resL_reg <= (others => '0');
88
         else
89
            if (en = '1') then
90
               -- Addition of inputs (2*i y 2*i+1), resulting in NumIn/2 outputs of this layer of the adder tree:
91
               for i in ((NumIn2)-1) downto 0 loop
92
                  resL_reg((Nbit*(i+1))-1 downto Nbit*i) <= std_logic_vector( signed(inputs((Nbit*((2*i)+1))-1 downto Nbit*2*i)) + signed(inputs((Nbit*((2*i)+2))-1 downto Nbit*((2*i)+1))) );
93
               end loop;
94
            end if;
95
         end if;
96
      end if;
97
   end process;
98
 
99
   res((Nbit*NumIn2)-1 downto 0) <= resL_reg;
100
 
101
-- Register the uneven input (if needed):
102
uneven_register:
103
   if (NumIn mod 2 = 1) generate
104
      process (clk)
105
      begin
106
         if (clk'event and clk = '1') then
107
            if (reset = '1') then
108
               resH_reg <= (others => '0');
109
            else
110
               if (en = '1') then
111
                  resH_reg <= inputs((Nbit*NumIn)-1 downto Nbit*(NumIn-1));
112
               end if;
113
            end if;
114
         end if;
115
      end process;
116
      res((Nbit*((NumIn2)+1))-1 downto Nbit*(NumIn2)) <= resH_reg;
117
   end generate;
118
 
119
   process (clk)
120
   begin
121
      if (clk'event and clk = '1') then
122
         if reset = '1' then
123
            next_en <= '0';
124
         else
125
            next_en <= en; -- Enable is delayed 1 cycle for the next layer of the adder tree
126
         end if;
127
      end if;
128
   end process;
129
 
130
recursion:
131
   if (NumIn > 2) generate
132
 
133 8 jstefanowi
      sub_adder_tree: adder_tree
134 3 ojosynariz
         generic map
135
         (
136
            NumIn => (NumIn2)+(NumIn mod 2),
137
            Nbit  => Nbit
138
         )
139
         port map
140
         (
141
            clk    => clk,
142
            reset  => reset,
143
            en     => next_en,
144
            inputs => res,
145
            en_out => en_out,
146
            output => output -- Solution is passed from the sub-adder trees to the top adder tree
147
         );
148
   end generate;
149
 
150
trivial_solution:
151
   if (NumIn = 2) generate
152
      en_out <= next_en;
153
      output <= res; -- Assign the final result to the adder tree output
154
   end generate;
155
 
156
end Behavioral;
157
 

powered by: WebSVN 2.1.0

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