VHDL-verification
Package to ease directed testing of HDL entities
txt_util.vhd
Go to the documentation of this file.
1 -------------------------------------------------------------------
2 --! @file
3 --! @author Stephan Doll, plus some in-house additions by Hipolito Guzman-Miranda
4 --! @todo Separate our local changes into a different file
5 --! @brief Package for VHDL text output, from Stephan Doll's VHDL verification course.
6 -------------------------------------------------------------------
7 library ieee;
8 use ieee.std_logic_1164.all;
9 use std.textio.all;
10 
11 --! @brief Allows for text output in simulation
12 package txt_util is
13 
14  --! prints a message to the screen
15  procedure print(text: string);
16 
17  --! @brief prints the message when active
18  --
19  -- useful for debug switches
20  procedure print(active: boolean; text: string);
21 
22  --! converts std_logic into a character
23  function chr(sl: std_logic) return character;
24 
25  --! converts an 8-bit integer into a printable character
26  function ascii (int: integer) return character;
27 
28  --! converts std_logic into a string (1 to 1)
29  function str(sl: std_logic) return string;
30 
31  --! converts std_logic_vector into a string (binary base)
32  function str(slv: std_logic_vector) return string;
33 
34  --! converts boolean into a string
35  function str(b: boolean) return string;
36 
37  --! @brief converts an integer into a single character
38  --
39  -- (can also be used for hex conversion and other bases)
40  function chr(int: integer) return character;
41 
42  --! converts integer into string using specified base
43  function str(int: integer; base: integer) return string;
44 
45  --! converts integer to string, using base 10
46  function str(int: integer) return string;
47 
48  --! convert std_logic_vector into a string in hex format
49  function hstr(slv: std_logic_vector) return string;
50 
51 
52  -- functions to manipulate strings
53  -----------------------------------
54 
55  --! convert a character to upper case
56  function to_upper(c: character) return character;
57 
58  --! convert a character to lower case
59  function to_lower(c: character) return character;
60 
61  --! convert a string to upper case
62  function to_upper(s: string) return string;
63 
64  --! convert a string to lower case
65  function to_lower(s: string) return string;
66 
67 
68 
69  -- functions to convert strings into other formats
70  --------------------------------------------------
71 
72  --! converts a character into std_logic
73  function to_std_logic(c: character) return std_logic;
74 
75  --! converts a string into std_logic_vector
76  function to_std_logic_vector(s: string) return std_logic_vector;
77 
78 
79 
80  -- file I/O
81  -----------
82 
83  --! read variable length string from input file
84  procedure str_read(file in_file: TEXT;
85  res_string: out string);
86 
87  --! print string to a file and start new line
88  procedure print(file out_file: TEXT;
89  new_string: in string);
90 
91  --! print character to a file and start new line
92  procedure print(file out_file: TEXT;
93  char: in character);
94 
95 end txt_util;
96 
97 
98 
99 
100 package body txt_util is
101 
102  --! prints text to the screen
103  procedure print(text: string) is
104  variable msg_line: line;
105  begin
106  write(msg_line, text);
107  writeline(output, msg_line);
108  end print;
109 
110  --! prints text to the screen when active
111  procedure print(active: boolean; text: string) is
112  begin
113  if active then
114  print(text);
115  end if;
116  end print;
117 
118 
119  --! converts std_logic into a character
120  function chr(sl: std_logic) return character is
121  variable c: character;
122  begin
123  case sl is
124  when 'U' => c:= 'U';
125  when 'X' => c:= 'X';
126  when '0' => c:= '0';
127  when '1' => c:= '1';
128  when 'Z' => c:= 'Z';
129  when 'W' => c:= 'W';
130  when 'L' => c:= 'L';
131  when 'H' => c:= 'H';
132  when '-' => c:= '-';
133  end case;
134  return c;
135  end chr;
136 
137 
138 
139  --! converts std_logic into a string (1 to 1)
140  function str(sl: std_logic) return string is
141  variable s: string(1 to 1);
142  begin
143  s(1) := chr(sl);
144  return s;
145  end str;
146 
147 
148  --! @brief converts std_logic_vector into a string (binary base)
149  --
150  -- @detailed (this also takes care of the fact that the range of
151  -- a string is natural while a std_logic_vector may
152  -- have an integer range)
153  function str(slv: std_logic_vector) return string is
154  variable result : string (1 to slv'length);
155  variable r : integer;
156  begin
157  r := 1;
158  for i in slv'range loop
159  result(r) := chr(slv(i));
160  r := r + 1;
161  end loop;
162  return result;
163  end str;
164 
165 
166  --! @brief converts boolean into a string
167  function str(b: boolean) return string is
168 
169  begin
170  if b then
171  return "true";
172  else
173  return "false";
174  end if;
175  end str;
176 
177 
178  --! @brief converts an integer into a character
179  --
180  -- @detailed for 0 to 9 the obvious mapping is used, higher
181  -- values are mapped to the characters A-Z
182  -- (this is useful for systems with base > 10)
183  -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
184  function chr(int: integer) return character is
185  variable c: character;
186  begin
187  case int is
188  when 0 => c := '0';
189  when 1 => c := '1';
190  when 2 => c := '2';
191  when 3 => c := '3';
192  when 4 => c := '4';
193  when 5 => c := '5';
194  when 6 => c := '6';
195  when 7 => c := '7';
196  when 8 => c := '8';
197  when 9 => c := '9';
198  when 10 => c := 'A';
199  when 11 => c := 'B';
200  when 12 => c := 'C';
201  when 13 => c := 'D';
202  when 14 => c := 'E';
203  when 15 => c := 'F';
204  when 16 => c := 'G';
205  when 17 => c := 'H';
206  when 18 => c := 'I';
207  when 19 => c := 'J';
208  when 20 => c := 'K';
209  when 21 => c := 'L';
210  when 22 => c := 'M';
211  when 23 => c := 'N';
212  when 24 => c := 'O';
213  when 25 => c := 'P';
214  when 26 => c := 'Q';
215  when 27 => c := 'R';
216  when 28 => c := 'S';
217  when 29 => c := 'T';
218  when 30 => c := 'U';
219  when 31 => c := 'V';
220  when 32 => c := 'W';
221  when 33 => c := 'X';
222  when 34 => c := 'Y';
223  when 35 => c := 'Z';
224  when others => c := '?';
225  end case;
226  return c;
227  end chr;
228 
229  -- Convert an 8-bit integer to an ascii value. Return '?' for all control
230  -- characters
231  function ascii (int: integer) return character is
232  variable ret : character;
233  begin
234  if (int < 0) or (int > 255) then
235  report "txt_util: ascii(): argument lower than 0 or higher than 255, expect '?' at the output";
236  end if;
237  case int is
238  when 32 => ret := ' ';
239  when 33 => ret := '!';
240  when 34 => ret := '"';
241  when 35 => ret := '#';
242  when 36 => ret := '$';
243  when 37 => ret := '%';
244  when 38 => ret := '&';
245  when 39 => ret := ''';
246  when 40 => ret := '(';
247  when 41 => ret := ')';
248  when 42 => ret := '*';
249  when 43 => ret := '+';
250  when 44 => ret := ',';
251  when 45 => ret := '-';
252  when 46 => ret := '.';
253  when 47 => ret := '/';
254  when 48 => ret := '0';
255  when 49 => ret := '1';
256  when 50 => ret := '2';
257  when 51 => ret := '3';
258  when 52 => ret := '4';
259  when 53 => ret := '5';
260  when 54 => ret := '6';
261  when 55 => ret := '7';
262  when 56 => ret := '8';
263  when 57 => ret := '9';
264  when 58 => ret := ':';
265  when 59 => ret := ';';
266  when 60 => ret := '<';
267  when 61 => ret := '=';
268  when 62 => ret := '>';
269  when 63 => ret := '?';
270  when 64 => ret := '@';
271  when 65 => ret := 'A';
272  when 66 => ret := 'B';
273  when 67 => ret := 'C';
274  when 68 => ret := 'D';
275  when 69 => ret := 'E';
276  when 70 => ret := 'F';
277  when 71 => ret := 'G';
278  when 72 => ret := 'H';
279  when 73 => ret := 'I';
280  when 74 => ret := 'J';
281  when 75 => ret := 'K';
282  when 76 => ret := 'L';
283  when 77 => ret := 'M';
284  when 78 => ret := 'N';
285  when 79 => ret := 'O';
286  when 80 => ret := 'P';
287  when 81 => ret := 'Q';
288  when 82 => ret := 'R';
289  when 83 => ret := 'S';
290  when 84 => ret := 'T';
291  when 85 => ret := 'U';
292  when 86 => ret := 'V';
293  when 87 => ret := 'W';
294  when 88 => ret := 'X';
295  when 89 => ret := 'Y';
296  when 90 => ret := 'Z';
297  when 91 => ret := '[';
298  when 92 => ret := '\';
299  when 93 => ret := ']';
300  when 94 => ret := '^';
301  when 95 => ret := '_';
302  when 96 => ret := '`';
303  when 97 => ret := 'a';
304  when 98 => ret := 'b';
305  when 99 => ret := 'c';
306  when 100 => ret := 'd';
307  when 101 => ret := 'e';
308  when 102 => ret := 'f';
309  when 103 => ret := 'g';
310  when 104 => ret := 'h';
311  when 105 => ret := 'i';
312  when 106 => ret := 'j';
313  when 107 => ret := 'k';
314  when 108 => ret := 'l';
315  when 109 => ret := 'm';
316  when 110 => ret := 'n';
317  when 111 => ret := 'o';
318  when 112 => ret := 'p';
319  when 113 => ret := 'q';
320  when 114 => ret := 'r';
321  when 115 => ret := 's';
322  when 116 => ret := 't';
323  when 117 => ret := 'u';
324  when 118 => ret := 'v';
325  when 119 => ret := 'w';
326  when 120 => ret := 'x';
327  when 121 => ret := 'y';
328  when 122 => ret := 'z';
329  when 123 => ret := '{';
330  when 124 => ret := '|';
331  when 125 => ret := '}';
332  when 126 => ret := '~';
333  when others => ret := '?';
334  end case;
335  return ret;
336  end ascii;
337 
338  --! @brief convert integer to string using specified base
339  --
340  -- @detailed (adapted from Steve Vogwell's posting in comp.lang.vhdl)
341  function str(int: integer; base: integer) return string is
342 
343  variable temp: string(1 to 10);
344  variable num: integer;
345  variable abs_int: integer;
346  variable len: integer := 1;
347  variable power: integer := 1;
348 
349  begin
350 
351  -- bug fix for negative numbers
352  abs_int := abs(int);
353 
354  num := abs_int;
355 
356  while num >= base loop -- Determine how many
357  len := len + 1; -- characters required
358  num := num / base; -- to represent the
359  end loop ; -- number.
360 
361  for i in len downto 1 loop -- Convert the number to
362  temp(i) := chr(abs_int/power mod base); -- a string starting
363  power := power * base; -- with the right hand
364  end loop ; -- side.
365 
366  -- return result and add sign if required
367  if int < 0 then
368  return '-'& temp(1 to len);
369  else
370  return temp(1 to len);
371  end if;
372 
373  end str;
374 
375 
376  --! convert integer to string, using base 10
377  function str(int: integer) return string is
378 
379  begin
380 
381  return str(int, 10) ;
382 
383  end str;
384 
385 
386 
387  --! converts a std_logic_vector into a hex string.
388  function hstr(slv: std_logic_vector) return string is
389  variable hexlen: integer;
390  variable longslv : std_logic_vector(67 downto 0) := (others => '0');
391  variable hex : string(1 to 16);
392  variable fourbit : std_logic_vector(3 downto 0);
393  begin
394  hexlen := (slv'left+1)/4;
395  if (slv'left+1) mod 4 /= 0 then
396  hexlen := hexlen + 1;
397  end if;
398  longslv(slv'left downto 0) := slv;
399  for i in (hexlen -1) downto 0 loop
400  fourbit := longslv(((i*4)+3) downto (i*4));
401  case fourbit is
402  when "0000" => hex(hexlen -I) := '0';
403  when "0001" => hex(hexlen -I) := '1';
404  when "0010" => hex(hexlen -I) := '2';
405  when "0011" => hex(hexlen -I) := '3';
406  when "0100" => hex(hexlen -I) := '4';
407  when "0101" => hex(hexlen -I) := '5';
408  when "0110" => hex(hexlen -I) := '6';
409  when "0111" => hex(hexlen -I) := '7';
410  when "1000" => hex(hexlen -I) := '8';
411  when "1001" => hex(hexlen -I) := '9';
412  when "1010" => hex(hexlen -I) := 'A';
413  when "1011" => hex(hexlen -I) := 'B';
414  when "1100" => hex(hexlen -I) := 'C';
415  when "1101" => hex(hexlen -I) := 'D';
416  when "1110" => hex(hexlen -I) := 'E';
417  when "1111" => hex(hexlen -I) := 'F';
418  when "ZZZZ" => hex(hexlen -I) := 'z';
419  when "UUUU" => hex(hexlen -I) := 'u';
420  when "XXXX" => hex(hexlen -I) := 'x';
421  when others => hex(hexlen -I) := '?';
422  end case;
423  end loop;
424  return hex(1 to hexlen);
425  end hstr;
426 
427 
428 
429  -- functions to manipulate strings
430  -----------------------------------
431 
432 
433  --! convert a character to upper case
434  function to_upper(c: character) return character is
435 
436  variable u: character;
437 
438  begin
439 
440  case c is
441  when 'a' => u := 'A';
442  when 'b' => u := 'B';
443  when 'c' => u := 'C';
444  when 'd' => u := 'D';
445  when 'e' => u := 'E';
446  when 'f' => u := 'F';
447  when 'g' => u := 'G';
448  when 'h' => u := 'H';
449  when 'i' => u := 'I';
450  when 'j' => u := 'J';
451  when 'k' => u := 'K';
452  when 'l' => u := 'L';
453  when 'm' => u := 'M';
454  when 'n' => u := 'N';
455  when 'o' => u := 'O';
456  when 'p' => u := 'P';
457  when 'q' => u := 'Q';
458  when 'r' => u := 'R';
459  when 's' => u := 'S';
460  when 't' => u := 'T';
461  when 'u' => u := 'U';
462  when 'v' => u := 'V';
463  when 'w' => u := 'W';
464  when 'x' => u := 'X';
465  when 'y' => u := 'Y';
466  when 'z' => u := 'Z';
467  when others => u := c;
468  end case;
469 
470  return u;
471 
472  end to_upper;
473 
474 
475  --! convert a character to lower case
476  function to_lower(c: character) return character is
477 
478  variable l: character;
479 
480  begin
481 
482  case c is
483  when 'A' => l := 'a';
484  when 'B' => l := 'b';
485  when 'C' => l := 'c';
486  when 'D' => l := 'd';
487  when 'E' => l := 'e';
488  when 'F' => l := 'f';
489  when 'G' => l := 'g';
490  when 'H' => l := 'h';
491  when 'I' => l := 'i';
492  when 'J' => l := 'j';
493  when 'K' => l := 'k';
494  when 'L' => l := 'l';
495  when 'M' => l := 'm';
496  when 'N' => l := 'n';
497  when 'O' => l := 'o';
498  when 'P' => l := 'p';
499  when 'Q' => l := 'q';
500  when 'R' => l := 'r';
501  when 'S' => l := 's';
502  when 'T' => l := 't';
503  when 'U' => l := 'u';
504  when 'V' => l := 'v';
505  when 'W' => l := 'w';
506  when 'X' => l := 'x';
507  when 'Y' => l := 'y';
508  when 'Z' => l := 'z';
509  when others => l := c;
510  end case;
511 
512  return l;
513 
514  end to_lower;
515 
516 
517 
518  --! convert a string to upper case
519  function to_upper(s: string) return string is
520 
521  variable uppercase: string (s'range);
522 
523  begin
524 
525  for i in s'range loop
526  uppercase(i):= to_upper(s(i));
527  end loop;
528  return uppercase;
529 
530  end to_upper;
531 
532 
533 
534  --! convert a string to lower case
535  function to_lower(s: string) return string is
536 
537  variable lowercase: string (s'range);
538 
539  begin
540 
541  for i in s'range loop
542  lowercase(i):= to_lower(s(i));
543  end loop;
544  return lowercase;
545 
546  end to_lower;
547 
548 
549 
550 -- functions to convert strings into other types
551 
552 
553 --! converts a character into a std_logic
554 function to_std_logic(c: character) return std_logic is
555  variable sl: std_logic;
556  begin
557  case c is
558  when 'U' =>
559  sl := 'U';
560  when 'X' =>
561  sl := 'X';
562  when '0' =>
563  sl := '0';
564  when '1' =>
565  sl := '1';
566  when 'Z' =>
567  sl := 'Z';
568  when 'W' =>
569  sl := 'W';
570  when 'L' =>
571  sl := 'L';
572  when 'H' =>
573  sl := 'H';
574  when '-' =>
575  sl := '-';
576  when others =>
577  sl := 'X';
578  end case;
579  return sl;
580  end to_std_logic;
581 
582 
583 --! converts a string into std_logic_vector
584 function to_std_logic_vector(s: string) return std_logic_vector is
585  variable slv: std_logic_vector(s'high-s'low downto 0);
586  variable k: integer;
587 begin
588  k := s'high-s'low;
589  for i in s'range loop
590  slv(k) := to_std_logic(s(i));
591  k := k - 1;
592  end loop;
593  return slv;
594 end to_std_logic_vector;
595 
596 
597 
598 
599 
600 
601 ----------------
602 -- file I/O --
603 ----------------
604 
605 
606 
607 --! read variable length string from input file
608 procedure str_read(file in_file: TEXT;
609  res_string: out string) is
610 
611  variable l: line;
612  variable c: character;
613  variable is_string: boolean;
614 
615  begin
616 
617  readline(in_file, l);
618  -- clear the contents of the result string
619  for i in res_string'range loop
620  res_string(i) := ' ';
621  end loop;
622  -- read all characters of the line, up to the length
623  -- of the results string
624  for i in res_string'range loop
625  read(l, c, is_string);
626  res_string(i) := c;
627  if not is_string then -- found end of line
628  exit;
629  end if;
630  end loop;
631 
632 end str_read;
633 
634 
635 --! print string to a file
636 procedure print(file out_file: TEXT;
637  new_string: in string) is
638 
639  variable l: line;
640 
641  begin
642 
643  write(l, new_string);
644  writeline(out_file, l);
645 
646 end print;
647 
648 
649 --! print character to a file and start new line
650 procedure print(file out_file: TEXT;
651  char: in character) is
652 
653  variable l: line;
654 
655  begin
656 
657  write(l, char);
658  writeline(out_file, l);
659 
660 end print;
661 
662 
663 
664 --! @brief appends contents of a string to a file until line feed occurs
665 --
666 -- @detailed (LF is considered to be the end of the string)
667 procedure str_write(file out_file: TEXT;
668  new_string: in string) is
669  begin
670 
671  for i in new_string'range loop
672  print(out_file, new_string(i));
673  if new_string(i) = LF then -- end of string
674  exit;
675  end if;
676  end loop;
677 
678 end str_write;
679 
680 
681 
682 
683 end txt_util;
std_logic to_std_logicc,
converts a character into std_logic
Definition: txt_util.vhd:73
_library_ ieeeieee
Use IEEE standard definitions library;.
string hstrslv,
convert std_logic_vector into a string in hex format
Definition: txt_util.vhd:49
character chrsl,
converts std_logic into a character
Definition: txt_util.vhd:23
printtext,
prints a message to the screen
Definition: txt_util.vhd:15
character asciiint,
converts an 8-bit integer into a printable character
Definition: txt_util.vhd:26
character to_upperc,
convert a character to upper case
Definition: txt_util.vhd:56
str_readres_string,
read variable length string from input file
Definition: txt_util.vhd:84
std_logic_vector to_std_logic_vectors,
converts a string into std_logic_vector
Definition: txt_util.vhd:76
string strsl,
converts std_logic into a string (1 to 1)
Definition: txt_util.vhd:29
character to_lowerc,
convert a character to lower case
Definition: txt_util.vhd:59
Allows for text output in simulation.
Definition: txt_util.vhd:12