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

Subversion Repositories xucpu

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

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 17 lcdsgmtr
  TYPE B32K_array_type IS ARRAY(0 TO 3) OF cstr_array_type(0 TO 8192);
28 2 lcdsgmtr
 
29 16 lcdsgmtr
  FUNCTION init_cstr (
30
    CONSTANT array_size : IN INTEGER;
31
    CONSTANT input_file : IN STRING)
32
    RETURN cstr_array_type;
33 17 lcdsgmtr
 
34
  PROCEDURE init_b32k_array (
35
    SIGNAL array_io   : INOUT B32K_array_type;
36
    CONSTANT filename : IN    STRING);
37
 
38 2 lcdsgmtr
  PROCEDURE init_var_array (
39
    VARIABLE array_in : INOUT cstr_array_type;
40
    CONSTANT filename : IN    STRING);
41
 
42
  PROCEDURE init_sig_array (
43
    SIGNAL array_in   : INOUT cstr_array_type;
44
    CONSTANT filename : IN    STRING);
45
 
46
  PROCEDURE dump_array (
47
    CONSTANT array_in : IN cstr_array_type);
48
 
49 16 lcdsgmtr
END PACKAGE hexio;
50 2 lcdsgmtr
 
51 16 lcdsgmtr
PACKAGE BODY hexio IS
52 2 lcdsgmtr
 
53
  -- Private declarations
54
  PROCEDURE read_hex (
55
    VARIABLE input_line : IN  STRING;
56
    VARIABLE hex_value  : OUT INTEGER);
57
 
58
  FUNCTION hex_char_to_value (
59
    CONSTANT chr : IN CHARACTER)
60
    RETURN INTEGER;
61
 
62
  PROCEDURE fill_var_array (
63
    CONSTANT value    : IN    INTEGER;
64
    VARIABLE in_array : INOUT cstr_array_type);
65
 
66
  PROCEDURE fill_sig_array (
67
    CONSTANT value  : IN    INTEGER;
68
    SIGNAL in_array : INOUT cstr_array_type);
69
 
70
  PROCEDURE read_file_into_var_array (
71
    VARIABLE array_in : INOUT cstr_array_type;
72
    CONSTANT filename : IN    STRING);
73
 
74
  PROCEDURE read_file_into_sig_array (
75
    SIGNAL array_in   : INOUT cstr_array_type;
76
    CONSTANT filename : IN    STRING);
77
 
78
  -- Procedure and function body definitions
79
  FUNCTION init_cstr (
80
    CONSTANT array_size : IN INTEGER;
81
    CONSTANT input_file : IN STRING)
82
    RETURN cstr_array_type IS
83
 
84
    VARIABLE rv : cstr_array_type(0 TO array_size - 1) := (OTHERS => 0);
85 17 lcdsgmtr
 
86 2 lcdsgmtr
  BEGIN  -- FUNCTION init_cstr
87
 
88
    init_var_array(rv, input_file);
89 17 lcdsgmtr
 
90 2 lcdsgmtr
    RETURN rv;
91
  END FUNCTION init_cstr;
92
 
93 17 lcdsgmtr
  PROCEDURE init_b32k_array (
94
    SIGNAL array_io   : INOUT B32K_array_type;
95
    CONSTANT filename : IN    STRING) IS
96
 
97
    FILE input_file : TEXT;
98
 
99
    VARIABLE input_line : LINE;
100
    VARIABLE fstatus    : FILE_OPEN_STATUS;
101
 
102
    VARIABLE a_index : INTEGER := 0;
103
    VARIABLE i_value : INTEGER := 0;
104
 
105
    VARIABLE output_line : LINE;
106
    VARIABLE line_value  : STRING(1 TO 4);
107
 
108
  BEGIN
109
 
110
    FOR i IN 0 TO 3 LOOP
111
      array_io(i) <= (OTHERS => 0);
112
    END LOOP;
113
 
114
    file_open(fstatus, input_file, filename, READ_MODE);
115
 
116
    IF fstatus = OPEN_OK THEN
117
 
118
      FOR i IN 0 TO 3 LOOP
119
        FOR j IN 0 TO 8191 LOOP
120
          -- Read the next line and put its contents in a string
121
          readline(input_file, input_line);
122
          read(input_line, line_value);
123
 
124
          -- Current debugging feedback
125
          write(output_line, line_value);
126
          writeline(OUTPUT, output_line);
127
 
128
          -- Turn a hex value into an integer value
129
          read_hex(line_value, i_value);
130
 
131
          array_io(i)(j) <= i_value;
132
 
