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

Subversion Repositories xucpu

[/] [xucpu/] [trunk/] [src/] [util/] [file/] [hexio.vhdl] - Blame information for rev 16

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

Line No. Rev Author Line
1 2 lcdsgmtr
-- Copyright 2015, Jürgen Defurne
2
--
3
-- This file is part of the Experimental Unstable CPU System.
4
--
5
-- The Experimental Unstable CPU System Is free software: you can redistribute
6
-- it and/or modify it under the terms of the GNU Lesser General Public License
7
-- as published by the Free Software Foundation, either version 3 of the
8
-- License, or (at your option) any later version.
9
--
10
-- The Experimental Unstable CPU System is distributed in the hope that it will
11
-- be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
13
-- General Public License for more details.
14
--
15
-- You should have received a copy of the GNU Lesser General Public License
16
-- along with Experimental Unstable CPU System. If not, see
17
-- http://www.gnu.org/licenses/lgpl.txt.
18
 
19
 
20
LIBRARY std;
21
USE std.textio.ALL;
22
 
23 16 lcdsgmtr
PACKAGE hexio IS
24 2 lcdsgmtr
 
25
  -- This type must be used for base memory arrays
26 16 lcdsgmtr
  TYPE cstr_array_type IS ARRAY(INTEGER RANGE <>) OF INTEGER RANGE 0 TO 65535;
27 2 lcdsgmtr
 
28 16 lcdsgmtr
  FUNCTION init_cstr (
29
    CONSTANT array_size : IN INTEGER;
30
    CONSTANT input_file : IN STRING)
31
    RETURN cstr_array_type;
32
 
33 2 lcdsgmtr
  PROCEDURE init_var_array (
34
    VARIABLE array_in : INOUT cstr_array_type;
35
    CONSTANT filename : IN    STRING);
36
 
37
  PROCEDURE init_sig_array (
38
    SIGNAL array_in   : INOUT cstr_array_type;
39
    CONSTANT filename : IN    STRING);
40
 
41
  PROCEDURE dump_array (
42
    CONSTANT array_in : IN cstr_array_type);
43
 
44 16 lcdsgmtr
END PACKAGE hexio;
45 2 lcdsgmtr
 
46 16 lcdsgmtr
PACKAGE BODY hexio IS
47 2 lcdsgmtr
 
48
  -- Private declarations
49
  PROCEDURE read_hex (
50
    VARIABLE input_line : IN  STRING;
51
    VARIABLE hex_value  : OUT INTEGER);
52
 
53
  FUNCTION hex_char_to_value (
54
    CONSTANT chr : IN CHARACTER)
55
    RETURN INTEGER;
56
 
57
  PROCEDURE fill_var_array (
58
    CONSTANT value    : IN    INTEGER;
59
    VARIABLE in_array : INOUT cstr_array_type);
60
 
61
  PROCEDURE fill_sig_array (
62
    CONSTANT value  : IN    INTEGER;
63
    SIGNAL in_array : INOUT cstr_array_type);
64
 
65
  PROCEDURE read_file_into_var_array (
66
    VARIABLE array_in : INOUT cstr_array_type;
67
    CONSTANT filename : IN    STRING);
68
 
69
  PROCEDURE read_file_into_sig_array (
70
    SIGNAL array_in   : INOUT cstr_array_type;
71
    CONSTANT filename : IN    STRING);
72
 
73
  -- Procedure and function body definitions
74
  FUNCTION init_cstr (
75
    CONSTANT array_size : IN INTEGER;
76
    CONSTANT input_file : IN STRING)
77
    RETURN cstr_array_type IS
78
 
79
    VARIABLE rv : cstr_array_type(0 TO array_size - 1) := (OTHERS => 0);
80
  BEGIN  -- FUNCTION init_cstr
81
 
82
    init_var_array(rv, input_file);
83
 
84
    RETURN rv;
85
  END FUNCTION init_cstr;
86
 
87
  -- Fill a signal array with the contents of a file
88
  PROCEDURE init_sig_array (
89
    SIGNAL array_in   : INOUT cstr_array_type;
90
    CONSTANT filename : IN    STRING) IS
91
 
92
  BEGIN
93
 
