fork download
  1. library IEEE;
  2. use IEEE.STD_LOGIC_1164.ALL;
  3. use IEEE.NUMERIC_STD.ALL;
  4.  
  5. entity Divider is
  6. Generic (
  7. N : integer := 8 -- Number of bits for dividend and divisor
  8. );
  9. Port (
  10. clk : in std_logic; -- Clock signal
  11. reset : in std_logic; -- Reset signal
  12. start : in std_logic; -- Start signal
  13. dividend: in std_logic_vector(N-1 downto 0); -- Dividend input
  14. divisor : in std_logic_vector(N-1 downto 0); -- Divisor input
  15. quotient: out std_logic_vector(N-1 downto 0); -- Quotient output
  16. remainder: out std_logic_vector(N-1 downto 0); -- Remainder output
  17. done : out std_logic -- Done flag
  18. );
  19. end Divider;
  20.  
  21. architecture Behavioral of Divider is
  22. signal dividend_reg : unsigned(N downto 0); -- Dividend register (N+1 bits for shifting)
  23. signal divisor_reg : unsigned(N downto 0); -- Divisor register (N+1 bits)
  24. signal remainder_reg: unsigned(N downto 0); -- Remainder register
  25. signal quotient_reg : unsigned(N-1 downto 0); -- Quotient register
  26. signal count : integer range 0 to N := 0; -- Bit counter
  27. signal busy : std_logic := '0'; -- Busy flag
  28. begin
  29.  
  30. process(clk, reset)
  31. begin
  32. if reset = '1' then
  33. -- Reset all signals
  34. dividend_reg <= (others => '0');
  35. divisor_reg <= (others => '0');
  36. remainder_reg <= (others => '0');
  37. quotient_reg <= (others => '0');
  38. count <= 0;
  39. busy <= '0';
  40. done <= '0';
  41. elsif rising_edge(clk) then
  42. if start = '1' and busy = '0' then
  43. -- Initialize registers
  44. dividend_reg <= unsigned('0' & dividend); -- Extend dividend to N+1 bits
  45. divisor_reg <= unsigned('0' & divisor); -- Extend divisor to N+1 bits
  46. remainder_reg <= (others => '0');
  47. quotient_reg <= (others => '0');
  48. count <= N;
  49. busy <= '1';
  50. done <= '0';
  51. elsif busy = '1' then
  52. if count > 0 then
  53. -- Shift left the remainder and quotient
  54. remainder_reg <= remainder_reg(N-1 downto 0) & dividend_reg(N-1);
  55. dividend_reg <= dividend_reg(N-2 downto 0) & '0';
  56.  
  57. -- Subtract divisor from remainder
  58. if remainder_reg >= divisor_reg then
  59. remainder_reg <= remainder_reg - divisor_reg;
  60. quotient_reg(count-1) <= '1';
  61. else
  62. quotient_reg(count-1) <= '0';
  63. end if;
  64.  
  65. -- Decrement bit counter
  66. count <= count - 1;
  67. else
  68. -- Division complete
  69. busy <= '0';
  70. done <= '1';
  71. end if;
  72. end if;
  73. end if;
  74. end process;
  75.  
  76. -- Map outputs
  77. quotient <= std_logic_vector(quotient_reg);
  78. remainder <= std_logic_vector(remainder_reg(N-1 downto 0));
  79.  
  80. end Behavioral;
Success #stdin #stdout 0.02s 25788KB
stdin
Standard input is empty
stdout
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity Divider is
    Generic (
        N : integer := 8  -- Number of bits for dividend and divisor
    );
    Port (
        clk     : in  std_logic;              -- Clock signal
        reset   : in  std_logic;              -- Reset signal
        start   : in  std_logic;              -- Start signal
        dividend: in  std_logic_vector(N-1 downto 0); -- Dividend input
        divisor : in  std_logic_vector(N-1 downto 0); -- Divisor input
        quotient: out std_logic_vector(N-1 downto 0); -- Quotient output
        remainder: out std_logic_vector(N-1 downto 0); -- Remainder output
        done    : out std_logic               -- Done flag
    );
end Divider;

architecture Behavioral of Divider is
    signal dividend_reg : unsigned(N downto 0); -- Dividend register (N+1 bits for shifting)
    signal divisor_reg  : unsigned(N downto 0); -- Divisor register (N+1 bits)
    signal remainder_reg: unsigned(N downto 0); -- Remainder register
    signal quotient_reg : unsigned(N-1 downto 0); -- Quotient register
    signal count        : integer range 0 to N := 0; -- Bit counter
    signal busy         : std_logic := '0'; -- Busy flag
begin

    process(clk, reset)
    begin
        if reset = '1' then
            -- Reset all signals
            dividend_reg <= (others => '0');
            divisor_reg <= (others => '0');
            remainder_reg <= (others => '0');
            quotient_reg <= (others => '0');
            count <= 0;
            busy <= '0';
            done <= '0';
        elsif rising_edge(clk) then
            if start = '1' and busy = '0' then
                -- Initialize registers
                dividend_reg <= unsigned('0' & dividend); -- Extend dividend to N+1 bits
                divisor_reg <= unsigned('0' & divisor);   -- Extend divisor to N+1 bits
                remainder_reg <= (others => '0');
                quotient_reg <= (others => '0');
                count <= N;
                busy <= '1';
                done <= '0';
            elsif busy = '1' then
                if count > 0 then
                    -- Shift left the remainder and quotient
                    remainder_reg <= remainder_reg(N-1 downto 0) & dividend_reg(N-1);
                    dividend_reg <= dividend_reg(N-2 downto 0) & '0';

                    -- Subtract divisor from remainder
                    if remainder_reg >= divisor_reg then
                        remainder_reg <= remainder_reg - divisor_reg;
                        quotient_reg(count-1) <= '1';
                    else
                        quotient_reg(count-1) <= '0';
                    end if;

                    -- Decrement bit counter
                    count <= count - 1;
                else
                    -- Division complete
                    busy <= '0';
                    done <= '1';
                end if;
            end if;
        end if;
    end process;

    -- Map outputs
    quotient <= std_logic_vector(quotient_reg);
    remainder <= std_logic_vector(remainder_reg(N-1 downto 0));

end Behavioral;