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

Subversion Repositories viterbi_decoder_axi4s

[/] [viterbi_decoder_axi4s/] [trunk/] [testbench/] [tb_dec_viterbi.vhd] - Blame information for rev 4

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

Line No. Rev Author Line
1 2 mfehrenz
--!
2
--! Copyright (C) 2011 - 2012 Creonic GmbH
3
--!
4
--! This file is part of the Creonic Viterbi Decoder, which is distributed
5
--! under the terms of the GNU General Public License version 2.
6
--!
7
--! @file
8
--! @brief  Generic Viterbi Decoder Testbench
9
--! @author Markus Fehrenz
10
--! @date   2011/12/05
11
--!
12
 
13
library ieee;
14
use ieee.std_logic_1164.all;
15
use ieee.numeric_std.all;
16
use std.textio.all;
17
 
18
library dec_viterbi;
19
use dec_viterbi.pkg_param.all;
20
use dec_viterbi.pkg_param_derived.all;
21
use dec_viterbi.pkg_types.all;
22
use dec_viterbi.pkg_helper.all;
23
use dec_viterbi.pkg_tb_fileio.all;
24
use dec_viterbi.txt_util.all;
25
 
26
 
27
entity tb_dec_viterbi is
28
        generic(
29
                CLK_PERIOD         : time    := 10 ns;   -- Clock period within simulation.
30
 
31
                BLOCK_LENGTH_START : natural := 200;     -- First block length to simulate.
32
                BLOCK_LENGTH_END   : natural := 300;     -- Last block length to simulate.
33 4 mfehrenz
                BLOCK_LENGTH_INCR  : integer := 100;       -- Increment from one block length to another.
34 2 mfehrenz
 
35
                SIM_ALL_BLOCKS     : boolean := true;  -- Set to true in order to simulate all blocks within a data file.
36
                SIM_BLOCK_START    : natural := 396;      -- If SIM_ALL_BLOCKS = false, gives block to start simulation with.
37
                SIM_BLOCK_END      : natural := 398;      -- If SIM_ALL_BLOCKS = false, gives last block of simulation.
38
 
39
                WINDOW_LENGTH      : natural := 55;     -- Window length to use for simulation.
40
                ACQUISITION_LENGTH : natural := 50;     -- Acquisition length to use for simulation.
41
 
42
                DATA_DIRECTORY     : string  := "../testbench/" -- Path to testbench data, relative to simulation directory.
43
        );
44
end entity tb_dec_viterbi;
45
 
46
 
47
architecture sim of tb_dec_viterbi is
48
 
49
        component dec_viterbi is
50
        port(
51
                aclk      : in std_logic;
52
                aresetn   : in std_logic;
53
 
54
                s_axis_input_tvalid : in std_logic;
55
                s_axis_input_tdata  : in std_logic_vector(31 downto 0);
56
                s_axis_input_tlast  : in std_logic;
57
                s_axis_input_tready : out std_logic;
58
 
59
                m_axis_output_tvalid : out std_logic;
60
                m_axis_output_tdata  : out std_logic;
61
                m_axis_output_tlast  : out std_logic;
62
                m_axis_output_tready : in std_logic;
63
 
64
                s_axis_ctrl_tvalid : in std_logic;
65
                s_axis_ctrl_tdata  : in std_logic_vector(31 downto 0);
66
                s_axis_ctrl_tlast  : in std_logic;
67
                s_axis_ctrl_tready : out std_logic
68
        );
69
        end component dec_viterbi;
70
 
71
        signal clk     : std_logic := '0';
72
        signal aresetn : std_logic;
73
 
74
        signal m_axis_input_tvalid : std_logic;
75
        signal m_axis_input_tlast  : std_logic;
76
        signal m_axis_input_tready : std_logic;
77
        signal m_axis_input_tdata  : std_logic_vector(31 downto 0);
78
 
79
        signal s_axis_output_tvalid : std_logic;
80
        signal s_axis_output_tlast  : std_logic;
