题解 | #编写乘法器求解算法表达式#
编写乘法器求解算法表达式
https://www.nowcoder.com/practice/c414335a34b842aeb9960acfe5fc879f
给的参考波形挺奇怪的,为了通过就给输入打了一拍。
解题思路:c = 12*a + 5*b = (2^3+2^2)*a + (2^2+2^0) * b。乘法可以用移位和加法实现。
`timescale 1ns/1ns
module calculation(
input clk,
input rst_n,
input [3:0] a,
input [3:0] b,
output [8:0] c
);
reg [3:0] a1, b1;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
a1 <= 4'd0;
b1 <= 4'd0;
end
else begin
a1 <= a;
b1 <= b;
end
end
reg [8:0] tmp_c;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
tmp_c <= 9'd0;
end
else
tmp_c <= (a1<<3) + (a1<<2) + (b1<<2) + b1;
end
assign c = tmp_c;
endmodule