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

Subversion Repositories raytrac

[/] [raytrac/] [branches/] [fp_sgdma/] [arithpack.vhd] - Blame information for rev 229

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 159 jguarin200
 
10
 
11 151 jguarin200
--! Memory Compiler Library
12 159 jguarin200
library altera_mf;
13
use altera_mf.all;
14 151 jguarin200
library lpm;
15
use lpm.all;
16
 
17
 
18
 
19
package arithpack is
20 152 jguarin200
 
21 203 jguarin200
        --!Constantes usadas por los RTLs
22 229 jguarin200
        --constant qz : integer := 00;constant qy : integer := 01;constant qx : integer := 02;constant qsc: integer := 03;
23
        --constant az : integer := 00;constant ay : integer := 01;constant ax : integer := 02;constant bz : integer := 03;constant by : integer := 04;constant bx : integer := 05;
24
        --constant f0   : integer := 00;constant f1 : integer := 01;constant f2 : integer := 02;constant f3 : integer := 03;constant f4 : integer := 04;constant f5 : integer := 05;
25
        --constant f6   : integer := 06;constant f7 : integer := 07;constant f8 : integer := 08;constant f9 : integer := 09;constant f10: integer := 10;constant f11: integer := 11;
26
        --constant s0   : integer := 00;constant s1 : integer := 01;constant s2 : integer := 02;constant s3 : integer := 03;constant s4 : integer := 04;constant s5 : integer := 05;
27
        --constant a0   : integer := 00;constant a1 : integer := 01;constant a2 : integer := 02; 
28
        --constant p0   : integer := 00;constant p1 : integer := 01;constant p2 : integer := 02;constant p3 : integer := 03;constant p4 : integer := 04;constant p5 : integer := 05;
29 189 jguarin200
 
30 203 jguarin200
 
31 156 jguarin200
        subtype xfloat32 is std_logic_vector(31 downto 0);
32
        type    v3f     is array(02 downto 0) of xfloat32;
33
 
34 203 jguarin200
        --! Constantes para definir bloques de valores de 32 bits single float
35 156 jguarin200
        type    vectorblock12 is array (11 downto 0) of xfloat32;
36
        type    vectorblock08 is array (07 downto 0) of xfloat32;
37 213 jguarin200
        type    vectorblock06 is array (05 downto 0) of xfloat32;
38 203 jguarin200
        type    vectorblock04 is array (03 downto 0) of xfloat32;
39
        type    vectorblock03 is array (02 downto 0) of xfloat32;
40
        type    vectorblock02 is array (01 downto 0) of xfloat32;
41 152 jguarin200
 
42 151 jguarin200
        --! Constante de reseteo
43
        constant rstMasterValue : std_logic :='0';
44 168 jguarin200
 
45 151 jguarin200
        --! Constantes periodicas.
46
        constant tclk   : time := 20 ns;
47
        constant tclk_2 : time := tclk/2;
48
        constant tclk_4 : time := tclk/4;
49
 
50 152 jguarin200
 
51 203 jguarin200
 
52
 
53 153 jguarin200
        type apCamera is record
54
                resx,resy : integer;
55
                width,height : real;
56
                dist : real;
57
        end record;
58
 
59
        --! Función que convierte un std_logic_vector en un numero entero
60
        function ap_slv2int(sl:std_logic_vector) return integer;
61
 
62
        --! Función que convierte un número flotante IEE754 single float, en un número std_logic_vector.
63
        function ap_fp2slv (f:real) return std_logic_vector;
64
 
65
        --! Función que convierte un número std_logic_vector en un ieee754 single float.
66
        function ap_slv2fp (sl:std_logic_vector) return real;
67
 
68
        --! Función que devuelve un vector en punto flotante IEEE754 a través de un   
69
        function ap_slv_calc_xyvec (x,y:integer; cam:apCamera) return v3f;
70
 
71 156 jguarin200
        --! Función que devuelve una cadena con el número flotante IEEE 754 ó a una cadena de cifras hexadecimales.
72 160 jguarin200
        procedure ap_slvf2string(l:inout line;sl:std_logic_vector);
73
        procedure ap_slv2hex (l:inout line;h:in std_logic_vector) ;
74 153 jguarin200
 
75
 
76 156 jguarin200
 
77
        --! Función que devuelve una cadena con los componentes de un vector R3 en punto flotante IEEE754        
78 160 jguarin200
        procedure ap_v3f2string(l:inout line;v:in v3f);
79 173 jguarin200
        procedure ap_xfp032string(l:inout line;vb03:in vectorblock03);