81
        signal s_axis_output_tready : std_logic;
82
        signal s_axis_output_tdata  : std_logic;
83
 
84
        signal m_axis_ctrl_tvalid : std_logic;
85
        signal m_axis_ctrl_tlast  : std_logic;
86
        signal m_axis_ctrl_tready : std_logic;
87
        signal m_axis_ctrl_tdata  : std_logic_vector(31 downto 0);
88
 
89
 
90
        --
91
        -- Input data send signals.
92
        --
93
 
94
        type t_send_data_fsm is (READ_FILE, CONFIGURE, SEND_DATA, SEND_DATA_FINISHED);
95
        signal send_data_fsm : t_send_data_fsm;
96
 
97
        signal block_send_end   : natural;
98
 
99
        signal current_block             : natural;
100
        signal current_block_length      : natural;
101
        signal current_block_length_tail : natural;
102
 
103
 
104
        --
105
        -- Output comparison signals.
106
        --
107
 
108
        signal sys_bit_counter : natural;
109
        signal decoded_hardware : std_logic_vector(0 to max(BLOCK_LENGTH_START, BLOCK_LENGTH_END));
110
 
111
        signal block_receive_complete : boolean;
112
        signal new_block_length       : boolean;
113
 
114
        signal first_block_out            : natural;
115
        signal last_block_out             : natural;
116
        signal current_block_out          : natural;
117
        signal current_block_length_out   : natural;
118
        signal current_block_length_out_d : natural;
119
        signal sys_bit_counter_out        : natural;
120
 
121
 
122
        -- Get filename that matches to our current configuration.
123
        function get_filename_part(v_block_length       : natural;
124
                                   v_window_length      : natural;
125
                                   v_acquisition_length : natural) return string is
126
 
127
        begin
128
                return "BL_" & str(v_block_length) & "_WL_" & str(v_window_length) & "_AL_" & str(v_acquisition_length);
129
        end function get_filename_part;
130
 
131
        shared variable v_decoded_software : t_nat_array_ptr;
132
 
133
begin
134
 
135
        clk <= not clk after CLK_PERIOD / 2;
136
 
137
 
138
        -- initial reset
139
        pr_reset : process is
140
        begin
141
                aresetn <= '0';
142
                wait for 2 * CLK_PERIOD;
143
                aresetn <= '1';
144
                wait;
145
        end process;
146
 
147
 
148
        -- Configuration and sending data to the core.
149
        pr_send : process(clk) is
150
                variable v_llr          : t_int_array_ptr;
151
                variable v_filepart_ptr : t_string_ptr;
152
                variable v_filename_ptr : t_string_ptr;
153
                variable v_num_lines    : natural := 0;
154
                variable v_num_blocks   : natural := 0;
155
                variable v_current_block_length : natural := 0;
156
                variable v_sys_bit_counter : integer := 0;
157
        begin
158
        if rising_edge(clk) then
159
                if aresetn = '0' then
160
 
161
                        --  ctrl_tlast is present but unused in the decoder
162
                        m_axis_ctrl_tlast  <= '0';
163
                        m_axis_ctrl_tvalid <= '0';
164
                        m_axis_ctrl_tdata  <= (others => '0');
165
 
166
                        m_axis_input_tlast  <= '0';
167
                        m_axis_input_tvalid <= '0';
168
                        m_axis_input_tdata  <= (others => '0');
169
 
170
                        current_block_length      <= BLOCK_LENGTH_START;
171
                        current_block_length_tail <= BLOCK_LENGTH_START + ENCODER_MEMORY_DEPTH;
172
                        current_block    <= 0;
173
                        block_send_end   <= 0;
174
                        sys_bit_counter  <= 0;
175
 
176
                        send_data_fsm <= READ_FILE;
177
 
178
                else
179
 
180
                        case send_data_fsm is
181
 
182
 
183
                                --
184
                                -- For each block length we have a different file as reference data.
185
                                -- Read it when all requested blocks of a file were simulated,
