-
Notifications
You must be signed in to change notification settings - Fork 0
/
string_pkg.vhd
60 lines (47 loc) · 1.39 KB
/
string_pkg.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
library ieee;
use ieee.std_logic_1164.all;
library STD;
use std.textio.all;
package string_pkg is
function to_string ( v : std_logic ) return string;
function to_string ( n : integer range -1024 to 1024 ) return string;
function nat_to_string ( nat : natural range 0 to 1024 ) return string;
end string_pkg;
package body string_pkg is
function to_string ( v : std_logic ) return string is
begin
case v is
when '1' => return "1";
when '0' => return "0";
when 'H' => return "H";
when 'L' => return "L";
when 'X' => return "X";
when '-' => return "-";
when 'U' => return "U";
when others => return "?";
end case;
end to_string;
function digit_to_char ( d : integer ) return character is
begin
return ( character'val( character'pos('0') + d ) );
end digit_to_char;
function nat_to_string ( nat : natural range 0 to 1024) return string is
variable res : string(1 to 4);
variable n : natural;
begin
n := nat;
for i in 4 downto 1 loop
res(i) := digit_to_char( n mod 10);
n := n / 10;
end loop;
return res;
end nat_to_string;
function to_string ( n : integer range -1024 to 1024) return string is
begin
if 0 > n then
return("-" & nat_to_string( abs(n)));
else
return(" " & nat_to_string( natural( n )));
end if;
end to_string;
end string_pkg;