复数乘法器研究

复数乘法器研究

wallace乘法器的基础上设计一个复数乘法器(有关wallace树乘法器的内容,

看我的相关博客,这里只是调用了Wallace树乘法器的verilog HDL代码),

复数的乘法算法是:

设复数x = a + b i, y = c + d i, 则复数相乘的结果为:x * y = (a + b i)*(c + d i) = (ac - bd) + i (ad + bc) 。

十分简单,只需要把部分积算出来然后就是加减运算了。

verilog HDL代码如下:

  1. //复数乘法器的verilog HDL代码  
  2.   
  3. module complex(a, b, c, d, out_real, out_im);  
  4.     input [3:0] a, b, c, d;  
  5.     output [8:0] out_real,out_im;  
  6.     wire [7:0] sub1, sub2, add1, add2;  
  7.       
  8.     wallace U1(.x(a), .y(c), .out(sub1));  
  9.     wallace U2(.x(b), .y(d), .out(sub2));  
  10.     wallace U3(.x(a), .y(d), .out(add1));  
  11.     wallace U4(.x(b), .y(c), .out(add2));  
  12.   
  13.     assign out_real = sub1 - sub2;  
  14.     assign out_im = add1 + add2;  
  15.       
  16. endmodule  
  17.   
  18. //下面是wallace树乘法器模块  
  19.     module wallace(x,y,out);  
  20.     parameter size = 4; //定义参数,乘法器的位数  
  21.     input [size - 1 : 0] x,y; //输入y是乘数,x是被乘数  
  22.     output [2*size - 1 : 0] out;  
  23.     wire [size*size - 1 : 0] a; //a为部分积  
  24.     wire [1 : 0] b0, b1; //第一级的输出,包含进位  
  25.     wire [1 : 0] c0, c1, c2, c3; //第二级的输出,包含进位  
  26.     wire [5 : 0] add_a, add_b; //第三极的输入  
  27.     wire [6 : 0] add_out; //第三极的输出  
  28.     wire [2*size - 1 : 0] out; //乘法器的输出(组合逻辑)  
  29.       
  30.     assign a = {x[3],x[2],x[3],x[1],x[2],x[3],x[0],x[1],  
  31.                 x[2],x[3],x[0],x[1],x[2],x[0],x[1],x[0]}  
  32.                 &{y[3],y[3],y[2],y[3],y[2],y[1],y[3],y[2]  
  33.                 ,y[1],y[0],y[2],y[1],y[0],y[1],y[0],y[0]}; //部分积  
  34.     hadd U1(.x(a[8]), .y(a[9]), .out(b0));  //2输入半加器(第一级)  
  35.     hadd U2(.x(a[11]), .y(a[12]), .out(b1));//第一级  
  36.     hadd U3(.x(a[4]), .y(a[5]), .out(c0)); //第二级  
  37.       
  38.     fadd U4(.x(a[6]), .y(a[7]), .z(b0[0]), .out(c1)); //3输入全加器(第二级)  
  39.     fadd U5(.x(b1[0]), .y(a[10]), .z(b0[1]), .out(c2));  
  40.     fadd U6(.x(a[13]), .y(a[14]), .z(b1[1]), .out(c3));  
  41.       
  42.       
  43.     assign add_a = {c3[1],c2[1],c1[1],c0[1],c0[0],a[2]}; //加法器(第三极)  
  44.     assign add_b = {a[15],c3[0],c2[0],c1[0],a[3],a[1]};  
  45.     assign add_out = add_a + add_b;  
  46.     assign out = {add_out,a[0]};  
  47.   
  48. endmodule  
  49.   
  50. //全加器模块  
  51. module fadd(x, y, z, out);  
  52.     input x, y, z;  
  53.     output [1 : 0] out;  
  54.     assign out = x + y + z;   
  55. endmodule  
  56.   
  57. //半加器模块  
  58. module hadd(x, y, out);  
  59.     input x, y;  
  60.     output [1 : 0] out;  
  61.     assign out = x + y;  
  62. endmodule  

测试文件代码如下:

  1. //复数乘法器测试文件  
  2.   
  3. `timescale 1ns/1ps  
  4. module complex_tb;  
  5.     reg [3:0] a, b, c, d;  
  6.     wire [8:0] out_real;  
  7.     wire [8:0] out_im;  
  8.     complex U1(.a(a), .b(b), .c(c), .d(d), .out_real(out_real),  
  9.                 .out_im(out_im));  
  10.                   
  11.     initial  
  12.     begin  
  13.         a = 2; b = 2; c = 5; d = 4;  
  14.         #10  
  15.         a = 4; b = 3; c = 2; d = 1;  
  16.         #10  
  17.         a = 3; b = 2; c = 3; d = 4;  
  18.     end  
  19.   
  20. endmodule  

ModelSim中仿真波形截图如下:

 

猜你喜欢

转载自blog.csdn.net/reborn_lee/article/details/80316028