186
                                -- or when we start the simulation.
187
                                --
188
                                when READ_FILE =>
189
 
190
                                        -- Read the appropriate file
191
                                        v_filepart_ptr := new string'(get_filename_part(current_block_length, WINDOW_LENGTH, ACQUISITION_LENGTH));
192
                                        v_filename_ptr := new string'(DATA_DIRECTORY & "llr_" &  v_filepart_ptr.all & "_in.txt");
193
                                        v_num_lines := get_num_lines(v_filename_ptr.all);
194
                                        read_file(v_llr, v_num_lines, BW_LLR_INPUT, v_filename_ptr.all);
195
 
196
                                        -- NUMBER_PARITY_BITS lines are stored in the file per payload bit!
197
                                        v_num_blocks := v_num_lines / ((current_block_length_tail) * NUMBER_PARITY_BITS);
198
 
199
                                        -- Determine the blocks to simulate.
200
                                        if SIM_ALL_BLOCKS then
201
                                                current_block    <= 0;
202
                                                block_send_end   <= v_num_blocks - 1;
203
                                        else
204
                                                current_block    <= SIM_BLOCK_START;
205
                                                block_send_end   <= SIM_BLOCK_END;
206
                                        end if;
207
                                        send_data_fsm <= CONFIGURE;
208
 
209
 
210
                                --
211
                                -- Configure the Viterbi decoder for every single block it has to process.
212
                                --
213
                                when CONFIGURE =>
214
 
215
                                        -- Set control data.
216
                                        m_axis_ctrl_tdata(16 + BW_MAX_WINDOW_LENGTH - 1 downto 16) <=
217
                                                std_logic_vector(to_unsigned(WINDOW_LENGTH, BW_MAX_WINDOW_LENGTH));
218
                                        m_axis_ctrl_tdata(     BW_MAX_WINDOW_LENGTH - 1 downto  0) <=
219
                                                std_logic_vector(to_unsigned(ACQUISITION_LENGTH, BW_MAX_WINDOW_LENGTH));
220
 
221
                                        -- Check whether configuration succeeded
222
                                        if m_axis_ctrl_tvalid = '1' and m_axis_ctrl_tready = '1' then
223
                                                m_axis_ctrl_tvalid <= '0';
224
                                                send_data_fsm <= SEND_DATA;
225
                                        else
226
                                                m_axis_ctrl_tvalid <= '1';
227
                                        end if;
228
 
229
 
230
                                --
231
                                -- Send all data of a block. If we are done with this, we check what to do next:
232
                                -- 1) Configure the decoder to process the next block of the same length.
233
                                -- 2) Read a new file if all blocks of this block length were simulated.
234
                                -- 3) Quit simulation if all blocks of all block lengths were simulated.
235
                                --
236
                                when SEND_DATA =>
237
 
238
                                        m_axis_input_tvalid <= '1';
239
 
240
                                        -- Data transmission => increase bit counter and update data for next cycle.
241
                                        if m_axis_input_tvalid = '1' and m_axis_input_tready = '1' then
242
                                                v_sys_bit_counter := sys_bit_counter + 1;
243
                                                sys_bit_counter   <= v_sys_bit_counter;
244
                                        else
245
                                                v_sys_bit_counter := sys_bit_counter;
246
                                        end if;
247
 
248
 
249
                                        if v_sys_bit_counter < current_block_length_tail then
250
                                                -- trim and move data to stream
251
                                                for j in 0 to NUMBER_PARITY_BITS - 1 loop
252
                                                        m_axis_input_tdata(j * 8 + BW_LLR_INPUT - 1 downto j * 8) <=
253
                                                                 std_logic_vector(to_signed(v_llr(current_block * (current_block_length_tail * NUMBER_PARITY_BITS) + v_sys_bit_counter * NUMBER_PARITY_BITS + j), BW_LLR_INPUT));
254
                                                end loop;
255
                                        end if;
256
 
257
 
258
                                        -- Next data will be last of block
