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

Subversion Repositories zpu

[/] [zpu/] [trunk/] [zpu/] [zpu4/] [src/] [txt_util.vhd] - Blame information for rev 93

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 93 oharboe
-- ZPU
2
--
3
-- Copyright 2004-2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
4
-- 
5
-- The FreeBSD license
6
-- 
7
-- Redistribution and use in source and binary forms, with or without
8
-- modification, are permitted provided that the following conditions
9
-- are met:
10
-- 
11
-- 1. Redistributions of source code must retain the above copyright
12
--    notice, this list of conditions and the following disclaimer.
13
-- 2. Redistributions in binary form must reproduce the above
14
--    copyright notice, this list of conditions and the following
15
--    disclaimer in the documentation and/or other materials
16
--    provided with the distribution.
17
-- 
18
-- THIS SOFTWARE IS PROVIDED BY THE ZPU PROJECT ``AS IS'' AND ANY
19
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
21
-- PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
-- ZPU PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23
-- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29
-- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
-- 
31
-- The views and conclusions contained in the software and documentation
32
-- are those of the authors and should not be interpreted as representing
33
-- official policies, either expressed or implied, of the ZPU Project.
34
 
35
library ieee;
36
use ieee.std_logic_1164.all;
37
use std.textio.all;
38
 
39
library work;
40
 
41
package txt_util is
42
 
43
    -- prints a message to the screen
44
    procedure print(text: string);
45
 
46
    -- prints the message when active
47
    -- useful for debug switches
48
    procedure print(active: boolean; text: string);
49
 
50
    -- converts std_logic into a character
51
    function chr(sl: std_logic) return character;
52
 
53
    -- converts std_logic into a string (1 to 1)
54
    function str(sl: std_logic) return string;
55
 
56
    -- converts std_logic_vector into a string (binary base)
57
    function str(slv: std_logic_vector) return string;
58
 
59
    -- converts boolean into a string
60
    function str(b: boolean) return string;
61
 
62
    -- converts an integer into a single character
63
    -- (can also be used for hex conversion and other bases)
64
    function chr(int: integer) return character;
65
 
66
    -- converts integer into string using specified base
67
    function str(int: integer; base: integer) return string;
68
 
69
    -- converts integer to string, using base 10
70
    function str(int: integer) return string;
71
 
72
    -- convert std_logic_vector into a string in hex format
73
    function hstr(slv: std_logic_vector) return string;
74
 
75
 
76
    -- functions to manipulate strings
77
    -----------------------------------
78
 
79
    -- convert a character to upper case
80
    function to_upper(c: character) return character;
81
 
82
    -- convert a character to lower case
83
    function to_lower(c: character) return character;
84
 
85
    -- convert a string to upper case
86
    function to_upper(s: string) return string;
87
 
88
    -- convert a string to lower case
89
    function to_lower(s: string) return string;
90
 
91
 
92
 
93
    -- functions to convert strings into other formats
94
    --------------------------------------------------
95
 
96
    -- converts a character into std_logic
97
    function to_std_logic(c: character) return std_logic;
98
 
99
    -- converts a string into std_logic_vector
100
    function to_std_logic_vector(s: string) return std_logic_vector;
101
 
102
 
103
 
104
    -- file I/O
105
    -----------
106
 
107
    -- read variable length string from input file
108
    procedure str_read(file in_file: TEXT;
109
                       res_string: out string);
110
 
111
    -- print string to a file and start new line
112
    procedure print(file out_file: TEXT;
113
                    new_string: in  string);
114
 
115
    -- print character to a file and start new line
116
    procedure print(file out_file: TEXT;
117
                    char:       in  character);
118
 
119
end txt_util;
120
 
121
 
122
 
123
 
124
package body txt_util is
125
 
126
 
127
 
128
 
129
   -- prints text to the screen
130
 
131
   procedure print(text: string) is
132
     variable msg_line: line;
133
     begin
134
       write(msg_line, text);
135
       writeline(output, msg_line);
136
   end print;
137
 
138
 
139
 
140
 
141
   -- prints text to the screen when active
142
 
143
   procedure print(active: boolean; text: string)  is
144
     begin
145
      if active then
146
         print(text);
147
      end if;
148
   end print;
149
 
150
 
151
   -- converts std_logic into a character
152
 
153
   function chr(sl: std_logic) return character is
154
    variable c: character;
155
    begin
156
      case sl is
157
         when 'U' => c:= 'U';
158
         when 'X' => c:= 'X';
159
         when '0' => c:= '0';
