forked from ZIKOAR/32-bit-processor-with-vhdl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDivision.vhd
63 lines (50 loc) · 1.69 KB
/
Division.vhd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity division is
generic (
n : positive := 32
);
port (
a, b : in std_logic_vector(n - 1 downto 0);
q, r : out std_logic_vector(n - 1 downto 0)
);
end entity;
architecture division_arch of division is
signal a_un : unsigned(31 downto 0);
signal zeros : std_logic_vector(n - 2 downto 0);
signal temp, as, bs, qs, rs : std_logic_vector(n - 1 downto 0);
signal sign : std_logic;
begin
sign <= a(a'left) xor b(b'left);
b_un <= unsigned(bs);
zeros <= (others => '0');
temp <= zeros & as(n - 1);
as <= a when a(a'left) = '0' else std_logic_vector(signed(not(a)) + 1);
bs <= b when b(b'left) = '0' else std_logic_vector(signed(not(b)) + 1);
q <= qs when sign = '0' else std_logic_vector(signed(not(qs)) + 1);
r <= rs when a(a'left) = '0' else std_logic_vector(signed(not(rs)) + 1);
process(a, b)
variable a_un : unsigned(n - 1 downto 0);
variable tempo : std_logic_vector(n - 1 downto 0);
begin
a_un := unsigned(temp);
for k in n - 1 downto 1 loop
tempo := zeros & as(k - 1);
if a_un >= b_un then
qs(k) <= '1';
a_un := ((a_un - b_un) + (a_un - b_un)) + unsigned(tempo);
else
qs(k) <= '0';
a_un := (a_un + a_un) + unsigned(tempo);
end if;
end loop;
if a_un >= b_un then
qs(0) <= '1';
a_un := (a_un - b_un);
else
qs(0) <= '0';
end if;
rs <= std_logic_vector(a_un);
end process;
end architecture;