259
                                        if v_sys_bit_counter = current_block_length_tail - 1 then
260
                                                m_axis_input_tlast <= '1';
261
                                        else
262
                                                m_axis_input_tlast <= '0';
263
                                        end if;
264
 
265
                                        -- We have just sent the very last bit of this block.
266
                                        if m_axis_input_tvalid = '1' and
267
                                           m_axis_input_tready = '1' and
268
                                           m_axis_input_tlast  = '1' then
269
--                                      if v_sys_bit_counter = current_block_length then
270
                                                sys_bit_counter <= 0;
271
                                                m_axis_input_tvalid <= '0';
272
 
273
                                                -- Did we process the last block of a block length?
274
                                                if current_block = block_send_end then
275
 
276
                                                        -- Go to next block length, if we are not done.
277
                                                        if current_block_length_tail = BLOCK_LENGTH_END + ENCODER_MEMORY_DEPTH then
278
                                                                send_data_fsm <= SEND_DATA_FINISHED;
279
                                                        else
280
                                                                send_data_fsm <= READ_FILE;
281
                                                                current_block_length   <= current_block_length + BLOCK_LENGTH_INCR;
282
                                                                current_block_length_tail   <= current_block_length + BLOCK_LENGTH_INCR + ENCODER_MEMORY_DEPTH;
283
                                                        end if;
284
                                                else
285
                                                        send_data_fsm <= CONFIGURE;
286
                                                        current_block <= current_block + 1;
287
                                                end if;
288
                                        end if;
289
 
290
 
291
                        --
292
                        -- We are done with all blocks, do nothing anynmore.
293
                        --
294
                        when SEND_DATA_FINISHED =>
295
 
296
                        end case;
297
                end if;
298
        end if;
299
        end process;
300
 
301
 
302
        --
303
        -- Process receives the decoded data from the Viterbi decoder
304
        -- The received data is compared to a test vector
305
        --
306
        pr_receive : process(clk) is
307
                variable v_filepart_ptr  : t_string_ptr;
308
                variable v_filename_ptr  : t_string_ptr;
309
                variable v_num_lines     : natural := 0;
310
        begin
311
        if rising_edge(clk) then
312
                if aresetn = '0' then
313
 
314
                        current_block_length_out   <= 0;
315
                        current_block_length_out_d <= 0;
316
                        sys_bit_counter_out        <= 0;
317
                        s_axis_output_tready       <= '1';
318
                        block_receive_complete     <= false;
319
                        new_block_length           <= false;
320
 
321
                else
322
 
323
                        block_receive_complete     <= false;
324
                        new_block_length           <= false;
325
                        current_block_length_out_d <= current_block_length_out;
326
 
327
                        -- Data passes the output interface.
328
                        if s_axis_output_tvalid = '1' and s_axis_output_tready = '1' then
329
 
330
                                decoded_hardware(sys_bit_counter_out) <= s_axis_output_tdata;
331
 
332
                                sys_bit_counter_out <= sys_bit_counter_out + 1;
333
 
334
                                -- This is the last bit of the flag.
335
                                if s_axis_output_tlast = '1' then
336
                                        block_receive_complete   <= true;
337
                                        current_block_length_out <= sys_bit_counter_out + 1;
338
                                        sys_bit_counter_out      <= 0;
339
 
340
                                        -- Block received and block length changed => read file of correct blocks.
341
                                        if current_block_length_out /= sys_bit_counter_out + 1 then
342
                                                new_block_length <= true;
343
                                                v_filepart_ptr := new string'(get_filename_part(sys_bit_counter_out + 1, WINDOW_LENGTH, ACQUISITION_LENGTH));
344
                                                v_filename_ptr := new string'(DATA_DIRECTORY & "decoded_" & v_filepart_ptr.all & "_out.txt");
345
                                                v_num_lines := get_num_lines(v_filename_ptr.all);
346
                                                read_file(v_decoded_software, v_num_lines, BW_LLR_INPUT, v_filename_ptr.all);
