|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
如何在verilog里定义小数,为什么我定义输入xy是小数在真正运行时自动取整?我该如何定义XY,才能使计算时是小数运算?
以下是我的Verilog代码
1 `timescale 1 ns/1 ns
2
3 module cordic_atan_tb();
4
5
6
7 //时钟周期,单位为ns,可在此修改时钟周期。
8 parameter CYCLE = 20;
9
10 //复位时间,此时表示复位3个时钟周期的时间。
11 parameter RST_TIME = 3 ;
12
13 // Inputs
14 reg clk_50M;
15 reg rst_n;
16 reg ena;
17 reg [31:0] x;
18 reg [31:0] y;
19 //reg [31:0] x_in;
20 //reg [31:0] y_in;
21 //wire [31:0] finished;
22 wire [31:0] atan;
23 // Instantiate the Unit Under Test (UUT)
24 Cordic_atan uut(
25 .clk_50M (clk_50M ),
26 .rst_n (rst_n ),
27 .x (x ),
28 .y (y ),
29 .ena (ena ),
30 .finished (finished ),
31 .atan (atan )
32 );
33
34
35
36
37 //生成本地时钟50M
38 initial begin
39 clk_50M = 0;
40 forever
41 #(CYCLE/2)
42 clk_50M=~clk_50M;
43 end
44
45 //产生复位信号
46 initial begin
47 rst_n = 1;
48 #2;
49 rst_n = 0;
50 #(CYCLE*RST_TIME);
51 rst_n = 1;
52 end
53
54 //输入信号din0赋值方式
55
56 initial begin
57 #50;
58 x = 0.2;
59 y = 0.5;
60 ena = 1;
61 /* while(x<=100)begin
62 #380;
63 x<=x+2'b10;
64 y<=y-2'b10;
65 end
66 */
67
68 end
69
70
71
72 endmodule
73
1
2
3 module Cordic_atan
4 (
5 clk_50M,rst_n,
6 x,
7 y,
8 ena ,
9 finished,
10 atan
11 );
12
13 input clk_50M;
14 input rst_n;
15 input [31:0] x;
16 input [31:0] y;
17 input [31:0] ena ;
18
19 output [31:0] finished;
20 output [31:0] atan;
21
22
23 `define angle_0 32'd2949120 //45°*2^16
24 `define angle_1 32'd1740992 //26.5651°*2^16
25 `define angle_2 32'd919872 //14.0362°*2^16
26 `define angle_3 32'd466944 //7.1250°*2^16
27 `define angle_4 32'd234368 //3.5763°*2^16
28 `define angle_5 32'd117312 //1.7899°*2^16
29 `define angle_6 32'd58688 //0.8952°*2^16
30 `define angle_7 32'd29312 //0.4476°*2^16
31 `define angle_8 32'd14656 //0.2238°*2^16
32 `define angle_9 32'd7360 //0.1119°*2^16
33 `define angle_10 32'd3648 //0.0560°*2^16
34 `define angle_11 32'd1856 //0.0280°*2^16
35 `define angle_12 32'd896 //0.0140°*2^16
36 `define angle_13 32'd448 //0.0070°*2^16
37 `define angle_14 32'd257 //0.0035°*2^16
38 `define angle_15 32'd128 //0.0018°*2^16
39
40 parameter Pipeline = 16;
41 parameter K = 32'h09b74; //K=0.607253*2^16,32'h09b74,
42
43 reg signed [31:0] atan;
44 reg signed [31:0] x0=0,y0=0,z0=0;
45 reg signed [31:0] x1=0,y1=0,z1=0;
46 reg signed [31:0] x2=0,y2=0,z2=0;
47 reg signed [31:0] x3=0,y3=0,z3=0;
48 reg signed [31:0] x4=0,y4=0,z4=0;
49 reg signed [31:0] x5=0,y5=0,z5=0;
50 reg signed [31:0] x6=0,y6=0,z6=0;
51 reg signed [31:0] x7=0,y7=0,z7=0;
52 reg signed [31:0] x8=0,y8=0,z8=0;
53 reg signed [31:0] x9=0,y9=0,z9=0;
54 reg signed [31:0] x10=0,y10=0,z10=0;
55 reg signed [31:0] x11=0,y11=0,z11=0;
56 reg signed [31:0] x12=0,y12=0,z12=0;
57 reg signed [31:0] x13=0,y13=0,z13=0;
58 reg signed [31:0] x14=0,y14=0,z14=0;
59 reg signed [31:0] x15=0,y15=0,z15=0;
60 reg signed [31:0] x16=0,y16=0,z16=0;
61 reg signed [31:0] x_in=0,y_in=0,z_in=0;
62 reg [5:0] count;
63
64 always@ (posedge clk_50M or negedge rst_n) begin
65 if(!rst_n)
66 count <= 6'b000000;
67 else if( ena )
68 begin
69 if( count!=6'b010010 )
70 count <= count+6'b000001;
71 else if( count == 6'b010010 )
72 count <= 0;
73 end
74 else
75 count <= 6'b000000;
76 end
77
78 assign finished = (count == 6'b010010)?1'b1:1'b0;
79
80 always@ (posedge clk_50M or negedge rst_n)
81 begin
82
83 if(!rst_n)
84 begin
85 x_in<= 1'b0;
86 y_in<= 1'b0;
87 z_in<= 1'b0;
88 end
89 else
90 begin
91 if(ena)
92 begin //?0???????????
93 case ({x[31],y[31]}) //????
94 2'b00:
95 begin //1??
96 x_in <= x;
97 y_in <= y;
98
99 end
100
101 2'b10:
102 begin //2??
103 x_in<= -x;
104 y_in<= y;
105
106 end
107
108 2'b11:
109 begin //3??
110 x_in<= -x;
111 y_in <= -y;
112
113 end
114
115 2'b01:
116 begin //4??
117 x_in <= x;
118 y_in <= -y;
119
120 end
121 default:
122 begin
123 x_in<= 1'b0;
124 y_in<= 1'b0;
125 z_in<= 1'b0;
126 end
127 endcase
128 end
129 else
130 begin
131 x_in<= 1'b0;
132 y_in<= 1'b0;
133 z_in<= 1'b0;
134 end
135
136 end
137 end
138 always @ (posedge clk_50M or negedge rst_n)
139 begin
140 if(!rst_n)
141 begin
142 x0 <= 1'b0;
143 y0 <= 1'b0;
144 z0 <= 1'b0;
145 end
146 else
147 begin
148 x0 <= x_in<<<16;
149 y0 <= y_in<<<16;
150 z0 <= 32'd0;
151 end
152 end
153
154
155 always @ (posedge clk_50M or negedge rst_n)
156 begin
157 if(!rst_n)
158 begin
159 x1 <= 1'b0;
160 y1 <= 1'b0;
161 z1 <= 1'b0;
162 end
163 else if(y0[31]==1'b1)//Di is -1;
164 begin
165 x1 <= x0 - y0;
166 y1 <= y0 + x0;
167 z1 <= z0 - `angle_0;
168 end
169 else
170 begin //Di is 1;
171 x1 <= x0 + y0;
172 y1 <= y0 - x0;
173 z1 <= z0 + `angle_0;
174 end
175 end
176
177 always @ (posedge clk_50M or negedge rst_n)
178 begin
179 if(!rst_n)
180 begin
181 x2 <= 1'b0;
182 y2 <= 1'b0;
183 z2 <= 1'b0;
184 end
185 else if( y1[31]==1'b1)
186 begin
187 x2 <= x1 - (y1 >>> 1);
188 y2 <= y1 + (x1 >>> 1);
189 z2 <= z1 - `angle_1;
190 end
191 else
192 begin
193 x2 <= x1 + (y1 >>> 1);
194 y2 <= y1 - (x1 >>> 1);
195 z2 <= z1 + `angle_1;
196 end
197 end
198
199 always @ (posedge clk_50M or negedge rst_n)
200 begin
201 if(!rst_n)
202 begin
203 x3 <= 1'b0;
204 y3 <= 1'b0;
205 z3 <= 1'b0;
206 end
207 else if( y2[31]==1'b1)
208 begin
209 x3 <= x2 - (y2 >>> 2);
210 y3 <= y2 + (x2 >>> 2);
211 z3 <= z2 - `angle_2;
212 end
213 else
214 begin
215 x3 <= x2 + (y2 >>> 2);
216 y3 <= y2 - (x2 >>> 2);
217 z3 <= z2 + `angle_2;
218 end
219 end
220
221 always @ (posedge clk_50M or negedge rst_n)
222 begin
223 if(!rst_n)
224 begin
225 x4 <= 1'b0;
226 y4 <= 1'b0;
227 z4 <= 1'b0;
228 end
229 else if( y3[31]==1'b1)
230 begin
231 x4 <= x3 - (y3 >>> 3);
232 y4 <= y3 + (x3 >>> 3);
233 z4 <= z3 - `angle_3;
234 end
235 else
236 begin
237 x4 <= x3 + (y3 >>> 3);
238 y4 <= y3 -(x3 >>> 3);
239 z4 <= z3 + `angle_3;
240 end
241 end
242
243 always @ (posedge clk_50M or negedge rst_n)
244 begin
245 if(!rst_n)
246 begin
247 x5 <= 1'b0;
248 y5 <= 1'b0;
249 z5 <= 1'b0;
250 end
251 else if( y4[31]==1'b1)
252 begin
253 x5 <= x4 - (y4 >>> 4);
254 y5 <= y4 + (x4 >>> 4);
255 z5 <= z4 -`angle_4;
256 end
257 else
258 begin
259 x5 <= x4 + (y4 >>> 4);
260 y5 <= y4 - (x4 >>> 4);
261 z5 <= z4 + `angle_4;
262 end
263 end
264
265 always @ (posedge clk_50M or negedge rst_n)
266 begin
267 if(!rst_n)
268 begin
269 x6 <= 1'b0;
270 y6 <= 1'b0;
271 z6 <= 1'b0;
272 end
273 else if( y5[31]==1'b1)
274 begin
275 x6 <= x5 - (y5 >>> 5);
276 y6 <= y5 + (x5 >>> 5);
277 z6 <= z5 - `angle_5;
278 end
279 else
280 begin
281 x6 <= x5 + (y5 >>> 5);
282 y6 <= y5 - (x5 >>> 5);
283 z6 <= z5 + `angle_5;
284 end
285 end
286
287 always @ (posedge clk_50M or negedge rst_n)
288 begin
289 if(!rst_n)
290 begin
291 x7 <= 1'b0;
292 y7 <= 1'b0;
293 z7 <= 1'b0;
294 end
295 else if(y6[31]==1'b1)
296 begin
297 x7 <= x6 - (y6 >>> 6);
298 y7 <= y6 + (x6 >>> 6);
299 z7 <= z6 - `angle_6;
300 end
301 else
302 begin
303 x7 <= x6 + (y6 >>> 6);
304 y7 <= y6 - (x6 >>> 6);
305 z7 <= z6 + `angle_6;
306 end
307 end
308
309 always @ (posedge clk_50M or negedge rst_n)
310 begin
311 if(!rst_n)
312 begin
313 x8 <= 1'b0;
314 y8 <= 1'b0;
315 z8 <= 1'b0;
316 end
317 else if(y7[31]==1'b1)
318 begin
319 x8 <= x7 - (y7 >>> 7);
320 y8 <= y7 + (x7 >>> 7);
321 z8 <= z7 - `angle_7;
322 end
323 else
324 begin
325 x8 <= x7 + (y7 >>> 7);
326 y8 <= y7 - (x7 >>> 7);
327 z8 <= z7 + `angle_7;
328 end
329 end
330
331 always @ (posedge clk_50M or negedge rst_n)
332 begin
333 if(!rst_n)
334 begin
335 x9 <= 1'b0;
336 y9 <= 1'b0;
337 z9 <= 1'b0;
338 end
339 else if(y8[31]==1'b1)
340 begin
341 x9 <= x8 - (y8 >>> 8);
342 y9 <= y8 + (x8 >>> 8);
343 z9 <= z8 - `angle_8;
344 end
345 else
346 begin
347 x9 <= x8 + (y8 >>> 8);
348 y9 <= y8 - (x8 >>> 8);
349 z9 <= z8 + `angle_8;
350 end
351 end
352
353 always @ (posedge clk_50M or negedge rst_n)
354 begin
355 if(!rst_n)
356 begin
357 x10 <= 1'b0;
358 y10 <= 1'b0;
359 z10 <= 1'b0;
360 end
361 else if( y9[31]==1'b1)
362 begin
363 x10 <= x9 - (y9 >>> 9);
364 y10 <= y9 + (x9 >>> 9);
365 z10 <= z9 - `angle_9;
366 end
367 else
368 begin
369 x10 <= x9 + (y9 >>> 9);
370 y10 <= y9 - (x9 >>> 9);
371 z10 <= z9 + `angle_9;
372 end
373 end
374
375 always @ (posedge clk_50M or negedge rst_n)
376 begin
377 if(!rst_n)
378 begin
379 x11 <= 1'b0;
380 y11 <= 1'b0;
381 z11 <= 1'b0;
382 end
383 else if( y10[31]==1'b1)
384 begin
385 x11 <= x10 - (y10 >>> 10);
386 y11 <= y10 + (x10 >>> 10);
387 z11 <= z10 - `angle_10;
388 end
389 else
390 begin
391 x11 <= x10 + (y10 >>> 10);
392 y11 <= y10 - (x10 >>> 10);
393 z11 <= z10 + `angle_10;
394 end
395 end
396
397 always @ (posedge clk_50M or negedge rst_n)
398 begin
399 if(!rst_n)
400 begin
401 x12 <= 1'b0;
402 y12 <= 1'b0;
403 z12 <= 1'b0;
404 end
405 else if(y11[31]==1'b1)
406 begin
407 x12 <= x11 - (y11 >>> 11);
408 y12 <= y11 + (x11 >>> 11);
409 z12 <= z11 - `angle_11;
410 end
411 else
412 begin
413 x12 <= x11 + (y11 >>> 11);
414 y12 <= y11 - (x11 >>> 11);
415 z12 <= z11 + `angle_11;
416 end
417 end
418
419 always @ (posedge clk_50M or negedge rst_n)
420 begin
421 if(!rst_n)
422 begin
423 x13 <= 1'b0;
424 y13 <= 1'b0;
425 z13 <= 1'b0;
426 end
427 else if( y12[31]==1'b1)
428 begin
429 x13 <= x12 - (y12 >>> 12);
430 y13 <= y12 + (x12 >>> 12);
431 z13 <= z12 - `angle_12;
432 end
433 else
434 begin
435 x13 <= x12 + (y12 >>> 12);
436 y13 <= y12 - (x12 >>> 12);
437 z13 <= z12 + `angle_12;
438 end
439 end
440
441 always @ (posedge clk_50M or negedge rst_n)
442 begin
443 if(!rst_n)
444 begin
445 x14 <= 1'b0;
446 y14 <= 1'b0;
447 z14 <= 1'b0;
448 end
449 else if( y13[31]==1'b1)
450 begin
451 x14 <= x13 - (y13 >>> 13);
452 y14 <= y13 + (x13 >>> 13);
453 z14 <= z13 - `angle_13;
454 end
455 else
456 begin
457 x14 <= x13 + (y13 >>> 13);
458 y14 <= y13 - (x13 >>> 13);
459 z14 <= z13 + `angle_13;
460 end
461 end
462
463 always @ (posedge clk_50M or negedge rst_n)
464 begin
465 if(!rst_n)
466 begin
467 x15 <= 1'b0;
468 y15 <= 1'b0;
469 z15 <= 1'b0;
470 end
471 else if(y14[31]==1'b1)
472 begin
473 x15 <= x14 - (y14 >>> 14);
474 y15 <= y14 + (x14 >>> 14);
475 z15 <= z14 - `angle_14;
476 end
477 else
478 begin
479 x15 <= x14 + (y14 >>> 14);
480 y15 <= y14 - (x14 >>> 14);
481 z15 <= z14 + `angle_14;
482 end
483 end
484
485 always @ (posedge clk_50M or negedge rst_n)
486 begin
487 if(!rst_n)
488 begin
489 x16 <= 1'b0;
490 y16 <= 1'b0;
491 z16 <= 1'b0;
492 end
493 else if( y15[31]==1'b1)
494 begin
495 x16 <= x15 - (y15 >>> 15);
496 y16 <= y15 + (x15 >>> 15);
497 z16 <= z15 - `angle_15;
498 end
499 else
500 begin
501 x16 <= x15 + (y15 >>> 15);
502 y16 <= y15 - (x15 >>> 15);
503 z16 <= z15 + `angle_15;
504 end
505 end
506
507 always @ (posedge clk_50M or negedge rst_n)
508 begin
509 if(!rst_n)
510 begin
511 atan <= 1'b0;
512 end
513 else if(finished)
514 begin //?0???????????
515 case ({x[31],y[31]}) //????
516 2'b00:
517 begin //1??
518 atan=z16>>16;
519 end
520
521 2'b10:
522 begin //2??
523 atan=-(z16>>16)+180;
524 end
525
526 2'b11:
527 begin //3??
528 atan=(z16>>16)+180;
529 end
530
531 2'b01:
532 begin //4??
533 atan=-(z16>>16)+360;
534 end
535 endcase
536 end
537 end
538 endmodule
|
|