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

Subversion Repositories raytrac

[/] [raytrac/] [trunk/] [sqrtdiv/] [sqrtdiv.vhd] - Blame information for rev 76

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

Line No. Rev Author Line
1 73 jguarin200
--! @file sqrtdiv.vhd
2
--! @brief Unidad aritm'etica para calcular la potencia de un n'umero entero elevado a la -1 (INVERSION) o a la 0.5 (SQUARE_ROOT).
3
--! @author Juli´n Andrés Guarín Reyes.
4
-- RAYTRAC
5
-- Author Julian Andres Guarin
6
-- sqrtdiv.vhd
7
-- This file is part of raytrac.
8
-- 
9
--     raytrac is free software: you can redistribute it and/or modify
10
--     it under the terms of the GNU General Public License as published by
11
--     the Free Software Foundation, either version 3 of the License, or
12
--     (at your option) any later version.
13
-- 
14
--     raytrac is distributed in the hope that it will be useful,
15
--     but WITHOUT ANY WARRANTY; without even the implied warranty of
16
--     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
--     GNU General Public License for more details.
18
-- 
19
--     You should have received a copy of the GNU General Public License
20
--     along with raytrac.  If not, see <http://www.gnu.org/licenses/>.
21
 
22
 
23 74 jguarin200
library ieee;
24 73 jguarin200
use ieee.std_logic_1164.all;
25
use ieee.std_logic_arith.all;
26
use ieee.std_logic_unsigned.all;
27
use ieee.math_real.all;
28
 
29
use work.arithpack.all;
30
 
31
 
32
entity sqrtdiv is
33
        generic (
34
                reginput: string        := "YES";
35
                c3width : integer       := 18;
36 74 jguarin200
                functype: string        := "INVERSION";
37 73 jguarin200
                iwidth  : integer       := 32;
38 74 jguarin200
                owidth  : integer       := 18;
39 73 jguarin200
                awidth  : integer       := 9
40
        );
41
        port (
42
                clk,rst : in std_logic;
43
                value   : in std_logic_vector (iwidth-1 downto 0);
44
                zero    : out std_logic;
45
                result  : out std_logic_vector (owidth-1 downto 0)
46
        );
47
end sqrtdiv;
48
 
49
architecture sqrtdiv_arch of sqrtdiv is
50
 
51
        --! expomantis::Primera etapa: Calculo parcial de la mantissa y el exponente.
52
        signal expomantisvalue  : std_logic_vector (iwidth-1 downto 0);
53
 
