整数到字符串在综合(宽度不匹配)中出错 [英] Integer to String goes wrong in Synthesis (Width Mismatch)

查看:136
本文介绍了整数到字符串在综合(宽度不匹配)中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将整数转换为字符串(使用integer'image(val)),并填充或将其限制为特定长度.我做了这个函数,当我使用report语句并进行仿真时,它的工作正常.

I am trying to convert a integer to string (using integer'image(val)) and either pad or limit it to a specific length. I have made this function which does the job just fine when I use a report statement and simulate.

function integer2string_pad(val: integer; stringSize: integer) return string is
    variable imageString: string(1 to integer'image(val)'length);
    variable returnString: string(1 to stringSize);
begin
    imageString := integer'image(val);

    -- Are we smaller than the desired size?
    if integer'image(val)'length < stringSize then
        -- Pad the string if we are
        returnString := integer'image(val) & (1 to stringSize-integer'image(val)'length => ' ');

    -- Are we to big for the desired size
    elsif integer'image(val)'length > stringSize then
        -- Only use the top most string bits and append a "." to the end signifing that there is more
        returnString := imageString(1 to stringSize-1) & ".";

    -- Otherwise we are just the right size
    else
        returnString := integer'image(val);
    end if;


    return returnString;
end function;

以下是该函数的一些示例输入,输出(下划线=空格,因为SO内联代码会截断多余的空格):

Here is some sample input, output of that function (underscore = space because SO inline code truncates extra space):

integer2string_pad(12, 6):12____

integer2string_pad(123456, 6):123456

integer2string_pad(1234567890, 6):12345.

integer2string_pad(0, 6):0_____

integer2string_pad(-123, 6):-123__

integer2string_pad(-1, 6):-1____

integer2string_pad(-123456, 6):-1234.

但是,当我进行合成时,我在将值分配给pongScoreLeftpongScoreRight的所有4行上都出现宽度不匹配错误.它还说它们的常数为0并被修剪掉.

But when I synthesize, I get width mismatch errors on all 4 lines where I assign values to pongScoreLeft or pongScoreRight. It also says they have a constant value of 0 and get trimmed out.

Width mismatch. <pongScoreLeft> has a width of 48 bits but assigned
expression is 6-bit wide. 
Width mismatch. <pongScoreRight> has a width
of 48 bits but assigned expression is 6-bit wide.
Width mismatch. <pongScoreLeft> has a width of 48 bits but assigned expression is 6-bit wide.
Width mismatch. <pongScoreRight> has a width of 48 bits but assigned expression is 6-bit wide.

产生这些宽度不匹配错误的VHDL:

VHDL that produces those width mismatch errors:

type type_score is
record
    left : integer range 0 to 255;
    right : integer range 0 to 255;
end record;
constant init_type_score: type_score := (left => 0, right => 0);

signal pongScore: type_score := init_type_score;
signal pongScoreLeft: string(1 to 6) := (others => NUL);
signal pongScoreRight: string(1 to 6) := (others => NUL);

...

scoreToString: process(clk)
begin
    if rising_edge(clk) then
        if reset = '1' then
            pongScoreLeft <= (others => NUL);
            pongScoreRight <= (others => NUL);
        else
            pongScoreLeft <= integer2string_pad(pongScore.left, 6);
            pongScoreRight <= integer2string_pad(pongScore.right, 6);
            --report "|" & integer2string_pad(pongScore.left, 6) & "|";

        end if;
    end if;
end process;

我的integer2string_pad函数出了什么问题?综合中出了什么问题?

What is wrong with my integer2string_pad function? What goes wrong in synthesis?

推荐答案

我不希望'image或'value支持合成-除了在详细说明时运行的断言中.它们将涉及很多处理.

I would not expect 'image or 'value to be supported for synthesis - other than in asserts that run at elaboration time. They would involve a lot of processing.

每当我将整数转换为ASCII时,我都会使用可合成的character'val和character'pos一次处理一个字符,因为它们不涉及任何处理.他们只是将字符转换为其基础的二进制表示形式.

Whenever I have converted integers to ASCII I have processed a character at a time, using character'val and character'pos, which are synthesisable, because they involve no processing; they just convert a character to/from its underlying binary representation.

想想如何实现形象!它涉及除以10的多个部分:如果将其展开为单个增量周期(这是非计时函数调用的语义所要求的),则这是很多硬件.

Think how you would implement 'image! It involves multiple divisions by 10 : that's a LOT of hardware if you unroll it into a single delta cycle (as required by the semantics of an unclocked function call)

每个(几个)时钟周期处理一个数字,您可以将其减少为单个除法,连续减法或过量6加法,或者根据您的硬件资源和时间预算来选择.

Processing a digit per (several) clock cycle(s) you can reduce that to a single division, or successive subtraction, or excess-6 addition, or however you want according to your hardware resources and time budget.

综合工具代表您做出这些决定确实没有任何意义.所以-尽管我承认这在理论上是可行的,但我会惊讶地看到一个合成工具能够正确地做到这一点. (太糟糕了,这种情况不太可能发生,如果您尝试使用synth工具的错误报告,我不会感到惊讶)

It really doesn't make sense for the synthesis tool to make these decisions on your behalf. So - while I concede it's theoretically possible, I would be surprised to see a synth tool that did it correctly. (OTOH it's such an unlikely scenario I'd not be surprised to see bugs in synth tool's error reporting should you try it)

这篇关于整数到字符串在综合(宽度不匹配)中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