题解 | #移位运算与乘法#
移位运算与乘法
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<=0;
end
else begin
count<=count+2'b01;
end
end
//******************************//
reg [7:0]d_reg;
always@(posedge clk or negedge rst)begin
if(!rst)begin
d_reg<=0;
out<=0;
input_grant<=0;
end
else begin
case(count)
2'b00:begin
input_grant<=2'b01;
out<=d;
d_reg<=d;
end
2'b01:begin
input_grant<=0;
out<=d_reg+{d_reg,1'b0};//一倍加上两倍等于三倍,也即乘上三
end
2'b10:begin
input_grant<=0;
out<=d_reg+{d_reg,1'b0}+{d_reg,2'b00};
end
2'b11:begin
input_grant<=0;
out<={d_reg,3'b000};
end
default:begin
input_grant<=0;
out<=d_reg;
end
endcase
end
end
//*************code***********//
endmodule
d_reg的设置还是必要的,因为从图中可以看出有一个128的d是没有被采集到的,说明这个采集d是需要再count的4个阶段都执行完的时候还能被采集到,才能算是真的有效数据,所以需要设置d_reg来储存d,而不是实时反映d,不然会报错,与题意不符。
