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

Subversion Repositories jart

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 82 jguarin200
-- Author:  
2 81 jguarin200
-- ***************************************************************************************************
3 82 jguarin200
--                     Juan Carlos Giraldo Carvajal M.Sc.
4 81 jguarin200
-- ***************************************************************************************************
5
--
6 82 jguarin200
--              
7 81 jguarin200
--
8
--
9 73 jguarin200
-- Project : JART, Just Another Ray Tracer.
10 68 jguarin200
 
11 73 jguarin200
-- The following code is licensed under GNU Public License
12
-- http://www.gnu.org/licenses/gpl-3.0.txt.
13
 
14
 -- This file is part of JART (Just Another Ray Tracer).
15
 
16
    -- JART (Just Another Ray Tracer) is free software: you can redistribute it and/or modify
17
    -- it under the terms of the GNU General Public License as published by
18
    -- the Free Software Foundation, either version 3 of the License, or
19
    -- (at your option) any later version.
20
 
21
    -- JART (Just Another Ray Tracer) is distributed in the hope that it will be useful,
22
    -- but WITHOUT ANY WARRANTY; without even the implied warranty of
23
    -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
    -- GNU General Public License for more details.
25
 
26
    -- You should have received a copy of the GNU General Public License
27
    -- along with JART (Just Another Ray Tracer).  If not, see <http://www.gnu.org/licenses/>.
28
 
29 68 jguarin200
-- A 1 clock x 4 stage pipe square root.
30
 
31 82 jguarin200
 
32 68 jguarin200
library ieee;
33
use ieee.std_logic_1164.all;
34
use ieee.std_logic_arith.all;
35
use work.powerGrid.all;
36
 
37
entity sqrt is
38
 
39
        generic (
40
                W2 : integer := 64
41
 
42
        );
43
        port
44
        (
45
                clk,rst,ena : in std_logic;
46
 
47
                radical         : in std_logic_vector (W2-1 downto 0);
48 69 jguarin200
                root            : out std_logic_vector ((W2/2)-1 downto 0)
49 68 jguarin200
        );
50
end entity;
51
 
52
 
53
architecture rtl of sqrt is
54
 
55
        constant WP                             : integer:= W2/2;
56
        constant WP_2                   : integer:= WP/2;
57
 
58
 
59
        signal sa0,sb0,sx0,sy0,sb0_1            : std_logic_vector (WP-1 downto 0);
60
        signal sa1,sb1,sx1,sy1,sb1_1,muxs1      : std_logic_vector (WP-1 downto 0);
61
        signal sa2,sb2,sx2,sy2                          : std_logic_vector (WP-1 downto 0);
62
        signal localenc1                                        : integer range 0 to WP-1;
63
        signal localenc2                                        : integer range 0 to WP-1;
64 69 jguarin200
        signal sho                                                      : std_logic;
65
        signal a,b,x,y                                          : std_logic_vector ((W2/2)-1 downto 0);
66
        signal decoder                                          : integer range 0 to (W2/2)-1;
67
        signal ab,xy                                            : std_logic_vector (W2-1 downto 0);
68 68 jguarin200
 
69
 
70
        begin
71
 
72
        -- 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
73
        sb0_1<=sa0(WP-2 downto 0) & '0';
74
        signalization : for i in 0 to WP-1 generate
75
 
76
                -- Stage 0. Functions for A,B,X and preliminar Y.
77
                sb0(i)<=radical(i*2);
78
                sa0(i)<=radical(i*2+1);
79
                sx0(i)<=sb0(i) or sa0(i);
80
                sy0(i)<=sb0(i) and sa0(i);
81
 
82
                -- Stage 1 : Function for signal Y.
83
                muxs1(i) <= sy1(i) or (not(sx1(i)) and sb1_1(i));
84
 
85 69 jguarin200
                -- Stage 3 :
86
                ab(i*2)         <= b(i);
87
                ab(i*2+1)       <= a(i);
88
                xy(i*2)         <= y(i);
89
                xy(i*2+1)       <= x(i);
90 68 jguarin200
 
91 69 jguarin200
 
92 68 jguarin200
        end generate signalization;
93
 
94
 
95
 
96
 
97
        stages: process (rst,clk,ena,sx0,sx1,localenc2)
98 69 jguarin200
                variable localenc0      : integer range 0 to WP-1;
99 68 jguarin200
        begin
100
 
101
                -- Highest signifcant pair enconder : look for the bit pair with the most significant bit.
102
                localenc0 := WP-1;
103
                stg0henc: while localenc0>0 loop
104
 
105
                        exit when (sx0(localenc0)='1');
106
                        localenc0:=localenc0-1;
107
                end loop;
108
 
109
 
110
 
111
 
112
                -- Clocking process;
113
                if rst='0' then
114
                        -- Stage 1
115
                        sa1<=(others => '0');
116
                        sb1<=(others => '0');
117
                        sx1<=(others => '0');
118
                        sy1<=(others => '0');
119
                        sb1_1<=(others => '0');
120
 
121
 
122
                        -- Stage 2
123
                        sx2<=(others => '0');
124
                        sa2<=(others => '0');
125
                        sb2<=(others => '0');
126
                        sy2<=(others => '0');
127
 
128
 
129
                        --Stage 3
130
                        x<=(others => '0');
131
                        y<=(others => '0');
132
                        a<=(others => '0');
133
                        b<=(others => '0');
134
 
135 69 jguarin200
                        --Stage 4
136
                        root <= (others=>'0');
137 68 jguarin200
 
138 69 jguarin200
 
139 68 jguarin200
 
140
                elsif rising_edge(clk) and ena='1' then
141
 
142
                        -- Stage01 
143
                        sa1<=sa0;
144
                        sb1<=sb0;
145
                        sx1<=sx0;
146
                        sy1<=sy0;
147
                        sb1_1<=sb0_1;
148
                        localenc1<=localenc0;
149
 
150
 
151
                        -- Stage12
152
                        sx2<=sx1;
153
                        sa2<=sa1;
154
                        sb2<=sb1;
155
                        sy2<= muxs1;
156
                        localenc2<=localenc1;
157
 
158
                        -- Stage 23 Shift 1 bit to right if the high bit in the highest significant pair is set.
159
                        if sa2(localenc2)='1' then
160
                                -- Shift Right
161
                                a <= '0' & sb2(WP-1 downto 1);
162
                                b <= sa2;
163
                                x <= '0' & sy2(WP-1 downto 1);
164
                                y <= sx2;
165
 
166
                        else
167
                                -- Leave me alone
168
                                x <= sx2;
169
                                y <= sy2;
170
                                a <= sa2;
171
                                b <= sb2;
172
                        end if;
173
 
174
                        decoder<=localenc2;
175
                        sho<=sa2(localenc2);
176
 
177 69 jguarin200
                        -- stage34
178
                        for i in 0 to WP-1 loop
179
                                if i>decoder then
180
                                        root(i)<='0';
181
                                elsif decoder-i>2 then
182
                                        root(i)<=ab(decoder+i+1);
183
                                elsif decoder-i=2 then
184
                                        root(i)<=(ab(decoder+i+1) and not(sho)) or (xy(decoder+i+1) and sho);
185
                                else
186
                                        root(i)<=xy(decoder+i+1);
187
                                end if;
188
                        end loop;
189
 
190 68 jguarin200
                end if;
191 69 jguarin200
 
192 68 jguarin200
        end process stages;
193
 
194
 
195
 
196
end rtl;
197
 

powered by: WebSVN 2.1.0

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