-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqrt.vhd
132 lines (109 loc) · 3.32 KB
/
sqrt.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
library ieee;
use ieee.std_logic_1164.all;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use ieee.numeric_std.all;
entity squart is port
(
numero : out STD_LOGIC_VECTOR (7 downto 0);
mul : out STD_LOGIC_vector (3 downto 0);
re : in std_logic;
clock : in std_logic;
data_in : in unsigned(15 downto 0);
data_out : out unsigned(15 downto 0));
end squart;
architecture behaviour of squart is
component seg_comp
port(
clk : in STD_LOGIC;
reset : in STD_LOGIC;
D0 : in STD_LOGIC_VECTOR (3 downto 0);
D1 : in STD_LOGIC_VECTOR (3 downto 0);
D2 : in STD_LOGIC_VECTOR (3 downto 0);
D3 : in STD_LOGIC_VECTOR (3 downto 0);
salida : out STD_LOGIC_VECTOR (7 downto 0);
multiple : out STD_LOGIC_VECTOR (3 downto 0)
);
end component;
component bin8bcd
port (
bin: in std_logic_vector (7 downto 0);
bcd: out std_logic_vector (11 downto 0)
);
end component;
signal bin : std_logic_vector (7 downto 0);
signal bcd : std_logic_vector (11 downto 0);
signal c1 , c2, c3 : std_logic;
signal reset : std_logic := '0';
signal D0 , D1, D2, D3, multiple : std_logic_vector ( 3 downto 0);
signal salida : std_logic_vector (7 downto 0);
begin
siete_segemntos: seg_comp
port map (
clock ,
reset,
D0,
D1,
D2,
D3,
salida,
multiple);
bit8_bcd : bin8bcd
port map (
bin,
bcd);
sqrt : process(data_in)
variable num : unsigned(15 downto 0) ;
variable res : unsigned(15 downto 0) ;
variable aux : unsigned(15 downto 0) ;
variable i : integer := 0;
variable int, a0, a1, a2, a3 : integer;
begin
num := data_in;
res := "0000000000000000";
aux := "0100000000000000";
for i in 0 to 8 loop
if (aux > num) then
aux := aux srl 2;
end if;
--report "num: " & std_logic'image(num(0)) & std_logic'image(num(1)) & std_logic'image(num(2)) & std_logic'image(num(3)) & std_logic'image(num(4));
--report "aux: " & std_logic'image(aux(0)) & std_logic'image(aux(1)) & std_logic'image(aux(2)) & std_logic'image(aux(3)) & std_logic'image(aux(4));
end loop;
for i in 0 to 8 loop
if (aux /= "0000000000000000") then
if (num >= res + aux) then
num := num - (res + aux);
res := (res srl 1) + aux;
else
res := res srl 1;
end if;
aux := aux srl 2;
end if;
--report std_logic'image(res(0)) & std_logic'image(res(1)) & std_logic'image(res(2)) & std_logic'image(res(3)) & std_logic'image(res(4));
end loop;
data_out <= res;
bin <= std_logic_vector(res(7 downto 0));
--report "bcd: " & std_logic'image(bcd(4)) & std_logic'image(bcd(3)) & std_logic'image(bcd(2)) & std_logic'image(bcd(1)) & std_logic'image(bcd(0));
D0 <= bcd(3 downto 0);
D1 <= bcd(7 downto 4);
D2 <= bcd(11 downto 8);
--D3 <= bcd(11 downto 8);
numero <= salida;
mul <= multiple;
reset <= re;
-- int := to_integer(res);
--
-- a0 := int mod 10;
-- int := (int / 16);
-- a1 := int mod 10;
-- int := int / 10;
-- a2 := int mod 10;
-- int := int / 10;
-- a3 := int mod 10;
-- --int := int / 10;
--
-- D0 <= std_logic_vector(to_unsigned(a0, 4));
-- D1 <= std_logic_vector(to_unsigned(a1, 4));
-- D2 <= std_logic_vector(to_unsigned(a2, 4));
-- D3 <= std_logic_vector(to_unsigned(a3, 4));
end process;
end behaviour;