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

Subversion Repositories raytrac

[/] [raytrac/] [branches/] [fp/] [arithpack.vhd] - Blame information for rev 153

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

Line No. Rev Author Line
1 151 jguarin200
library ieee;
2
use ieee.std_logic_1164.all;
3 153 jguarin200
use ieee.std_logic_arith.all;
4
use ieee.math_real.all;
5 151 jguarin200
 
6 153 jguarin200
library std;
7
use std.textio.all;
8
 
9 151 jguarin200
--! Memory Compiler Library
10
library lpm;
11
use lpm.all;
12
 
13
 
14
 
15
package arithpack is
16
        --! Estados para la maquina de estados.
17
        type macState is (LOAD_INSTRUCTION,FLUSH_ARITH_PIPELINE,EXECUTE_INSTRUCTION);
18
        --! Estados para el controlador de interrupciones.
19
        type iCtrlState is (WAITING_FOR_AN_EVENT,FIRING_INTERRUPTIONS,SUSPEND);
20 152 jguarin200
 
21
        --! Float data blocks
22
        constant floatwidth : integer := 32;
23
        constant widthadmemblock : integer := 9;
24
 
25
        type    vectorblock12 is array (11 downto 0) of std_logic_vector(floatwidth-1 downto 0);
26
        type    vectorblock08 is array (07 downto 0) of std_logic_vector(floatwidth-1 downto 0);
27
        type    vectorblock06 is array (05 downto 0) of std_logic_vector(floatwidth-1 downto 0);
28
        type    vectorblock04 is array (03 downto 0) of std_logic_vector(floatwidth-1 downto 0);
29
        type    vectorblock03 is array (02 downto 0) of std_logic_vector(floatwidth-1 downto 0);
30
        type    vectorblock02 is array (01 downto 0) of std_logic_vector(floatwidth-1 downto 0);
31
        type    vectorblockadd02 is array (01 downto 0) of std_logic_vector(widthadmemblock-1 downto 0);
32
 
33 153 jguarin200
        type    v3f     is array(02 downto 0) of std_logic_vector(31 downto 0);
34 152 jguarin200
 
35 153 jguarin200
 
36
 
37 151 jguarin200
        --! Constante de reseteo
38
        constant rstMasterValue : std_logic :='0';
39
        --! Constantes periodicas.
40
        constant tclk   : time := 20 ns;
41
        constant tclk_2 : time := tclk/2;
42
        constant tclk_4 : time := tclk/4;
43
 
44 152 jguarin200
 
45
        component raytrac
46
        port (
47
 
48
                clk : in std_logic;
49
                rst : in std_logic;
50
 
51
                --! Señal de lectura de alguna de las colas de resultados.
52
                rd      : in std_logic;
53
 
54
                --! Señal de escritura en alguno de los bloques de memoria de operandos o en la cola de instrucciones.
55
                wr      : in std_logic;
56
 
57
                --! Direccion de escritura o lectura
58
                add : in std_logic_vector (12 downto 0);
59
 
60
                --! datos de entrada
61
                d       : in std_logic_vector (31 downto 0);
62
 
63
                --! Interrupciones
64
                int     : out std_logic_vector (7 downto 0);
65
 
66
                --! Salidas
67
                q : out std_logic_vector (31 downto 0)
68
 
69
 
70
 
71
        );
72
        end component;
73
 
74
        --! Componentes Aritméticos
75
 
76
        component fadd32
77
        port (
78
                clk : in std_logic;
79
                dpc : in std_logic;
80
                a32 : in std_logic_vector (31 downto 0);
81
                b32 : in std_logic_vector (31 downto 0);
82
                c32 : out std_logic_vector (31 downto 0)
83
        );
84
        end component;
85
        component fmul32
86
        port (
87
                clk : in std_logic;
88
                a32 : in std_logic_vector (31 downto 0);
89
                b32 : in std_logic_vector (31 downto 0);
90
                p32 : out std_logic_vector (31 downto 0)
91
        );
