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

Subversion Repositories astron_adder

[/] [astron_adder/] [trunk/] [tb_common_adder_tree.vhd] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 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
-- Usage:
23
-- > as 10
24
-- > run -all
25
-- . Observe in_data_arr_p and the expected result and the result of the DUT in the Wave window
26
-- . This TB verifies the DUT architecture that was compile last. Default after a fresh mk the (str)
27
--   is compiled last, to simulate the (recursive) manually compile it and the simulate again.
28
--   Within the recursive architecture it is not possible to explicitely configure it to recursively 
29
--   use the recursive architecture using FOR ALL : ENTITY because the instance label is within a
30
--   generate block.
31
-- . The p_verify makes the tb self checking and asserts when the results are not equal
32
 
33
LIBRARY IEEE, common_pkg_lib, common_components_lib;
34
USE IEEE.std_logic_1164.ALL;
35
USE IEEE.numeric_std.ALL;
36
USE common_pkg_lib.common_pkg.ALL;
37
USE common_pkg_lib.tb_common_pkg.ALL;
38
 
39
 
40
ENTITY tb_common_adder_tree IS
41
  GENERIC (
42
    g_representation : STRING  := "SIGNED";
43
    g_pipeline       : NATURAL := 1;  -- amount of pipelining per stage
44
    g_nof_inputs     : NATURAL := 31;  -- >= 1
45
    g_symbol_w       : NATURAL := 8;
46
    g_sum_w          : NATURAL := 8  -- worst case bit growth requires g_symbol_w + ceil_log2(g_nof_inputs);
47
  );
48
END tb_common_adder_tree;
49
 
50
 
51
ARCHITECTURE tb OF tb_common_adder_tree IS
52
 
53
  CONSTANT clk_period      : TIME := 10 ns;
54
 
55
  CONSTANT c_data_vec_w    : NATURAL := g_nof_inputs*g_symbol_w;
56
  CONSTANT c_nof_stages    : NATURAL := ceil_log2(g_nof_inputs);
57
 
58
  CONSTANT c_pipeline_tree : NATURAL := g_pipeline*c_nof_stages;
59
 
60
  TYPE t_symbol_arr IS ARRAY (INTEGER RANGE <>) OF STD_LOGIC_VECTOR(g_symbol_w-1 DOWNTO 0);
61
 
62
  -- Use the same symbol value g_nof_inputs time in the data_vec
63
  FUNCTION func_data_vec(symbol : INTEGER) RETURN STD_LOGIC_VECTOR IS
64
    VARIABLE v_data_vec : STD_LOGIC_VECTOR(c_data_vec_w-1 DOWNTO 0);
65
  BEGIN
66
    FOR I IN 0 TO g_nof_inputs-1 LOOP
67
      v_data_vec((I+1)*g_symbol_w-1 DOWNTO I*g_symbol_w) := TO_UVEC(symbol, g_symbol_w);
68
    END LOOP;
69
    RETURN v_data_vec;
70
  END;
71
 
72
  -- Calculate the expected result of the sum of the symbols in the data_vec
73
  FUNCTION func_result(data_vec : STD_LOGIC_VECTOR) RETURN STD_LOGIC_VECTOR IS
74
    VARIABLE v_result : INTEGER;
75
  BEGIN
76
    v_result := 0;
77
    IF g_representation="SIGNED" THEN
78
      FOR I IN 0 TO g_nof_inputs-1 LOOP
79
        v_result := v_result + TO_SINT(data_vec((I+1)*g_symbol_w-1 DOWNTO I*g_symbol_w));
80
      END LOOP;
81
      v_result := RESIZE_SINT(v_result, g_sum_w);
82
      RETURN TO_SVEC(v_result, g_sum_w);
83
    ELSE
84
      FOR I IN 0 TO g_nof_inputs-1 LOOP
85
        v_result := v_result + TO_UINT(data_vec((I+1)*g_symbol_w-1 DOWNTO I*g_symbol_w));
86
      END LOOP;
87
      v_result := RESIZE_UINT(v_result, g_sum_w);
88
      RETURN TO_UVEC(v_result, g_sum_w);
89
    END IF;
90
  END;
91
 
92
  SIGNAL rst                : STD_LOGIC;
93
  SIGNAL clk                : STD_LOGIC := '1';
94
  SIGNAL tb_end             : STD_LOGIC := '0';
95
 
96
  SIGNAL result_comb        : STD_LOGIC_VECTOR(g_sum_w-1 DOWNTO 0);  -- expected combinatorial sum
97
 
