2 use ieee.std_logic_1164.all;
8 -- prints a message to the screen
9 procedure print(text: string);
11 -- prints the message when active
12 -- useful for debug switches
13 procedure print(active: boolean; text: string);
15 -- converts std_logic into a character
16 function chr(sl: std_logic) return character;
18 -- converts std_logic into a string (1 to 1)
19 function str(sl: std_logic) return string;
21 -- converts std_logic_vector into a string (binary base)
22 function str(slv: std_logic_vector) return string;
24 -- converts boolean into a string
25 function str(b: boolean) return string;
27 -- converts an integer into a single character
28 -- (can also be used for hex conversion and other bases)
29 function chr(int: integer) return character;
31 -- converts integer into string using specified base
32 function str(int: integer; base: integer) return string;
34 -- converts integer to string, using base 10
35 function str(int: integer) return string;
37 -- convert std_logic_vector into a string in hex format
38 function hstr(slv: std_logic_vector) return string;
41 -- functions to manipulate strings
42 -----------------------------------
44 -- convert a character to upper case
45 function to_upper(c: character) return character;
47 -- convert a character to lower case
48 function to_lower(c: character) return character;
50 -- convert a string to upper case
51 function to_upper(s: string) return string;
53 -- convert a string to lower case
54 function to_lower(s: string) return string;
58 -- functions to convert strings into other formats
59 --------------------------------------------------
61 -- converts a character into std_logic
62 function to_std_logic(c: character) return std_logic;
64 -- converts a string into std_logic_vector
65 function to_std_logic_vector(s: string) return std_logic_vector;
67 -- convert Hex string to 32-bit into std_logic_vector
68 function strhex_to_slv(s : string) return std_logic_vector;
75 -- read variable length string from input file
76 procedure str_read(file in_file: TEXT;
77 res_string: out string);
79 -- print string to a file and start new line
80 procedure print(file out_file: TEXT;
81 new_string: in string);
83 -- print character to a file and start new line
84 procedure print(file out_file: TEXT;
92 package body txt_util is
97 -- prints text to the screen
99 procedure print(text: string) is
100 variable msg_line: line;
102 write(msg_line, text);
103 writeline(output, msg_line);
109 -- prints text to the screen when active
111 procedure print(active: boolean; text: string) is
119 -- converts std_logic into a character
121 function chr(sl: std_logic) return character is
122 variable c: character;
140 -- converts std_logic into a string (1 to 1)
142 function str(sl: std_logic) return string is
143 variable s: string(1 to 1);
151 -- converts std_logic_vector into a string (binary base)
152 -- (this also takes care of the fact that the range of
153 -- a string is natural while a std_logic_vector may
154 -- have an integer range)
156 function str(slv: std_logic_vector) return string is
157 variable result : string (1 to slv'length);
158 variable r : integer;
161 for i in slv'range loop
162 result(r) := chr(slv(i));
169 function str(b: boolean) return string is
180 -- converts an integer into a character
181 -- for 0 to 9 the obvious mapping is used, higher
182 -- values are mapped to the characters A-Z
183 -- (this is usefull for systems with base > 10)
184 -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
186 function chr(int: integer) return character is
187 variable c: character;
226 when others => c := '?';
233 -- convert integer to string using specified base
234 -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
236 function str(int: integer; base: integer) return string is
238 variable temp: string(1 to 10);
239 variable num: integer;
240 variable abs_int: integer;
241 variable len: integer := 1;
242 variable power: integer := 1;
246 -- bug fix for negative numbers
251 while num >= base loop -- Determine how many
252 len := len + 1; -- characters required
253 num := num / base; -- to represent the
254 end loop ; -- number.
256 for i in len downto 1 loop -- Convert the number to
257 temp(i) := chr(abs_int/power mod base); -- a string starting
258 power := power * base; -- with the right hand
261 -- return result and add sign if required
263 return '-'& temp(1 to len);
265 return temp(1 to len);
271 -- convert integer to string, using base 10
272 function str(int: integer) return string is
276 return str(int, 10) ;
282 -- converts a std_logic_vector into a hex string.
283 function hstr(slv: std_logic_vector) return string is
284 variable hexlen: integer;
285 variable longslv : std_logic_vector(67 downto 0) := (others => '0');
286 variable hex : string(1 to 16);
287 variable fourbit : std_logic_vector(3 downto 0);
289 hexlen := (slv'left+1)/4;
290 if (slv'left+1) mod 4 /= 0 then
291 hexlen := hexlen + 1;
293 longslv(slv'left downto 0) := slv;
294 for i in (hexlen -1) downto 0 loop
295 fourbit := longslv(((i*4)+3) downto (i*4));
297 when "0000" => hex(hexlen -I) := '0';
298 when "0001" => hex(hexlen -I) := '1';
299 when "0010" => hex(hexlen -I) := '2';
300 when "0011" => hex(hexlen -I) := '3';
301 when "0100" => hex(hexlen -I) := '4';
302 when "0101" => hex(hexlen -I) := '5';
303 when "0110" => hex(hexlen -I) := '6';
304 when "0111" => hex(hexlen -I) := '7';
305 when "1000" => hex(hexlen -I) := '8';
306 when "1001" => hex(hexlen -I) := '9';
307 when "1010" => hex(hexlen -I) := 'A';
308 when "1011" => hex(hexlen -I) := 'B';
309 when "1100" => hex(hexlen -I) := 'C';
310 when "1101" => hex(hexlen -I) := 'D';
311 when "1110" => hex(hexlen -I) := 'E';
312 when "1111" => hex(hexlen -I) := 'F';
313 when "ZZZZ" => hex(hexlen -I) := 'z';
314 when "UUUU" => hex(hexlen -I) := 'u';
315 when "XXXX" => hex(hexlen -I) := 'x';
316 when others => hex(hexlen -I) := '?';
319 return hex(1 to hexlen);
324 -- functions to manipulate strings
325 -----------------------------------
328 -- convert a character to upper case
330 function to_upper(c: character) return character is
332 variable u: character;
337 when 'a' => u := 'A';
338 when 'b' => u := 'B';
339 when 'c' => u := 'C';
340 when 'd' => u := 'D';
341 when 'e' => u := 'E';
342 when 'f' => u := 'F';
343 when 'g' => u := 'G';
344 when 'h' => u := 'H';
345 when 'i' => u := 'I';
346 when 'j' => u := 'J';
347 when 'k' => u := 'K';
348 when 'l' => u := 'L';
349 when 'm' => u := 'M';
350 when 'n' => u := 'N';
351 when 'o' => u := 'O';
352 when 'p' => u := 'P';
353 when 'q' => u := 'Q';
354 when 'r' => u := 'R';
355 when 's' => u := 'S';
356 when 't' => u := 'T';
357 when 'u' => u := 'U';
358 when 'v' => u := 'V';
359 when 'w' => u := 'W';
360 when 'x' => u := 'X';
361 when 'y' => u := 'Y';
362 when 'z' => u := 'Z';
363 when others => u := c;
371 -- convert a character to lower case
373 function to_lower(c: character) return character is
375 variable l: character;
380 when 'A' => l := 'a';
381 when 'B' => l := 'b';
382 when 'C' => l := 'c';
383 when 'D' => l := 'd';
384 when 'E' => l := 'e';
385 when 'F' => l := 'f';
386 when 'G' => l := 'g';
387 when 'H' => l := 'h';
388 when 'I' => l := 'i';
389 when 'J' => l := 'j';
390 when 'K' => l := 'k';
391 when 'L' => l := 'l';
392 when 'M' => l := 'm';
393 when 'N' => l := 'n';
394 when 'O' => l := 'o';
395 when 'P' => l := 'p';
396 when 'Q' => l := 'q';
397 when 'R' => l := 'r';
398 when 'S' => l := 's';
399 when 'T' => l := 't';
400 when 'U' => l := 'u';
401 when 'V' => l := 'v';
402 when 'W' => l := 'w';
403 when 'X' => l := 'x';
404 when 'Y' => l := 'y';
405 when 'Z' => l := 'z';
406 when others => l := c;
415 -- convert a string to upper case
417 function to_upper(s: string) return string is
419 variable uppercase: string (s'range);
423 for i in s'range loop
424 uppercase(i):= to_upper(s(i));
432 -- convert a string to lower case
434 function to_lower(s: string) return string is
436 variable lowercase: string (s'range);
440 for i in s'range loop
441 lowercase(i):= to_lower(s(i));
449 -- functions to convert strings into other types
452 -- converts a character into a std_logic
454 function to_std_logic(c: character) return std_logic is
455 variable sl: std_logic;
483 -- converts a string into std_logic_vector
485 function to_std_logic_vector(s: string) return std_logic_vector is
486 variable slv: std_logic_vector(s'high-s'low downto 0);
490 for i in s'range loop
491 slv(k) := to_std_logic(s(i));
495 end to_std_logic_vector;
498 -- convert Hex string to 32-bit SLV
499 function strhex_to_slv(s : string) return std_logic_vector is
500 variable int : string(1 to s'length) := s;
501 variable ptr : integer range 0 to 32 := 0;
502 variable slv: std_logic_vector(31 downto 0) := (others=>'0');
504 for i in int'reverse_range loop
506 when '0' => slv(ptr+3 downto ptr) := "0000"; ptr := ptr+4;
507 when '1' => slv(ptr+3 downto ptr) := "0001"; ptr := ptr+4;
508 when '2' => slv(ptr+3 downto ptr) := "0010"; ptr := ptr+4;
509 when '3' => slv(ptr+3 downto ptr) := "0011"; ptr := ptr+4;
510 when '4' => slv(ptr+3 downto ptr) := "0100"; ptr := ptr+4;
511 when '5' => slv(ptr+3 downto ptr) := "0101"; ptr := ptr+4;
512 when '6' => slv(ptr+3 downto ptr) := "0110"; ptr := ptr+4;
513 when '7' => slv(ptr+3 downto ptr) := "0111"; ptr := ptr+4;
514 when '8' => slv(ptr+3 downto ptr) := "1000"; ptr := ptr+4;
515 when '9' => slv(ptr+3 downto ptr) := "1001"; ptr := ptr+4;
516 when 'a'|'A' => slv(ptr+3 downto ptr) := "1010"; ptr := ptr+4;
517 when 'b'|'B' => slv(ptr+3 downto ptr) := "1011"; ptr := ptr+4;
518 when 'c'|'C' => slv(ptr+3 downto ptr) := "1100"; ptr := ptr+4;
519 when 'd'|'D' => slv(ptr+3 downto ptr) := "1101"; ptr := ptr+4;
520 when 'e'|'E' => slv(ptr+3 downto ptr) := "1110"; ptr := ptr+4;
521 when 'f'|'F' => slv(ptr+3 downto ptr) := "1111"; ptr := ptr+4;
533 -- read variable length string from input file
535 procedure str_read(file in_file: TEXT;
536 res_string: out string) is
539 variable c: character;
540 variable is_string: boolean;
544 readline(in_file, l);
545 -- clear the contents of the result string
546 for i in res_string'range loop
547 res_string(i) := ' ';
549 -- read all characters of the line, up to the length
550 -- of the results string
551 for i in res_string'range loop
552 read(l, c, is_string);
554 if not is_string then -- found end of line
562 -- print string to a file
563 procedure print(file out_file: TEXT;
564 new_string: in string) is
570 write(l, new_string);
571 writeline(out_file, l);
576 -- print character to a file and start new line
577 procedure print(file out_file: TEXT;
578 char: in character) is
585 writeline(out_file, l);
591 -- appends contents of a string to a file until line feed occurs
592 -- (LF is considered to be the end of the string)
594 procedure str_write(file out_file: TEXT;
595 new_string: in string) is
598 for i in new_string'range loop
599 print(out_file, new_string(i));
600 if new_string(i) = LF then -- end of string