92
        end component;
93
 
94
 
95
        --! Contadores para la máquina de estados.
96
 
97 151 jguarin200
        component customCounter
98
        generic (
99
                EOBFLAG         : string ;
100
                ZEROFLAG        : string ;
101
                BACKWARDS       : string ;
102
                EQUALFLAG       : string ;
103
                subwidth        : integer;
104
                width           : integer
105
 
106
        );
107
        port (
108
                clk,rst,go,set  : in std_logic;
109
                setValue,cmpBlockValue          : in std_Logic_vector(width-1 downto subwidth);
110
                zero_flag,eob_flag,eq_flag      : out std_logic;
111
                count                   : out std_logic_vector(width-1 downto 0)
112
        );
113
        end component;
114
 
115
        --! LPM Memory Compiler.
116
        component scfifo
117
        generic (
118
                add_ram_output_register :string;
119
                almost_full_value               :natural;
120
                allow_wrcycle_when_full :string;
121
                intended_device_family  :string;
122
                lpm_hint                                :string;
123
                lpm_numwords                    :natural;
124
                lpm_showahead                   :string;
125
                lpm_type                                :string;
126
                lpm_width                               :natural;
127
                lpm_widthu                              :natural;
128
                overflow_checking               :string;
129
                underflow_checking              :string;
130
                use_eab                                 :string
131
        );
132
        port(
133
                rdreq           : in std_logic;
134
                aclr            : in std_logic;
135
                empty           : out std_logic;
136
                clock           : in std_logic;
137
                q                       : out std_logic_vector(lpm_width-1 downto 0);
138
                wrreq           : in std_logic;
139
                data            : in std_logic_vector(lpm_width-1 downto 0);
140
                almost_full : out std_logic;
141
                full            : out std_logic
142
        );
143
        end component;
144
 
145
 
146
        component altsyncram
147
        generic (
148
                address_aclr_b                  : string;
149
                address_reg_b                   : string;
150
                clock_enable_input_a    : string;
151
                clock_enable_input_b    : string;
152
                clock_enable_output_b   : string;
153
                intended_device_family  : string;
154
                lpm_type                                : string;
155
                numwords_a                              : natural;
156
                numwords_b                              : natural;
157
                operation_mode                  : string;
158
                outdata_aclr_b                  : string;
159
                outdata_reg_b                   : string;
160
                power_up_uninitialized  : string;
161
                ram_block_type                  : string;
162
                rdcontrol_reg_b                 : string;
163
                read_during_write_mode_mixed_ports      : string;
164
                widthad_a                               : natural;
165
                widthad_b                               : natural;
166
                width_a                                 : natural;
167
                width_b                                 : natural;
168
                width_byteena_a                 : natural
169
        );
170
        port (
171
                wren_a          : in std_logic;
172
                clock0          : in std_logic;
173
                address_a       : in std_logic_vector(8 downto 0);
174
                address_b       : in std_logic_vector(8 downto 0);
175
                rden_b          : in std_logic;
176
                q_b                     : out std_logic_vector(31 downto 0);
177
                data_a          : in std_logic_vector(31 downto 0)
178
 
179
        );
180
        end component;
181
 
182
        --! Maquina de Estados.
183
        component sm
184 152 jguarin200
 
185 151 jguarin200
        port (
186
 
187
                --! Señales normales de secuencia.
188
                clk,rst:                        in std_logic;
189 152 jguarin200
                --! Vector con las instrucción codficada
190 151 jguarin200
                instrQq:in std_logic_vector(31 downto 0);
191 152 jguarin200
                --! Señal de cola vacia.
192 151 jguarin200
                instrQ_empty:in std_logic;
193
                adda,addb:out std_logic_vector (8 downto 0);
194
                sync_chain_0,instrRdAckd:out std_logic;
195
                full_r:         in std_logic;   --! Indica que la cola de resultados no puede aceptar mas de 32 elementos.
196
                --! End Of Instruction Event
197
                eoi     : out std_logic;
198
 
199
                --! DataPath Control uca code.
200
                dpc_uca : out std_logic_vector (2 downto 0);
201
                state   : out macState
202
        );
