Chinaunix首页 | 论坛 | 博客
  • 博客访问: 458106
  • 博文数量: 62
  • 博客积分: 1742
  • 博客等级: 中尉
  • 技术积分: 859
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-06 00:13
个人简介

这是一句很长很长而且又很啰嗦并且很无聊的废话...

文章分类

全部博文(62)

文章存档

2013年(1)

2012年(13)

2011年(48)

分类: LINUX

2012-09-20 00:50:35

完成后面的习题,这里以16位为测试,通过改变N,可以实现N位乘法阵列.下面是Verilog代码.

点击(此处)折叠或打开

  1. module hw_mul(x, y, out);
  2.     parameter n = 16;
  3.     input [n-1:0]x, y;
  4.     output [n*2-1:0]out;
  5.     genvar i, j, k, l;
  6.     wire [n:0]tmp_sun[n-1], tmp_out[n-1];
  7.     wire [n:0]tmp_x;
  8.     
  9.     assign tmp_x = {1'b0,x};
  10.     assign out[0] = tmp_x[0] & y[0];
  11.     generate
  12.         assign tmp_out[0][0] = 1'b0;
  13.         for(i=0;i<n;i=i+1)//循环相加顶层乘数的前两位与上被乘数的部分积.
  14.         begin:add_bt
  15.             top_m tm (tmp_x[i+1], y[0], tmp_x[i], y[1], tmp_out[0][i], tmp_sun[0][i], tmp_out[0][i+1]);
  16.         end
  17.         assign out[1] = tmp_sun[0][0];
  18.         assign tmp_sun[0][n] = tmp_out[0][n];
  19.         
  20.         for(j=2;j<n;j=j+1)//从第三位开始的每一位与上被乘数,然后跟上一层的部分积结果相加.
  21.         begin:ftf
  22.             for(k=0;k<n;k=k+1)
  23.             begin:add_bb
  24.                 btm_m bm (tmp_sun[j-2][k+1], tmp_x[k], y[j], tmp_out[j-1][k], tmp_sun[j-1][k], tmp_out[j-1][k+1]);
  25.             end
  26.             assign out[j] = tmp_sun[j-1][0];
  27.             assign tmp_sun[j-1][n] = tmp_out[j-1][n];
  28.         end
  29.         
  30.         for(l=n;l<n*2;l=l+1)
  31.         begin:add_o
  32.             assign out[l] = tmp_sun[n-2][l-n+1];
  33.         end
  34.     endgenerate
  35. endmodule

  36. module top_m(m0, q0, m1, q1, cin, sun, cout);//顶层乘法单元
  37.     input m0, m1, q0, q1;
  38.     input cin;
  39.     output sun, cout;
  40.     wire x, y;
  41.     
  42.     assign x = m0 & q0;
  43.     assign y = m1 & q1;
  44.     full_add fa (x, y, cin, sun, cout);
  45. endmodule

  46. module btm_m(sunin, m, q, cin, sun, cout);//下层乘法单元
  47.     input sunin, m, q, cin;
  48.     output sun, cout;
  49.     wire y;
  50.     
  51.     assign y = m & q;
  52.     full_add fa (sunin, y, cin, sun, cout);
  53. endmodule

  54. module full_add(x, y, cin, sun, cout);//一位全加器
  55.     input x, y, cin;
  56.     output reg sun, cout;

  57.     always@(x, y, cin)
  58.     begin
  59.         sun <= x^y^cin;
  60.         cout <= x&y | x&cin | y&cin;
  61.     end
  62. endmodule
      下面是该乘法阵列的功能仿真.

小结:
     Verilog的循环性语句,如for,while并不像C语言那样有指令判断条件再跳转之类的,这里的循环是给编译器用的,它只是按你给定的循环重复生成N个用给定的线网连接起来的模块,等于你手工写重复模块一样的.
阅读(3237) | 评论(0) | 转发(0) |
0

上一篇:超前进位加法器

下一篇:LCD LVDS BT656 SDI

给主人留下些什么吧!~~