题解1 | #移位运算与乘法#
移位运算与乘法
https://www.nowcoder.com/practice/1dd22852bcac42ce8f781737f84a3272
基本思路:
- 通过移位进行乘法运算,右移n位即为*(2^n)
- 使用cnt记录d有效后进行了几次运算
- 使用d_temp锁存有效d进行运算
- 以cnt的数值为标志进行out、input_grant运算,在一个always里完成
注意事项:
- 使用时序电路时,表达式右边的变量均为上一拍的取值
`timescale 1ns/1ns
module multi_sel(
input [7:0]d,
input clk,
input rst,
output reg input_grant,
output reg [10:0]out
);
//*************code***********//
reg [10:0] d_temp;
reg [2:0] cnt = 0;
always @(posedge clk or negedge rst)
begin
if (!rst)
begin
cnt <= 0;
d_temp <= 0;
out <= 0;
input_grant <= 0;
end
else
begin
case(cnt) // for shown diagram, at beat 3, start case
3'd0: begin cnt <= cnt +1; d_temp <= d; out <= d; input_grant <=1;end //for shown diagram, at beat 3, d_temp gets d's value at beat 2
3'd1: begin cnt <= cnt +1; out <= (d_temp << 2) -d_temp; input_grant <=0; end
3'd2: begin cnt <= cnt +1; out <= (d_temp << 3) -d_temp; end
3'd3: begin cnt <= 0; out <= d_temp << 3; end
endcase
end
end
//*************code***********//
endmodule
腾讯云智研发成长空间 5088人发布
