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

Subversion Repositories xucpu

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

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 27 lcdsgmtr
    VARIABLE memory_block : INTEGER RANGE 0 TO 3    := 0;
152
    VARIABLE address      : INTEGER RANGE 0 TO 8191 := 0;
153
 
154
    VARIABLE end_of_mem : BOOLEAN := FALSE;
155
 
156 17 lcdsgmtr
  BEGIN
157
 
158
    FOR i IN 0 TO 3 LOOP
159 25 lcdsgmtr
      array_io(i) := (OTHERS => 0);
160 17 lcdsgmtr
    END LOOP;
161
 
162
    file_open(fstatus, input_file, filename, READ_MODE);
163
 
164
    IF fstatus = OPEN_OK THEN
165 27 lcdsgmtr
      WHILE NOT endfile(input_file) AND NOT end_of_mem LOOP
166
        -- Read the next line and put its contents in a string
167
        readline(input_file, input_line);
168
        read(input_line, line_value);
169 17 lcdsgmtr
 
170 27 lcdsgmtr
        -- Current debugging feedback
171
        write(output_line, line_value);
172
        writeline(OUTPUT, output_line);
173 17 lcdsgmtr
 
174 27 lcdsgmtr
        -- Turn a hex value into an integer value
175
        read_hex(line_value, i_value);
176 17 lcdsgmtr
 
177 27 lcdsgmtr
        array_io(memory_block)(address) := i_value;
178 17 lcdsgmtr
 
179 27 lcdsgmtr
        IF memory_block = 3 AND address = 8191 THEN
180
          end_of_mem := TRUE;
181
        ELSE
182
          IF address = 8191 THEN
183
            memory_block := memory_block + 1;
184
            address      := 0;
185
          ELSE
186
            address := address + 1;
187
          END IF;
188
        END IF;
189 17 lcdsgmtr
 
