题解 | #编写乘法器求解算法表达式#
编写乘法器求解算法表达式
https://www.nowcoder.com/practice/c414335a34b842aeb9960acfe5fc879f
`timescale 1ns/1ns module mult( input [3:0] A, input [3:0] B, output [7:0] C ); wire [7:0] C_tmp [0:3]; genvar i; generate for(i=0;i<=3;i=i+1) begin: bit_shift assign C_tmp[i] = B[i]?(A<<i):'d0; end endgenerate assign C = C_tmp[0] + C_tmp[1] + C_tmp[2] + C_tmp[3]; endmodule module calculation( input clk, input rst_n, input [3:0] a, input [3:0] b, output [8:0] c ); wire [7:0] add1, add2; mult u_mult1( .A(a), .B(4'd12), .C(add1) ); mult u_mult2( .A(b), .B(4'd5), .C(add2) ); reg [8:0] c_reg; always@(posedge clk or negedge rst_n) begin if(!rst_n) begin c_reg <= 'd0; end else begin c_reg <= add1 + add2; end end assign c = c_reg; endmodule