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 ieee.numeric_std.ALL;
|
25 |
|
|
USE common_pkg_lib.common_pkg.ALL;
|
26 |
|
|
|
27 |
|
|
ENTITY common_round IS
|
28 |
|
|
|
29 |
|
|
--
|
30 |
|
|
-- ISE XST results for rounding 36b --> 18b:
|
31 |
|
|
-- int clip --> slices FFs LUTs
|
32 |
|
|
-- 1) signed TRUE 63 54 80 -- increases with input widths > 18b
|
33 |
|
|
-- 2) signed FALSE 59 54 73 -- increases with input widths > 18b
|
34 |
|
|
-- 3) unsigned TRUE 34 37 43 -- same for all input widths > 18b
|
35 |
|
|
-- 4) unsigned FALSE 21 37 19 -- same for all input widths > 18b
|
36 |
|
|
--
|
37 |
|
|
-- If the input comes from a product and is rounded to the input width then g_round_clip can safely be FALSE, because e.g. for unsigned
|
38 |
|
|
-- 4b*4b=8b->4b the maximum product is 15*15=225 <= 255-8, so wrapping will never occur.
|
39 |
|
|
--
|
40 |
|
|
|
41 |
|
|
GENERIC (
|
42 |
|
|
g_representation : STRING := "SIGNED"; -- SIGNED (round +-0.5 away from zero to +- infinity) or UNSIGNED rounding (round 0.5 up to + inifinity)
|
43 |
|
|
g_round : BOOLEAN := TRUE; -- when TRUE round the input, else truncate the input
|
44 |
|
|
g_round_clip : BOOLEAN := FALSE; -- when TRUE clip rounded input >= +max to avoid wrapping to output -min (signed) or 0 (unsigned)
|
45 |
|
|
g_pipeline_input : NATURAL := 0; -- >= 0
|
46 |
|
|
g_pipeline_output : NATURAL := 1; -- >= 0, use g_pipeline_input=0 and g_pipeline_output=0 for combinatorial output
|
47 |
|
|
g_in_dat_w : NATURAL := 36;
|
48 |
|
|
g_out_dat_w : NATURAL := 18
|
49 |
|
|
);
|
50 |
|
|
PORT (
|
51 |
|
|
clk : IN STD_LOGIC;
|
52 |
|
|
clken : IN STD_LOGIC := '1';
|
53 |
|
|
in_dat : IN STD_LOGIC_VECTOR(g_in_dat_w-1 DOWNTO 0);
|
54 |
|
|
out_dat : OUT STD_LOGIC_VECTOR(g_out_dat_w-1 DOWNTO 0)
|
55 |
|
|
);
|
56 |
|
|
END;
|
57 |
|
|
|
58 |
|
|
|
59 |
|
|
ARCHITECTURE rtl OF common_round IS
|
60 |
|
|
|
61 |
|
|
CONSTANT c_remove_w : INTEGER := g_in_dat_w-g_out_dat_w;
|
62 |
|
|
|
63 |
|
|
SIGNAL reg_dat : STD_LOGIC_VECTOR(in_dat'RANGE);
|
64 |
|
|
SIGNAL res_dat : STD_LOGIC_VECTOR(out_dat'RANGE);
|
65 |
|
|
|
66 |
|
|
BEGIN
|
67 |
|
|
|
68 |
|
|
u_input_pipe : ENTITY common_components_lib.common_pipeline
|
69 |
|
|
GENERIC MAP (
|
70 |
|
|
g_representation => g_representation,
|
71 |
|
|
g_pipeline => g_pipeline_input,
|
72 |
|
|
g_in_dat_w => g_in_dat_w,
|
73 |
|
|
g_out_dat_w => g_in_dat_w
|
74 |
|
|
)
|
75 |
|
|
PORT MAP (
|
76 |
|
|
clk => clk,
|
77 |
|
|
clken => clken,
|
78 |
|
|
in_dat => in_dat,
|
79 |
|
|
out_dat => reg_dat
|
80 |
|
|
);
|
81 |
|
|
|
82 |
|
|
-- Increase to out_dat width
|
83 |
|
|
no_s : IF c_remove_w<=0 AND g_representation="SIGNED" GENERATE
|
84 |
|
|
res_dat <= RESIZE_SVEC(reg_dat, g_out_dat_w);
|
85 |
|
|
END GENERATE;
|
86 |
|
|
no_u : IF c_remove_w<=0 AND g_representation="UNSIGNED" GENERATE
|
87 |
|
|
res_dat <= RESIZE_UVEC(reg_dat, g_out_dat_w);
|
88 |
|
|
END GENERATE;
|
89 |
|
|
|
90 |
|
|
-- Decrease to out_dat width by c_remove_w number of LSbits
|
91 |
|
|
-- . rounding
|
92 |
|
|
gen_s : IF c_remove_w>0 AND g_round=TRUE AND g_representation="SIGNED" GENERATE
|
93 |
|
|
res_dat <= s_round(reg_dat, c_remove_w, g_round_clip);
|
94 |
|
|
END GENERATE;
|
95 |
|
|
gen_u : IF c_remove_w>0 AND g_round=TRUE AND g_representation="UNSIGNED" GENERATE
|
96 |
|
|
res_dat <= u_round(reg_dat, c_remove_w, g_round_clip);
|
97 |
|
|
END GENERATE;
|
98 |
|
|
-- . truncating
|
99 |
|
|
gen_t : IF c_remove_w>0 AND g_round=FALSE GENERATE
|
100 |
|
|
res_dat <= truncate(reg_dat, c_remove_w);
|
101 |
|
|
END GENERATE;
|
102 |
|
|
|
103 |
|
|
u_output_pipe : ENTITY common_components_lib.common_pipeline
|
104 |
|
|
GENERIC MAP (
|
105 |
|
|
g_representation => g_representation,
|
106 |
|
|
g_pipeline => g_pipeline_output,
|
107 |
|
|
g_in_dat_w => g_out_dat_w,
|
108 |
|
|
g_out_dat_w => g_out_dat_w
|
109 |
|
|
)
|
110 |
|
|
PORT MAP (
|
111 |
|
|
clk => clk,
|
112 |
|
|
clken => clken,
|
113 |
|
|
in_dat => res_dat,
|
114 |
|
|
out_dat => out_dat
|
115 |
|
|
);
|
116 |
|
|
|
117 |
|
|
END rtl;
|