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

Subversion Repositories viterbi_decoder_axi4s

[/] [viterbi_decoder_axi4s/] [trunk/] [testbench/] [pkg_tb_fileio.vhd] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mfehrenz
--!
2
--! Copyright (C) 2010 - 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  Testbench file reading package
9
--! @author Matthias Alles
10
--! @date   2010/04/05
11
--!
12
--! @details Offers functions to read files
13
--!
14
 
15
library ieee;
16
use ieee.std_logic_1164.all;
17
use ieee.numeric_std.all;
18
use std.textio.all;
19
 
20
 
21
package pkg_tb_fileio is
22
 
23
 
24
        ------------------------------
25
        -- Data type definitions
26
        ------------------------------
27
 
28
        --! Data type to store the file conent in integer format.
29
        type t_int_array is array (natural range <>) of integer;
30
 
31
        --! Data type to store the file conent in natural format.
32
        type t_nat_array is array (natural range <>) of natural;
33
 
34
        --! Access type of t_int_array.
35
        type t_int_array_ptr is access t_int_array;
36
 
37
        --! Access type of t_nat_array.
38
        type t_nat_array_ptr is access t_nat_array;
39
 
40
        --! String access type, e.g., for file names of variable length.
41
        type t_string_ptr is access string;
42
 
43
 
44
        ------------------------------
45
        -- File I/O Functions.
46
        ------------------------------
47
 
48
        --!
49
        --! Open file, get number of lines, create a new memory of type t_int_array
50
        --! at address data, read the file, saturate the values to bit_width bits.
51
        --! Format within file: One signed integer per line (decimal).
52
        --!
53
        procedure read_file (data      : inout t_int_array_ptr;
54
                             num_lines : inout natural;
55
                             bit_width : in natural;
56
                             filename  : in string);
57
 
58
 
59
        --!
60
        --! Open file, get number of lines, create a new memory of type t_int_array
61
        --! at address data, read the file, saturate the values to bit_width bits.
62
        --! Format within file: One signed integer per line (decimal).
63
        --!
64
        procedure read_file (data      : inout t_nat_array_ptr;
65
                             num_lines : inout natural;
66
                             bit_width : in natural;
67
                             filename  : in string);
68
 
69
        --!
70
        --! Open file, get number of lines, create new memorys of type t_int_array
71
        --! at address data, read the file, saturate each single value to bit_width bits.
72
        --! Format within file: One "(real,imag)" couple per line. real and imag have to
73
        --! be one signed integer (decimal).
74
        --!
75
        procedure read_file_complex (data_real : inout t_int_array_ptr;
76
                                     data_imag : inout t_int_array_ptr;
77
                                     num_lines : inout natural;
78
                                     bit_width : in natural;
79
                                     filename  : in string);
80
 
81
        --! Open file and return the number of lines stored within.
82
        impure function get_num_lines(filename : in string) return natural;
83
 
84
 
85
        -----------------------------------------------------
86
        -- Obsolete functions, should no longer be used!!
87
        -----------------------------------------------------
88
 
89
        --! Read no_values lines of file and return the values as integer array. OBSOLETE.
90
        procedure read_file (data      : inout t_int_array;
91
                             bit_width : in natural;
92
                             no_values : in natural;
93
                             filename  : in string);
94
 
95
        --! Read no_values lines of file and return the values as natural array. OBSOLETE.
96
        procedure read_file (data      : inout t_nat_array;
97
                             bit_width : in natural;
98
                             no_values : in natural;
99
                             filename  : in string);
100
 
101
 
102
end pkg_tb_fileio;
103
 
104
 
105
package body pkg_tb_fileio is
106
 
107
 
108
        procedure read_file (data      : inout t_int_array_ptr;
109
                             num_lines : inout natural;
110
                             bit_width : in natural;
111
                             filename  : in string) is
112
 
113
                file     file_handler : text open read_mode is filename;
114
                variable line_in      : line;
115
                variable line_out     : line;
116
                variable value        : integer;
117
 
118
        begin