98
  SIGNAL in_data_vec        : STD_LOGIC_VECTOR(c_data_vec_w-1 DOWNTO 0) := (OTHERS=>'0');
99
  SIGNAL in_data_vec_p      : STD_LOGIC_VECTOR(c_data_vec_w-1 DOWNTO 0);
100
  SIGNAL in_data_arr_p      : t_symbol_arr(0 TO g_nof_inputs-1);
101
 
102
  SIGNAL result_expected    : STD_LOGIC_VECTOR(g_sum_w-1 DOWNTO 0);  -- expected pipelined sum
103
  SIGNAL result_dut         : STD_LOGIC_VECTOR(g_sum_w-1 DOWNTO 0);  -- DUT sum
104
 
105
BEGIN
106
 
107
  clk <= NOT clk OR tb_end AFTER clk_period/2;
108
  rst <= '1', '0' AFTER clk_period*3;
109
 
110
  p_stimuli : PROCESS
111
  BEGIN
112
    in_data_vec <= (OTHERS=>'0');
113
    proc_common_wait_until_low(clk, rst);
114
    proc_common_wait_some_cycles(clk, 5);
115
 
116
    -- Apply equal symbol value inputs
117
    FOR I IN 0 TO 2**g_symbol_w-1 LOOP
118
      in_data_vec <= func_data_vec(I);
119
      proc_common_wait_some_cycles(clk, 1);
120
    END LOOP;
121
    in_data_vec <= (OTHERS=>'0');
122
    proc_common_wait_some_cycles(clk, 50);
123
    tb_end <= '1';
124
 
125
    WAIT;
126
  END PROCESS;
127
 
128
  -- For easier manual analysis in the wave window:
129
  -- . Pipeline the in_data_vec to align with the result
130
  -- . Map the concatenated symbols in in_data_vec into an in_data_arr_p array 
131
  u_data_vec_p : ENTITY common_components_lib.common_pipeline
132
  GENERIC MAP (
133
    g_representation => g_representation,
134
    g_pipeline       => c_pipeline_tree,
135
    g_reset_value    => 0,
136
    g_in_dat_w       => c_data_vec_w,
137
    g_out_dat_w      => c_data_vec_w
138
  )
139
  PORT MAP (
140
    rst     => rst,
141
    clk     => clk,
142
    clken   => '1',
143
    in_dat  => in_data_vec,
144
    out_dat => in_data_vec_p
145
  );
146
 
147
  p_data_arr : PROCESS(in_data_vec_p)
148
  BEGIN
149
    FOR I IN 0 TO g_nof_inputs-1 LOOP
150
      in_data_arr_p(I) <= in_data_vec_p((I+1)*g_symbol_w-1 DOWNTO I*g_symbol_w);
151
    END LOOP;
152
  END PROCESS;
153
 
154
  result_comb <= func_result(in_data_vec);
155
 
156
  u_result : ENTITY common_components_lib.common_pipeline
157
  GENERIC MAP (
158
    g_representation => g_representation,
159
    g_pipeline       => c_pipeline_tree,
160
    g_reset_value    => 0,
161
    g_in_dat_w       => g_sum_w,
162
    g_out_dat_w      => g_sum_w
163
  )
164
  PORT MAP (
165
    rst     => rst,
166
    clk     => clk,
167
    clken   => '1',
168
    in_dat  => result_comb,
169
    out_dat => result_expected
170
  );
171
 
172
  -- Using work.common_adder_tree(recursive) will only invoke the recursive architecture once, because the next recursive level will default to using the last compiled architecture
173
  -- Therefore only instatiatiate the DUT once in this tb and use compile order to influence which architecture is used.
174
  dut : ENTITY work.common_adder_tree  -- uses last compile architecture
175
  GENERIC MAP (
176
    g_representation => g_representation,
177
    g_pipeline       => g_pipeline,
178
    g_nof_inputs     => g_nof_inputs,
179
    g_dat_w          => g_symbol_w,
180
    g_sum_w          => g_sum_w
181
  )
182
  PORT MAP (
183
    clk    => clk,
184
    in_dat => in_data_vec,
185
    sum    => result_dut
186
  );
187
 
188
  p_verify : PROCESS(rst, clk)
189
  BEGIN
190
    IF rst='0' THEN
191
      IF rising_edge(clk) THEN
192
        ASSERT result_dut = result_expected REPORT "Error: wrong result_dut" SEVERITY ERROR;
193
      END IF;
194
    END IF;
195
  END PROCESS;
196
 
197
END tb;

powered by: WebSVN 2.1.0

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