PlTbUtils  1.3
PlTbUtils is a collection of functions, procedures and components for easily creating stimuli and checking response in automatic self-checking testbenches.
txt_util.vhd
Go to the documentation of this file.
1 
23 
24 library ieee;
25  use ieee.std_logic_1164.all;
26  use std.textio.all;
27 
30 
31 package txt_util is
32 
39  procedure print(active: boolean; text: string);
40 
46  procedure print(text: string);
47 
49  function chr(sl: std_logic) return character;
50 
52  function str(sl: std_logic) return string;
53 
55  function str(slv: std_logic_vector) return string;
56 
58  function str(b: boolean) return string;
59 
62  function chr(int: integer) return character;
63 
65  function str(int: integer; base: integer) return string;
66 
68  function str(int: integer) return string;
69 
71  function hstr(slv: std_logic_vector) return string;
72 
73  -- functions to manipulate strings
74 
76  function to_upper(c: character) return character;
77 
79  function to_lower(c: character) return character;
80 
82  function to_upper(s: string) return string;
83 
85  function to_lower(s: string) return string;
86 
88  function is_whitespace(c: character) return boolean;
89 
91  function strip_whitespace(s: string) return string;
92 
94  function first_string(s: string) return string;
95 
98  procedure chomp(variable s: inout string; variable shead: out string);
99 
100  -- functions to convert strings into other formats
101 
103  function to_std_logic(c: character) return std_logic;
104 
106  function chr_to_slv(c: character) return std_logic_vector;
107 
109  function chr_to_int(c: character) return integer;
110 
112  function to_std_logic_vector(s: string) return std_logic_vector;
113 
115  function hstr_to_slv(s: string) return std_logic_vector;
116 
118  function str_to_int(s: string) return integer;
119 
120  -- file I/O
121 
123  procedure str_read(
124  file in_file: TEXT;
125  res_string: out string
126  );
127 
134  procedure print(
135  file out_file: text;
136  new_string: in string
137  );
138 
145  procedure print(
146  file out_file: text;
147  char: in character
148  );
149 
150 end package txt_util;
151 
152 package body txt_util is
153 
155 
156  procedure print(text: string) is
157  variable msg_line : line;
158  begin
159  write(msg_line, text);
160  writeline(output, msg_line);
161  end print;
162 
164 
165  procedure print(active: boolean; text: string) is
166  begin
167 
168  if (active) then
169  print(text);
170  end if;
171 
172  end print;
173 
175 
176  function chr(sl: std_logic) return character is
177  variable c : character;
178  begin
179 
180  case sl is
181 
182  when 'U' =>
183  c := 'U';
184  when 'X' =>
185  c := 'X';
186  when '0' =>
187  c := '0';
188  when '1' =>
189  c := '1';
190  when 'Z' =>
191  c := 'Z';
192  when 'W' =>
193  c := 'W';
194  when 'L' =>
195  c := 'L';
196  when 'H' =>
197  c := 'H';
198  when '-' =>
199  c := '-';
200 
201  end case;
202 
203  return c;
204  end chr;
205 
207 
208  function str(sl: std_logic) return string is
209  variable s : string(1 to 1);
210  begin
211  s(1) := chr(sl);
212  return s;
213  end str;
214 
220 
221  function str(slv: std_logic_vector) return string is
222  variable result : string (1 to slv'length);
223  variable r : integer;
224  begin
225  r := 1;
226  for i in slv'range loop
227  result(r) := chr(slv(i));
228  r := r + 1;
229  end loop;
230  return result;
231  end str;
232 
234 
235  function str(b: boolean) return string is
236 
237  begin
238 
239  if (b) then
240  return "true";
241  else
242  return "false";
243  end if;
244 
245  end str;
246 
253 
254  function chr(int: integer) return character is
255  variable c : character;
256  begin
257 
258  case int is
259 
260  when 0 =>
261  c := '0';
262  when 1 =>
263  c := '1';
264  when 2 =>
265  c := '2';
266  when 3 =>
267  c := '3';
268  when 4 =>
269  c := '4';
270  when 5 =>
271  c := '5';
272  when 6 =>
273  c := '6';
274  when 7 =>
275  c := '7';
276  when 8 =>
277  c := '8';
278  when 9 =>
279  c := '9';
280  when 10 =>
281  c := 'A';
282  when 11 =>
283  c := 'B';
284  when 12 =>
285  c := 'C';
286  when 13 =>
287  c := 'D';
288  when 14 =>
289  c := 'E';
290  when 15 =>
291  c := 'F';
292  when 16 =>
293  c := 'G';
294  when 17 =>
295  c := 'H';
296  when 18 =>
297  c := 'I';
298  when 19 =>
299  c := 'J';
300  when 20 =>
301  c := 'K';
302  when 21 =>
303  c := 'L';
304  when 22 =>
305  c := 'M';
306  when 23 =>
307  c := 'N';
308  when 24 =>
309  c := 'O';
310  when 25 =>
311  c := 'P';
312  when 26 =>
313  c := 'Q';
314  when 27 =>
315  c := 'R';
316  when 28 =>
317  c := 'S';
318  when 29 =>
319  c := 'T';
320  when 30 =>
321  c := 'U';
322  when 31 =>
323  c := 'V';
324  when 32 =>
325  c := 'W';
326  when 33 =>
327  c := 'X';
328  when 34 =>
329  c := 'Y';
330  when 35 =>
331  c := 'Z';
332  when others =>
333  c := '?';
334 
335  end case;
336 
337  return c;
338  end chr;
339 
343 
344  function str(int: integer; base: integer) return string is
345 
346  variable temp : string(1 to 10);
347  variable num : integer;
348  variable abs_int : integer;
349  variable len : integer := 1;
350  variable power : integer := 1;
351 
352  begin
353 
354  -- bug fix for negative numbers
355  abs_int := abs(int);
356 
357  num := abs_int;
358 
359  while num >= base loop -- Determine how many
360  len := len + 1; -- characters required
361  num := num / base; -- to represent the
362  end loop; -- number.
363 
364  for i in len downto 1 loop -- Convert the number to
365  temp(i) := chr(abs_int / power mod base); -- a string starting
366  power := power * base; -- with the right hand
367  end loop; -- side.
368 
370  if (int < 0) then
371  return '-'& temp(1 to len);
372  else
373  return temp(1 to len);
374  end if;
375 
376  end str;
377 
379 
380  function str(int: integer) return string is
381 
382  begin
383 
384  return str(int, 10);
385 
386  end str;
387 
389 
390  function hstr(slv: std_logic_vector) return string is
391  variable hexlen : integer;
392  variable longslv : std_logic_vector(67 downto 0) := (others => '0');
393  variable hex : string(1 to 16);
394  variable fourbit : std_logic_vector(3 downto 0);
395  begin
396  hexlen := (slv'left + 1) / 4;
397 
398  if (slv'left + 1) mod 4 /= 0 then
399  hexlen := hexlen + 1;
400  end if;
401 
402  longslv(slv'left downto 0) := slv;
403  for i in (hexlen -1) downto 0 loop
404  fourbit := longslv(((i * 4) + 3) downto (i * 4));
405 
406  case fourbit is
407 
408  when "0000" =>
409  hex(hexlen - I) := '0';
410  when "0001" =>
411  hex(hexlen - I) := '1';
412  when "0010" =>
413  hex(hexlen - I) := '2';
414  when "0011" =>
415  hex(hexlen - I) := '3';
416  when "0100" =>
417  hex(hexlen - I) := '4';
418  when "0101" =>
419  hex(hexlen - I) := '5';
420  when "0110" =>
421  hex(hexlen - I) := '6';
422  when "0111" =>
423  hex(hexlen - I) := '7';
424  when "1000" =>
425  hex(hexlen - I) := '8';
426  when "1001" =>
427  hex(hexlen - I) := '9';
428  when "1010" =>
429  hex(hexlen - I) := 'A';
430  when "1011" =>
431  hex(hexlen - I) := 'B';
432  when "1100" =>
433  hex(hexlen - I) := 'C';
434  when "1101" =>
435  hex(hexlen - I) := 'D';
436  when "1110" =>
437  hex(hexlen - I) := 'E';
438  when "1111" =>
439  hex(hexlen - I) := 'F';
440  when "ZZZZ" =>
441  hex(hexlen - I) := 'z';
442  when "UUUU" =>
443  hex(hexlen - I) := 'u';
444  when "XXXX" =>
445  hex(hexlen - I) := 'x';
446  when others =>
447  hex(hexlen - I) := '?';
448 
449  end case;
450 
451  end loop;
452  return hex(1 to hexlen);
453  end hstr;
454 
455  -- functions to manipulate strings
456 
458 
459  function to_upper(c: character) return character is
460 
461  variable u : character;
462 
463  begin
464 
465  case c is
466 
467  when 'a' =>
468  u := 'A';
469  when 'b' =>
470  u := 'B';
471  when 'c' =>
472  u := 'C';
473  when 'd' =>
474  u := 'D';
475  when 'e' =>
476  u := 'E';
477  when 'f' =>
478  u := 'F';
479  when 'g' =>
480  u := 'G';
481  when 'h' =>
482  u := 'H';
483  when 'i' =>
484  u := 'I';
485  when 'j' =>
486  u := 'J';
487  when 'k' =>
488  u := 'K';
489  when 'l' =>
490  u := 'L';
491  when 'm' =>
492  u := 'M';
493  when 'n' =>
494  u := 'N';
495  when 'o' =>
496  u := 'O';
497  when 'p' =>
498  u := 'P';
499  when 'q' =>
500  u := 'Q';
501  when 'r' =>
502  u := 'R';
503  when 's' =>
504  u := 'S';
505  when 't' =>
506  u := 'T';
507  when 'u' =>
508  u := 'U';
509  when 'v' =>
510  u := 'V';
511  when 'w' =>
512  u := 'W';
513  when 'x' =>
514  u := 'X';
515  when 'y' =>
516  u := 'Y';
517  when 'z' =>
518  u := 'Z';
519  when others =>
520  u := c;
521 
522  end case;
523 
524  return u;
525 
526  end to_upper;
527 
529 
530  function to_lower(c: character) return character is
531 
532  variable l : character;
533 
534  begin
535 
536  case c is
537 
538  when 'A' =>
539  l := 'a';
540  when 'B' =>
541  l := 'b';
542  when 'C' =>
543  l := 'c';
544  when 'D' =>
545  l := 'd';
546  when 'E' =>
547  l := 'e';
548  when 'F' =>
549  l := 'f';
550  when 'G' =>
551  l := 'g';
552  when 'H' =>
553  l := 'h';
554  when 'I' =>
555  l := 'i';
556  when 'J' =>
557  l := 'j';
558  when 'K' =>
559  l := 'k';
560  when 'L' =>
561  l := 'l';
562  when 'M' =>
563  l := 'm';
564  when 'N' =>
565  l := 'n';
566  when 'O' =>
567  l := 'o';
568  when 'P' =>
569  l := 'p';
570  when 'Q' =>
571  l := 'q';
572  when 'R' =>
573  l := 'r';
574  when 'S' =>
575  l := 's';
576  when 'T' =>
577  l := 't';
578  when 'U' =>
579  l := 'u';
580  when 'V' =>
581  l := 'v';
582  when 'W' =>
583  l := 'w';
584  when 'X' =>
585  l := 'x';
586  when 'Y' =>
587  l := 'y';
588  when 'Z' =>
589  l := 'z';
590  when others =>
591  l := c;
592 
593  end case;
594 
595  return l;
596 
597  end to_lower;
598 
600 
601  function to_upper(s: string) return string is
602 
603  variable uppercase : string (s'range);
604 
605  begin
606 
607  for i in s'range loop
608  uppercase(i) := to_upper(s(i));
609  end loop;
610  return uppercase;
611 
612  end to_upper;
613 
615 
616  function to_lower(s: string) return string is
617 
618  variable lowercase : string (s'range);
619 
620  begin
621 
622  for i in s'range loop
623  lowercase(i) := to_lower(s(i));
624  end loop;
625  return lowercase;
626 
627  end to_lower;
628 
630 
631  function is_whitespace(c: character) return boolean is
632 
633  begin
634 
635  if (c = ' ') or (c = HT) then
636  return true;
637  else
638  return false;
639  end if;
640 
641  end is_whitespace;
642 
644 
645  function strip_whitespace(s: string) return string is
646 
647  variable stemp : string (s'range);
648  variable j, k : positive := 1;
649 
650  begin
651 
652  -- fill stemp with blanks
653  for i in s'range loop
654  stemp(i) := ' ';
655  end loop;
656 
657  -- find first non-whitespace in s
658  for i in s'range loop
659 
660  if (is_whitespace(s(i))) then
661  j := j + 1;
662  else
663  exit;
664  end if;
665 
666  end loop;
667  -- j points to first non-whitespace
668 
669  -- copy remainder of s into stemp
670  -- starting at 1
671  for i in j to s'length loop
672  stemp(k) := s(i);
673  k := k + 1;
674  end loop;
675 
676  return stemp;
677 
678  end strip_whitespace;
679 
681 
682  function first_string(s: string) return string is
683 
684  variable stemp, s2 : string (s'range);
685 
686  begin
687 
688  -- fill s2 with blanks
689  for i in s'range loop
690  s2(i) := ' ';
691  end loop;
692 
693  -- remove leading whitespace
694  stemp := strip_whitespace(s);
695 
696  -- copy until first whitespace
697  for i in stemp'range loop
698 
699  if (not is_whitespace(stemp(i))) then
700  s2(i) := stemp(i);
701  else
702  exit;
703  end if;
704 
705  end loop;
706 
707  return s2;
708 
709  end first_string;
710 
712 
713  procedure chomp(variable s: inout string; variable shead: out string) is
714 
715  variable stemp, stemp2 : string (s'range);
716  variable j, k : positive := 1;
717 
718  begin
719 
720  -- fill stemp and stemp2 with blanks
721  for i in s'range loop
722  stemp(i) := ' '; stemp2(i) := ' ';
723  end loop;
724 
725  stemp := strip_whitespace(s);
726 
727  shead := first_string(stemp);
728 
729  -- find first whitespace in stemp
730  for i in stemp'range loop
731 
732  if (not is_whitespace(stemp(i))) then
733  j := j + 1;
734  else
735  exit;
736  end if;
737 
738  end loop;
740 
743  for i in j to stemp'length loop
744  stemp2(k) := stemp(i);
745  k := k + 1;
746  end loop;
747 
748  s := stemp2;
749 
750  end chomp;
751 
752  -- functions to convert strings into other types
753 
755 
756  function to_std_logic(c: character) return std_logic is
757  variable sl : std_logic;
758  begin
759 
760  case c is
761 
762  when 'U' =>
763  sl := 'U';
764  when 'X' =>
765  sl := 'X';
766  when '0' =>
767  sl := '0';
768  when '1' =>
769  sl := '1';
770  when 'Z' =>
771  sl := 'Z';
772  when 'W' =>
773  sl := 'W';
774  when 'L' =>
775  sl := 'L';
776  when 'H' =>
777  sl := 'H';
778  when '-' =>
779  sl := '-';
780  when others =>
781  sl := 'X';
782 
783  end case;
784 
785  return sl;
786  end to_std_logic;
787 
789 
790  function chr_to_slv(c: character) return std_logic_vector is
791  variable slv : std_logic_vector(3 downto 0);
792  begin
793 
794  case c is
795 
796  when '0' =>
797  slv := "0000";
798  when '1' =>
799  slv := "0001";
800  when '2' =>
801  slv := "0010";
802  when '3' =>
803  slv := "0011";
804  when '4' =>
805  slv := "0100";
806  when '5' =>
807  slv := "0101";
808  when '6' =>
809  slv := "0110";
810  when '7' =>
811  slv := "0111";
812  when '8' =>
813  slv := "1000";
814  when '9' =>
815  slv := "1001";
816  when 'A' | 'a' =>
817  slv := "1010";
818  when 'B' | 'b' =>
819  slv := "1011";
820  when 'C' | 'c' =>
821  slv := "1100";
822  when 'D' | 'd' =>
823  slv := "1101";
824  when 'E' | 'e' =>
825  slv := "1110";
826  when 'F' | 'f' =>
827  slv := "1111";
828  when others =>
829  null;
830 
831  end case;
832 
833  return slv;
834  end chr_to_slv;
835 
837 
838  function chr_to_int(c: character) return integer is
839  variable x : integer;
840  begin
841 
842  case c is
843 
844  when '0' =>
845  x := 0;
846  when '1' =>
847  x := 1;
848  when '2' =>
849  x := 2;
850  when '3' =>
851  x := 3;
852  when '4' =>
853  x := 4;
854  when '5' =>
855  x := 5;
856  when '6' =>
857  x := 6;
858  when '7' =>
859  x := 7;
860  when '8' =>
861  x := 8;
862  when '9' =>
863  x := 9;
864  when others =>
865  null;
866 
867  end case;
868 
869  return x;
870  end chr_to_int;
871 
873 
874  function to_std_logic_vector(s: string) return std_logic_vector is
875  variable slv : std_logic_vector(s'high - s'low downto 0);
876  variable k : integer;
877  begin
878  k := s'high - s'low;
879  for i in s'range loop
880  slv(k) := to_std_logic(s(i));
881  k := k - 1;
882  end loop;
883  return slv;
884  end to_std_logic_vector;
885 
887 
888  function hstr_to_slv(s: string) return std_logic_vector is
889  variable slv : std_logic_vector(((s'length*4)-1) downto 0) := (others => '0');
890  variable k : integer;
891  begin
892  for i in s'range loop
893  slv := slv((slv'length - 5) downto 0) & chr_to_slv(s(i));
894  end loop;
895  return slv;
896  end hstr_to_slv;
897 
899 
900  function str_to_int(s: string) return integer is
901  variable k : integer;
902  begin
903  k := 0;
904  for i in s'range loop
905  k := (k * 10) + chr_to_int(s(i));
906  end loop;
907  return k;
908  end str_to_int;
909 
910  -- file I/O --
911 
913 
914  procedure str_read(file in_file: TEXT;
915  res_string: out string) is
916 
917  variable l : line;
918  variable c : character;
919  variable is_string : boolean;
920 
921  begin
922 
923  readline(in_file, l);
924  -- clear the contents of the result string
925  for i in res_string'range loop
926  res_string(i) := ' ';
927  end loop;
928  -- read all characters of the line, up to the length
929  -- of the results string
930  for i in res_string'range loop
931 
932  -- JFF - new
933  --
934  read(l, c, is_string);
935 
936  if (is_string) then
937  res_string(i) := c;
938  else
939  exit;
940  end if;
941 
942  -- JFF - was duplicating the last char if no
943  -- space at the end of the line
944  --
945  -- read(l, c, is_string);
946  -- res_string(i) := c;
947  -- if not is_string then --! found end of line
948  -- exit;
949  -- end if;
950 
951  end loop;
952 
953  end str_read;
954 
956  procedure print(file out_file: TEXT;
957  new_string: in string) is
958 
959  variable l : line;
960 
961  begin
962 
963  write(l, new_string);
964  writeline(out_file, l);
965 
966  end print;
967 
969  procedure print(file out_file: TEXT;
970  char: in character) is
971 
972  variable l : line;
973 
974  begin
975 
976  write(l, char);
977  writeline(out_file, l);
978 
979  end print;
980 
983 
984  procedure str_write(file out_file: TEXT;
985  new_string: in string) is
986  begin
987 
988  for i in new_string'range loop
989  print(out_file, new_string(i));
990 
991  if (new_string(i) = LF) then -- end of string
992  exit;
993  end if;
994 
995  end loop;
996 
997  end str_write;
998 
999 end txt_util;
1000 
Defines useful functions an procedures for text handling text in VHDL.
Definition: txt_util.vhd:31
integer chr_to_intc,
converts a character into int (JFF)
Definition: txt_util.vhd:109
std_logic_vector chr_to_slvc,
converts a hex character into std_logic_vector(JFF)
Definition: txt_util.vhd:106
string strsl,
converts std_logic into a string (1 to 1)
Definition: txt_util.vhd:52
boolean is_whitespacec,
checks if whitespace (JFF)
Definition: txt_util.vhd:88
chomps,shead,
finds the first non-whitespace substring in a string and (JFF)
Definition: txt_util.vhd:98
integer str_to_ints,
converts a decimal string into an integer (JFF)
Definition: txt_util.vhd:118
std_logic to_std_logicc,
converts a character into std_logic
Definition: txt_util.vhd:103
character chrsl,
converts std_logic into a character
Definition: txt_util.vhd:49
string first_strings,
return first nonwhitespace substring (JFF)
Definition: txt_util.vhd:94
character to_upperc,
convert a character to upper case
Definition: txt_util.vhd:76
std_logic_vector to_std_logic_vectors,
converts a binary string into std_logic_vector
Definition: txt_util.vhd:112
string hstrslv,
convert std_logic_vector into a string in hex format
Definition: txt_util.vhd:71
character to_lowerc,
convert a character to lower case
Definition: txt_util.vhd:79
string strip_whitespaces,
remove leading whitespace (JFF)
Definition: txt_util.vhd:91
str_readres_string,
read variable length string from input file
Definition: txt_util.vhd:123
std_logic_vector hstr_to_slvs,
converts a hex string into std_logic_vector(JFF)
Definition: txt_util.vhd:115
printactive,text,
prints the message when active
Definition: txt_util.vhd:39