| 1 |
2 |
danv |
-------------------------------------------------------------------------------
|
| 2 |
|
|
--
|
| 3 |
|
|
-- Copyright (C) 2009
|
| 4 |
|
|
-- ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
|
| 5 |
|
|
-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
|
| 6 |
|
|
--
|
| 7 |
|
|
-- This program is free software: you can redistribute it and/or modify
|
| 8 |
|
|
-- it under the terms of the GNU General Public License as published by
|
| 9 |
|
|
-- the Free Software Foundation, either version 3 of the License, or
|
| 10 |
|
|
-- (at your option) any later version.
|
| 11 |
|
|
--
|
| 12 |
|
|
-- This program is distributed in the hope that it will be useful,
|
| 13 |
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 14 |
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 15 |
|
|
-- GNU General Public License for more details.
|
| 16 |
|
|
--
|
| 17 |
|
|
-- You should have received a copy of the GNU General Public License
|
| 18 |
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
| 19 |
|
|
--
|
| 20 |
|
|
-------------------------------------------------------------------------------
|
| 21 |
|
|
|
| 22 |
|
|
LIBRARY IEEE, common_pkg_lib, common_components_lib;
|
| 23 |
|
|
USE IEEE.std_logic_1164.ALL;
|
| 24 |
|
|
USE common_pkg_lib.common_pkg.ALL;
|
| 25 |
|
|
|
| 26 |
|
|
ENTITY common_add_sub IS
|
| 27 |
|
|
GENERIC (
|
| 28 |
|
|
g_direction : STRING := "ADD"; -- or "SUB", or "BOTH" and use sel_add
|
| 29 |
|
|
g_representation : STRING := "SIGNED"; -- or "UNSIGNED", important if g_out_dat_w > g_in_dat_w, not relevant if g_out_dat_w = g_in_dat_w
|
| 30 |
|
|
g_pipeline_input : NATURAL := 0; -- 0 or 1
|
| 31 |
|
|
g_pipeline_output : NATURAL := 1; -- >= 0
|
| 32 |
|
|
g_in_dat_w : NATURAL := 8;
|
| 33 |
|
|
g_out_dat_w : NATURAL := 9 -- only support g_out_dat_w=g_in_dat_w and g_out_dat_w=g_in_dat_w+1
|
| 34 |
|
|
);
|
| 35 |
|
|
PORT (
|
| 36 |
|
|
clk : IN STD_LOGIC;
|
| 37 |
|
|
clken : IN STD_LOGIC := '1';
|
| 38 |
|
|
sel_add : IN STD_LOGIC := '1'; -- only used for g_direction "BOTH"
|
| 39 |
|
|
in_a : IN STD_LOGIC_VECTOR(g_in_dat_w-1 DOWNTO 0);
|
| 40 |
|
|
in_b : IN STD_LOGIC_VECTOR(g_in_dat_w-1 DOWNTO 0);
|
| 41 |
|
|
result : OUT STD_LOGIC_VECTOR(g_out_dat_w-1 DOWNTO 0)
|
| 42 |
|
|
);
|
| 43 |
|
|
END common_add_sub;
|
| 44 |
|
|
|
| 45 |
|
|
ARCHITECTURE str OF common_add_sub IS
|
| 46 |
|
|
|
| 47 |
|
|
CONSTANT c_res_w : NATURAL := g_in_dat_w+1;
|
| 48 |
|
|
|
| 49 |
|
|
SIGNAL in_a_p : STD_LOGIC_VECTOR(in_a'RANGE);
|
| 50 |
|
|
SIGNAL in_b_p : STD_LOGIC_VECTOR(in_b'RANGE);
|
| 51 |
|
|
|
| 52 |
|
|
SIGNAL in_add : STD_LOGIC;
|
| 53 |
|
|
SIGNAL sel_add_p : STD_LOGIC;
|
| 54 |
|
|
|
| 55 |
|
|
SIGNAL result_p : STD_LOGIC_VECTOR(c_res_w-1 DOWNTO 0);
|
| 56 |
|
|
|
| 57 |
|
|
BEGIN
|
| 58 |
|
|
|
| 59 |
|
|
in_add <= '1' WHEN g_direction="ADD" OR (g_direction="BOTH" AND sel_add='1') ELSE '0';
|
| 60 |
|
|
|
| 61 |
|
|
no_input_reg : IF g_pipeline_input=0 GENERATE -- wired input
|
| 62 |
|
|
in_a_p <= in_a;
|
| 63 |
|
|
in_b_p <= in_b;
|
| 64 |
|
|
sel_add_p <= in_add;
|
| 65 |
|
|
END GENERATE;
|
| 66 |
|
|
gen_input_reg : IF g_pipeline_input>0 GENERATE -- register input
|
| 67 |
|
|
p_reg : PROCESS(clk)
|
| 68 |
|
|
BEGIN
|
| 69 |
|
|
IF rising_edge(clk) THEN
|
| 70 |
|
|
IF clken='1' THEN
|
| 71 |
|
|
in_a_p <= in_a;
|
| 72 |
|
|
in_b_p <= in_b;
|
| 73 |
|
|
sel_add_p <= in_add;
|
| 74 |
|
|
END IF;
|
| 75 |
|
|
END IF;
|
| 76 |
|
|
END PROCESS;
|
| 77 |
|
|
END GENERATE;
|
| 78 |
|
|
|
| 79 |
|
|
gen_signed : IF g_representation = "SIGNED" GENERATE
|
| 80 |
|
|
result_p <= ADD_SVEC(in_a_p, in_b_p, c_res_w) WHEN sel_add_p='1' ELSE SUB_SVEC(in_a_p, in_b_p, c_res_w);
|
| 81 |
|
|
END GENERATE;
|
| 82 |
|
|
gen_unsigned : IF g_representation = "UNSIGNED" GENERATE
|
| 83 |
|
|
result_p <= ADD_UVEC(in_a_p, in_b_p, c_res_w) WHEN sel_add_p='1' ELSE SUB_UVEC(in_a_p, in_b_p, c_res_w);
|
| 84 |
|
|
END GENERATE;
|
| 85 |
|
|
|
| 86 |
|
|
u_output_pipe : ENTITY common_components_lib.common_pipeline -- pipeline output
|
| 87 |
|
|
GENERIC MAP (
|
| 88 |
|
|
g_representation => g_representation,
|
| 89 |
|
|
g_pipeline => g_pipeline_output, -- 0 for wires, >0 for register stages
|
| 90 |
|
|
g_in_dat_w => result'LENGTH,
|
| 91 |
|
|
g_out_dat_w => result'LENGTH
|
| 92 |
|
|
)
|
| 93 |
|
|
PORT MAP (
|
| 94 |
|
|
clk => clk,
|
| 95 |
|
|
clken => clken,
|
| 96 |
|
|
in_dat => result_p(result'RANGE),
|
| 97 |
|
|
out_dat => result
|
| 98 |
|
|
);
|
| 99 |
|
|
|
| 100 |
|
|
END str;
|