-------------------------------------------------------------------------------
--
-- Project name: General simulation helpers
--
-- (C) Copyright 2013-2017 DFC Design, s.r.o., Brno, Czech Republic
-- Author: Marek Kvas (m.kvas@dspfpga.com)
--
-------------------------------------------------------------------------------
--
-- Package that should easy printing in simulation testbenches.
--
-------------------------------------------------------------------------------



library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use std.textio.all;
use ieee.std_logic_textio.all;


package tb_print_pkg is

   procedure print(s : string);
   procedure cprint(cond : boolean; s : string);
   function hstr (val :  std_logic_vector) return string;
   



end package;




package body tb_print_pkg is

   -- General unconditional print of any string to console
   procedure print(s : string) is 
      variable buf : line ; 
   begin
      write(buf, ">>>>>" & s) ; 
      WriteLine(OUTPUT, buf) ; 
   end procedure print ; 

   -- Conditional print of any string to console
   -- message is printed when condition cond is true
   procedure cprint(cond : boolean; s : string) is 
      variable buf : line ; 
   begin
      if cond then
         write(buf, ">>>>>" & s) ; 
         WriteLine(OUTPUT, buf) ; 
      end if;
   end procedure cprint ; 

   -- Transform std_logic_vector to hex number
   -- This is probably useless for VHDL 2008
   -- as to_hstring does the same
   function hstr (val :  std_logic_vector) return string is
      variable bin_len : integer := val'length + 2;
      variable hex_len : integer := (val'length+3)/4;
      variable e_len : integer := hex_len*4;
      variable eval : std_logic_vector(e_len - 1 downto 0);
      variable res : string(1 to hex_len + bin_len);
      variable hstr_map : string(1 to 16) := "0123456789ABCDEF";
   begin
      eval := (others => '0');
      eval(val'range) := val;
      for i in 1 to hex_len loop
         res(hex_len - i + 1) :=hstr_map(1 + to_integer(unsigned(
                                          eval(i*4-1 downto (i-1)*4))));
      end loop;
      res(hex_len + 1) := '(';
      res(res'right) := ')';
      for i in val'range loop
        if val(val'left - i) = '1' then
           res(i + hex_len + 2) := '1';
        else
           res(i + hex_len + 2) := '0';
        end if;
      end loop;
      return res;
   end function;

  
   
   


end package body;
