我试着用下面的方法除两个整数:
variable m0Low : integer := 0;
variable m1Low : integer := 0;
m1Low := divide(m1Low,m0Low);
带功能:
function divide (a : UNSIGNED; b : UNSIGNED) return UNSIGNED is
variable a1 : unsigned(a'length-1 downto 0):=a;
variable b1 : unsigned(b'length-1 downto 0):=b;
variable p1 : unsigned(b'length downto 0):= (others => '0');
variable i : integer:=0;
begin
for i in 0 to b'length-1 loop
p1(b'length-1 downto 1) := p1(b'length-2 downto 0);
p1(0) := a1(a'length-1);
a1(a'length-1 downto 1) := a1(a'length-2 downto 0);
p1 := p1-b1;
if(p1(b'length-1) ='1') then
a1(0) :='0';
p1 := p1+b1;
else
a1(0) :='1';
end if;
end loop;
return a1;
end divide;
然而,我得到以下错误:Divide can not have such operands in this context.
我试图将变量转换为无符号m1Low := divide(unsigned(m1Low),unsigned(m0Low));
但我得到以下错误:The expression can not be converted to type unsigned.
你知道我能做什么吗?谢谢哈里斯
将整型转换为无符号或有符号数据类型,
use IEEE.NUMERIC_STD.all;
必须使用
to_unsigned(I,U’length);
to_signed(I,S’length)
其中I为整数值,U'length为无符号向量长度(位位数)。
我没有验证你的代码和它是如何工作的,但我对你的代码的纠正只是,
m1Low := to_integer(divide(to_unsigned(m1Low, N),to_unsigned(m0Low, N)));
你应该指定N,它的长度取决于你的设计。我使用to_integer(),因为你的函数返回无符号值的整数变量。
如果要将整数作为无符号向量传递,则需要转换它们,而不是对进行类型转换。
首先需要numeric_std
库:
use ieee.numeric_std.all;
则可以使用to_unsigned
将整数转换为无符号向量。对于该函数,您需要知道要转换为的无符号向量的长度,因此使用'length
属性:
destination_vector := to_unsigned(source_integer, destination_vector'length);
你可以像这样从无符号转换回整数(不需要告诉输入的长度,因为函数输入的信息是直接对函数可用的):
destination_integer := to_integer(source_vector);