Integer to String goes wrong in Synthesis (Width Mismatch)

戏子无情 提交于 2019-12-31 05:29:07

问题


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;

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.


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 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;

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


回答1:


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.

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.

EDIT: 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)

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.

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)



来源:https://stackoverflow.com/questions/20667051/integer-to-string-goes-wrong-in-synthesis-width-mismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!