如何在VHDL中将整数写入十六进制的stdout?

时间:2016-06-17 11:02:34

标签: vhdl ghdl

我可以使用:

将标题integer打印到stdout
library std;
use std.textio.all;

entity min is
end min;

architecture behav of min is
begin
    process is
        variable my_line : line;
    begin
        write(my_line, 16);
        writeline(output, my_line);
        wait;
    end process;
end behav;

输出:

16

但是如何输出:

10
0x10

3 个答案:

答案 0 :(得分:10)

假设整数i和VHDL-2008,您可以使用:

write(output, integer'image(i) & LF);  -- Plain integer value
write(output, "0x" & to_hstring(to_signed(i, 32)) & LF);  -- Hexadecimal representation

您需要use std.textio.all;才能使用此功能。更改32以减少十六进制值的长度。我选择了32,因此它可以代表大多数模拟器中的任何整数值。

这些也适用于report语句,例如

report "i = 0x" & to_hstring(to_signed(i, 32));

答案 1 :(得分:4)

没有标准的库实现,但是例如我们的PoC-LibaryPoC.Strings包中有几个格式化函数。最重要的是,我们有一个to_string(...)函数,它接受一个格式字符,如h,用于十六进制输出。

如何将这样的整数写入十六进制转换?

  1. 将INTEGER转换为二进制表示
  2. 将二进制值分组为4位组
  3. 将每个组转换为0..F
  4. 范围内的整数/ alpha
  5. 如果愿意,可以预先0x
  6. 所以这是一个将整数转换为二进制表示的包装器:

    -- format a natural as HEX string
    function raw_format_nat_hex(Value : NATURAL) return STRING is
    begin
      return raw_format_slv_hex(std_logic_vector(to_unsigned(Value, log2ceil(Value+1))));
    end function;
    

    现在进行分组和转换

    -- format a std_logic_vector as HEX string
    function raw_format_slv_hex(slv : STD_LOGIC_VECTOR) return STRING is
      variable Value                : STD_LOGIC_VECTOR(4*div_ceil(slv'length, 4) - 1 downto 0);
      variable Digit                : STD_LOGIC_VECTOR(3 downto 0);
      variable Result               : STRING(1 to div_ceil(slv'length, 4));
      variable j                    : NATURAL;
    begin
      Value := resize(slv, Value'length);
      j             := 0;
      for i in Result'reverse_range loop
        Digit       := Value((j * 4) + 3 downto (j * 4));
        Result(i)   := to_HexChar(unsigned(Digit));
        j           := j + 1;
      end loop;
      return Result;
    end function;
    
    -- convert an unsigned value(4 bit) to a HEX digit (0-F)
    function to_HexChar(Value : UNSIGNED) return CHARACTER is
      constant HEX : STRING := "0123456789ABCDEF";
    begin
      if (Value < 16) then
        return HEX(to_integer(Value)+1);
      else
        return 'X';
      end if;
    end function;
    
    -- return TRUE, if input is a power of 2
    function div_ceil(a : NATURAL; b : POSITIVE) return NATURAL is  -- calculates: ceil(a / b)
    begin
      return (a + (b - 1)) / b;
    end function;
    
    -- return log2; always rounded up
    function log2ceil(arg : positive) return natural is
      variable tmp : positive;
      variable log : natural;
    begin
      if arg = 1 then   return 0; end if;
      tmp := 1;
      log := 0;
      while arg > tmp loop
        tmp := tmp * 2;
        log := log + 1;
      end loop;
      return log;
    end function;
    

    注意:这些功能不会添加0x

答案 2 :(得分:3)

您可以使用hwrite包中的IEEE.std_logic_textio程序:

library IEEE;                                                   -- ADDED
use IEEE.std_logic_1164.all;                                    -- ADDED
use IEEE.numeric_std.all;                                       -- ADDED
use IEEE.std_logic_textio.all;                                  -- ADDED

library std;
use std.textio.all;

entity min is
end min;

architecture behav of min is
begin
    process is
        variable my_line : line;
    begin
        hwrite(my_line, std_logic_vector(to_unsigned(16,8)));   -- CHANGED
        writeline(output, my_line);
        wait;
    end process;
end behav;

hwrite过程将std_logic_vector写入文件。因此,您必须将integer转换为std_logic_vector,但是(您还需要在to_unsigned函数中指定多个位)。

http://www.edaplayground.com/x/exs

相关问题