1

我为二进制除法器编写了代码,它接受 8 位被除数、3 位除数并给出 5 位商(3 位余数)。我确实花了几个小时试图修复一个给出错误结果的错误,但我无法识别它。任何帮助将不胜感激!我的输入基本上得到了错误的答案,但我不知道为什么。有一条总线接收值,在第一个时钟周期 st 为 1 时,加载被除数寄存器。在之后的第二个时钟周期,加载除数寄存器并为接下来的三个时钟周期进行计算。

V信号是表示发生溢出的输出(结果不能适合商的五位),my st是启动过程的开始信号,sh是移位寄存器的移位信号, su 是减法器的减法信号。

 library IEEE;
 use IEEE.STD_LOGIC_1164.all;
 use IEEE.STD_LOGIC_ARITH.all;
 use IEEE.STD_LOGIC_UNSIGNED.all;

 entity Divider is
         Port (bus_in: in std_logic_vector(8 downto 0); 
                  St, Clk, reset: in std_logic;
                  Quotient: out std_logic_vector(4 downto 0);
                  Remainder: out std_logic_vector(2 downto 0);
                  v: out std_logic);
 end Divider;
 architecture Behavioral of Divider is
 signal State, NextState: integer range 0 to 5;
 signal C, Ld1, Ld2, Su, Sh: std_logic;
 signal Divisor: std_logic_vector(2 downto 0);
 signal Subout: std_logic_vector(3 downto 0);
 signal Dividend: std_logic_vector(8 downto 0);
 begin
         Subout <= Dividend(8 downto 5) - ('0' & divisor);
         C <= not Subout (3);
         Remainder <= Dividend(7 downto 5);
         Quotient <= Dividend(4 downto 0);
State_Graph: process (State, St, C)
 begin
         Ld1 <= '0'; 
         Ld2<='0'; 
         v <= '0'; 
         Sh <= '0'; 
         Su <= '0'; 
         case State is
                 when 0 =>
                         if (St = '1') then 
                                 Ld1 <= '1'; 
                                 NextState <= 1;
                         else 
                                 NextState <= 0; 
                         end if;
                 when 1 =>
                        if (St = '1') then 
                                 Ld2 <= '1'; 
                                 NextState <= 2;
                         else 
                                 Ld2<='1';
                                 NextState <= 2; 
                         end if;
                 when 2 =>
                         if (C = '1') then 
                                 v <= '1'; 
                                 NextState <= 0;
                         else 
                                 Sh <= '1'; 
                                 NextState <= 3; 
                         end if;
                 when 3 | 4  =>
                         if (C = '1') then 
                                 Su <= '1'; 
                                 NextState <= State; 
                         else 
                                 Sh <= '1'; 
                                 NextState <= State + 1; 
                         end if;
                 when 5 =>
                         if (C = '1') then 
                                 Su <= '1'; 
                         end if;

                         NextState <= 0;
         end case;
 end process State_Graph;
 Update: process (Clk)
 begin
         if Clk'event and Clk = '1' then
                 State <= NextState;
                 --if Load = '1' then 
                 --        Dividend <= '0' & bus_in; 
                 --end if;

                 if Ld1 = '1' then
                        Dividend <= '0'&Bus_in(7 downto 0); 
                end if;
                if Ld2 = '1' then
                        Divisor <= Bus_in(2 downto 0); 
                end if;

                if Su = '1' then 
                        Dividend(8 downto 5) <= Subout; 
                        Dividend(0) <= '1'; 
                end if; 
                if Sh = '1' then --94 
                        Dividend <= Dividend(7 downto 0) & '0'; 
                end if;
         end if;
 end process update;
 end Behavioral; 

这是我的输入和输出:[信号]:http: //imgur.com/fqfiYJZ 1 上传图片

图片显示我的除数和被除数寄存器加载正确。所以我认为问题出在实际的除法代码上。状态机似乎也正常工作。

4

1 回答 1

-1

不要自己写这个。你在重新发明轮子。

要么写q <= a / b;

或使用您的 FPGA 供应商提供的 IP 核。

于 2014-04-30T08:05:27.267 回答