94
    fill_sig_array(0, array_in);
95
    read_file_into_sig_array(array_in, filename);
96
 
97
  END PROCEDURE init_sig_array;
98
 
99
  -- General procedure to fill an array of integers. This is to make sure that
100
  -- the array does not contain any meta-data any more.
101
  PROCEDURE fill_sig_array (
102
    CONSTANT value  : IN    INTEGER;
103
    SIGNAL in_array : INOUT cstr_array_type) IS
104
 
105
  BEGIN  -- PROCEDURE fill_array
106
 
107
    FOR i IN in_array'RANGE LOOP
108
      in_array(i) <= value;
109
    END LOOP;  -- i
110
  END PROCEDURE fill_sig_array;
111
 
112
  -- Read the file into the signal array
113
  PROCEDURE read_file_into_sig_array (
114
    SIGNAL array_in   : INOUT cstr_array_type;
115
    CONSTANT filename : IN    STRING) IS
116
 
117
    FILE input_file : TEXT;
118
 
119
    VARIABLE input_line : LINE;
120
    VARIABLE fstatus    : FILE_OPEN_STATUS;
121
 
122
    VARIABLE a_index : INTEGER := 0;
123
    VARIABLE i_value : INTEGER := 0;
124
 
125
    VARIABLE output_line : LINE;
126
    VARIABLE line_value  : STRING(1 TO 4);
127
 
128
  BEGIN  -- PROCEDURE read_file_into_sig_array
129
 
130
    file_open(fstatus, input_file, filename, READ_MODE);
131
 
132
    IF fstatus = OPEN_OK THEN
133
      WHILE NOT endfile(input_file) LOOP
134
        -- Read the next line and put its contents in a string
135
        readline(input_file, input_line);
136
        read(input_line, line_value);
137
 
138
        -- Current debugging feedback
139
        write(output_line, line_value);
140
        writeline(OUTPUT, output_line);
141
 
142
        -- Turn a hex value into an integer value
143
        read_hex(line_value, i_value);
144
 
145
        array_in(a_index) <= i_value;
146
        a_index           := a_index + 1;
147
 
