题解 | #移位运算与乘法#
移位运算与乘法
https://www.nowcoder.com/practice/1dd22852bcac42ce8f781737f84a3272
/*
此题要注意d值在cnt=0时进入reg_d,即d=128时的波形比较特别
*/
`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 [7:0] reg_d;
always@(posedge clk or negedge rst) begin
if(!rst) begin
cnt <= 0;
end
else if(cnt == 2'b11) begin
cnt <= 0;
end
else begin
cnt <= cnt + 1 ;
end
end
always@(posedge clk or negedge rst) begin
if(!rst) begin
reg_d <= 0;
out <= 0;
input_grant <= 0 ;
end
else begin
case(cnt)
2'b00:begin
reg_d <= d;
out <= d;
input_grant <= 1'b1 ;
end
2'b01:begin
out <= (reg_d<<2)-reg_d;
input_grant <= 0 ;
end
2'b10:begin
out <= (reg_d<<3)-reg_d;
input_grant <= 0 ;
end
2'b11:begin
out <= reg_d<<3;
input_grant <= 0 ;
end
default : begin
out <= 11'b0;
input_grant <= 0 ;
end
endcase
end
end
//*************code***********//
endmodule
