Update README.md
[mymicroprocessor.git] / Lab_2 / DataPaths / easyprint.vhd
blob57bdec66179040c1b8e4cdba23bd59224a268386
2 library IEEE;
3 use IEEE.STD_LOGIC_1164.all;
4 USE ieee.numeric_std.ALL;
6 package easyprint is
8 function s_tostr(val : std_logic_vector) return string;
9 function u_tostr(val : std_logic_vector) return string;
11 function to_bstring(sl : std_logic) return string;
12 function to_bstring(slv : std_logic_vector) return string;
15 end easyprint;
17 package body easyprint is
19 -- ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
20 -- From http://stackoverflow.com/a/24336034 By Morten Zilmer
21 -- Allows printing a std_logic_vector as a string that represents it's binary form.
22 -- ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
23 function to_bstring(sl : std_logic) return string is
24 variable sl_str_v : string(1 to 3); -- std_logic image with quotes around
25 begin
26 sl_str_v := std_logic'image(sl);
27 return "" & sl_str_v(2); -- "" & character to get string
28 end function;
30 function to_bstring(slv : std_logic_vector) return string is
31 alias slv_norm : std_logic_vector(1 to slv'length) is slv;
32 variable sl_str_v : string(1 to 1); -- String of std_logic
33 variable res_v : string(1 to slv'length);
34 begin
35 for idx in slv_norm'range loop
36 sl_str_v := to_bstring(slv_norm(idx));
37 res_v(idx) := sl_str_v(1);
38 end loop;
39 return res_v;
40 end function;
41 -- ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ==== ====
44 -- converts an std_logic_vector to a string that represents it's signed value
45 function s_tostr(val : std_logic_vector) return string is
46 begin
47 return integer'image( to_integer(signed(val)) );
48 end function;
50 -- converts an std_logic_vector to a string that represents it's unsigned value
51 function u_tostr(val : std_logic_vector) return string is
52 begin
53 return integer'image( to_integer(unsigned(val)) );
54 end function;
56 end easyprint;