题解 | #流水线乘法器#
流水线乘法器
https://www.nowcoder.com/practice/be97f63817c543fe9260d46d971a7283
想参数化来着,但是感觉参数化太难写了。
`timescale 1ns/1ns module multi_pipe#( parameter size = 4 )( input clk , input rst_n , input [size-1:0] mul_a , input [size-1:0] mul_b , output reg [size*2-1:0] mul_out ); wire [size*2-1:0] add_tmp [size-1:0]; genvar i; generate for(i=0;i<size;i=i+1) begin: shift assign add_tmp[i] = mul_b[i]?({{size{1'b0}},mul_a}<<i):'d0; end endgenerate reg [size*2-1:0] mul_reg1, mul_reg2; always@(posedge clk or negedge rst_n) begin if(!rst_n) begin mul_reg1 <= 'd0; mul_reg2 <= 'd0; end else begin mul_reg1 <= add_tmp[0] + add_tmp[1]; mul_reg2 <= add_tmp[2] + add_tmp[3]; end end always@(posedge clk or negedge rst_n) begin if(!rst_n) begin mul_out <= 'd0; end else begin mul_out <= mul_reg1 + mul_reg2; end end endmodule