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

Subversion Repositories jart

[/] [jart/] [branches/] [ver0branch/] [sqrt.vhd] - Blame information for rev 69

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 68 jguarin200
 
2
-- A 1 clock x 4 stage pipe square root.
3
 
4
 
5
library ieee;
6
use ieee.std_logic_1164.all;
7
use ieee.std_logic_arith.all;
8
use work.powerGrid.all;
9
 
10
entity sqrt is
11
 
12
        generic (
13
                W2 : integer := 64
14
 
15
        );
16
        port
17
        (
18
                clk,rst,ena : in std_logic;
19
 
20
                radical         : in std_logic_vector (W2-1 downto 0);
21 69 jguarin200
                root            : out std_logic_vector ((W2/2)-1 downto 0)
22 68 jguarin200
        );
23
end entity;
24
 
25
 
26
architecture rtl of sqrt is
27
 
28
        constant WP                             : integer:= W2/2;
29
        constant WP_2                   : integer:= WP/2;
30
 
31
 
32
        signal sa0,sb0,sx0,sy0,sb0_1            : std_logic_vector (WP-1 downto 0);
33
        signal sa1,sb1,sx1,sy1,sb1_1,muxs1      : std_logic_vector (WP-1 downto 0);
34
        signal sa2,sb2,sx2,sy2                          : std_logic_vector (WP-1 downto 0);
35
        signal localenc1                                        : integer range 0 to WP-1;
36
        signal localenc2                                        : integer range 0 to WP-1;
37 69 jguarin200
        signal sho                                                      : std_logic;
38
        signal a,b,x,y                                          : std_logic_vector ((W2/2)-1 downto 0);
39
        signal decoder                                          : integer range 0 to (W2/2)-1;
40
        signal ab,xy                                            : std_logic_vector (W2-1 downto 0);
41 68 jguarin200
 
42
 
43
        begin
44
 
45
        -- Logic function signals ...... if some day there's a paper of how this logic circuit works, it will be easier to comprehend this block of code
46
        sb0_1<=sa0(WP-2 downto 0) & '0';
47
        signalization : for i in 0 to WP-1 generate
48
 
49
                -- Stage 0. Functions for A,B,X and preliminar Y.
50
                sb0(i)<=radical(i*2);
51
                sa0(i)<=radical(i*2+1);
52
                sx0(i)<=sb0(i) or sa0(i);
53
                sy0(i)<=sb0(i) and sa0(i);
54
 
55
                -- Stage 1 : Function for signal Y.
56
                muxs1(i) <= sy1(i) or (not(sx1(i)) and sb1_1(i));
57
 
58 69 jguarin200
                -- Stage 3 :
59
                ab(i*2)         <= b(i);
60
                ab(i*2+1)       <= a(i);
61
                xy(i*2)         <= y(i);
62
                xy(i*2+1)       <= x(i);
63 68 jguarin200
 
64 69 jguarin200
 
65 68 jguarin200
        end generate signalization;
66
 
67
 
68
 
69
 
70
        stages: process (rst,clk,ena,sx0,sx1,localenc2)
71 69 jguarin200
                variable localenc0      : integer range 0 to WP-1;
72 68 jguarin200
        begin
73
 
74
                -- Highest signifcant pair enconder : look for the bit pair with the most significant bit.
75
                localenc0 := WP-1;
76
                stg0henc: while localenc0>0 loop
77
 
78
                        exit when (sx0(localenc0)='1');
79
                        localenc0:=localenc0-1;
80
                end loop;
81
 
82
 
83
 
84
 
85
                -- Clocking process;
86
                if rst='0' then
87
                        -- Stage 1
88
                        sa1<=(others => '0');
89
                        sb1<=(others => '0');
90
                        sx1<=(others => '0');
91
                        sy1<=(others => '0');
92
                        sb1_1<=(others => '0');
93
 
94
 
95
                        -- Stage 2
96
                        sx2<=(others => '0');
97
                        sa2<=(others => '0');
98
                        sb2<=(others => '0');
99
                        sy2<=(others => '0');
100
 
101
 
102
                        --Stage 3
103
                        x<=(others => '0');
104
                        y<=(others => '0');
105
                        a<=(others => '0');
106
                        b<=(others => '0');
107
 
108 69 jguarin200
                        --Stage 4
109
                        root <= (others=>'0');
110 68 jguarin200
 
111 69 jguarin200
 
112 68 jguarin200
 
113
                elsif rising_edge(clk) and ena='1' then
114
 
115
                        -- Stage01 
116
                        sa1<=sa0;
117
                        sb1<=sb0;
118
                        sx1<=sx0;
119
                        sy1<=sy0;
120
                        sb1_1<=sb0_1;
121
                        localenc1<=localenc0;
122
 
123
 
124
                        -- Stage12
125
                        sx2<=sx1;
126
                        sa2<=sa1;
127
                        sb2<=sb1;
128
                        sy2<= muxs1;
129
                        localenc2<=localenc1;
130
 
131
                        -- Stage 23 Shift 1 bit to right if the high bit in the highest significant pair is set.
132
                        if sa2(localenc2)='1' then
133
                                -- Shift Right
134
                                a <= '0' & sb2(WP-1 downto 1);
135
                                b <= sa2;
136
                                x <= '0' & sy2(WP-1 downto 1);
137
                                y <= sx2;
138
 
139
                        else
140
                                -- Leave me alone
141
                                x <= sx2;
142
                                y <= sy2;
143
                                a <= sa2;
144
                                b <= sb2;
145
                        end if;
146
 
147
                        decoder<=localenc2;
148
                        sho<=sa2(localenc2);
149
 
150 69 jguarin200
                        -- stage34
151
                        for i in 0 to WP-1 loop
152
                                if i>decoder then
153
                                        root(i)<='0';
154
                                elsif decoder-i>2 then
155
                                        root(i)<=ab(decoder+i+1);
156
                                elsif decoder-i=2 then
157
                                        root(i)<=(ab(decoder+i+1) and not(sho)) or (xy(decoder+i+1) and sho);
158
                                else
159
                                        root(i)<=xy(decoder+i+1);
160
                                end if;
161
                        end loop;
162
 
163 68 jguarin200
                end if;
164 69 jguarin200
 
165 68 jguarin200
        end process stages;
166
 
167
 
168
 
169
end rtl;
170
 

powered by: WebSVN 2.1.0

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