203
        end component;
204
        --! Maquina de Interrupciones
205
        component im
206
        generic (
207
                num_events : integer ;
208
                cycles_to_wait : integer
209
        );
210
        port (
211
                clk,rst:                in std_logic;
212
                rfull_events:   in std_logic_vector(num_events-1 downto 0);      --! full results queue events
213
                eoi_events:             in std_logic_vector(num_events-1 downto 0);      --! end of instruction related events
214
                eoi_int:                out std_logic_vector(num_events-1 downto 0);--! end of instruction related interruptions
215
                rfull_int:              out std_logic_vector(num_events-1downto 0);      --! full results queue related interruptions
216
                state:                  out iCtrlState
217
        );
218
        end component;
219
        --! Bloque de memorias
220
        component memblock
221
        generic (
222
                blocksize                                       : integer;
223
                external_writeable_blocks       : integer;
224
                external_readable_blocks        : integer;
225
                external_readable_widthad       : integer;
226
                external_writeable_widthad      : integer
227
        );
228
        port (
229
 
230
 
231
                clk,rst,dpfifo_rd,normfifo_rd,dpfifo_wr,normfifo_wr : in std_logic;
232
                instrfifo_rd : in std_logic;
233
                resultfifo_wr: in std_logic_vector(external_readable_blocks-1 downto 0);
234
                instrfifo_empty: out std_logic; ext_rd,ext_wr: in std_logic;
235
                ext_wr_add : in std_logic_vector(external_writeable_widthad+widthadmemblock-1 downto 0);
236
                ext_rd_add : in std_logic_vector(external_readable_widthad-1 downto 0);
237 152 jguarin200
                ext_d: in std_logic_vector(floatwidth-1 downto 0);
238
                int_d : in std_logic_vector(external_readable_blocks*floatwidth-1 downto 0);
239 151 jguarin200
                resultfifo_full  : out std_logic_vector(3 downto 0);
240 152 jguarin200
                ext_q,instrfifo_q : out std_logic_vector(floatwidth-1 downto 0);
241
                int_q : out std_logic_vector(external_writeable_blocks*floatwidth-1 downto 0);
242 151 jguarin200
                int_rd_add : in std_logic_vector(2*widthadmemblock-1 downto 0);
243 152 jguarin200
                dpfifo_d : in std_logic_vector(floatwidth*2-1 downto 0);
244
                normfifo_d : in std_logic_vector(floatwidth*3-1 downto 0);
245
                dpfifo_q : out std_logic_vector(floatwidth*2-1 downto 0);
246
                normfifo_q : out std_logic_vector(floatwidth*3-1 downto 0)
247 151 jguarin200
        );
248
        end component;
249
        --! Bloque decodificacion DataPath Control.
250
        component dpc