190 27 lcdsgmtr
        write(output_line, STRING'("Index :"));
191
        write(output_line, memory_block*8192+address);
192
        write(output_line, STRING'(" Value: "));
193
        write(output_line, i_value);
194
        writeline(OUTPUT, output_line);
195 17 lcdsgmtr
      END LOOP;
196
 
197
      file_close(input_file);
198
 
199
    END IF;
200
 
201
  END PROCEDURE init_b32k_array;
202
 
203 27 lcdsgmtr
-- Fill a signal array with the contents of a file
204 2 lcdsgmtr
  PROCEDURE init_sig_array (
205
    SIGNAL array_in   : INOUT cstr_array_type;
206
    CONSTANT filename : IN    STRING) IS
207
 
208
  BEGIN
209
 
210
    fill_sig_array(0, array_in);
211
    read_file_into_sig_array(array_in, filename);
212
 
213
  END PROCEDURE init_sig_array;
214
 
215 27 lcdsgmtr
-- General procedure to fill an array of integers. This is to make sure that
216
-- the array does not contain any meta-data any more.
217 2 lcdsgmtr
  PROCEDURE fill_sig_array (
218
    CONSTANT value  : IN    INTEGER;
219
    SIGNAL in_array : INOUT cstr_array_type) IS
220
 
221
  BEGIN  -- PROCEDURE fill_array
222
    FOR i IN in_array'RANGE LOOP
223
      in_array(i) <= value;
224
    END LOOP;  -- i
225
  END PROCEDURE fill_sig_array;
226
 
227 27 lcdsgmtr
-- Read the file into the signal array
228 2 lcdsgmtr
  PROCEDURE read_file_into_sig_array (
229
    SIGNAL array_in   : INOUT cstr_array_type;
230
    CONSTANT filename : IN    STRING) IS
231
 
232
    FILE input_file : TEXT;
233
 
234
    VARIABLE input_line : LINE;
235
    VARIABLE fstatus    : FILE_OPEN_STATUS;
236
 
237
    VARIABLE a_index : INTEGER := 0;
238
    VARIABLE i_value : INTEGER := 0;
239
 
240
    VARIABLE output_line : LINE;
241
    VARIABLE line_value  : STRING(1 TO 4);
242
 
243
  BEGIN  -- PROCEDURE read_file_into_sig_array
244
 
245
    file_open(fstatus, input_file, filename, READ_MODE);
246
 
247
    IF fstatus = OPEN_OK THEN
248
      WHILE NOT endfile(input_file) LOOP
249
        -- Read the next line and put its contents in a string
250
        readline(input_file, input_line);
251
        read(input_line, line_value);
252
 
253
        -- Current debugging feedback
254
        write(output_line, line_value);
255
        writeline(OUTPUT, output_line);
256
 
257
        -- Turn a hex value into an integer value
258
        read_hex(line_value, i_value);
259
 
260
        array_in(a_index) <= i_value;
261
        a_index           := a_index + 1;
262
 
263
        write(output_line, STRING'("Index :"));
264
        write(output_line, a_index);
265
        write(output_line, STRING'(" Value: "));
266
        write(output_line, i_value);
267
        writeline(OUTPUT, output_line);
268
      END LOOP;
269
 
270
      file_close(input_file);
271
 
272
    END IF;
273
 
274
  END PROCEDURE read_file_into_sig_array;
275
 
276 27 lcdsgmtr
-- Initialise a variable array
277 2 lcdsgmtr
  PROCEDURE init_var_array (
278
    VARIABLE array_in : INOUT cstr_array_type;
279
    CONSTANT filename : IN    STRING) IS
280
 
281
  BEGIN
282
 
283
    fill_var_array(0, array_in);
284
    read_file_into_var_array(array_in, filename);
285
 
286
  END PROCEDURE init_var_array;
287
 
288 27 lcdsgmtr
-- General procedure to fill a variable array of integers. This is to make
289
-- sure that the array does not contain any meta-data any more.
290 2 lcdsgmtr
  PROCEDURE fill_var_array (
291
    CONSTANT value    : IN    INTEGER;
292
    VARIABLE in_array : INOUT cstr_array_type) IS
293
 
294
  BEGIN  -- PROCEDURE fill_array
295
 
296
    FOR i IN in_array'RANGE LOOP
297
      in_array(i) := value;
298
    END LOOP;  -- i
299 17 lcdsgmtr
 
300 2 lcdsgmtr
  END PROCEDURE fill_var_array;
301
 
302
  PROCEDURE read_file_into_var_array (
303
    VARIABLE array_in : INOUT cstr_array_type;
304
    CONSTANT filename : IN    STRING) IS
305
 
306
    FILE input_file : TEXT;
307
 
308
    VARIABLE input_line : LINE;
309
    VARIABLE fstatus    : FILE_OPEN_STATUS;
310
 
311
    VARIABLE a_index : INTEGER := 0;
312
    VARIABLE i_value : INTEGER := 0;
313
 
314
    VARIABLE output_line : LINE;
315
    VARIABLE line_value  : STRING(1 TO 4);
316
 
317
  BEGIN  -- PROCEDURE read_file
318
 
319
    file_open(fstatus, input_file, filename, READ_MODE);
320
 
321
    IF fstatus = OPEN_OK THEN
322
      WHILE NOT endfile(input_file) LOOP
323
        -- Read the next line and put its contents in a string
324
        readline(input_file, input_line);
325
        read(input_line, line_value);
326
 
327
        -- Current debugging feedback
328
        write(output_line, line_value);
329
        writeline(OUTPUT, output_line);
330
 
331
        -- Turn a hex value into an integer value
332
        read_hex(line_value, i_value);
333
 
334
        array_in(a_index) := i_value;
335
        a_index           := a_index + 1;
336
 
337
        write(output_line, STRING'("Index :"));
338
        write(output_line, a_index);
339
        write(output_line, STRING'(" Value: "));
340
        write(output_line, i_value);
341
        writeline(OUTPUT, output_line);
342
      END LOOP;
343
 
344
      file_close(input_file);
345
 
346
    END IF;
347
 
348
  END PROCEDURE read_file_into_var_array;
349
 
350 27 lcdsgmtr
-- Shared and generic procedures
351 2 lcdsgmtr
 
352 27 lcdsgmtr
-- Read a hexadecimal value from the input string and turn it into an integer.
353 2 lcdsgmtr
  PROCEDURE read_hex (
354
    VARIABLE input_line : IN  STRING;
355
    VARIABLE hex_value  : OUT INTEGER) IS
356
 
357
    VARIABLE input_length : INTEGER := input_line'LENGTH;
358
    VARIABLE chr          : CHARACTER;
359
    VARIABLE output_line  : LINE;
360
 
361
    VARIABLE chr_value : INTEGER := 0;
362
    VARIABLE radix     : INTEGER := 1;
363
    VARIABLE result    : INTEGER := 0;
364
 
365
  BEGIN  -- PROCEDURE read_hex
366
 
367
    FOR i IN input_line'REVERSE_RANGE LOOP
368
      chr       := input_line(i);
369
      chr_value := hex_char_to_value(chr);
370
      result    := chr_value * radix + result;
371
      radix     := radix * 16;
372
    END LOOP;
373
 
374
    hex_value := result;
375
 
376
  END PROCEDURE read_hex;
377
 
378 27 lcdsgmtr
-- Return the integer value matching with the hexadecimal character
379 2 lcdsgmtr
  FUNCTION hex_char_to_value (
380
    CONSTANT chr : IN CHARACTER)
381
    RETURN INTEGER IS
382
 
383
    VARIABLE digit : INTEGER := 0;
384
  BEGIN  -- PROCEDURE hex_char_to_value
385
 
386
    CASE chr IS
387
      WHEN '0'    => digit := 0;
388
      WHEN '1'    => digit := 1;
389
      WHEN '2'    => digit := 2;
390
      WHEN '3'    => digit := 3;
391
      WHEN '4'    => digit := 4;
392
      WHEN '5'    => digit := 5;
393
      WHEN '6'    => digit := 6;
394
      WHEN '7'    => digit := 7;
395
      WHEN '8'    => digit := 8;
396
      WHEN '9'    => digit := 9;
397
      WHEN 'A'    => digit := 10;
398
      WHEN 'B'    => digit := 11;
399
      WHEN 'C'    => digit := 12;
400
      WHEN 'D'    => digit := 13;
401
      WHEN 'E'    => digit := 14;
402
      WHEN 'F'    => digit := 15;
403
      WHEN OTHERS => digit := 0;
404
    END CASE;
405
 
406
    RETURN digit;
407
 
408
  END FUNCTION hex_char_to_value;
409
 
410
  PROCEDURE dump_array (
411
    CONSTANT array_in : IN cstr_array_type) IS
412
 
413
    VARIABLE output_line : LINE;
414
  BEGIN  -- PROCEDURE dump_array
415
 
416
    FOR i IN array_in'RANGE LOOP
417
 
418 17 lcdsgmtr
      write(output_line, STRING'("Index: "));
419 2 lcdsgmtr
      write(output_line, i);
420
 
421 17 lcdsgmtr
      write(output_line, STRING'(" Value: "));
422 2 lcdsgmtr
      write(output_line, array_in(i));
423
 
424
      writeline(OUTPUT, output_line);
425 17 lcdsgmtr
 
426 2 lcdsgmtr
    END LOOP;  -- i
427 17 lcdsgmtr
 
428
 
429 2 lcdsgmtr
  END PROCEDURE dump_array;
430
 
431 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.