题解 | #移位运算与乘法#
移位运算与乘法
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] cnt ;
reg [10:0] din ;
always @(posedge clk or negedge rst) begin
if(!rst)begin
cnt <=2'b00;
end
else if(cnt==2'b11) begin
cnt <=0;
end
else cnt <= cnt+1'b1;
end
always @(posedge clk or negedge rst) begin
if(!rst)begin
input_grant <= 1'b0;
out <= 11'b0;
din = 1'b0;
end else begin
case (cnt)
2'b00: begin
din = d;
input_grant <= 1'b1;
out <= din;
end
2'b01: begin
input_grant <=1'b0;
out <= (din<<2'd2)-din;
end
2'b10:begin
input_grant <=1'b0;
out <= (din<<2'd3)-din;
end
2'b11:begin
input_grant <= 1'b0;
out <= din<<2'd3;
end
endcase
end
end
//*************code***********//
endmodule
本题的核心点是接收到一个输入后,会将本次的输入相应的四个输出按照周期输出结束后才继续接收下一个输入,对于这种类似于线程型的任务,可以用计数器进行计数。
代码中需要注意的点:din 接收赋值需要采用阻塞赋值,否则接收的第一个值将会被错过!