347
 
348
                                                if SIM_ALL_BLOCKS then
349
                                                        first_block_out <= 0;
350
                                                        last_block_out  <= v_num_lines / (sys_bit_counter_out + 1);
351
                                                else
352
                                                        first_block_out <= SIM_BLOCK_START;
353
                                                        last_block_out  <= SIM_BLOCK_END;
354
                                                end if;
355
 
356
                                        end if;
357
                                end if;
358
                        end if;
359
 
360
                end if; -- reset
361
        end if;
362
        end process pr_receive;
363
 
364
 
365
 
366
        --
367
        -- Compare the block we just received from the decoder with the data stored within the files.
368
        -- Stop simulation if everything was simulated.
369
        --
370
        pr_compare : process(clk) is
371
                variable v_bit_error_count : natural := 0;
372
                variable v_line_out        : line;
373
                variable v_current_block   : natural;
374
        begin
375
        if rising_edge(clk) then
376
                if aresetn = '0' then
377
 
378
                        current_block_out <= 0;
379
 
380
                else
381
 
382
                        -- Which is our current block?
383
                        if new_block_length then
384
                                v_current_block := first_block_out;
385
                        else
386
                                v_current_block := current_block_out;
387
                        end if;
388
 
389
                        -- We got a whole block from the decoder, compare whether decoding was successful.
390
                        if block_receive_complete then
391
 
392
                                for i in 0 to current_block_length_out - 1 loop
393
                                        if (v_decoded_software(v_current_block * current_block_length_out + i) = 0 and decoded_hardware(i) = '1') or
394
                                           (v_decoded_software(v_current_block * current_block_length_out + i) = 1 and decoded_hardware(i) = '0') then
395
 
396
                                                v_bit_error_count := v_bit_error_count + 1;
397
 
398
                                           assert false report "Decoded bit " & str(i) & " in block " & str(v_current_block) & " does not match!"
399
                                                        severity warning;
400
                                        end if;
401
                                end loop;
402
 
403
                                -- Dump message.
404
                                write(v_line_out, string'("Block length: ")  & str(current_block_length_out));
405
                                write(v_line_out, string'(", Block: ")       & str(current_block_out));
406
                                write(v_line_out, string'(", errors: ")      & str(v_bit_error_count));
407
 
408
                                if current_block_out /= last_block_out - 1 then
409
                                        current_block_out <= current_block_out + 1;
410
                                else
411
                                        current_block_out <= 0;
412
 
413
                                        -- Stop simulation, if we are done with all blocks of all block lengths.
414
                                        if current_block_length_out = BLOCK_LENGTH_END then
415
                                                 assert false report "End" severity failure;
416
                                        end if;
417
                                end if;
418
                        end if;
419
                end if;
420
        end if;
421
        end process pr_compare;
422
 
423
 
424
        inst_dec_viterbi : dec_viterbi
425
        port map(
426
                aclk => clk,
427
                aresetn => aresetn,
428
 
429
                s_axis_input_tvalid  => m_axis_input_tvalid,
430
                s_axis_input_tdata   => m_axis_input_tdata,
431
                s_axis_input_tlast   => m_axis_input_tlast,
432
                s_axis_input_tready  => m_axis_input_tready,
433
 
434
                m_axis_output_tvalid => s_axis_output_tvalid,
435
                m_axis_output_tdata  => s_axis_output_tdata,
436
                m_axis_output_tlast  => s_axis_output_tlast,
437
                m_axis_output_tready => s_axis_output_tready,
438
 
439
                s_axis_ctrl_tvalid   => m_axis_ctrl_tvalid,
440
                s_axis_ctrl_tdata    => m_axis_ctrl_tdata,
441
                s_axis_ctrl_tlast    => m_axis_ctrl_tlast,
442
                s_axis_ctrl_tready   => m_axis_ctrl_tready
443
        );
444
 
445
end architecture sim;

powered by: WebSVN 2.1.0

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