【问题标题】:How to typecast integer to unsigned in VHDL如何在VHDL中将整数类型转换为无符号
【发布时间】:2013-05-16 19:30:53
【问题描述】:

我正在尝试将两个整数除以如下:

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.

知道我能做什么吗? 谢谢 哈里斯

【问题讨论】:

  • 有什么理由不能只使用除法运算符? m1Low := M1Low/m0Low?
  • 它给出的错误是 / 符号的右边部分必须是 2 的幂!不知道为什么。
  • 啊,那是因为您的合成器不够聪明,无法为非静态、非 2 次幂构建分频器。评估更好(即更昂贵;() 综合工具可能会有所帮助。或者使用 CoreGen 构建分频器核心。

标签: vhdl


【解决方案1】:

要将整数转换为无符号或有符号数据类型,

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

希望这些简单的笔记对你有所帮助。

【讨论】:

    【解决方案2】:

    如果你想将整数作为无符号向量传递,你需要convert它们,而不是typecast它们。

    首先你需要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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 2014-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      相关资源
      最近更新 更多