我正在尝试将整数转换为字符串(使用integer'image(val)
)并将其填充或限制为特定长度。我已经制作了这个函数,当我使用时它可以很好地完成工作report
陈述并模拟。
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 内联代码截断了额外的空格):
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.
但是当我合成的时候,我在分配值的所有 4 行上遇到宽度不匹配错误pongScoreLeft
or pongScoreRight
。它还表示它们的常量值为 0 并被修剪掉。
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:
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
功能?合成过程中出了什么问题?