148
        write(output_line, STRING'("Index :"));
149
        write(output_line, a_index);
150
        write(output_line, STRING'(" Value: "));
151
        write(output_line, i_value);
152
        writeline(OUTPUT, output_line);
153
      END LOOP;
154
 
155
      file_close(input_file);
156
 
157
    END IF;
158
 
159
  END PROCEDURE read_file_into_sig_array;
160
 
161
  --
162
  --
163
 
164
  -- Initialise a variable array
165
  PROCEDURE init_var_array (
166
    VARIABLE array_in : INOUT cstr_array_type;
167
    CONSTANT filename : IN    STRING) IS
168
 
169
  BEGIN
170
 
171
    fill_var_array(0, array_in);
172
    read_file_into_var_array(array_in, filename);
173
 
174
  END PROCEDURE init_var_array;
175
 
176
  -- General procedure to fill an array of integers. This is to make sure that
177
  -- the array does not contain any meta-data any more.
178
  PROCEDURE fill_var_array (
179
    CONSTANT value    : IN    INTEGER;
180
    VARIABLE in_array : INOUT cstr_array_type) IS
181
 
182
  BEGIN  -- PROCEDURE fill_array
183
 
184
    FOR i IN in_array'RANGE LOOP
185
      in_array(i) := value;
186
    END LOOP;  -- i
187
 
188
  END PROCEDURE fill_var_array;
189
 
190
  PROCEDURE read_file_into_var_array (
191
    VARIABLE array_in : INOUT cstr_array_type;
192
    CONSTANT filename : IN    STRING) IS
193
 
194
    FILE input_file : TEXT;
195
 
196
    VARIABLE input_line : LINE;
197
    VARIABLE fstatus    : FILE_OPEN_STATUS;
198
 
199
    VARIABLE a_index : INTEGER := 0;
200
    VARIABLE i_value : INTEGER := 0;
201
 
202
    VARIABLE output_line : LINE;
203
    VARIABLE line_value  : STRING(1 TO 4);
204
 
205
  BEGIN  -- PROCEDURE read_file
206
 
207
    file_open(fstatus, input_file, filename, READ_MODE);
208
 
209
    IF fstatus = OPEN_OK THEN
210
      WHILE NOT endfile(input_file) LOOP
211
        -- Read the next line and put its contents in a string
212
        readline(input_file, input_line);
213
        read(input_line, line_value);
214
 
215
        -- Current debugging feedback
216
        write(output_line, line_value);
217
        writeline(OUTPUT, output_line);
218
 
219
        -- Turn a hex value into an integer value
220
        read_hex(line_value, i_value);
221
 
222
        array_in(a_index) := i_value;
223
        a_index           := a_index + 1;
224
 
225
        write(output_line, STRING'("Index :"));
226
        write(output_line, a_index);
227
        write(output_line, STRING'(" Value: "));
228
        write(output_line, i_value);
229
        writeline(OUTPUT, output_line);
230
      END LOOP;
231
 
232
      file_close(input_file);
233
 
234
    END IF;
235
 
236
  END PROCEDURE read_file_into_var_array;
237
 
238
  -- Shared and generic procedures
239
 
240
  -- Read a hexadecimal value from the input string and turn it into an integer.
241
  PROCEDURE read_hex (
242
    VARIABLE input_line : IN  STRING;
243
    VARIABLE hex_value  : OUT INTEGER) IS
244
 
245
    VARIABLE input_length : INTEGER := input_line'LENGTH;
246
    VARIABLE chr          : CHARACTER;
247
    VARIABLE output_line  : LINE;
248
 
249
    VARIABLE chr_value : INTEGER := 0;
250
    VARIABLE radix     : INTEGER := 1;
251
    VARIABLE result    : INTEGER := 0;
252
 
253
  BEGIN  -- PROCEDURE read_hex
254
 
255
    FOR i IN input_line'REVERSE_RANGE LOOP
256
      chr       := input_line(i);
257
      chr_value := hex_char_to_value(chr);
258
      result    := chr_value * radix + result;
259
      radix     := radix * 16;
260
    END LOOP;
261
 
262
    hex_value := result;
263
 
264
  END PROCEDURE read_hex;
265
 
266
  -- Return the integer value matching with the hexadecimal character
267
  FUNCTION hex_char_to_value (
268
    CONSTANT chr : IN CHARACTER)
269
    RETURN INTEGER IS
270
 
271
    VARIABLE digit : INTEGER := 0;
272
  BEGIN  -- PROCEDURE hex_char_to_value
273
 
274
    CASE chr IS
275
      WHEN '0'    => digit := 0;
276
      WHEN '1'    => digit := 1;
277
      WHEN '2'    => digit := 2;
278
      WHEN '3'    => digit := 3;
279
      WHEN '4'    => digit := 4;
280
      WHEN '5'    => digit := 5;
281
      WHEN '6'    => digit := 6;
282
      WHEN '7'    => digit := 7;
283
      WHEN '8'    => digit := 8;
284
      WHEN '9'    => digit := 9;
285
      WHEN 'A'    => digit := 10;
286
      WHEN 'B'    => digit := 11;
287
      WHEN 'C'    => digit := 12;
288
      WHEN 'D'    => digit := 13;
289
      WHEN 'E'    => digit := 14;
290
      WHEN 'F'    => digit := 15;
291
      WHEN OTHERS => digit := 0;
292
    END CASE;
293
 
294
    RETURN digit;
295
 
296
  END FUNCTION hex_char_to_value;
297
 
298
  PROCEDURE dump_array (
299
    CONSTANT array_in : IN cstr_array_type) IS
300
 
301
    VARIABLE output_line : LINE;
302
  BEGIN  -- PROCEDURE dump_array
303
 
304
    FOR i IN array_in'RANGE LOOP
305
 
306
      write(output_line, string'("Index: "));
307
      write(output_line, i);
308
 
309
      write(output_line, string'(" Value: "));
310
      write(output_line, array_in(i));
311
 
312
      writeline(OUTPUT, output_line);
313
 
314
    END LOOP;  -- i
315
 
316
 
317
  END PROCEDURE dump_array;
318
 
319 16 lcdsgmtr
END PACKAGE BODY hexio;

powered by: WebSVN 2.1.0

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