133
          write(output_line, STRING'("Index :"));
134
          write(output_line, i*8192+j);
135
          write(output_line, STRING'(" Value: "));
136
          write(output_line, i_value);
137
          writeline(OUTPUT, output_line);
138
        END LOOP;
139
      END LOOP;
140
 
141
      file_close(input_file);
142
 
143
    END IF;
144
 
145
  END PROCEDURE init_b32k_array;
146
 
147 2 lcdsgmtr
  -- Fill a signal array with the contents of a file
148
  PROCEDURE init_sig_array (
149
    SIGNAL array_in   : INOUT cstr_array_type;
150
    CONSTANT filename : IN    STRING) IS
151
 
152
  BEGIN
153
 
154
    fill_sig_array(0, array_in);
155
    read_file_into_sig_array(array_in, filename);
156
 
157
  END PROCEDURE init_sig_array;
158
 
159
  -- General procedure to fill an array of integers. This is to make sure that
160
  -- the array does not contain any meta-data any more.
161
  PROCEDURE fill_sig_array (
162
    CONSTANT value  : IN    INTEGER;
163
    SIGNAL in_array : INOUT cstr_array_type) IS
164
 
165
  BEGIN  -- PROCEDURE fill_array
166
    FOR i IN in_array'RANGE LOOP
167
      in_array(i) <= value;
168
    END LOOP;  -- i
169
  END PROCEDURE fill_sig_array;
170
 
171
  -- Read the file into the signal array
172
  PROCEDURE read_file_into_sig_array (
173
    SIGNAL array_in   : INOUT cstr_array_type;
174
    CONSTANT filename : IN    STRING) IS
175
 
176
    FILE input_file : TEXT;
177
 
178
    VARIABLE input_line : LINE;
179
    VARIABLE fstatus    : FILE_OPEN_STATUS;
180
 
181
    VARIABLE a_index : INTEGER := 0;
182
    VARIABLE i_value : INTEGER := 0;
183
 
184
    VARIABLE output_line : LINE;
185
    VARIABLE line_value  : STRING(1 TO 4);
186
 
187
  BEGIN  -- PROCEDURE read_file_into_sig_array
188
 
189
    file_open(fstatus, input_file, filename, READ_MODE);
190
 
191
    IF fstatus = OPEN_OK THEN
192
      WHILE NOT endfile(input_file) LOOP
193
        -- Read the next line and put its contents in a string
194
        readline(input_file, input_line);
195
        read(input_line, line_value);
196
 
197
        -- Current debugging feedback
198
        write(output_line, line_value);
199
        writeline(OUTPUT, output_line);
200
 
201
        -- Turn a hex value into an integer value
202
        read_hex(line_value, i_value);
203
 
204
        array_in(a_index) <= i_value;
205
        a_index           := a_index + 1;
206
 
207
        write(output_line, STRING'("Index :"));
208
        write(output_line, a_index);
209
        write(output_line, STRING'(" Value: "));
210
        write(output_line, i_value);
211
        writeline(OUTPUT, output_line);
212
      END LOOP;
213
 
214
      file_close(input_file);
215
 
216
    END IF;
217
 
218
  END PROCEDURE read_file_into_sig_array;
219
 
220
  -- Initialise a variable array
221
  PROCEDURE init_var_array (
222
    VARIABLE array_in : INOUT cstr_array_type;
223
    CONSTANT filename : IN    STRING) IS
224
 
225
  BEGIN
226
 
227
    fill_var_array(0, array_in);
228
    read_file_into_var_array(array_in, filename);
229
 
230
  END PROCEDURE init_var_array;
231
 
232
  -- General procedure to fill an array of integers. This is to make sure that
233
  -- the array does not contain any meta-data any more.
234
  PROCEDURE fill_var_array (
235
    CONSTANT value    : IN    INTEGER;
236
    VARIABLE in_array : INOUT cstr_array_type) IS
237
 
238
  BEGIN  -- PROCEDURE fill_array
239
 
240
    FOR i IN in_array'RANGE LOOP
241
      in_array(i) := value;
242
    END LOOP;  -- i
243 17 lcdsgmtr
 
244 2 lcdsgmtr
  END PROCEDURE fill_var_array;
245
 
246
  PROCEDURE read_file_into_var_array (
247
    VARIABLE array_in : INOUT cstr_array_type;
248
    CONSTANT filename : IN    STRING) IS
249
 
250
    FILE input_file : TEXT;
251
 
252
    VARIABLE input_line : LINE;
253
    VARIABLE fstatus    : FILE_OPEN_STATUS;
254
 
255
    VARIABLE a_index : INTEGER := 0;
256
    VARIABLE i_value : INTEGER := 0;
257
 
258
    VARIABLE output_line : LINE;
259
    VARIABLE line_value  : STRING(1 TO 4);
260
 
261
  BEGIN  -- PROCEDURE read_file
262
 
263
    file_open(fstatus, input_file, filename, READ_MODE);
264
 
265
    IF fstatus = OPEN_OK THEN
266
      WHILE NOT endfile(input_file) LOOP
267
        -- Read the next line and put its contents in a string
268
        readline(input_file, input_line);
269
        read(input_line, line_value);
270
 
271
        -- Current debugging feedback
272
        write(output_line, line_value);
273
        writeline(OUTPUT, output_line);
274
 
275
        -- Turn a hex value into an integer value
276
        read_hex(line_value, i_value);
277
 
278
        array_in(a_index) := i_value;
279
        a_index           := a_index + 1;
280
 
281
        write(output_line, STRING'("Index :"));
282
        write(output_line, a_index);
283
        write(output_line, STRING'(" Value: "));
284
        write(output_line, i_value);
285
        writeline(OUTPUT, output_line);
286
      END LOOP;
287
 
288
      file_close(input_file);
289
 
290
    END IF;
291
 
292
  END PROCEDURE read_file_into_var_array;
293
 
294
  -- Shared and generic procedures
295
 
296
  -- Read a hexadecimal value from the input string and turn it into an integer.
297
  PROCEDURE read_hex (
298
    VARIABLE input_line : IN  STRING;
299
    VARIABLE hex_value  : OUT INTEGER) IS
300
 
301
    VARIABLE input_length : INTEGER := input_line'LENGTH;
302
    VARIABLE chr          : CHARACTER;
303
    VARIABLE output_line  : LINE;
304
 
305
    VARIABLE chr_value : INTEGER := 0;
306
    VARIABLE radix     : INTEGER := 1;
307
    VARIABLE result    : INTEGER := 0;
308
 
309
  BEGIN  -- PROCEDURE read_hex
310
 
311
    FOR i IN input_line'REVERSE_RANGE LOOP
312
      chr       := input_line(i);
313
      chr_value := hex_char_to_value(chr);
314
      result    := chr_value * radix + result;
315
      radix     := radix * 16;
316
    END LOOP;
317
 
318
    hex_value := result;
319
 
320
  END PROCEDURE read_hex;
321
 
322
  -- Return the integer value matching with the hexadecimal character
323
  FUNCTION hex_char_to_value (
324
    CONSTANT chr : IN CHARACTER)
325
    RETURN INTEGER IS
326
 
327
    VARIABLE digit : INTEGER := 0;
328
  BEGIN  -- PROCEDURE hex_char_to_value
329
 
330
    CASE chr IS
331
      WHEN '0'    => digit := 0;
332
      WHEN '1'    => digit := 1;
333
      WHEN '2'    => digit := 2;
334
      WHEN '3'    => digit := 3;
335
      WHEN '4'    => digit := 4;
336
      WHEN '5'    => digit := 5;
337
      WHEN '6'    => digit := 6;
338
      WHEN '7'    => digit := 7;
339
      WHEN '8'    => digit := 8;
340
      WHEN '9'    => digit := 9;
341
      WHEN 'A'    => digit := 10;
342
      WHEN 'B'    => digit := 11;
343
      WHEN 'C'    => digit := 12;
344
      WHEN 'D'    => digit := 13;
345
      WHEN 'E'    => digit := 14;
346
      WHEN 'F'    => digit := 15;
347
      WHEN OTHERS => digit := 0;
348
    END CASE;
349
 
350
    RETURN digit;
351
 
352
  END FUNCTION hex_char_to_value;
353
 
354
  PROCEDURE dump_array (
355
    CONSTANT array_in : IN cstr_array_type) IS
356
 
357
    VARIABLE output_line : LINE;
358
  BEGIN  -- PROCEDURE dump_array
359
 
360
    FOR i IN array_in'RANGE LOOP
361
 
362 17 lcdsgmtr
      write(output_line, STRING'("Index: "));
363 2 lcdsgmtr
      write(output_line, i);
364
 
365 17 lcdsgmtr
      write(output_line, STRING'(" Value: "));
366 2 lcdsgmtr
      write(output_line, array_in(i));
367
 
368
      writeline(OUTPUT, output_line);
369 17 lcdsgmtr
 
370 2 lcdsgmtr
    END LOOP;  -- i
371 17 lcdsgmtr
 
372
 
373 2 lcdsgmtr
  END PROCEDURE dump_array;
374
 
375 16 lcdsgmtr
END PACKAGE BODY hexio;

powered by: WebSVN 2.1.0

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