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

Har*_*Pap 4 vhdl

我试图划分两个整数如下:

variable m0Low : integer := 0;
variable m1Low : integer := 0;
m1Low := divide(m1Low,m0Low);
Run Code Online (Sandbox Code Playgroud)

具有以下功能:

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;
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误: Divide can not have such operands in this context.

我试图将变量转换为unsigned m1Low := divide(unsigned(m1Low),unsigned(m0Low));

但是我收到以下错误: The expression can not be converted to type unsigned.

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

Moh*_*ana 7

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

use IEEE.NUMERIC_STD.all;
Run Code Online (Sandbox Code Playgroud)

你必须使用,

to_unsigned(I,U’length);
to_signed(I,S’length)
Run Code Online (Sandbox Code Playgroud)

其中I是整数值,U'length是无符号向量长度(位数).

我没有验证你的代码以及它是如何工作的,但我对你的代码的修正只是,

m1Low := to_integer(divide(to_unsigned(m1Low, N),to_unsigned(m0Low, N)));
Run Code Online (Sandbox Code Playgroud)

你应该指定N,其中矢量的长度取决于你的设计.我使用了to_integer(),因为你的函数将无符号值返回到整数变量.

希望这个简单的笔记能帮到你.