如何在VHDL中将整数转换为无符号类型

4

我试图进行以下两个整数的除法:

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;

然而,我收到以下错误: 在此上下文中,除法不能具有这样的操作数。 我试图将变量转换为无符号 m1Low := divide(unsigned(m1Low),unsigned(m0Low)); 但我得到以下错误: 表达式无法转换为类型 unsigned。 有什么想法吗? 谢谢 哈里斯

你为什么不能直接使用除法运算符呢?m1Low := M1Low/m0Low - Martin Thompson
它报错说右侧的 / 符号必须是 2 的幂次方!不知道为什么。 - Haris Pap
啊,那是因为你的合成器不够聪明,无法为非静态、非2的幂次方构建除法器。评估更好(即更昂贵;())的综合工具可能会有所帮助。或者使用CoreGen构建除法器核心。 - Martin Thompson
2个回答

7

将整数转换为无符号或有符号数据类型,需要进行以下操作:

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()。

希望这些简单的说明能对您有所帮助。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接