1 ------------------------------------------------------------------- 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 ------------------------------------------------------------------- 8 use ieee.std_logic_1164.
all;
11 --! @brief Allows for text output in simulation 14 --! prints a message to the screen 17 --! @brief prints the message when active 19 -- useful for debug switches 20 procedure print(
active: ; text: );
22 --! converts std_logic into a character 23 function chr(sl: )
return ;
25 --! converts an 8-bit integer into a printable character 28 --! converts std_logic into a string (1 to 1) 29 function str(sl: )
return ;
31 --! converts std_logic_vector into a string (binary base) 32 function str(slv: )
return ;
34 --! converts boolean into a string 35 function str(b: )
return ;
37 --! @brief converts an integer into a single character 39 -- (can also be used for hex conversion and other bases) 40 function chr(int: )
return ;
42 --! converts integer into string using specified base 43 function str(int: ;
base: )
return ;
45 --! converts integer to string, using base 10 46 function str(int: )
return ;
48 --! convert std_logic_vector into a string in hex format 49 function hstr(slv: )
return ;
52 -- functions to manipulate strings 53 ----------------------------------- 55 --! convert a character to upper case 58 --! convert a character to lower case 61 --! convert a string to upper case 64 --! convert a string to lower case 69 -- functions to convert strings into other formats 70 -------------------------------------------------- 72 --! converts a character into std_logic 75 --! converts a string into std_logic_vector 83 --! read variable length string from input file 87 --! print string to a file and start new line 88 procedure print(
file out_file: TEXT;
91 --! print character to a file and start new line 92 procedure print(
file out_file: TEXT;
102 --! prints text to the screen 104 variable msg_line: line;
106 write
(msg_line, text
);
107 writeline
(output, msg_line
);
110 --! prints text to the screen when active 111 procedure print(
active: ; text: )
is 119 --! converts std_logic into a character 120 function chr(sl: )
return is 139 --! converts std_logic into a string (1 to 1) 140 function str(sl: )
return is 141 variable s:
(1 to 1);
148 --! @brief converts std_logic_vector into a string (binary base) 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: )
return is 154 variable result :
(1 to slv'
length);
158 for i
in slv'
range loop 159 result
(r
) := chr
(slv
(i
));
166 --! @brief converts boolean into a string 167 function str(b: )
return is 178 --! @brief converts an integer into a character 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: )
return is 224 when others => c := '?';
229 -- Convert an 8-bit integer to an ascii value. Return '?' for all control 231 function ascii (int: )
return is 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";
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 := '?';
338 --! @brief convert integer to string using specified base 340 -- @detailed (adapted from Steve Vogwell's posting in comp.lang.vhdl) 341 function str(int: ;
base: )
return is 343 variable temp:
(1 to 10);
347 variable power: :=
1;
351 -- bug fix for negative numbers 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. 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 366 -- return result and add sign if required 368 return '-'& temp
(1 to len
);
370 return temp
(1 to len
);
376 --! convert integer to string, using base 10 377 function str(int: )
return is 381 return str
(int,
10) ;
387 --! converts a std_logic_vector into a hex string. 390 variable longslv :
(67 downto 0) :=
(others => '
0'
);
391 variable hex :
(1 to 16);
392 variable fourbit :
(3 downto 0);
394 hexlen :=
(slv'
left+
1)/
4;
395 if (slv'
left+
1) mod 4 /=
0 then 396 hexlen := hexlen +
1;
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));
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
) := '?';
424 return hex
(1 to hexlen
);
429 -- functions to manipulate strings 430 ----------------------------------- 433 --! convert a character to upper case 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;
475 --! convert a character to lower case 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;
518 --! convert a string to upper case 521 variable uppercase:
(s'
range);
525 for i
in s'
range loop 526 uppercase
(i
):= to_upper
(s
(i
));
534 --! convert a string to lower case 537 variable lowercase:
(s'
range);
541 for i
in s'
range loop 542 lowercase
(i
):= to_lower
(s
(i
));
550 -- functions to convert strings into other types 553 --! converts a character into a std_logic 583 --! converts a string into std_logic_vector 585 variable slv:
(s'
high-s'
low downto 0);
589 for i
in s'
range loop 590 slv
(k
) := to_std_logic
(s
(i
));
594 end to_std_logic_vector;
607 --! read variable length string from input file 613 variable is_string: ;
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
) := ' ';
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
);
627 if not is_string
then -- found end of line 635 --! print string to a file 636 procedure print(
file out_file: TEXT;
643 write
(l, new_string
);
644 writeline
(out_file, l
);
649 --! print character to a file and start new line 650 procedure print(
file out_file: TEXT;
658 writeline
(out_file, l
);
664 --! @brief appends contents of a string to a file until line feed occurs 666 -- @detailed (LF is considered to be the end of the string) 667 procedure str_write(
file out_file: TEXT;
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 std_logic to_std_logicc,
converts a character into std_logic
_library_ ieeeieee
Use IEEE standard definitions library;.
string hstrslv,
convert std_logic_vector into a string in hex format
character chrsl,
converts std_logic into a character
printtext,
prints a message to the screen
character asciiint,
converts an 8-bit integer into a printable character
character to_upperc,
convert a character to upper case
str_readres_string,
read variable length string from input file
std_logic_vector to_std_logic_vectors,
converts a string into std_logic_vector
string strsl,
converts std_logic into a string (1 to 1)
character to_lowerc,
convert a character to lower case
Allows for text output in simulation.