1 |
2 |
danv |
-------------------------------------------------------------------------------
|
2 |
|
|
--
|
3 |
3 |
danv |
-- Copyright 2020
|
4 |
2 |
danv |
-- ASTRON (Netherlands Institute for Radio Astronomy) <http://www.astron.nl/>
|
5 |
|
|
-- P.O.Box 2, 7990 AA Dwingeloo, The Netherlands
|
6 |
3 |
danv |
--
|
7 |
|
|
-- Licensed under the Apache License, Version 2.0 (the "License");
|
8 |
|
|
-- you may not use this file except in compliance with the License.
|
9 |
|
|
-- You may obtain a copy of the License at
|
10 |
|
|
--
|
11 |
|
|
-- http://www.apache.org/licenses/LICENSE-2.0
|
12 |
|
|
--
|
13 |
|
|
-- Unless required by applicable law or agreed to in writing, software
|
14 |
|
|
-- distributed under the License is distributed on an "AS IS" BASIS,
|
15 |
|
|
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16 |
|
|
-- See the License for the specific language governing permissions and
|
17 |
|
|
-- limitations under the License.
|
18 |
2 |
danv |
--
|
19 |
|
|
-------------------------------------------------------------------------------
|
20 |
|
|
|
21 |
|
|
LIBRARY IEEE, common_pkg_lib, common_components_lib;
|
22 |
|
|
USE IEEE.std_logic_1164.ALL;
|
23 |
|
|
USE common_pkg_lib.common_pkg.ALL;
|
24 |
|
|
|
25 |
|
|
ENTITY common_add_sub IS
|
26 |
|
|
GENERIC (
|
27 |
|
|
g_direction : STRING := "ADD"; -- or "SUB", or "BOTH" and use sel_add
|
28 |
|
|
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
|
29 |
|
|
g_pipeline_input : NATURAL := 0; -- 0 or 1
|
30 |
|
|
g_pipeline_output : NATURAL := 1; -- >= 0
|
31 |
|
|
g_in_dat_w : NATURAL := 8;
|
32 |
|
|
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
|
33 |
|
|
);
|
34 |
|
|
PORT (
|
35 |
|
|
clk : IN STD_LOGIC;
|
36 |
|
|
clken : IN STD_LOGIC := '1';
|
37 |
|
|
sel_add : IN STD_LOGIC := '1'; -- only used for g_direction "BOTH"
|
38 |
|
|
in_a : IN STD_LOGIC_VECTOR(g_in_dat_w-1 DOWNTO 0);
|
39 |
|
|
in_b : IN STD_LOGIC_VECTOR(g_in_dat_w-1 DOWNTO 0);
|
40 |
|
|
result : OUT STD_LOGIC_VECTOR(g_out_dat_w-1 DOWNTO 0)
|
41 |
|
|
);
|
42 |
|
|
END common_add_sub;
|
43 |
|
|
|
44 |
|
|
ARCHITECTURE str OF common_add_sub IS
|
45 |
|
|
|
46 |
|
|
CONSTANT c_res_w : NATURAL := g_in_dat_w+1;
|
47 |
|
|
|
48 |
|
|
SIGNAL in_a_p : STD_LOGIC_VECTOR(in_a'RANGE);
|
49 |
|
|
SIGNAL in_b_p : STD_LOGIC_VECTOR(in_b'RANGE);
|
50 |
|
|
|
51 |
|
|
SIGNAL in_add : STD_LOGIC;
|
52 |
|
|
SIGNAL sel_add_p : STD_LOGIC;
|
53 |
|
|
|
54 |
|
|
SIGNAL result_p : STD_LOGIC_VECTOR(c_res_w-1 DOWNTO 0);
|
55 |
|
|
|
56 |
|
|
BEGIN
|
57 |
|
|
|
58 |
|
|
in_add <= '1' WHEN g_direction="ADD" OR (g_direction="BOTH" AND sel_add='1') ELSE '0';
|
59 |
|
|
|
60 |
|
|
no_input_reg : IF g_pipeline_input=0 GENERATE -- wired input
|
61 |
|
|
in_a_p <= in_a;
|
62 |
|
|
in_b_p <= in_b;
|
63 |
|
|
sel_add_p <= in_add;
|
64 |
|
|
END GENERATE;
|
65 |
|
|
gen_input_reg : IF g_pipeline_input>0 GENERATE -- register input
|
66 |
|
|
p_reg : PROCESS(clk)
|
67 |
|
|
BEGIN
|
68 |
|
|
IF rising_edge(clk) THEN
|
69 |
|
|
IF clken='1' THEN
|
70 |
|
|
in_a_p <= in_a;
|
71 |
|
|
in_b_p <= in_b;
|
72 |
|
|
sel_add_p <= in_add;
|
73 |
|
|
END IF;
|
74 |
|
|
END IF;
|
75 |
|
|
END PROCESS;
|
76 |
|
|
END GENERATE;
|
77 |
|
|
|
78 |
|
|
gen_signed : IF g_representation = "SIGNED" GENERATE
|
79 |
|
|
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);
|
80 |
|
|
END GENERATE;
|
81 |
|
|
gen_unsigned : IF g_representation = "UNSIGNED" GENERATE
|
82 |
|
|
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);
|
83 |
|
|
END GENERATE;
|
84 |
|
|
|
85 |
|
|
u_output_pipe : ENTITY common_components_lib.common_pipeline -- pipeline output
|
86 |
|
|
GENERIC MAP (
|
87 |
|
|
g_representation => g_representation,
|
88 |
|
|
g_pipeline => g_pipeline_output, -- 0 for wires, >0 for register stages
|
89 |
|
|
g_in_dat_w => result'LENGTH,
|
90 |
|
|
g_out_dat_w => result'LENGTH
|
91 |
|
|
)
|
92 |
|
|
PORT MAP (
|
93 |
|
|
clk => clk,
|
94 |
|
|
clken => clken,
|
95 |
|
|
in_dat => result_p(result'RANGE),
|
96 |
|
|
out_dat => result
|
97 |
|
|
);
|
98 |
|
|
|
99 |
|
|
END str;
|