80 157 jguarin200
 
81
        --! Función que formatea una instrucción
82 161 jguarin200
        function ap_format_instruction(i:string;ac_o,ac_f,bd_o,bd_f:std_logic_vector;comb:std_logic) return std_logic_vector;
83 157 jguarin200
 
84
        --! Función que devuelve una cadena de caracteres de un solo caracter con el valor de un bit std_logic
85 160 jguarin200
        procedure ap_sl2string(l:inout line;s:std_logic);
86
 
87 168 jguarin200
        --! Procedimiento para mostrar vectores en forma de arreglos de flotantes
88 160 jguarin200
        procedure ap_xfp122string(l:inout line;vb12:in vectorblock12);
89
        procedure ap_xfp082string(l:inout line;vb08:in vectorblock08);
90
        procedure ap_xfp062string(l:inout line;vb06:in vectorblock06);
91
        procedure ap_xfp042string(l:inout line;vb04:in vectorblock04);
92 168 jguarin200
        procedure ap_xfp022string(l:inout line;vb02:in vectorblock02);
93
 
94 156 jguarin200
 
95 151 jguarin200
end package;
96 153 jguarin200
 
97
 
98
package body arithpack is
99 160 jguarin200
 
100 168 jguarin200
        procedure ap_xfp022string(l:inout line; vb02:in vectorblock02) is
101
        begin
102
                for i in 01 downto 0 loop
103
                        write(l,string'(" ["&integer'image(i)&"]"));
104
                        write(l,string'(" "));
105
                        ap_slvf2string(l,vb02(i));
106
                end loop;
107
 
108
        end procedure;
109 160 jguarin200
        procedure ap_xfp122string(l:inout line; vb12:in vectorblock12) is
110 153 jguarin200
 
111 160 jguarin200
        begin
112
                for i in 11 downto 0 loop
113 168 jguarin200
                        write(l,string'(" ["&integer'image(i)&"]"));
114 160 jguarin200
                        write(l,string'(" "));
115
                        ap_slvf2string(l,vb12(i));
116
                end loop;
117
        end procedure;
118
 
119
        procedure ap_xfp082string(l:inout line; vb08:in vectorblock08) is
120
 
121
        begin
122
                for i in 07 downto 0 loop
123
                        write(l,string'(" ["&integer'image(i)&"]"));
124
                        write(l,string'(" "));
125
                        ap_slvf2string(l,vb08(i));
126
                end loop;
127
        end procedure;
128
 
129
        procedure ap_xfp062string(l:inout line; vb06:in vectorblock06) is
130
 
131
        begin
132
                for i in 05 downto 0 loop
133 168 jguarin200
                        write(l,string'(" ["&integer'image(i)&"]"));
134 160 jguarin200
                        write(l,string'(" "));
135
                        ap_slvf2string(l,vb06(i));
136
                end loop;
137
        end procedure;
138
 
139
        procedure ap_xfp042string(l:inout line; vb04:in vectorblock04) is
140
 
141
        begin
142
                for i in 03 downto 0 loop
143 168 jguarin200
                        write(l,string'(" ["&integer'image(i)&"]"));