251
        port (
252
                clk,rst                                 : in    std_logic;
253 152 jguarin200
                paraminput                              : in    std_logic_vector ((12*floatwidth)-1 downto 0);   --! Vectores A,B,C,D
254
                prd32blko                               : in    std_logic_vector ((06*floatwidth)-1 downto 0);   --! Salidas de los 6 multiplicadores.
255
                add32blko                               : in    std_logic_vector ((04*floatwidth)-1 downto 0);   --! Salidas de los 4 sumadores.
256
                sqr32blko,inv32blko             : in    std_logic_vector (floatwidth-1 downto 0);                --! Salidas de la raiz cuadradas y el inversor.
257
                fifo32x23_q                             : in    std_logic_vector (03*floatwidth-1 downto 0);             --! Salida de la cola intermedia.
258
                fifo32x09_q                             : in    std_logic_vector (02*floatwidth-1 downto 0);     --! Salida de las colas de producto punto. 
259 151 jguarin200
                unary,crossprod,addsub  : in    std_logic;                                                                      --! Bit con el identificador del bloque AB vs CD e identificador del sub bloque (A/B) o (C/D). 
260 152 jguarin200
                sync_chain_0                    : in    std_logic;                                                                      --! Señal de dato valido que se va por toda la cadena de sincronizacion.
261
                eoi_int                                 : in    std_logic;                                                                      --! Señal de interrupción de final de instrucci&ocaute;n.
262
                eoi_demuxed_int                 : out   std_logic_vector (3 downto 0);                           --! Señal de interrup&ocaute;n de final de instrucción pero esta vez va asociada a la instruccón UCA.
263
                sqr32blki,inv32blki             : out   std_logic_vector (floatwidth-1 downto 0);                --! Salidas de las 2 raices cuadradas y los 2 inversores.
264
                fifo32x26_d                             : out   std_logic_vector (03*floatwidth-1 downto 0);             --! Entrada a la cola intermedia para la normalización.
265
                fifo32x09_d                             : out   std_logic_vector (02*floatwidth-1 downto 0);             --! Entrada a las colas intermedias del producto punto.         
266
                prd32blki                               : out   std_logic_vector ((12*floatwidth)-1 downto 0);   --! Entrada de los 12 factores en el bloque de multiplicación respectivamente.
267
                add32blki                               : out   std_logic_vector ((08*floatwidth)-1 downto 0);   --! Entrada de los 8 sumandos del bloque de 4 sumadores.  
268 151 jguarin200
                resw                                    : out   std_logic_vector (4 downto 0);                           --! Salidas de escritura y lectura en las colas de resultados.
269
                fifo32x09_w                             : out   std_logic;
270
                fifo32x23_w,fifo32x09_r : out   std_logic;
271
                fifo32x23_r                             : out   std_logic;
272
                resf_vector                             : in    std_logic_vector(3 downto 0);                            --! Entradas de la señal de full de las colas de resultados. 
273
                resf_event                              : out   std_logic;                                                                      --! Salida decodificada que indica que la cola de resultados de la operación que está en curso.
274 152 jguarin200
                resultoutput                    : out   std_logic_vector ((08*floatwidth)-1 downto 0)    --! 8 salidas de resultados, pues lo máximo que podrá calcularse por cada clock son 2 vectores.
275 151 jguarin200
        );
276
        end component;
277
        --! Bloque Aritmetico de Sumadores y Multiplicadores (madd)
278
        component arithblock
279
        port (
280
 
281
                clk     : in std_logic;
282
                rst : in std_logic;
283
 
284
                dpc : in std_logic;
285
 
286
                f       : in std_logic_vector (12*32-1 downto 0);
287
                a       : in std_logic_vector (8*32-1 downto 0);
288
 
289
                s       : out std_logic_vector (4*32-1 downto 0);
290
                p       : out std_logic_vector (6*32-1 downto 0)
291
 
292
        );
293
        end component;
294
        --! Bloque de Raiz Cuadrada
295
        component sqrt32
296
        port (
297
 
298
                clk     : in std_logic;
299
                rd32: in std_logic_vector(31 downto 0);
300
                sq32: out std_logic_vector(31 downto 0)
301
        );
302
        end component;
303
        --! Bloque de Inversores.
304
        component invr32
305
        port (
306
 
307
                clk             : in std_logic;
308
                dvd32   : in std_logic_vector(31 downto 0);
309
                qout32  : out std_logic_vector(31 downto 0)
310
        );
311
        end component;
312 153 jguarin200
 
313
 
314
 
315
 
316
        type apCamera is record
317
                resx,resy : integer;
318
                width,height : real;
319
                dist : real;
320
        end record;
321
 
322
        --! Función que convierte un std_logic_vector en un numero entero
