1 -----------------------------------------------------------------
2 -- Copyright (c) 1997 Ben Cohen. All rights reserved.
3 -- This model can be used in conjunction with the Kluwer Academic books
4 -- "VHDL Coding Styles and Methodologies", ISBN: 0-7923-9598-0
5 -- "VHDL Amswers to Frequently Asked Questions", Kluwer Academic
6 -- by Ben Cohen. email: vhdlcohen@aol.com
7 --
8 -- This source file for the Image Package
9 -- may be used and distributed without restriction provided
10 -- that this copyright statement is not removed from the file
11 -- and that any derivative work contains this copyright notice.
12 ---------------------------------------------------------------
13 library IEEE;
14 use IEEE.Std_Logic_1164.all;
15 use IEEE.Std_Logic_TextIO.all;
16 use IEEE.Std_Logic_Arith.all;
17
18 library Std;
19 use STD.TextIO.all;
20
21 package image_pkg is
22 function Image(In_Image : Time) return String;
23 function Image(In_Image : Bit) return String;
24 function Image(In_Image : Bit_Vector) return String;
25 function Image(In_Image : Integer) return String;
26 function Image(In_Image : Real) return String;
27 function Image(In_Image : Std_uLogic) return String;
28 function Image(In_Image : Std_uLogic_Vector) return String;
29 function Image(In_Image : Std_Logic_Vector) return String;
30 function Image(In_Image : Signed) return String;
31 function Image(In_Image : UnSigned) return String;
32
33 function HexImage(InStrg : String) return String;
34 function HexImage(In_Image : Bit_Vector) return String;
35 function HexImage(In_Image : Std_uLogic_Vector) return String;
36 function HexImage(In_Image : Std_Logic_Vector) return String;
37 function HexImage(In_Image : Signed) return String;
38 function HexImage(In_Image : UnSigned) return String;
39
40 function DecImage(In_Image : Bit_Vector) return String;
41 function DecImage(In_Image : Std_uLogic_Vector) return String;
42 function DecImage(In_Image : Std_Logic_Vector) return String;
43 function DecImage(In_Image : Signed) return String;
44 function DecImage(In_Image : UnSigned) return String;
45 end image_pkg;
46
47 package body image_pkg is
48 function Image(In_Image : Time) return String is
49 variable L : Line; -- access type
50 variable W : String(1 to 25) := (others => ' ');
51 -- Long enough to hold a time string
52 begin
53 -- the WRITE procedure creates an object with "NEW".
54 -- L is passed as an output of the procedure.
55 Std.TextIO.WRITE(L, in_image);
56 -- Copy L.all onto W
57 W(L.all'range) := L.all;
58 Deallocate(L);
59 return W;
60 end Image;
61
62 function Image(In_Image : Bit) return String is
63 variable L : Line; -- access type
64 variable W : String(1 to 3) := (others => ' ');
65 begin
66 Std.TextIO.WRITE(L, in_image);
67 W(L.all'range) := L.all;
68 Deallocate(L);
69 return W;
70 end Image;
71
72 function Image(In_Image : Bit_Vector) return String is
73 variable L : Line; -- access type
74 variable W : String(1 to In_Image'length) := (others => ' ');
75 begin
76 Std.TextIO.WRITE(L, in_image);
77 W(L.all'range) := L.all;
78 Deallocate(L);
79 return W;
80 end Image;
81
82 function Image(In_Image : Integer) return String is
83 variable L : Line; -- access type
84 variable W : String(1 to 32) := (others => ' ');
85 -- Long enough to hold a time string
86 begin
87 Std.TextIO.WRITE(L, in_image);
88 W(L.all'range) := L.all;
89 Deallocate(L);
90 return W;
91 end Image;
92
93 function Image(In_Image : Real) return String is
94 variable L : Line; -- access type
95 variable W : String(1 to 32) := (others => ' ');
96 -- Long enough to hold a time string
97 begin
98 Std.TextIO.WRITE(L, in_image);
99 W(L.all'range) := L.all;
100 Deallocate(L);
101 return W;
102 end Image;
103
104 function Image(In_Image : Std_uLogic) return String is
105 variable L : Line; -- access type
106 variable W : String(1 to 3) := (others => ' ');
107 begin
108 IEEE.Std_Logic_Textio.WRITE(L, in_image);
109 W(L.all'range) := L.all;
110 Deallocate(L);
111 return W;
112 end Image;
113
114 function Image(In_Image : Std_uLogic_Vector) return String is
115 variable L : Line; -- access type
116 variable W : String(1 to In_Image'length) := (others => ' ');
117 begin
118 IEEE.Std_Logic_Textio.WRITE(L, in_image);
119 W(L.all'range) := L.all;
120 Deallocate(L);
121 return W;
122 end Image;
123
124 function Image(In_Image : Std_Logic_Vector) return String is
125 variable L : Line; -- access type
126 variable W : String(1 to In_Image'length) := (others => ' ');
127 begin
128 IEEE.Std_Logic_TextIO.WRITE(L, In_Image);
129 W(L.all'range) := L.all;
130 Deallocate(L);
131 return W;
132 end Image;
133
134 function Image(In_Image : Signed) return String is
135 begin
136 return Image(Std_Logic_Vector(In_Image));
137 end Image;
138
139 function Image(In_Image : UnSigned) return String is
140 begin
141 return Image(Std_Logic_Vector(In_Image));
142 end Image;
143
144 function HexImage(InStrg : String) return String is
145 subtype Int03_Typ is Integer range 0 to 3;
146 variable Result : string(1 to ((InStrg'length - 1)/4)+1) :=
147 (others => '0');
148 variable StrTo4 : string(1 to Result'length * 4) :=
149 (others => '0');
150 variable MTspace : Int03_Typ; -- Empty space to fill in
151 variable Str4 : String(1 to 4);
152 variable Group_v : Natural := 0;
153 begin
154 MTspace := Result'length * 4 - InStrg'length;
155 StrTo4(MTspace + 1 to StrTo4'length) := InStrg; -- padded with '0'
156 Cnvrt_Lbl : for I in Result'range loop
157 Group_v := Group_v + 4; -- identifies end of bit # in a group of 4
158 Str4 := StrTo4(Group_v - 3 to Group_v); -- get next 4 characters
159 case Str4 is
160 when "0000" => Result(I) := '0';
161 when "0001" => Result(I) := '1';
162 when "0010" => Result(I) := '2';
163 when "0011" => Result(I) := '3';
164 when "0100" => Result(I) := '4';
165 when "0101" => Result(I) := '5';
166 when "0110" => Result(I) := '6';
167 when "0111" => Result(I) := '7';
168 when "1000" => Result(I) := '8';
169 when "1001" => Result(I) := '9';
170 when "1010" => Result(I) := 'A';
171 when "1011" => Result(I) := 'B';
172 when "1100" => Result(I) := 'C';
173 when "1101" => Result(I) := 'D';
174 when "1110" => Result(I) := 'E';
175 when "1111" => Result(I) := 'F';
176 when others => Result(I) := 'X';
177 end case; -- Str4
178 end loop Cnvrt_Lbl;
179
180 return Result;
181 end HexImage;
182
183
184 function HexImage(In_Image : Bit_Vector) return String is
185 begin
186 return HexImage(Image(In_Image));
187 end HexImage;
188
189 function HexImage(In_Image : Std_uLogic_Vector) return String is
190 begin
191 return HexImage(Image(In_Image));
192 end HexImage;
193
194 function HexImage(In_Image : Std_Logic_Vector) return String is
195 begin
196 return HexImage(Image(In_Image));
197 end HexImage;
198
199 function HexImage(In_Image : Signed) return String is
200 begin
201 return HexImage(Image(In_Image));
202 end HexImage;
203
204 function HexImage(In_Image : UnSigned) return String is
205 begin
206 return HexImage(Image(In_Image));
207 end HexImage;
208
209 function DecImage(In_Image : Bit_Vector) return String is
210 variable In_Image_v : Bit_Vector(In_Image'length downto 1) := In_Image;
211 begin
212 if In_Image'length > 31 then
213 assert False
214 report "Number too large for Integer, clipping to 31 bits"
215 severity Warning;
216 return Image(Conv_Integer
217 (Unsigned(To_StdLogicVector
218 (In_Image_v(31 downto 1)))));
219 else
220 return Image(Conv_Integer(Unsigned(To_StdLogicVector(In_Image))));
221 end if;
222 end DecImage;
223
224 function DecImage(In_Image : Std_uLogic_Vector) return String is
225 variable In_Image_v : Std_uLogic_Vector(In_Image'length downto 1)
226 := In_Image;
227 begin
228 if In_Image'length > 31 then
229 assert False
230 report "Number too large for Integer, clipping to 31 bits"
231 severity Warning;
232 return Image(Conv_Integer(Unsigned(In_Image_v(31 downto 1))));
233 else
234 return Image(Conv_Integer(Unsigned(In_Image)));
235 end if;
236 end DecImage;
237
238 function DecImage(In_Image : Std_Logic_Vector) return String is
239 variable In_Image_v : Std_Logic_Vector(In_Image'length downto 1)
240 := In_Image;
241 begin
242 if In_Image'length > 31 then
243 assert False
244 report "Number too large for Integer, clipping to 31 bits"
245 severity Warning;
246 return Image(Conv_Integer(Unsigned(In_Image_v(31 downto 1))));
247 else
248 return Image(Conv_Integer(Unsigned(In_Image)));
249 end if;
250 end DecImage;
251
252 function DecImage(In_Image : Signed) return String is
253 variable In_Image_v : Signed(In_Image'length downto 1) := In_Image;
254 begin
255 if In_Image'length > 31 then
256 assert False
257 report "Number too large for Integer, clipping to 31 bits"
258 severity Warning;
259 return Image(Conv_Integer(In_Image_v(31 downto 1)));
260 else
261 return Image(Conv_Integer(In_Image));
262 end if;
263 end DecImage;
264
265 function DecImage(In_Image : UnSigned) return String is
266 variable In_Image_v : UnSigned(In_Image'length downto 1) := In_Image;
267 begin
268 if In_Image'length > 31 then
269 assert False
270 report "Number too large for Integer, clipping to 31 bits"
271 severity Warning;
272 return Image(Conv_Integer(In_Image_v(31 downto 1)));
273 else
274 return Image(Conv_Integer(In_Image));
275 end if;
276 end DecImage;
277
278 end image_pkg;