在除法器代码VHDL中,输出总是零(商和余数)

在除法器代码VHDL中,输出总是零(商和余数),vhdl,Vhdl,在下面显示的代码中,输出总是零商和余数。 即使我把b的值赋给余数,它也等于0。我已经检查了很多次,但我无法理解问题是什么。编译时,它会显示两个警告: - Initial value of "b" depends on value of signal "divisor". 有什么问题 -分隔器 library ieee; use ieee.numeric_bit.all; entity unsigned_divider is port( -- the two inputs di

在下面显示的代码中,输出总是零商和余数。 即使我把b的值赋给余数,它也等于0。我已经检查了很多次,但我无法理解问题是什么。编译时,它会显示两个警告:

- Initial value of "b" depends on value of signal "divisor".
有什么问题

-分隔器

library ieee;  
use ieee.numeric_bit.all;  

entity unsigned_divider is  
port(
-- the two inputs  
dividend: in bit_vector(15 downto 0);  
divisor : in bit_vector(15 downto 0);  
-- the two outputs  
quotient : out bit_vector(15 downto 0);  
remainder : out bit_vector(15 downto 0)  
);  
end entity unsigned_divider;  

architecture behave of unsigned_divider is  

begin  
process  

variable a : bit_vector(15 downto 0):=dividend;  
variable b : bit_vector(15 downto 0):=divisor;  

variable p : bit_vector(15 downto 0):= (others => '0');  
variable i : integer:=0;  
begin  

for i in 0 to 15 loop  
p(15 downto 1) := p(14 downto 0);  
p(0) := a(15);  
a(15 downto 1) := a(14 downto 0);  
p := bit_vector(unsigned(p) -  unsigned(b));  

if(p(15) ='1') then  
a(0) :='0';  
p := bit_vector(unsigned(p) +  unsigned(b));  
else  
a(0) :='1';  
end if;  
wait for 1 ns;  

end loop;

quotient <= a after 1 ns;  
remainder <= p  after 1 ns;  

end process;
end behave;

可变任务立即生效;但是,在创建该变量时,该信号没有值,因此您不能期望赋值

variable a : bit_vector(15 downto 0):=dividend;  
variable b : bit_vector(15 downto 0):=divisor;
正确地工作。我有点惊讶,没有人抱怨变量a的赋值。也许这是你的第二个警告。您应该按照您的方式定义变量,但将赋值留待以后在流程的开始部分进行


另外,您可能希望更改余数您应该将流程语句部分中的变量a和b显式赋值为顺序信号赋值。声明:

        variable a : bit_vector(15 downto 0):=dividend;  
        variable b : bit_vector(15 downto 0):=divisor;  
应该是:

        variable a : bit_vector(15 downto 0);  
        variable b : bit_vector(15 downto 0);  
在流程声明部分中,流程开始后的部分:

        a := dividend;
        b := divisor;
这些解决了natipar提到的问题,即在初始化期间仅将值分配给a和b

此外,如果您希望有1ns的延迟,您应该有一个明确的等待语句作为流程语句流程语句部分的最后一个顺序语句:

wait on dividend, divisor;
这些使您的流程语句看起来像这样,并添加了缩进:

    process  

        variable a : bit_vector(15 downto 0); -- := dividend;  
        variable b : bit_vector(15 downto 0); -- := divisor;  

        variable p : bit_vector(15 downto 0) :=  (others => '0');  
        variable i : integer := 0;  
    begin  

        a := dividend;
        b := divisor;

        for i in 0 to 15 loop  
            p(15 downto 1) := p(14 downto 0);  
            p(0) := a(15);  
            a(15 downto 1) := a(14 downto 0);  
            p := bit_vector(unsigned(p) -  unsigned(b));  

            if  p(15)  = '1'  then  
                a(0) :='0';  
                p := bit_vector(unsigned(p) +  unsigned(b));  
            else  
                a(0) := '1';
            end if;  
            wait for 1 ns;  

        end loop;

        quotient <= a after 1 ns;  
        remainder <= p  after 1 ns;  

        wait on dividend, divisor;

    end process;
ghdl-一种无符号除法器.vhdl ghdl-e无符号除法器\u tb ghdl-r无符号除法器\u tb 无符号除法器。vhdl:83:9:@0ms:报告注释:商=0000000000000000 无符号除法器。vhdl:84:9:@0ms:报告注释:余数=0000000000000000 无符号除法器。vhdl:83:9:@17ns:报告注释:商=1111 无符号除法器。vhdl:84:9:@17ns:报告注释:余数=0000000000000000

关于解释,在0毫秒时报告的事务是由于细化而执行的默认分配

你的算法给出了一个被0除的错误答案

向测试台添加刺激过程:

STIMULUS:
    process
    begin
        wait for 20 ns;
        dividend <= x"ffff";
        divisor <= x"000f";
    end process;
表明它也可以得到正确的答案:

无符号除法器。vhdl:83:9:@37ns:报告注释:商=0001 无符号除法器。vhdl:84:9:@37ns:报告注释:余数=0000000000000000

通过测试台,在刺激过程中添加等待语句和任务,您可以进一步探索


我本人一直是非还原除法的爱好者,因为加减法在时钟除法器中占用一个时钟。

而且不要忘记定义触发进程的信号!如果流程语句中的保留字流程后出现流程敏感度列表,则流程语句不应包含显式等待语句。IEEE标准1076-2008 11.3过程声明,第8段,第一句。在本文件中,应一词用于表示强制性要求,1.3本文件的结构和术语,1.3.1概述,第4段,第一句。包含显式等待语句的进程不会包含进程敏感度列表。对此我非常抱歉,我将删除这句话。非常感谢,先生……这真的帮了我的忙
STIMULUS:
    process
    begin
        wait for 20 ns;
        dividend <= x"ffff";
        divisor <= x"000f";
    end process;