323
        function ap_slv2int(sl:std_logic_vector) return integer;
324
 
325
        --! Función que convierte un número flotante IEE754 single float, en un número std_logic_vector.
326
        function ap_fp2slv (f:real) return std_logic_vector;
327
 
328
        --! Función que convierte un número std_logic_vector en un ieee754 single float.
329
        function ap_slv2fp (sl:std_logic_vector) return real;
330
 
331
        --! Función que devuelve un vector en punto flotante IEEE754 a través de un   
332
        function ap_slv_calc_xyvec (x,y:integer; cam:apCamera) return v3f;
333
 
334
 
335
 
336
 
337
 
338 151 jguarin200
end package;
339 153 jguarin200
 
340
 
341
package body arithpack is
342
 
343
        function ap_slv2int (sl:std_logic_vector) return integer is
344
                alias s : std_logic_vector (sl'high downto sl'low) is sl;
345
                variable i : integer;
346
        begin
347
                i:=0;
348
                for index in s'high downto s'low loop
349
                        if s(index)='1' then
350
                                i:=i*2+1;
351
                        else
352
                                i:=i*2;
353
                        end if;
354
                end loop;
355
                return i;
356
 
357
        end function;
358
        function ap_fp2slv (f:real) return std_logic_vector is
359
                variable faux : real;
360
                variable sef : std_logic_vector (31 downto 0);
361
        begin
362
                --! Signo
363
                if (f<0.0) then
364
                        sef(31) := '1';
365
                else
366
                        sef(31) := '0';
367
                end if;
368
 
369
                --! Exponente
370
                sef(30 downto 23) := conv_std_logic_vector(integer(floor(log(f,2.0))),8);
371
 
372
                --! Fraction
373
                faux :=f/floor(log(f,2.0));
374
                faux := faux - 1.0;
375
 
376
                sef(22 downto 0)  := conv_std_logic_vector(integer(faux),23);
377
 
378
                return sef;
379
 
380
        end function;
381
 
382
        function ap_slv2fp(sl:std_logic_vector) return real is
383
                variable expo,frc:integer;
384
                alias s: std_logic_vector(31 downto 0) is sl;
385
                variable f: real;
386
 
387
        begin
388
 
389
 
390
                expo:=ap_slv2int(s(30 downto 23)) - 127;
391
                expo:=2**expo;
392
                frc:=ap_slv2int('1'&s(22 downto 0));
393
                f:=real(frc)*(2.0**(-23.0));
394
                f:=f*real(expo);
395
 
396
                if s(31)='1' then
397
                        return -f;
398
                else
399
                        return f;
400
                end if;
401
 
402
 
403
        end function;
404
 
405
        function ap_slv_calc_xyvec (x,y:integer; cam:apCamera) return v3f is
406
 
407
 
408
                variable dx,dy : real;
409
                variable v : v3f;
410
        begin
411
 
412
                dx := cam.width/real(cam.resx);
413
                dy := cam.height/real(cam.resy);
414
 
415
                --! Eje X: Tomando el dedo &iacute;ndice de la mano derecha, este eje queda apuntando en la direcci&on en la que mira la c&aacute;mara u observador siempre.
416
                v(0):=ap_fp2slv(cam.dist);
417
 
418
                --! Eje Y: Tomando el dedo coraz&oacute;n de la mano derecha, este eje queda apuntando a la izquierda del observador, desde el observador.
419
                v(1):=ap_fp2slv(dx*real(cam.resx)*0.5-dx*0.5);
420
 
421
                --! Eje Z: Tomando el dedo pulgar de la mano derecha, este eje queda apuntando hacia arriba del observador, desde el observador.
422
                v(2):=ap_fp2slv(dy*real(cam.resy)*0.5-dy*0.5);
423
 
424
                return v;
425
 
426
        end function;
427
 
428
end package body;

powered by: WebSVN 2.1.0

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