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

Subversion Repositories xucpu

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

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