题解 | #移位运算与乘法#
移位运算与乘法
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
count<=0;
out<=0;
input_grant<=0;
d_reg<=0;
end
else begin
case(count)
2'b00:begin
d_reg<=d;
input_grant<=2'b01;
out<=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
关键是理解题意,题目表述的意思是只接受0状态时的值
另一个就是在状态机0状态的设置时,注意是并发的,并不能直接写out<=d_reg
上海得物信息集团有限公司公司福利 1166人发布