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

Subversion Repositories raytrac

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

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 81 jguarin200
                iwidth  : integer       := 32;
38 73 jguarin200
                awidth  : integer       := 9
39
        );
40
        port (
41
                clk,rst : in std_logic;
42
                value   : in std_logic_vector (iwidth-1 downto 0);
43
                zero    : out std_logic;
44 81 jguarin200
 
45
                sqr             : out std_logic_vector (15 downto 0);
46
                inv             : out std_logic_vector (16 downto 0)
47 73 jguarin200
        );
48
end sqrtdiv;
49
 
50
architecture sqrtdiv_arch of sqrtdiv is
51
 
52
        --! expomantis::Primera etapa: Calculo parcial de la mantissa y el exponente.
53
        signal expomantisvalue  : std_logic_vector (iwidth-1 downto 0);
54
 
55
        signal expomantisexp    : std_logic_vector (2*integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
56
        signal expomantisadd    : std_logic_vector (2*awidth-1 downto 0);
57
        signal expomantiszero   : std_logic;
58
 
59
        --! funky::Segunda etapa: Calculo del valor de la funcion evaluada en f.
60
        signal funkyadd                 : std_logic_vector (2*awidth-1 downto 0);
61
        signal funkyexp                 : std_logic_vector (2*integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
62
        signal funkyzero                : std_logic;
63
 
64 81 jguarin200
        signal funkyq                   : std_logic_vector (2*c3width-1  downto 0);
65 73 jguarin200
        signal funkyselector    : std_logic;
66
 
67
        --! cumpa::Tercera etapa: Selecci'on de valores de acuerdo al exp escogido.
68
        signal cumpaexp                 : std_logic_vector (2*integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
69 81 jguarin200
        signal cumpaq                   : std_logic_vector (2*c3width-1 downto 0);
70 73 jguarin200
        signal cumpaselector    : std_logic;
71
        signal cumpazero                : std_logic;
72
 
73 74 jguarin200
        signal cumpaN                   : std_logic_vector (integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
74 81 jguarin200
        signal cumpaF                   : std_logic_vector (c3width-1 downto 0);
75 73 jguarin200
 
76
        --! chief::Cuarta etapa: Corrimiento a la izquierda o derecha, para el caso de la ra'iz cuadrada o la inversi'on respectivamente. 
77
 
78 74 jguarin200
        signal chiefN                   : std_logic_vector (integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
79 81 jguarin200
        signal chiefF                   : std_logic_vector (c3width-1 downto 0);
80
        signal chiefQ                   : std_logic_vector (c3width-1 downto 0);
81 73 jguarin200
 
82 81 jguarin200
        --! inverseDistance::Quinta etapa
83 73 jguarin200
 
84 81 jguarin200
        signal iDistN                   : std_logic_vector (integer(ceil(log(real(iwidth),2.0)))-1 downto 0);
85
        signal iDistF                   : std_logic_vector (c3width-1 downto 0);
86 74 jguarin200
        --! Constantes para manejar el tama&ntilde;o de los vectores
87
        constant exp1H : integer := 2*integer(ceil(log(real(iwidth),2.0)))-1;
88
        constant exp1L : integer := integer(ceil(log(real(iwidth),2.0)));
89
        constant exp0H : integer := exp1L-1;
90
        constant exp0L : integer := 0;
91
        constant add1H : integer := 2*awidth-1;
92
        constant add1L : integer := awidth;
93 75 jguarin200
        constant add0H : integer := awidth-1;
94 74 jguarin200
        constant add0L : integer := 0;
95
 
96
 
97 81 jguarin200
        constant c3qHH : integer := 2*c3width-1;
98
        constant c3qHL : integer := c3width;
99
        constant c3qLH : integer := c3width-1;
100 74 jguarin200
        constant c3qLL : integer := 0;
101
 
102 73 jguarin200
begin
103
 
104 74 jguarin200
        --! expomantis.
105 73 jguarin200
        expomantisreg:
106
        if reginput="YES" generate
107
                expomantisProc:
108
                process (clk,rst)
109
                begin
110
                        if rst=rstMasterValue then
111
                                expomantisvalue <= (others =>'0');
112
                        elsif clk'event and clk='1' then
113 74 jguarin200
                                expomantisvalue <= value;
114 73 jguarin200
                        end if;
115
                end process expomantisProc;
116
        end generate expomantisreg;
117 74 jguarin200
 
118
        expomantisnoreg:
119 73 jguarin200
        if reginput ="NO" generate
120
                expomantisvalue<=value;
121
        end generate expomantisnoreg;
122 74 jguarin200
 
123 73 jguarin200
        expomantisshifter2x:shifter2xstage
124
        generic map(awidth,iwidth)
125
        port map(expomantisvalue,expomantisexp,expomantisadd,expomantiszero);
126
 
127
        --! funky.
128
        funkyProc:
129 74 jguarin200
        process (clk,rst,expomantisexp, expomantiszero)
130 73 jguarin200
        begin
131
                if rst=rstMasterValue then
132
                        funkyexp <= (others => '0');
133
                        funkyzero <= '0';
134 75 jguarin200
                        funkyadd <= (others => '0');
135
 
136 74 jguarin200
                elsif clk'event and clk='1' then
137 75 jguarin200
 
138 74 jguarin200
                        funkyexp(exp1H downto 0) <= expomantisexp(exp1H downto 0);
139 73 jguarin200
                        funkyzero <= expomantiszero;
140 75 jguarin200
                        funkyadd <= expomantisadd;
141
 
142 73 jguarin200
                end if;
143
        end process funkyProc;
144 75 jguarin200
 
145 73 jguarin200
        funkyget:
146
        process (funkyexp)
147
        begin
148 74 jguarin200
                if (funkyexp(exp0H downto 0)>funkyexp(exp1H downto exp1L)) then
149 73 jguarin200
                        funkyselector<='0';
150
                else
151
                        funkyselector<='1';
152
                end if;
153
        end process funkyget;
154 74 jguarin200
 
155 81 jguarin200
 
156 73 jguarin200
 
157 81 jguarin200
        sqrt: funcinvr
158
        generic map (memoryPath&"memsqrt.mif")
159
        port map(
160
                funkyadd(awidth-1 downto 0),
161
                clk,
162
                funkyq(c3qLH downto c3qLL));
163
 
164
        sqrt2x: funcinvr
165
        generic map (memoryPath&"memsqrt2f.mif")
166
        port map(
167
                funkyadd(2*awidth-1 downto awidth),
168
                clk,
169
                funkyq(c3qHH downto c3qHL));
170 79 jguarin200
 
171 73 jguarin200
 
172
        --! cumpa.
173
        cumpaProc:
174
        process (clk,rst)
175
        begin
176
                if rst=rstMasterValue then
177 74 jguarin200
                        cumpaselector <= '0';
178
                        cumpazero <= '0';
179 73 jguarin200
                        cumpaexp <= (others => '0');
180
                        cumpaq <= (others => '0');
181
                elsif clk'event and clk='1' then
182
                        cumpaselector <= funkyselector;
183
                        cumpazero <= funkyzero;
184
                        cumpaexp <= funkyexp;
185
                        cumpaq <= funkyq;
186
                end if;
187
        end process cumpaProc;
188
        cumpaMux:
189
        process (cumpaq,cumpaexp,cumpaselector)
190
        begin
191
                if cumpaselector='0' then
192 74 jguarin200
                        cumpaN<=cumpaexp(exp0H downto exp0L);
193
                        cumpaF<=cumpaq(c3qLH downto c3qLL);
194 73 jguarin200
                else
195 74 jguarin200
                        cumpaN<=cumpaexp(exp1H downto exp1L);
196
                        cumpaF<=cumpaq(c3qHH downto c3qHL);
197 73 jguarin200
                end if;
198
        end process cumpaMux;
199
 
200 81 jguarin200
        --! Branching.
201
        -- exp <= chiefN;
202
        -- fadd <= chiefF(c3width-2 downto c3width-1-awidth);
203 73 jguarin200
        chiefProc:
204
        process (clk,rst)
205
        begin
206
                if rst=rstMasterValue then
207
                        chiefF <= (others => '0');
208
                        chiefN <= (others => '0');
209
                elsif clk'event and clk='1' then
210
                        chiefF <= cumpaF;
211
                        chiefN <= cumpaN;
212
                        zero <= cumpazero;
213
                end if;
214
        end process chiefProc;
215 74 jguarin200
        chiefShifter: RLshifter
216 81 jguarin200
        generic map("SQUARE_ROOT",c3width,iwidth,16)
217 74 jguarin200
        port map(
218
                chiefN,
219
                chiefF,
220 81 jguarin200
                sqr);
221
        branching_invfunc:
222
        if functype="INVERSION" generate
223
                sqrt: funcinvr
224
                generic map (memoryPath&"meminvr.mif")
225
                port map(
226
                        chiefF(c3width-2 downto c3width-1-awidth),
227
                        clk,
228
                        chiefQ(c3width-1 downto 0));
229
 
230
 
231
 
232
        end generate branching_invfunc;
233 73 jguarin200
 
234 81 jguarin200
        branchingHighLander:
235
        if functype="SQUARE_ROOT" generate
236
                chiefQ <= (others => '0');
237
        end generate branchingHighLander;
238
 
239
        --! Inverse
240
        inverseDistanceOK:
241
        if functype="INVERSION" generate
242
 
243
                inverseDistance:
244
                process (clk,rst)
245
                begin
246
 
247
                        if rst=rstMasterValue then
248
                                iDistN <= (others => '0');
249
                                iDistF <= (others => '0');
250
                        elsif clk'event and clk='1' then
251
                                iDistN <= chiefN;
252
                                iDistF <= chiefQ;
253
                        end if;
254
 
255
 
256
                end process inverseDistance;
257
 
258
                inverseShifter: RLshifter
259
                generic map("INVERSION",c3width,iwidth,17)
260
                port map(
261
                        iDistN,
262
                        iDistF,
263
                        inv);
264
        end generate inverseDistanceOK;
265
 
266
        inverseDistanceNOTOK:
267
        if functype = "SQUARE_ROOT" generate
268
                iDistN <= (others => '0');
269
                iDistF <= (others => '0');
270
                inv <= (others => '0');
271
        end generate inverseDistanceNOTOK;
272
 
273 73 jguarin200
end sqrtdiv_arch;
274
 
275
 
276
 
277
 
278
 
279
 
280
 

powered by: WebSVN 2.1.0

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