1、parameter参数传递的16位乘法器 选用课后习题设计题目:设计一个4位乘法器,为此首先设计一个4位加法器,用例化语句调用这个加法器,用移位相加的方式完成乘法。并以此为基础,使用parameter参数传递的功能,设计一个16位乘法器。加法器采用行波进位的方法。4位加法器设计如下:module ripple_carry_adder(x, y, cin, sum, cout); parameter N = 4; input cin; input N-1:0 x, y; output N-1:0 sum; output cout; reg cout; reg N-1:0 sum; reg qN:
2、0; always (x or y or cin)begin:ADDER integer i; q0 = cin; for(i=0; i=N-1; i=i+1)begin qi+1 = (xi&yi) | (xi&qi) | (yi&qi); sumi = xi yi qi; end cout = qN; endendmodule以此为基础的4位乘法器如下:module top(load, rst, clk, dataa, datab, sum); parameter n = 4; input load; input rst; input clk; input n-1:0 dataa; inp
3、ut n-1:0 datab; output 2*n-1:0 sum; parameter 1:0 state_s1 = 0, state_s2 = 1; reg 1:0 next_state; reg 2*n-1:0 dataa_register; reg n-1:0 datab_register; reg 2*n-1:0 sum_tmp,sum_tmp2; wire cout; assign sum = sum_tmp; always (negedge rst or posedge clk) begin: integer i; if (rst = 1b0) begin for (i = 0
4、; i = 2 * n - 1; i = i + 1) begin dataa_registeri = 1b0; sum_tmpi = 1b0; end for (i = 0; i = n - 1; i = i + 1) datab_registeri = 1b0; next_state = state_s1; end else case (next_state) state_s1 : if (load = 1b1) begin dataa_registern - 1:0 = dataa; datab_register = datab; for (i = 0; i = 2 * n - 1; i
5、 = i + 1) sum_tmpi = 1b0; next_state = state_s2; end state_s2 : begin if (datab_register0 = 1b1) / sum_tmp = sum_tmp + dataa_register;sum_tmp=(n-1)b0,cout,sum_tmp2n-1:0; if (datab_register = 4h0) next_state = state_s1; else begin datab_registern - 2:0 = datab_registern - 1:1; datab_registern - 1 = 1
6、b0; dataa_register2 * n - 1:1 = dataa_register2 * n - 2:0; dataa_register0 = 1b0; end end endcase end ripple_carry_adder #(N = 4) u0(.x(sum_tmp), .y(dataa_register), .cin(1b0), .sum(sum_tmp2), .cout(cout);endmodule以此为基础的16位乘法器为:module top(load, rst, clk, dataa, datab, sum,ready); parameter n = 16; i
7、nput load; input rst; input clk; input n-1:0 dataa; input n-1:0 datab; output 2*n-1:0 sum; output ready; parameter 1:0 state_s1 = 0, state_s2 = 1; reg 1:0 next_state; reg 2*n-1:0 dataa_register; reg n-1:0 datab_register; reg 2*n-1:0 sum_tmp; reg ready; wire 2*n-1:0 sum_tmp2; wire cout; integer i; as
8、sign sum = sum_tmp; always (negedge rst or posedge clk) begin if (rst = 1b0) begin ready=1b0; for (i = 0; i = 2 * n - 1; i = i + 1) begin dataa_registeri = 1b0; sum_tmpi = 1b0; end for (i = 0; i = n - 1; i = i + 1) datab_registeri = 1b0; next_state = state_s1; end else case (next_state) state_s1 : i
9、f (load = 1b1) begin dataa_register2*n - 1:n=0; dataa_registern - 1:0 = dataa; datab_register = datab; ready=1b0; for (i = 0; i = 2 * n - 1; i = i + 1) sum_tmpi = 1b0; next_state = state_s2; end state_s2 : begin if (datab_register0 = 1b1) / sum_tmp = sum_tmp + dataa_register; sum_tmp=sum_tmp2; if (d
10、atab_register = 4h0) begin next_state = state_s1; ready=1b1; end else begin datab_registern - 2:0 = datab_registern - 1:1; datab_registern - 1 = 1b0; dataa_register2 * n - 1:1 = dataa_register2 * n - 2:0; dataa_register0 = 1b0; end end endcase end ripple_carry_adder#(2*n) u0(.x(sum_tmp), .y(dataa_register), .cin(1b0), .sum(sum_tmp2), .cout(cout);endmoduleNoYesNoYesNoNoYesYes移位后数据是否加0加法器adder载入新数输出复位Datab=1b1移位state_s2Load=1state_s1ret=0开始流程图:仿真图:4位加法器4位乘法器16位乘法器7