OpenCores
URL https://opencores.org/ocsvn/uart_block/uart_block/trunk

Subversion Repositories uart_block

[/] [uart_block/] [trunk/] [hdl/] [iseProject/] [divisor.vhd] - Blame information for rev 36

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 36 leonardoar
--! @file
2
--! @brief Unsigned division circuit, based on slow division algorithm (Restoring division)
3
--! http://en.wikipedia.org/wiki/Division_%28digital%29
4
--! The problem with this algorithm is that will take the same ammount of ticks (on this case 32) of
5
--! it's operands to resolve...
6 5 leonardoar
library IEEE;
7
use IEEE.STD_LOGIC_1164.ALL;
8 9 leonardoar
use IEEE.std_logic_arith.all;
9
 
10
--! Use CPU Definitions package
11
use work.pkgDefinitions.all;
12 5 leonardoar
 
13
entity divisor is
14 36 leonardoar
    Port ( rst : in  STD_LOGIC;                                                                                                         --! Reset input
15
           clk : in  STD_LOGIC;                                                                                                         --! Clock input
16
           quotient : out  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);   --! Division result (32 bits)
17
                          reminder : out  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);    --! Reminder result (32 bits)
18
           numerator : in  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);   --! Numerator (32 bits)
19
           divident : in  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);    --! "Divide by" number (32 bits)
20 5 leonardoar
           done : out  STD_LOGIC);
21
end divisor;
22
 
23 36 leonardoar
--! @brief Top divisor architecture
24
--! @details http://en.wikipedia.org/wiki/Division_%28digital%29
25 5 leonardoar
architecture Behavioral of divisor is
26
 
27
begin
28
 
29
        -- Division algorithm Q=N/D
30 14 leonardoar
        process (rst, clk, numerator, divident)
31 5 leonardoar
        variable Q : unsigned(quotient'length-1 downto 0);
32
        variable R : unsigned(reminder'length-1 downto 0);
33
        variable D : unsigned(reminder'length-1 downto 0);
34
        variable N : unsigned(reminder'length-1 downto 0);
35
        variable iteractions : integer;
36
        begin
37
                if (rst = '1') then
38
                        quotient <= (others => '0');
39
                        reminder <= (others => '0');
40
                        done <= '0';
41
 
42
                        -- Initialize variables
43
                        iteractions := quotient'length;
44
                        D := unsigned(divident);
45
                        N := unsigned(numerator);
46
                        -- initialize quotient and remainder to zero
47
                        Q := (others => '0');
48
                        R := (others => '0');
49
                elsif rising_edge(clk) then
50
                        if iteractions > 0 then
51
                                iteractions := iteractions - 1;
52
                                -- left-shift R by 1 bit 
53
                                R := (R((R'HIGH - 1) downto 0) & '0');
54
 
55
                                --set the least-significant bit of R equal to bit i of the numerator(dividend)
56
                                R(0)     := N(iteractions);
57
 
58
                                if (R >= D) then
59
                                        R := R - D;
60
                                        Q(iteractions) := '1';
61
                                end if;
62
                        else
63 36 leonardoar
                                -- We have the results here...
64 5 leonardoar
                                done <= '1';
65
                                quotient <= CONV_STD_LOGIC_VECTOR(Q,32);
66
                                reminder <= CONV_STD_LOGIC_VECTOR(R,32);
67 15 leonardoar
 
68
                                -- Used to avoid transparent latch (Good practise)
69
                                D := unsigned(divident);
70
                                N := unsigned(numerator);
71 5 leonardoar
                        end if;
72
                end if;
73
        end process;
74
 
75
end Behavioral;
76
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.