source: firmware/FAD/FACT_FAD_lib/hdl/text_util_pkg.vhd@ 11757

Last change on this file since 11757 was 11755, checked in by neise, 13 years ago
reinit of this svn repos .... it was all too messy deleted the old folders and restarted with FACT_FAD_lib only. (well and the testbenches)
File size: 14.2 KB
Line 
1library ieee;
2use ieee.std_logic_1164.all;
3use std.textio.all;
4
5
6package txt_util is
7
8 -- prints a message to the screen
9 procedure print(text: string);
10
11 -- prints the message when active
12 -- useful for debug switches
13 procedure print(active: boolean; text: string);
14
15 -- converts std_logic into a character
16 function chr(sl: std_logic) return character;
17
18 -- converts std_logic into a string (1 to 1)
19 function str(sl: std_logic) return string;
20
21 -- converts std_logic_vector into a string (binary base)
22 function str(slv: std_logic_vector) return string;
23
24 -- converts boolean into a string
25 function str(b: boolean) return string;
26
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;
30
31 -- converts integer into string using specified base
32 function str(int: integer; base: integer) return string;
33
34 -- converts integer to string, using base 10
35 function str(int: integer) return string;
36
37 -- convert std_logic_vector into a string in hex format
38 function hstr(slv: std_logic_vector) return string;
39
40
41 -- functions to manipulate strings
42 -----------------------------------
43
44 -- convert a character to upper case
45 function to_upper(c: character) return character;
46
47 -- convert a character to lower case
48 function to_lower(c: character) return character;
49
50 -- convert a string to upper case
51 function to_upper(s: string) return string;
52
53 -- convert a string to lower case
54 function to_lower(s: string) return string;
55
56
57
58 -- functions to convert strings into other formats
59 --------------------------------------------------
60
61 -- converts a character into std_logic
62 function to_std_logic(c: character) return std_logic;
63
64 -- converts a string into std_logic_vector
65 function to_std_logic_vector(s: string) return std_logic_vector;
66
67
68
69 -- file I/O
70 -----------
71
72 -- read variable length string from input file
73 procedure str_read(file in_file: TEXT;
74 res_string: out string);
75
76 -- print string to a file and start new line
77 procedure print(file out_file: TEXT;
78 new_string: in string);
79
80 -- print character to a file and start new line
81 procedure print(file out_file: TEXT;
82 char: in character);
83
84end txt_util;
85
86
87
88
89package body txt_util is
90
91
92
93
94 -- prints text to the screen
95
96 procedure print(text: string) is
97 variable msg_line: line;
98 begin
99 write(msg_line, text);
100 writeline(output, msg_line);
101 end print;
102
103
104
105
106 -- prints text to the screen when active
107
108 procedure print(active: boolean; text: string) is
109 begin
110 if active then
111 print(text);
112 end if;
113 end print;
114
115
116 -- converts std_logic into a character
117
118 function chr(sl: std_logic) return character is
119 variable c: character;
120 begin
121 case sl is
122 when 'U' => c:= 'U';
123 when 'X' => c:= 'X';
124 when '0' => c:= '0';
125 when '1' => c:= '1';
126 when 'Z' => c:= 'Z';
127 when 'W' => c:= 'W';
128 when 'L' => c:= 'L';
129 when 'H' => c:= 'H';
130 when '-' => c:= '-';
131 end case;
132 return c;
133 end chr;
134
135
136
137 -- converts std_logic into a string (1 to 1)
138
139 function str(sl: std_logic) return string is
140 variable s: string(1 to 1);
141 begin
142 s(1) := chr(sl);
143 return s;
144 end str;
145
146
147
148 -- converts std_logic_vector into a string (binary base)
149 -- (this also takes care of the fact that the range of
150 -- a string is natural while a std_logic_vector may
151 -- have an integer range)
152
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 function str(b: boolean) return string is
167
168 begin
169 if b then
170 return "true";
171 else
172 return "false";
173 end if;
174 end str;
175
176
177 -- converts an integer into a character
178 -- for 0 to 9 the obvious mapping is used, higher
179 -- values are mapped to the characters A-Z
180 -- (this is usefull for systems with base > 10)
181 -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
182
183 function chr(int: integer) return character is
184 variable c: character;
185 begin
186 case int is
187 when 0 => c := '0';
188 when 1 => c := '1';
189 when 2 => c := '2';
190 when 3 => c := '3';
191 when 4 => c := '4';
192 when 5 => c := '5';
193 when 6 => c := '6';
194 when 7 => c := '7';
195 when 8 => c := '8';
196 when 9 => c := '9';
197 when 10 => c := 'A';
198 when 11 => c := 'B';
199 when 12 => c := 'C';
200 when 13 => c := 'D';
201 when 14 => c := 'E';
202 when 15 => c := 'F';
203 when 16 => c := 'G';
204 when 17 => c := 'H';
205 when 18 => c := 'I';
206 when 19 => c := 'J';
207 when 20 => c := 'K';
208 when 21 => c := 'L';
209 when 22 => c := 'M';
210 when 23 => c := 'N';
211 when 24 => c := 'O';
212 when 25 => c := 'P';
213 when 26 => c := 'Q';
214 when 27 => c := 'R';
215 when 28 => c := 'S';
216 when 29 => c := 'T';
217 when 30 => c := 'U';
218 when 31 => c := 'V';
219 when 32 => c := 'W';
220 when 33 => c := 'X';
221 when 34 => c := 'Y';
222 when 35 => c := 'Z';
223 when others => c := '?';
224 end case;
225 return c;
226 end chr;
227
228
229
230 -- convert integer to string using specified base
231 -- (adapted from Steve Vogwell's posting in comp.lang.vhdl)
232
233 function str(int: integer; base: integer) return string is
234
235 variable temp: string(1 to 10);
236 variable num: integer;
237 variable abs_int: integer;
238 variable len: integer := 1;
239 variable power: integer := 1;
240
241 begin
242
243 -- bug fix for negative numbers
244 abs_int := abs(int);
245
246 num := abs_int;
247
248 while num >= base loop -- Determine how many
249 len := len + 1; -- characters required
250 num := num / base; -- to represent the
251 end loop ; -- number.
252
253 for i in len downto 1 loop -- Convert the number to
254 temp(i) := chr(abs_int/power mod base); -- a string starting
255 power := power * base; -- with the right hand
256 end loop ; -- side.
257
258 -- return result and add sign if required
259 if int < 0 then
260 return '-'& temp(1 to len);
261 else
262 return temp(1 to len);
263 end if;
264
265 end str;
266
267
268 -- convert integer to string, using base 10
269 function str(int: integer) return string is
270
271 begin
272
273 return str(int, 10) ;
274
275 end str;
276
277
278
279 -- converts a std_logic_vector into a hex string.
280 function hstr(slv: std_logic_vector) return string is
281 variable hexlen: integer;
282 variable longslv : std_logic_vector(67 downto 0) := (others => '0');
283 variable hex : string(1 to 16);
284 variable fourbit : std_logic_vector(3 downto 0);
285 begin
286 hexlen := (slv'left+1)/4;
287 if (slv'left+1) mod 4 /= 0 then
288 hexlen := hexlen + 1;
289 end if;
290 longslv(slv'left downto 0) := slv;
291 for i in (hexlen -1) downto 0 loop
292 fourbit := longslv(((i*4)+3) downto (i*4));
293 case fourbit is
294 when "0000" => hex(hexlen -I) := '0';
295 when "0001" => hex(hexlen -I) := '1';
296 when "0010" => hex(hexlen -I) := '2';
297 when "0011" => hex(hexlen -I) := '3';
298 when "0100" => hex(hexlen -I) := '4';
299 when "0101" => hex(hexlen -I) := '5';
300 when "0110" => hex(hexlen -I) := '6';
301 when "0111" => hex(hexlen -I) := '7';
302 when "1000" => hex(hexlen -I) := '8';
303 when "1001" => hex(hexlen -I) := '9';
304 when "1010" => hex(hexlen -I) := 'A';
305 when "1011" => hex(hexlen -I) := 'B';
306 when "1100" => hex(hexlen -I) := 'C';
307 when "1101" => hex(hexlen -I) := 'D';
308 when "1110" => hex(hexlen -I) := 'E';
309 when "1111" => hex(hexlen -I) := 'F';
310 when "ZZZZ" => hex(hexlen -I) := 'z';
311 when "UUUU" => hex(hexlen -I) := 'u';
312 when "XXXX" => hex(hexlen -I) := 'x';
313 when others => hex(hexlen -I) := '?';
314 end case;
315 end loop;
316 return hex(1 to hexlen);
317 end hstr;
318
319
320
321 -- functions to manipulate strings
322 -----------------------------------
323
324
325 -- convert a character to upper case
326
327 function to_upper(c: character) return character is
328
329 variable u: character;
330
331 begin
332
333 case c is
334 when 'a' => u := 'A';
335 when 'b' => u := 'B';
336 when 'c' => u := 'C';
337 when 'd' => u := 'D';
338 when 'e' => u := 'E';
339 when 'f' => u := 'F';
340 when 'g' => u := 'G';
341 when 'h' => u := 'H';
342 when 'i' => u := 'I';
343 when 'j' => u := 'J';
344 when 'k' => u := 'K';
345 when 'l' => u := 'L';
346 when 'm' => u := 'M';
347 when 'n' => u := 'N';
348 when 'o' => u := 'O';
349 when 'p' => u := 'P';
350 when 'q' => u := 'Q';
351 when 'r' => u := 'R';
352 when 's' => u := 'S';
353 when 't' => u := 'T';
354 when 'u' => u := 'U';
355 when 'v' => u := 'V';
356 when 'w' => u := 'W';
357 when 'x' => u := 'X';
358 when 'y' => u := 'Y';
359 when 'z' => u := 'Z';
360 when others => u := c;
361 end case;
362
363 return u;
364
365 end to_upper;
366
367
368 -- convert a character to lower case
369
370 function to_lower(c: character) return character is
371
372 variable l: character;
373
374 begin
375
376 case c is
377 when 'A' => l := 'a';
378 when 'B' => l := 'b';
379 when 'C' => l := 'c';
380 when 'D' => l := 'd';
381 when 'E' => l := 'e';
382 when 'F' => l := 'f';
383 when 'G' => l := 'g';
384 when 'H' => l := 'h';
385 when 'I' => l := 'i';
386 when 'J' => l := 'j';
387 when 'K' => l := 'k';
388 when 'L' => l := 'l';
389 when 'M' => l := 'm';
390 when 'N' => l := 'n';
391 when 'O' => l := 'o';
392 when 'P' => l := 'p';
393 when 'Q' => l := 'q';
394 when 'R' => l := 'r';
395 when 'S' => l := 's';
396 when 'T' => l := 't';
397 when 'U' => l := 'u';
398 when 'V' => l := 'v';
399 when 'W' => l := 'w';
400 when 'X' => l := 'x';
401 when 'Y' => l := 'y';
402 when 'Z' => l := 'z';
403 when others => l := c;
404 end case;
405
406 return l;
407
408 end to_lower;
409
410
411
412 -- convert a string to upper case
413
414 function to_upper(s: string) return string is
415
416 variable uppercase: string (s'range);
417
418 begin
419
420 for i in s'range loop
421 uppercase(i):= to_upper(s(i));
422 end loop;
423 return uppercase;
424
425 end to_upper;
426
427
428
429 -- convert a string to lower case
430
431 function to_lower(s: string) return string is
432
433 variable lowercase: string (s'range);
434
435 begin
436
437 for i in s'range loop
438 lowercase(i):= to_lower(s(i));
439 end loop;
440 return lowercase;
441
442 end to_lower;
443
444
445
446-- functions to convert strings into other types
447
448
449-- converts a character into a std_logic
450
451function to_std_logic(c: character) return std_logic is
452 variable sl: std_logic;
453 begin
454 case c is
455 when 'U' =>
456 sl := 'U';
457 when 'X' =>
458 sl := 'X';
459 when '0' =>
460 sl := '0';
461 when '1' =>
462 sl := '1';
463 when 'Z' =>
464 sl := 'Z';
465 when 'W' =>
466 sl := 'W';
467 when 'L' =>
468 sl := 'L';
469 when 'H' =>
470 sl := 'H';
471 when '-' =>
472 sl := '-';
473 when others =>
474 sl := 'X';
475 end case;
476 return sl;
477 end to_std_logic;
478
479
480-- converts a string into std_logic_vector
481
482function to_std_logic_vector(s: string) return std_logic_vector is
483 variable slv: std_logic_vector(s'high-s'low downto 0);
484 variable k: integer;
485begin
486 k := s'high-s'low;
487 for i in s'range loop
488 slv(k) := to_std_logic(s(i));
489 k := k - 1;
490 end loop;
491 return slv;
492end to_std_logic_vector;
493
494
495
496
497
498
499----------------
500-- file I/O --
501----------------
502
503
504
505-- read variable length string from input file
506
507procedure str_read(file in_file: TEXT;
508 res_string: out string) is
509
510 variable l: line;
511 variable c: character;
512 variable is_string: boolean;
513
514 begin
515
516 readline(in_file, l);
517 -- clear the contents of the result string
518 for i in res_string'range loop
519 res_string(i) := ' ';
520 end loop;
521 -- read all characters of the line, up to the length
522 -- of the results string
523 for i in res_string'range loop
524 read(l, c, is_string);
525 res_string(i) := c;
526 if not is_string then -- found end of line
527 exit;
528 end if;
529 end loop;
530
531end str_read;
532
533
534-- print string to a file
535procedure print(file out_file: TEXT;
536 new_string: in string) is
537
538 variable l: line;
539
540 begin
541
542 write(l, new_string);
543 writeline(out_file, l);
544
545end print;
546
547
548-- print character to a file and start new line
549procedure print(file out_file: TEXT;
550 char: in character) is
551
552 variable l: line;
553
554 begin
555
556 write(l, char);
557 writeline(out_file, l);
558
559end print;
560
561
562
563-- appends contents of a string to a file until line feed occurs
564-- (LF is considered to be the end of the string)
565
566procedure str_write(file out_file: TEXT;
567 new_string: in string) is
568 begin
569
570 for i in new_string'range loop
571 print(out_file, new_string(i));
572 if new_string(i) = LF then -- end of string
573 exit;
574 end if;
575 end loop;
576
577end str_write;
578
579
580
581
582end txt_util;
583
584
Note: See TracBrowser for help on using the repository browser.