160
         when '1' => c:= '1';
161
         when 'Z' => c:= 'Z';
162
         when 'W' => c:= 'W';
163
         when 'L' => c:= 'L';
164
         when 'H' => c:= 'H';
165
         when '-' => c:= '-';
166
      end case;
167
    return c;
168
   end chr;
169
 
170
 
171
 
172
   -- converts std_logic into a string (1 to 1)
173
 
174
   function str(sl: std_logic) return string is
175
    variable s: string(1 to 1);
176
    begin
177
        s(1) := chr(sl);
178
        return s;
179
   end str;
180
 
181
 
182
 
183
   -- converts std_logic_vector into a string (binary base)
184
   -- (this also takes care of the fact that the range of
185
   --  a string is natural while a std_logic_vector may
186
   --  have an integer range)
187
 
188
   function str(slv: std_logic_vector) return string is
189
     variable result : string (1 to slv'length);
190
     variable r : integer;
191
   begin
192
     r := 1;
193
     for i in slv'range loop
194
        result(r) := chr(slv(i));
195
        r := r + 1;
196
     end loop;
197
     return result;
198
   end str;
199
 
200
 
201
   function str(b: boolean) return string is
202
 
203
    begin
204
       if b then
205
          return "true";
206
      else
207
        return "false";
208
       end if;
209
    end str;
210
 
211
 
212
   -- converts an integer into a character
213
   -- for 0 to 9 the obvious mapping is used, higher
214
   -- values are mapped to the characters A-Z
215
   -- (this is usefull for systems with base > 10)
216
   -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
217
 
218
   function chr(int: integer) return character is
219
    variable c: character;
220
   begin
221
        case int is
222
          when  0 => c := '0';
223
          when  1 => c := '1';
224
          when  2 => c := '2';
225
          when  3 => c := '3';
226
          when  4 => c := '4';
227
          when  5 => c := '5';
228
          when  6 => c := '6';
229
          when  7 => c := '7';
230
          when  8 => c := '8';
231
          when  9 => c := '9';
232
          when 10 => c := 'A';
233
          when 11 => c := 'B';
234
          when 12 => c := 'C';
235
          when 13 => c := 'D';
236
          when 14 => c := 'E';
237
          when 15 => c := 'F';
238
          when 16 => c := 'G';
239
          when 17 => c := 'H';
240
          when 18 => c := 'I';
241
          when 19 => c := 'J';
242
          when 20 => c := 'K';
243
          when 21 => c := 'L';
244
          when 22 => c := 'M';
245
          when 23 => c := 'N';
246
          when 24 => c := 'O';
247
          when 25 => c := 'P';
248
          when 26 => c := 'Q';
249
          when 27 => c := 'R';
250
          when 28 => c := 'S';
251
          when 29 => c := 'T';
252
          when 30 => c := 'U';
253
          when 31 => c := 'V';
254
          when 32 => c := 'W';
255
          when 33 => c := 'X';
256
          when 34 => c := 'Y';
257
          when 35 => c := 'Z';
258
          when others => c := '?';
259
        end case;
260
        return c;
261
    end chr;
262
 
263
 
264
 
265
   -- convert integer to string using specified base
266
   -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
267
 
268
   function str(int: integer; base: integer) return string is
269
 
270
    variable temp:      string(1 to 10);
271
    variable num:       integer;
272
    variable abs_int:   integer;
273
    variable len:       integer := 1;
274
    variable power:     integer := 1;
275
 
276
   begin
277
 
278
    -- bug fix for negative numbers
279
    abs_int := abs(int);
280
 
281
    num     := abs_int;
282
 
283
    while num >= base loop                     -- Determine how many
284
      len := len + 1;                          -- characters required
285
      num := num / base;                       -- to represent the
286
    end loop ;                                 -- number.
287
 
288
    for i in len downto 1 loop                 -- Convert the number to
289
      temp(i) := chr(abs_int/power mod base);  -- a string starting
290
      power := power * base;                   -- with the right hand
291
    end loop ;                                 -- side.
292
 
293
    -- return result and add sign if required
294
    if int < 0 then
295
       return '-'& temp(1 to len);
296
     else
297
       return temp(1 to len);
298
    end if;
299
 
300
   end str;
301
 
302
 
303
  -- convert integer to string, using base 10
304
  function str(int: integer) return string is
305
 
306
   begin
307
 
308
    return str(int, 10) ;
309
 
310
   end str;
311
 
312
 
313
 
314
   -- converts a std_logic_vector into a hex string.
315
   function hstr(slv: std_logic_vector) return string is
316
       variable hexlen: integer;
317
       variable longslv : std_logic_vector(67 downto 0) := (others => '0');
318
       variable hex : string(1 to 16);
319
       variable fourbit : std_logic_vector(3 downto 0);
320
     begin
321
       hexlen := (slv'left+1)/4;
322
       if (slv'left+1) mod 4 /= 0 then
323
         hexlen := hexlen + 1;
324
       end if;
325
       longslv(slv'left downto 0) := slv;
326
       for i in (hexlen -1) downto 0 loop
327
         fourbit := longslv(((i*4)+3) downto (i*4));
328
         case fourbit is
329
           when "0000" => hex(hexlen -I) := '0';
330
           when "0001" => hex(hexlen -I) := '1';
331
           when "0010" => hex(hexlen -I) := '2';
332
           when "0011" => hex(hexlen -I) := '3';
333
           when "0100" => hex(hexlen -I) := '4';
334
           when "0101" => hex(hexlen -I) := '5';
335
           when "0110" => hex(hexlen -I) := '6';
336
           when "0111" => hex(hexlen -I) := '7';
337
           when "1000" => hex(hexlen -I) := '8';
338
           when "1001" => hex(hexlen -I) := '9';
339
           when "1010" => hex(hexlen -I) := 'A';
340
           when "1011" => hex(hexlen -I) := 'B';
341
           when "1100" => hex(hexlen -I) := 'C';
342
           when "1101" => hex(hexlen -I) := 'D';
343
           when "1110" => hex(hexlen -I) := 'E';
344
           when "1111" => hex(hexlen -I) := 'F';
345
           when "ZZZZ" => hex(hexlen -I) := 'z';
346
           when "UUUU" => hex(hexlen -I) := 'u';
347
           when "XXXX" => hex(hexlen -I) := 'x';
348
           when others => hex(hexlen -I) := '?';
349
         end case;
350
       end loop;
351
       return hex(1 to hexlen);
352
     end hstr;
353
 
354
 
355
 
356
   -- functions to manipulate strings
357
   -----------------------------------
358
 
359
 
360
   -- convert a character to upper case
361
 
362
   function to_upper(c: character) return character is
363
 
364
      variable u: character;
365
 
366
    begin
367
 
368
       case c is
369
        when 'a' => u := 'A';
370
        when 'b' => u := 'B';
371
        when 'c' => u := 'C';
372
        when 'd' => u := 'D';
373
        when 'e' => u := 'E';
374
        when 'f' => u := 'F';
375
        when 'g' => u := 'G';
376
        when 'h' => u := 'H';
377
        when 'i' => u := 'I';
378
        when 'j' => u := 'J';
379
        when 'k' => u := 'K';
380
        when 'l' => u := 'L';
381
        when 'm' => u := 'M';
382
        when 'n' => u := 'N';
383
        when 'o' => u := 'O';
384
        when 'p' => u := 'P';
385
        when 'q' => u := 'Q';
386
        when 'r' => u := 'R';
387
        when 's' => u := 'S';
388
        when 't' => u := 'T';
389
        when 'u' => u := 'U';
390
        when 'v' => u := 'V';
391
        when 'w' => u := 'W';
392
        when 'x' => u := 'X';
393
        when 'y' => u := 'Y';
394
        when 'z' => u := 'Z';
395
        when others => u := c;
396
    end case;
397
 
398
      return u;
399
 
400
   end to_upper;
401
 
402
 
403
   -- convert a character to lower case
404
 
405
   function to_lower(c: character) return character is
406
 
407
      variable l: character;
408
 
409
    begin
410
 
411
       case c is
412
        when 'A' => l := 'a';
413
        when 'B' => l := 'b';
414
        when 'C' => l := 'c';
415
        when 'D' => l := 'd';
416
        when 'E' => l := 'e';
417
        when 'F' => l := 'f';
418
        when 'G' => l := 'g';
419
        when 'H' => l := 'h';
420
        when 'I' => l := 'i';
421
        when 'J' => l := 'j';
422
        when 'K' => l := 'k';
423
        when 'L' => l := 'l';
424
        when 'M' => l := 'm';
425
        when 'N' => l := 'n';
426
        when 'O' => l := 'o';
427
        when 'P' => l := 'p';
428
        when 'Q' => l := 'q';
429
        when 'R' => l := 'r';
430
        when 'S' => l := 's';
431
        when 'T' => l := 't';
432
        when 'U' => l := 'u';
433
        when 'V' => l := 'v';
434
        when 'W' => l := 'w';
435
        when 'X' => l := 'x';
436
        when 'Y' => l := 'y';
437
        when 'Z' => l := 'z';
438
        when others => l := c;
439
    end case;
440
 
441
      return l;
442
 
443
   end to_lower;
444
 
445
 
446
 
447
   -- convert a string to upper case
448
 
449
   function to_upper(s: string) return string is
450
 
451
     variable uppercase: string (s'range);
452
 
453
   begin
454
 
455
     for i in s'range loop
456
        uppercase(i):= to_upper(s(i));
457
     end loop;
458
     return uppercase;
459
 
460
   end to_upper;
461
 
462
 
463
 
464
   -- convert a string to lower case
465
 
466
   function to_lower(s: string) return string is
467
 
468
     variable lowercase: string (s'range);
469
 
470
   begin
471
 
472
     for i in s'range loop
473
        lowercase(i):= to_lower(s(i));
474
     end loop;
475
     return lowercase;
476
 
477
   end to_lower;
478
 
479
 
480
 
481
-- functions to convert strings into other types
482
 
483
 
484
-- converts a character into a std_logic
485
 
486
function to_std_logic(c: character) return std_logic is
487
    variable sl: std_logic;
488
    begin
489
      case c is
490
        when 'U' =>
491
           sl := 'U';
492
        when 'X' =>
493
           sl := 'X';
494
        when '0' =>
495
           sl := '0';
496
        when '1' =>
497
           sl := '1';
498
        when 'Z' =>
499
           sl := 'Z';
500
        when 'W' =>
501
           sl := 'W';
502
        when 'L' =>
503
           sl := 'L';
504
        when 'H' =>
505
           sl := 'H';
506
        when '-' =>
507
           sl := '-';
508
        when others =>
509
           sl := 'X';
510
    end case;
511
   return sl;
512
  end to_std_logic;
513
 
514
 
515
-- converts a string into std_logic_vector
516
 
517
function to_std_logic_vector(s: string) return std_logic_vector is
518
  variable slv: std_logic_vector(s'high-s'low downto 0);
519
  variable k: integer;
520
begin
521
   k := s'high-s'low;
522
  for i in s'range loop
523
     slv(k) := to_std_logic(s(i));
524
     k      := k - 1;
525
  end loop;
526
  return slv;
527
end to_std_logic_vector;
528
 
529
 
530
 
531
 
532
 
533
 
534
----------------
535
--  file I/O  --
536
----------------
537
 
538
 
539
 
540
-- read variable length string from input file
541
 
542
procedure str_read(file in_file: TEXT;
543
                   res_string: out string) is
544
 
545
       variable l:         line;
546
       variable c:         character;
547
       variable is_string: boolean;
548
 
549
   begin
550
 
551
     readline(in_file, l);
552
     -- clear the contents of the result string
553
     for i in res_string'range loop
554
         res_string(i) := ' ';
555
     end loop;
556
     -- read all characters of the line, up to the length  
557
     -- of the results string
558
     for i in res_string'range loop
559
        read(l, c, is_string);
560
        res_string(i) := c;
561
        if not is_string then -- found end of line
562
           exit;
563
        end if;
564
     end loop;
565
 
566
end str_read;
567
 
568
 
569
-- print string to a file
570
procedure print(file out_file: TEXT;
571
                new_string: in  string) is
572
 
573
       variable l: line;
574
 
575
   begin
576
 
577
     write(l, new_string);
578
     writeline(out_file, l);
579
 
580
end print;
581
 
582
 
583
-- print character to a file and start new line
584
procedure print(file out_file: TEXT;
585
                char: in  character) is
586
 
587
       variable l: line;
588
 
589
   begin
590
 
591
     write(l, char);
592
     writeline(out_file, l);
593
 
594
end print;
595
 
596
 
597
 
598
-- appends contents of a string to a file until line feed occurs
599
-- (LF is considered to be the end of the string)
600
 
601
procedure str_write(file out_file: TEXT;
602
                    new_string: in  string) is
603
 begin
604
 
605
   for i in new_string'range loop
606
      print(out_file, new_string(i));
607
      if new_string(i) = LF then -- end of string
608
         exit;
609
      end if;
610
   end loop;
611
 
612
end str_write;
613
 
614
 
615
 
616
 
617
end txt_util;
618
 
619
 
620
 
621
 

powered by: WebSVN 2.1.0

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