我可以打印一个integer小数到标准输出:
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;
Run Code Online (Sandbox Code Playgroud)
哪个输出:
16
Run Code Online (Sandbox Code Playgroud)
但是如何输出代替:
10
0x10
Run Code Online (Sandbox Code Playgroud)
sca*_*eff 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
Run Code Online (Sandbox Code Playgroud)
你需要有use std.textio.all;这个工作.更改32以减少十六进制值的长度.我选择了32,因此它可以代表大多数模拟器中的任何整数值.
这些也适用于report陈述,例如
report "i = 0x" & to_hstring(to_signed(i, 32));
Run Code Online (Sandbox Code Playgroud)
没有标准库实现,但是例如我们的PoC-Libary在PoC.Strings包中有几个格式化函数。最重要的是,我们有一个to_string(...)函数,它接受类似于十六进制输出的格式字符h。
如何将这样的整数转换为十六进制?
0x如果需要的话可以在前面加上所以这里是一个将整数转换为二进制表示的包装器:
-- 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;
Run Code Online (Sandbox Code Playgroud)
现在分组和转换
-- 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;
Run Code Online (Sandbox Code Playgroud)
注意:这些函数不前置0x.