54
        signal expomantisexp    : std_logic_vector (2*integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
55
        signal expomantisadd    : std_logic_vector (2*awidth-1 downto 0);
56
        signal expomantiszero   : std_logic;
57
 
58
        --! funky::Segunda etapa: Calculo del valor de la funcion evaluada en f.
59
        signal funkyadd                 : std_logic_vector (2*awidth-1 downto 0);
60
        signal funkyexp                 : std_logic_vector (2*integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
61
        signal funkyzero                : std_logic;
62
 
63 75 jguarin200
        signal funkyq                   : std_logic_vector (2*c3width+3 downto 0);
64 73 jguarin200
        signal funkyselector    : std_logic;
65
 
66
        --! cumpa::Tercera etapa: Selecci'on de valores de acuerdo al exp escogido.
67
        signal cumpaexp                 : std_logic_vector (2*integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
68 75 jguarin200
        signal cumpaq                   : std_logic_vector (2*c3width+3 downto 0);
69 73 jguarin200
        signal cumpaselector    : std_logic;
70
        signal cumpazero                : std_logic;
71
 
72 74 jguarin200
        signal cumpaN                   : std_logic_vector (integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
73 75 jguarin200
        signal cumpaF                   : std_logic_vector (c3width+1 downto 0);
74 73 jguarin200
 
75
        --! chief::Cuarta etapa: Corrimiento a la izquierda o derecha, para el caso de la ra'iz cuadrada o la inversi'on respectivamente. 
76
 
77 74 jguarin200
        signal chiefN                   : std_logic_vector (integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
78 75 jguarin200
        signal chiefF                   : std_logic_vector (c3width+1 downto 0);
79 73 jguarin200
 
80
 
81 74 jguarin200
        --! Constantes para manejar el tama&ntilde;o de los vectores
82
        constant exp1H : integer := 2*integer(ceil(log(real(iwidth),2.0)))-1;
83
        constant exp1L : integer := integer(ceil(log(real(iwidth),2.0)));
84
        constant exp0H : integer := exp1L-1;
85
        constant exp0L : integer := 0;
86
        constant add1H : integer := 2*awidth-1;
87
        constant add1L : integer := awidth;
88 75 jguarin200
        constant add0H : integer := awidth-1;
89 74 jguarin200
        constant add0L : integer := 0;
90
 
91
 
92 75 jguarin200
        constant c3qHH : integer := 2*c3width+3;
93
        constant c3qHL : integer := c3width+2;
94
        constant c3qLH : integer := c3width+1;
95 74 jguarin200
        constant c3qLL : integer := 0;
96
 
97 73 jguarin200
begin
98
 
99 74 jguarin200
        --! expomantis.
100 73 jguarin200
        expomantisreg:
101
        if reginput="YES" generate
102
                expomantisProc:
103
                process (clk,rst)
104
                begin
105
                        if rst=rstMasterValue then
106
                                expomantisvalue <= (others =>'0');
107
                        elsif clk'event and clk='1' then
108 74 jguarin200
                                expomantisvalue <= value;
109 73 jguarin200
                        end if;
110
                end process expomantisProc;
111
        end generate expomantisreg;
112 74 jguarin200
 
113
        expomantisnoreg:
114 73 jguarin200
        if reginput ="NO" generate
115
                expomantisvalue<=value;
116
        end generate expomantisnoreg;
117 74 jguarin200
 
118 73 jguarin200
        expomantisshifter2x:shifter2xstage
119
        generic map(awidth,iwidth)
120
        port map(expomantisvalue,expomantisexp,expomantisadd,expomantiszero);
121
 
122
        --! funky.
123
        funkyProc:
124 74 jguarin200
        process (clk,rst,expomantisexp, expomantiszero)
125 73 jguarin200
        begin
126
                if rst=rstMasterValue then
127
                        funkyexp <= (others => '0');
128
                        funkyzero <= '0';
129 75 jguarin200
                        funkyadd <= (others => '0');
130
 
131 74 jguarin200
                elsif clk'event and clk='1' then
132 75 jguarin200
 
133 74 jguarin200
                        funkyexp(exp1H downto 0) <= expomantisexp(exp1H downto 0);
134 73 jguarin200
                        funkyzero <= expomantiszero;
135 75 jguarin200
                        funkyadd <= expomantisadd;
136
 
137 73 jguarin200
                end if;
138
        end process funkyProc;
139 75 jguarin200
 
140 73 jguarin200
        funkyget:
141
        process (funkyexp)
142
        begin
143 74 jguarin200
                if (funkyexp(exp0H downto 0)>funkyexp(exp1H downto exp1L)) then
144 73 jguarin200
                        funkyselector<='0';
145
                else
146
                        funkyselector<='1';
147
                end if;
148
        end process funkyget;
149 74 jguarin200
 
150 73 jguarin200
        funkyinversion:
151
        if functype="INVERSION" generate
152
                meminvr:func
153 76 jguarin200
                generic map (memoryPath&"meminvr.mif")
154 73 jguarin200
                port map(
155 75 jguarin200
                        expomantisadd(awidth-1 downto 0),
156
                        expomantisadd(2*awidth-1 downto awidth),
157 73 jguarin200
                        clk,
158 75 jguarin200
                        funkyq(c3qLH-2 downto c3qLL),
159
                        funkyq(c3qHH-2 downto c3qHL));
160
                funkynibbles:
161
                process(funkyadd)
162
                begin
163
 
164
                        if funkyadd(awidth-1 downto 0) = conv_std_logic_vector(0,awidth) then
165
                                funkyq(c3qLH downto c3qLH-1) <= "10";
166
                        else
167
                                funkyq(c3qLH downto c3qLH-1) <= "01";
168
                        end if;
169
                        if funkyadd(2*awidth-1 downto awidth) = conv_std_logic_vector(0,awidth) then
170
                                funkyq(c3qHH downto c3qHH-1) <= "10";
171
                        else
172
                                funkyq(c3qHH downto c3qHH-1) <= "01";
173
                        end if;
174
                end process funkynibbles;
175
 
176 73 jguarin200
        end generate funkyinversion;
177
        funkysquare_root:
178
        if functype="SQUARE_ROOT" generate
179
                sqrt: func
180 76 jguarin200
                generic map (memoryPath&"memsqrt.mif")
181 73 jguarin200
                port map(
182 75 jguarin200
                        expomantisadd(awidth-1 downto 0),
183 74 jguarin200
                        (others => '0'),
184 73 jguarin200
                        clk,
185 75 jguarin200
                        funkyq(c3qLH-2 downto c3qLL),
186 73 jguarin200
                        open);
187
 
188
                sqrt2x: func
189 76 jguarin200
                generic map ("memsqrt2f.mif")
190 73 jguarin200
                port map(
191 74 jguarin200
                        (others => '0'),
192 75 jguarin200
                        expomantisadd(2*awidth-1 downto awidth),
193 73 jguarin200
                        clk,
194
                        open,
195 75 jguarin200
                        funkyq(c3qHH-2 downto c3qHL));
196
                funkynibbles:
197
                process(funkyadd)
198
                begin
199
                        --! Siempre ser'a 2 el nibble mas significativo cuando estemos calculando f**0.5. 
200
                        funkyq(c3qLH downto c3qLH-1) <= "10";
201
                        --! Siempre ser'a 1 el bit mas significativo cuando estemos calculando 2f**0.5.
202
                        funkyq(c3qHH) <= '1';
203
                        if funkyadd(2*awidth-1 downto awidth) >= conv_std_logic_vector(64,awidth) then
204
                                funkyq(c3qHH-1) <= '1';
205
                        else
206
                                funkyq(c3qHH-1) <= '0';
207
                        end if;
208
                end process funkynibbles;
209
 
210 73 jguarin200
        end generate funkysquare_root;
211
 
212
        --! cumpa.
213
        cumpaProc:
214
        process (clk,rst)
215
        begin
216
                if rst=rstMasterValue then
217 74 jguarin200
                        cumpaselector <= '0';
218
                        cumpazero <= '0';
219 73 jguarin200
                        cumpaexp <= (others => '0');
220
                        cumpaq <= (others => '0');
221
                elsif clk'event and clk='1' then
222
                        cumpaselector <= funkyselector;
223
                        cumpazero <= funkyzero;
224
                        cumpaexp <= funkyexp;
225
                        cumpaq <= funkyq;
226
                end if;
227
        end process cumpaProc;
228
        cumpaMux:
229
        process (cumpaq,cumpaexp,cumpaselector)
230
        begin
231
                if cumpaselector='0' then
232 74 jguarin200
                        cumpaN<=cumpaexp(exp0H downto exp0L);
233
                        cumpaF<=cumpaq(c3qLH downto c3qLL);
234 73 jguarin200
                else
235 74 jguarin200
                        cumpaN<=cumpaexp(exp1H downto exp1L);
236
                        cumpaF<=cumpaq(c3qHH downto c3qHL);
237 73 jguarin200
                end if;
238
        end process cumpaMux;
239
 
240
        --! chief.
241
        chiefProc:
242
        process (clk,rst)
243
        begin
244
                if rst=rstMasterValue then
245
                        chiefF <= (others => '0');
246
                        chiefN <= (others => '0');
247
                elsif clk'event and clk='1' then
248
                        chiefF <= cumpaF;
249
                        chiefN <= cumpaN;
250
                        zero <= cumpazero;
251
                end if;
252
        end process chiefProc;
253 74 jguarin200
        chiefShifter: RLshifter
254 75 jguarin200
        generic map(functype,c3width+2,iwidth,owidth)
255 74 jguarin200
        port map(
256
                chiefN,
257
                chiefF,
258
                result);
259 73 jguarin200
 
260
end sqrtdiv_arch;
261
 
262
 
263
 
264
 
265
 
266
 
267
 

powered by: WebSVN 2.1.0

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