题解 | #移位运算与乘法#
移位运算与乘法
https://www.nowcoder.com/practice/1dd22852bcac42ce8f781737f84a3272
`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 [1:0] count;
always@(posedge clk or negedge rst)begin
if(!rst)begin
count<=1'd0;
end
else
count<=count+1'b1;
end
reg [10:0] d_reg;
always@(posedge clk or negedge rst)begin
if(!rst)begin
d_reg<=11'd0;
input_grant<=1'b0;
out<=11'd0;
end
else if(count==0)begin
d_reg<=d;
out<=d;
input_grant<=1'b1;
end
else if(count==1)begin
d_reg<=d_reg;
out<=d_reg*3;
input_grant<=1'b0;
end
else if(count==2)begin
d_reg<=d_reg;
out<=d_reg*7;
input_grant<=1'b0;
end
else if(count==3)begin
d_reg<=d_reg;
out<=d_reg*8;
input_grant<=1'b0;
end
else begin
d_reg<=d_reg;
input_grant<=input_grant;
out<=out;
end
end
//*************code***********//
endmodule
这题原理从波形上查看是需要使用一个计数器,用于来控制数据进行乘法,也相当于是进行一个循环位移之后进行乘法计算。主要分成两步首先就是用中间寄存器采集输入数据。
