【问题标题】:VHDL Code for Binary Division bug二进制除法错误的 VHDL 代码
【发布时间】:2014-06-14 13:48:37
【问题描述】:

我为二进制除法器编写了代码,它接受 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/fqfiYJZ1

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

【问题讨论】:

  • 什么是不正确的结果?我们不会通读代码墙来尝试猜测问题出在哪里。
  • 我真的没有任何语法错误。我发现我的代码基本上会为我的输入吐出不正确的结果。
  • 完全正确。看到输入和不正确的输出可以告诉我们很多。
  • 刚刚发布了我的信号截图。感谢您的帮助!

标签: debugging logic vhdl division circuit


【解决方案1】:

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

要么写q &lt;= a / b;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多