144 160 jguarin200
                        write(l,string'(" "));
145
                        ap_slvf2string(l,vb04(i));
146
                end loop;
147
        end procedure;
148
 
149
 
150
        procedure ap_sl2string(l:inout line; s:in std_logic)is
151 157 jguarin200
                variable tmp:string(1 to 1);
152
        begin
153
 
154
                case s is
155
                        when '1' =>
156
                                tmp:="1";
157
                        when '0' =>
158
                                tmp:="0";
159
                        when 'U' =>
160
                                tmp:="U";
161
                        when 'X' =>
162
                                tmp:="X";
163
                        when 'Z' =>
164
                                tmp:="Z";
165
                        when 'W' =>
166
                                tmp:="W";
167
                        when 'L' =>
168
                                tmp:="L";
169
                        when 'H' =>
170
                                tmp:="H";
171
                        when others =>
172
                                tmp:="-"; -- Don't care
173
                end case;
174 168 jguarin200
                write(l,string'(" "));
175 160 jguarin200
                write(l,string'(tmp));
176 168 jguarin200
                write(l,string'(" "));
177 157 jguarin200
 
178 160 jguarin200
 
179
 
180
        end procedure;
181 157 jguarin200
 
182 161 jguarin200
        function ap_format_instruction(i:string;ac_o,ac_f,bd_o,bd_f:std_logic_vector;comb:std_logic) return std_logic_vector is
183 157 jguarin200
 
184
                alias aco : std_logic_vector (4 downto 0) is ac_o;
185
                alias acf : std_logic_vector (4 downto 0) is ac_f;
186
                alias bdo : std_logic_vector (4 downto 0) is bd_o;
187
                alias bdf : std_logic_vector (4 downto 0) is bd_f;
188
                variable ins : std_logic_vector (31 downto 0);
189
                alias it : string (1 to 3) is i;
190
        begin
191
 
192
                case it is
193
                        when "mag" =>
194
                                ins(31 downto 29) := "100";
195 160 jguarin200
                                ins(04 downto 00) := '1'&x"8";
196 157 jguarin200
                        when "nrm" =>
197 163 jguarin200
                                ins(31 downto 29) := "110";
198 160 jguarin200
                                ins(04 downto 00) := '1'&x"d";
199 157 jguarin200
                        when "add" =>
200
                                ins(31 downto 29) := "001";
201 160 jguarin200
                                ins(04 downto 00) := '0'&x"a";
202 157 jguarin200
                        when "sub" =>
203
                                ins(31 downto 29) := "011";
204 160 jguarin200
                                ins(04 downto 00) := '0'&x"a";
205 157 jguarin200
                        when "dot" =>
206
                                ins(31 downto 29) := "000";
207 160 jguarin200
                                ins(04 downto 00) := '1'&x"7";
208 157 jguarin200
                        when "crs" =>
209
                                ins(31 downto 29) := "010";
210 160 jguarin200
                                ins(04 downto 00) := '0'&x"e";
211 157 jguarin200
                        when others =>
212
                                ins(31 downto 29) := "111";
213 160 jguarin200
                                ins(04 downto 00) := '0'&x"5";
214 157 jguarin200
                end case;
215
                ins(28 downto 24) := aco;
216
                ins(23 downto 19) := acf;
217
                ins(18 downto 14) := bdo;
218
                ins(13 downto 09) := bdf;
219
                ins(08) := comb;
220
                ins(07 downto 05) := "000";
221
                return ins;
222
 
223
 
224
        end function;
225
 
226
 
227
 
228 160 jguarin200
        procedure ap_v3f2string(l:inout line;v:in v3f) is
229 155 jguarin200
        begin
230 168 jguarin200
                write(l,string'("[X]"));
231
                write(l,string'(" "));
232
                ap_slvf2string(l,v(2));
233
                write(l,string'("[Y]"));
234
                write(l,string'(" "));
235
                ap_slvf2string(l,v(1));
236
                write(l,string'("[Z]"));
237
                write(l,string'(" "));
238
                ap_slvf2string(l,v(0));
239 160 jguarin200
        end procedure;
240 173 jguarin200
        procedure ap_xfp032string(l:inout line;vb03:in vectorblock03) is
241
        begin
242
                write(l,string'("[X]"));
243
                write(l,string'(" "));
244
                ap_slvf2string(l,vb03(2));
245
                write(l,string'("[Y]"));
246
                write(l,string'(" "));
247
                ap_slvf2string(l,vb03(1));
248
                write(l,string'("[Z]"));
249
                write(l,string'(" "));
250
                ap_slvf2string(l,vb03(0));
251
        end procedure;
252 156 jguarin200
 
253
        constant hexchars : string (1 to 16) := "0123456789ABCDEF";
254 160 jguarin200
        procedure ap_slv2hex (l:inout line;h:in std_logic_vector) is
255
                variable index_high,index_low,highone,nc : integer;
256
        begin
257
                highone := h'high-h'low;
258
                nc:=0;
259
                for i in h'high downto h'low loop
260
                        if h(i)/='0' and h(i)/='1' then
261
                                nc:=1;
262
                        end if;
263 156 jguarin200
                end loop;
264 160 jguarin200
 
265
                if nc=1 then
266
                        for i in h'high downto h'low loop
267
                                ap_sl2string(l,h(i));
268
                        end loop;
269
                else
270
                        for i in (highone)/4 downto 0 loop
271
                                index_low:=i*4;
272
                                if (index_low+3)>highone then
273
                                        index_high := highone;
274
                                else
275
                                        index_high := i*4+3;
276
                                end if;
277
                                write(l,hexchars(1+ieee.std_logic_unsigned.conv_integer(h(index_high+h'low downto index_low+h'low))));
278
                        end loop;
279
                end if;
280
        end procedure;
281
 
282 153 jguarin200
        function ap_slv2int (sl:std_logic_vector) return integer is
283
                alias s : std_logic_vector (sl'high downto sl'low) is sl;
284
                variable i : integer;
285
        begin
286
                i:=0;
287
                for index in s'high downto s'low loop
288
                        if s(index)='1' then
289
                                i:=i*2+1;
290
                        else
291
                                i:=i*2;
292
                        end if;
293
                end loop;
294
                return i;
295
 
296
        end function;
297
        function ap_fp2slv (f:real) return std_logic_vector is
298
                variable faux : real;
299
                variable sef : std_logic_vector (31 downto 0);
300
        begin
301
                --! Signo
302
                if (f<0.0) then
303
                        sef(31) := '1';
304 160 jguarin200
                        faux:=f*(-1.0);
305 153 jguarin200
                else
306
                        sef(31) := '0';
307 160 jguarin200
                        faux:=f;
308 153 jguarin200
                end if;
309
 
310
                --! Exponente
311 160 jguarin200
                sef(30 downto 23) := conv_std_logic_vector(127+integer(floor(log(faux,2.0))),8);
312 153 jguarin200
 
313
                --! Fraction
314 160 jguarin200
                faux :=faux/(2.0**real(floor(log(faux,2.0))));
315 153 jguarin200
                faux := faux - 1.0;
316
 
317 160 jguarin200
                sef(22 downto 0)  := conv_std_logic_vector(integer(faux*(2.0**23.0)),23);
318 153 jguarin200
 
319
                return sef;
320
 
321
        end function;
322
 
323
        function ap_slv2fp(sl:std_logic_vector) return real is
324 160 jguarin200
                variable frc:integer;
325 153 jguarin200
                alias s: std_logic_vector(31 downto 0) is sl;
326 160 jguarin200
                variable f,expo: real;
327 153 jguarin200
 
328
        begin
329
 
330
 
331 160 jguarin200
                expo:=real(ap_slv2int(s(30 downto 23)) - 127);
332
                expo:=(2.0)**(expo);
333 153 jguarin200
                frc:=ap_slv2int('1'&s(22 downto 0));
334
                f:=real(frc)*(2.0**(-23.0));
335
                f:=f*real(expo);
336
 
337
                if s(31)='1' then
338
                        return -f;
339
                else
340
                        return f;
341 160 jguarin200
                end if;
342 153 jguarin200
 
343 160 jguarin200
 
344 153 jguarin200
 
345 160 jguarin200
 
346 153 jguarin200
        end function;
347
 
348
        function ap_slv_calc_xyvec (x,y:integer; cam:apCamera) return v3f is
349
 
350
 
351
                variable dx,dy : real;
352
                variable v : v3f;
353
        begin
354
 
355
                dx := cam.width/real(cam.resx);
356
                dy := cam.height/real(cam.resy);
357
 
358 160 jguarin200
                --! Eje Z: 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.
359 153 jguarin200
                v(0):=ap_fp2slv(cam.dist);
360
 
361 160 jguarin200
                --! Eje X: Tomando el dedo coraz&oacute;n de la mano derecha, este eje queda apuntando a la izquierda del observador, desde el observador.
362
                v(2):=ap_fp2slv(dx*real(cam.resx)*0.5-real(x)*dx-dx*0.5);
363 153 jguarin200
 
364 160 jguarin200
                --! Eje Y: Tomando el dedo pulgar de la mano derecha, este eje queda apuntando hacia arriba del observador, desde el observador.
365
                v(1):=ap_fp2slv(dy*real(cam.resy)*0.5-real(y)*dy-dy*0.5);
366 153 jguarin200
 
367
                return v;
368
 
369
        end function;
370 155 jguarin200
 
371 160 jguarin200
        procedure ap_slvf2string(l:inout line;sl:std_logic_vector) is
372 155 jguarin200
                alias f: std_logic_vector(31 downto 0) is sl;
373
                variable r: real;
374
 
375
        begin
376
 
377
                r:=ap_slv2fp(f);
378 160 jguarin200
                write(l,string'(real'image(r)));
379
                write(l,string'(" [ s:"));
380
                ap_slv2hex(l,f(31 downto 31));
381
                write(l,string'(" f: "));
382
                ap_slv2hex(l,f(30 downto 23));
383
                write(l,string'(" m: "));
384
                ap_slv2hex(l,f(22 downto 00));
385
                write(l,string'(" ]"));
386 155 jguarin200
 
387 160 jguarin200
        end procedure;
388 155 jguarin200
 
389
 
390
 
391 153 jguarin200
 
392
end package body;

powered by: WebSVN 2.1.0

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