我正在使用 VHDL-2008,我想很好地格式化实数是类似于这个 c 语言函数的字符串:
sprintf(str, "%9.6f", myreal);
Run Code Online (Sandbox Code Playgroud)
目前,我正在以这种方式格式化我的实数:
architecture sim of testbench is
constant myreal :real := 3343.2342;
begin
process
begin
report real'image(real_number);
wait;
end process
end architecture;
Run Code Online (Sandbox Code Playgroud)
这不允许对 VHDL 中实数的格式进行足够的控制。我想要的是控制 vhdl 格式,更像是 c 语言“%n.mf”格式器。
基本上,GHDL模拟器中的VHDL默认值总是以科学记数法打印实数,小数点左边一位,分数和指数,这很烦人。
VHDL 2008 提供了 3 种风格的 to_string for real:
function TO_STRING (VALUE: REAL) return STRING;
function TO_STRING (VALUE: REAL; DIGITS: NATURAL) return STRING;
function TO_STRING (VALUE: REAL; FORMAT: STRING) return STRING;
Run Code Online (Sandbox Code Playgroud)
第一种情况以简单格式返回实数,第二种情况以 DIGITS 为显示在小数点右侧的位数返回它,第三种情况接受来自 C 的 sprintf 样式格式字符串:
entity real_test is
end entity real_test;
architecture test of real_test is
begin
process
variable r : real;
begin
r := 3.25432;
report to_string(r);
report to_string(r, 3);
report to_string(r, "%.2f");
wait;
end process;
end architecture;
Run Code Online (Sandbox Code Playgroud)
EXECUTION:: NOTE : 3.25432
EXECUTION:: Time: 0 ps, Iteration: 0, Instance: /real_test, Process: line__7.
EXECUTION:: NOTE : 3.254
EXECUTION:: Time: 0 ps, Iteration: 0, Instance: /real_test, Process: line__7.
EXECUTION:: NOTE : 3.25
EXECUTION:: Time: 0 ps, Iteration: 0, Instance: /real_test, Process: line__7.
Run Code Online (Sandbox Code Playgroud)