1 |
|
2 | library ieee;
|
3 | use ieee.std_logic_1164.all;
|
4 | use ieee.std_logic_arith.all;
|
5 | use ieee.std_logic_unsigned.all;
|
6 |
|
7 | entity moore_sqrt is
|
8 | port (clk : in std_logic;
|
9 | enable : in std_logic;
|
10 | input : in std_logic_vector (15 downto 0);
|
11 | data_ready : out std_logic;
|
12 | output : out std_logic_vector (31 downto 0)
|
13 | );
|
14 | end moore_sqrt;
|
15 |
|
16 | architecture behavioral of moore_sqrt is
|
17 | ------------------------------------------------------------
|
18 | function division (x : std_logic_vector; y : std_logic_vector) return std_logic_vector is
|
19 | variable a1 : std_logic_vector(x'length-1 downto 0):=x;
|
20 | variable b1 : std_logic_vector(y'length-1 downto 0):=y;
|
21 | variable p1 : std_logic_vector(y'length downto 0):= (others => '0');
|
22 | variable i : integer:=0;
|
23 | begin
|
24 | for i in 0 to y'length-1 loop
|
25 | p1(y'length-1 downto 1) := p1(y'length-2 downto 0);
|
26 | p1(0) := a1(x'length-1);
|
27 | a1(x'length-1 downto 1) := a1(x'length-2 downto 0);
|
28 | p1 := p1-b1;
|
29 | if(p1(y'length-1) ='1') then
|
30 | a1(0) :='0';
|
31 | p1 := p1+b1;
|
32 | else
|
33 | a1(0) :='1';
|
34 | end if;
|
35 | end loop;
|
36 | return a1;
|
37 | end division;
|
38 | --------------------------------------------------------------
|
39 | type state_type is (s0, s1, s2, s3, s4, s5, s6); --type of state machine
|
40 | signal current_state,next_state: state_type; --current and next state declaration
|
41 |
|
42 | signal xk : std_logic_vector (31 downto 0);
|
43 | signal temp : std_logic_vector (31 downto 0);
|
44 | signal latched_input : std_logic_vector (15 downto 0);
|
45 | signal iterations : integer := 0;
|
46 | signal max_iterations : integer := 10; --corresponds with accuracy
|
47 |
|
48 | begin
|
49 |
|
50 | process (clk,enable)
|
51 | begin
|
52 | if enable = '0' then
|
53 | current_state <= s0;
|
54 | elsif clk'event and clk = '1' then
|
55 | current_state <= next_state; --state change
|
56 | end if;
|
57 | end process;
|
58 |
|
59 | --state machine
|
60 | process (current_state)
|
61 | begin
|
62 | case current_state is
|
63 | when s0 => -- reset
|
64 | output <= "00000000000000000000000000000000";
|
65 | data_ready <= '0';
|
66 | next_state <= s1;
|
67 | when s1 => -- latching input data
|
68 | latched_input <= input;
|
69 | next_state <= s2;
|
70 | when s2 => -- start calculating
|
71 | -- initial value is set as a half of input data
|
72 | output <= "00000000000000000000000000000000";
|
73 | data_ready <= '0';
|
74 | xk <= "0000000000000000" & division(latched_input, "0000000000000010");
|
75 | next_state <= s3;
|
76 | iterations <= 0;
|
77 | when s3 => -- division
|
78 | temp <= division ("0000" & latched_input & "000000000000", xk);
|
79 | next_state <= s4;
|
80 | when s4 => -- calculating
|
81 | if(iterations < max_iterations) then
|
82 | xk <= xk + temp;
|
83 | next_state <= s5;
|
84 | iterations <= iterations + 1;
|
85 | else
|
86 | next_state <= s6;
|
87 | end if;
|
88 | when s5 => -- shift logic right by 1
|
89 | xk <= division(xk, "00000000000000000000000000000010");
|
90 | next_state <= s3;
|
91 | when s6 => -- stop - proper data
|
92 | -- output <= division(xk, "00000000000000000000000001000000"); --the nearest integer value
|
93 | output <= xk; -- fixed point 24.6, sqrt = output/64;
|
94 | data_ready <= '1';
|
95 | end case;
|
96 | end process;
|
97 | end behavioral;
|