119
                write(line_out, string'("Reading "));
120
                write(line_out, filename );
121
                write(line_out, string'("."));
122
                writeline(output, line_out);
123
 
124
                num_lines := get_num_lines(filename);
125
 
126
                deallocate(data);
127
 
128
                data := new t_int_array(0 to num_lines - 1);
129
 
130
                for i in 0 to num_lines - 1 loop
131
 
132
                        -- read integer value from line
133
                        readline(file_handler, line_in);
134
                        read(line_in, value);
135
 
136
                        -- saturate
137
                        if value > 2 ** (bit_width - 1) - 1 then
138
                                value := 2 ** (bit_width - 1) - 1;
139
                        elsif value < -2 ** (bit_width - 1) then
140
                                value := -2 ** (bit_width - 1);
141
                        end if;
142
 
143
                        data(i) := value;
144
                end loop;
145
 
146
        end read_file;
147
 
148
 
149
        procedure read_file (data      : inout t_nat_array_ptr;
150
                             num_lines : inout natural;
151
                             bit_width : in natural;
152
                             filename  : in string) is
153
 
154
                file     file_handler : text open read_mode is filename;
155
                variable line_in      : line;
156
                variable line_out     : line;
157
                variable value        : integer;
158
 
159
        begin
160
                write(line_out, string'("Reading "));
161
                write(line_out, filename );
162
                write(line_out, string'("."));
163
                writeline(output, line_out);
164
 
165
                num_lines := get_num_lines(filename);
166
 
167
                deallocate(data);
168
 
169
                data := new t_nat_array(0 to num_lines - 1);
170
 
171
                for i in 0 to num_lines - 1 loop
172
 
173
                        -- read integer value from line
174
                        readline(file_handler, line_in);
175
                        read(line_in, value);
176
 
177
                        -- saturate
178
                        if value > 2 ** bit_width - 1 then
179
                                value := 2 ** bit_width - 1;
180
                        end if;
181
 
182
                        data(i) := value;
183
                end loop;
184
 
185
        end read_file;
186
 
187
        procedure read_file_complex (data_real : inout t_int_array_ptr;
188
                                     data_imag : inout t_int_array_ptr;
189
                                     num_lines : inout natural;
190
                                     bit_width : in natural;
191
                                     filename  : in string) is
192
 
193
                file     file_handler : text open read_mode is filename;
194
                variable line_in      : line;
195
                variable line_out     : line;
196
                variable char         : character;
197
                variable value        : integer;
198
 
199
        begin
200
                write(line_out, string'("Reading "));
201
                write(line_out, filename );
202
                write(line_out, string'("."));
203
                writeline(output, line_out);
204
 
205
                num_lines := get_num_lines(filename);
206
 
207
                deallocate(data_real);
208
                deallocate(data_imag);
209
                data_real := new t_int_array(0 to num_lines - 1);
210
                data_imag := new t_int_array(0 to num_lines - 1);
211
 
212
                for i in 0 to num_lines - 1 loop
213
 
214
                        -- read integer value from line
215
                        readline(file_handler, line_in);
216
 
217
                        -- read "("
218
                        read(line_in, char);
219
 
220
                        -- read number
221
                        read(line_in, value);
222
 
223
                        -- saturate
224
                        if value > 2 ** (bit_width - 1) - 1 then
225
                                value := 2 ** (bit_width - 1) - 1;
226
                        elsif value < -2 ** (bit_width - 1) then
227
                                value := -2 ** (bit_width - 1);
228
                        end if;
229
 
230
                        data_real.all(i) := value;
231
 
232
                        -- read ","
233
                        read(line_in, char);
234
 
235
                        -- read number
236
                        read(line_in, value);
237
 
238
                        -- saturate
239
                        if value > 2 ** (bit_width - 1) - 1 then
240
                                value := 2 ** (bit_width - 1) - 1;
241
                        elsif value < -2 ** (bit_width - 1) then
242
                                value := -2 ** (bit_width - 1);
243
                        end if;
244
 
245
                        data_imag.all(i) := value;
246
                end loop;
247
 
248
        end read_file_complex;
249
 
250
 
251
        impure function get_num_lines(filename : in string) return natural is
252
                file     file_handler : text open read_mode is filename;
253
                variable line_in      : line;
254
                variable num_lines    : natural := 0;
255
        begin
256
 
257
                while not endfile(file_handler) loop
258
                        readline(file_handler, line_in);
259
                        num_lines := num_lines + 1;
260
                end loop;
261
 
262
                return num_lines;
263
        end get_num_lines;
264
 
265
 
266
 
267
        -----------------------------------------------------
268
        -- Obsolete functions, should no longer be used!!
269
        -----------------------------------------------------
270
 
271
        procedure read_file (data      : inout t_int_array;
272
                             bit_width : in natural;
273
                             no_values : in natural;
274
                             filename  : in string) is
275
 
276
                file     file_handler : text open read_mode is filename;
277
                variable line_in      : line;
278
                variable line_out     : line;
279
                variable value        : integer;
280
 
281
        begin
282
                write(line_out, string'("Reading "));
283
                write(line_out, filename );
284
                write(line_out, string'("."));
285
                writeline(output, line_out);
286
 
287
                for i in 0 to no_values - 1 loop
288
 
289
                        -- read integer value from line
290
                        readline(file_handler, line_in);
291
                        read(line_in, value);
292
 
293
                        -- saturate
294
                        if value > 2 ** (bit_width - 1) - 1 then
295
                                value := 2 ** (bit_width - 1) - 1;
296
                        elsif value < -2 ** (bit_width - 1) then
297
                                value := -2 ** (bit_width - 1);
298
                        end if;
299
 
300
                        data(i) := value;
301
                end loop;
302
 
303
        end read_file;
304
 
305
 
306
        procedure read_file (data      : inout t_nat_array;
307
                             bit_width : in natural;
308
                             no_values : in natural;
309
                             filename  : in string) is
310
 
311
                file     file_handler : text open read_mode is filename;
312
                variable line_in      : line;
313
                variable line_out     : line;
314
                variable value        : natural;
315
 
316
        begin
317
                write(line_out, string'("Reading "));
318
                write(line_out, filename );
319
                write(line_out, string'("."));
320
                writeline(output, line_out);
321
 
322
                for i in 0 to no_values - 1 loop
323
 
324
                        -- read integer value from line
325
                        readline(file_handler, line_in);
326
                        read(line_in, value);
327
 
328
                        -- saturate natural
329
                        if value > 2 ** bit_width - 1 then
330
                                value := 2 ** bit_width - 1;
331
                        end if;
332
 
333
                        data(i) := value;
334
                end loop;
335
 
336
        end read_file;
337
end pkg_tb_fileio;

powered by: WebSVN 2.1.0

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