题解 | #编写乘法器求解算法表达式#
编写乘法器求解算法表达式
https://www.nowcoder.com/practice/c414335a34b842aeb9960acfe5fc879f
`timescale 1ns/1ns
module calculation(
input clk,
input rst_n,
input [3:0] a,
input [3:0] b,
output [8:0] c
);
wire[7:0] a_temp, b_temp;
reg[8:0] c_temp;
mul mul1(
.clk(clk),
.rst_n(rst_n),
.a(a),
.b(4'd12),
.c(a_temp)
);
mul mul2(
.clk(clk),
.rst_n(rst_n),
.a(b),
.b(4'd5),
.c(b_temp)
);
always@(posedge clk or negedge rst_n) begin
if(!rst_n)
c_temp <= 8'd0;
else
c_temp <= a_temp + b_temp;
end
assign c = c_temp;
endmodule
module mul(
input clk,
input rst_n,
input[3:0] a,
input[3:0] b,
output reg[7:0] c
);
always@(posedge clk or negedge rst_n) begin
if(!rst_n)
c <= 8'd0;
else
c <= ({4{b[0]}} & a)
+(({4{b[1]}} & a) << 1)
+(({4{b[2]}} & a) << 2)
+(({4{b[3]}} & a) << 3);
end
endmodule
实在是不知道这个题目要例化一个乘法器,为什么后面还有再隔一个周期才能输出c,而不能直接assign c = a_temp + b_temp;
乘法器就用最简单的移位相加乘法器来做了,毕竟不允许用乘法。

