2013-05-16 39 views
4

我想分两个整数如下:如何强制转换整数无符号的VHDL

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.

任何想法我可以做什么? 感谢 哈里斯

+0

任何原因,你不能只使用除法运算符? 'm1Low:= M1Low/m0Low'? –

+0

它给出/ sign的右侧部分必须是2的幂的错误!不知道为什么。 –

+0

啊,那是因为你的合成器不够聪明,无法为非静态的,非2的功率建立一个分频器。评估更好(即更昂贵;()综合工具可能会有所帮助;或者使用CoreGen构建分频器内核。 –

回答

6

为整数转换为无符号或符号的数据类型上,

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()是因为你的函数将无符号值返回给整型变量。

希望这个简单的笔记可以帮助你。

+0

正确!非常感谢 –

4

如果你想传递整数作为无符号的载体,你需要转换他们,而不是类型转换他们。

首先你想要的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); 
相关问题