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

Subversion Repositories xucpu

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

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