【问题标题】:VHDL integer to stringVHDL整数到字符串
【发布时间】:2020-10-06 16:25:53
【问题描述】:

假设您想在 VHDL 中将整数转换为字符串以显示在 VGA 监视器上。但是,您不能使用 ieee 2008 标准,因为您必须使用 xilinx ISE 14.7。我有以下代码用于将整数类型转换为字符串类型,但在 while 循环和 for 循环中出现“超出非静态循环限制”错误:

-- convert integer to string using specified base
-- (adapted from Steve Vogwell's posting in comp.lang.vhdl)

function str(int: integer; base: integer) return string is

variable temp:      string(1 to 10);
variable num:       integer;
variable abs_int:   integer;
variable len:       integer := 1;
variable power:     integer := 1;

begin

-- bug fix for negative numbers
abs_int := abs(int);

num     := abs_int;

while num >= base loop                     -- Determine how many
  len := len + 1;                          -- characters required
  num := num / base;                       -- to represent the
end loop ;                                 -- number.

for i in len downto 1 loop                 -- Convert the number to
  temp(i) := chr(abs_int/power mod base);  -- a string starting
  power := power * base;                   -- with the right hand
end loop ;                                 -- side.

-- return result and add sign if required
if int < 0 then
   return '-'& temp(1 to len);
 else
   return temp(1 to len);
end if;

end str;

我通过将其变形为这个怪物来“解决”错误:

-- convert integer to string using specified base
-- (adapted from Steve Vogwell's posting in comp.lang.vhdl)

function str(int: integer; base: integer) return string is

    variable temp:      string(1 to 9);
    variable num:       integer;
    variable abs_int:   integer;
    variable len:       integer := 1;
    variable power:     integer := 1;

    begin

    -- bug fix for negative numbers
    abs_int := abs(int);

    num     := abs_int;

    for i in 0 to 100 loop
        if (num >= base) then                   -- Determine how many
          len := len + 1;                       -- characters required
          num := num / base;                    -- to represent the
        else                                    -- number.
            exit;
        end if;
    end loop ;                                 

    for i in 9 downto 1 loop                 -- Convert the number to
        if (i <= len) then
            temp(i) := chr(abs_int/power mod base);  -- a string starting
            power := power * base;                   -- with the right hand
        else
            exit;
        end if;
    end loop ;                                 -- side.

    -- return result and add sign if required
    if int < 0 then
       return '-'& temp(1 to len);
     else
       return temp(1 to len);
    end if;

end str;

有没有一种无延迟的方法可以将整数转换为字符串?

【问题讨论】:

  • 听起来好像您正在尝试使用'image() 函数和string 类型(以它的无约束形式)进行合成。这行不通。一个小软处理器似乎最适合这样的任务(例如 xilinx picoblaze)。它也可以在 FPGA 逻辑中完成,但它不会是有史以来最好的资源利用方式。
  • 我认为您需要一个查找表。但是为什么你需要一个用于 VGA 的字符串呢?监视器仅显示像素。字符最有可能通过图形缓冲区操作显示。
  • 我可以轻松地在屏幕上显示字符串,因此我进入了将整数转换为字符串的下一个逻辑步骤,以便我也可以显示它们。上面的函数和 integer'image() 都不适用于变量 int。将作为变量的整数转换为字符串的常见简单解决方案是什么?

标签: vhdl


【解决方案1】:

如果I 是一个整数,则integer'image(I) 是它的字符串表示形式。

【讨论】:

  • 没错。尽管在使用 integer'image(i) 时出现“表达式不是常量”错误。我只是想将一个 int (快速变化的)转换为一个显示在监视器上的字符串。
  • “怪物”中的两个返回表达式也不返回恒定长度的字符串表达式。
  • 不是字符串不是固定长度,而是抱怨传入的整数不是固定的。一旦我将它从 integer'image(1234) 更改为 integer'image(some_var) 它就会抱怨。
  • 'image()中的参数不需要是常数。但是通过阅读其他 cmets,我看到了您正在尝试做的事情,因此它必须是合成的常数也就不足为奇了。
【解决方案2】:

我从各个方向着手,最后发现我必须制作一个巨大的箱子才能让它工作。现在我终于可以显示对调试非常有帮助的快速变化的变量了。不幸的是,解决方案必须如此繁琐。

(我已经有一个 ROM 用于显示将结果字符串发送到的文本。)

function int_to_str(int : integer) return string is
    variable a : natural := 0;
    variable r : string(1 to 11);

begin
    a := abs (int);

    case a is
        when 0    => r := "0          ";
        when 1    => r := "1          ";
        when 2    => r := "2          ";
        when 3    => r := "3          ";
        .
        .
        .
        when 1000 => r := "1000       ";

        when others => r := "???????????";
    end case;

    if (int < 0) then
        r := '-' & r(1 to 10);
    end if;

    return r;
end int_to_str;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 2017-10-29
    相关资源
    最近更新 更多