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

Subversion Repositories jart

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

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

powered by: WebSVN 2.1.0

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